X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FButtonController.cpp;h=ccac674e4aa449f8a58edf2490663a7ee117fec1;hb=b6eacd8d4f86734e8abef3335b190ce12a6a11b5;hp=26279aa0ee7b049ef68ad15610e97e98ac2f1e2b;hpb=212386be8a1573fb52b5da718961835816a3c8e2;p=lyx.git diff --git a/src/frontends/qt4/ButtonController.cpp b/src/frontends/qt4/ButtonController.cpp index 26279aa0ee..ccac674e4a 100644 --- a/src/frontends/qt4/ButtonController.cpp +++ b/src/frontends/qt4/ButtonController.cpp @@ -11,26 +11,137 @@ #include #include "ButtonController.h" -#include "debug.h" + #include "qt_helpers.h" +#include "support/debug.h" + +#include #include #include #include +#include #include + namespace lyx { namespace frontend { +static void setWidgetEnabled(QWidget * obj, bool enabled) +{ + if (QLineEdit * le = qobject_cast(obj)) + le->setReadOnly(!enabled); + else + obj->setEnabled(enabled); + + obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus); +} + + +///////////////////////////////////////////////////////////////////////// +// +// CheckedLineEdit +// +///////////////////////////////////////////////////////////////////////// + +class CheckedLineEdit +{ +public: + CheckedLineEdit(QLineEdit * input, QWidget * label = 0); + bool check() const; + +private: + // non-owned + QLineEdit * input_; + QWidget * label_; +}; + + +CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label) + : input_(input), label_(label) +{} + + +bool CheckedLineEdit::check() const +{ + QValidator const * validator = input_->validator(); + if (!validator) + return true; + + QString t = input_->text(); + int p = 0; + bool const valid = validator->validate(t, p) == QValidator::Acceptable; + + // Visual feedback. + setValid(input_, valid); + if (label_) + setValid(label_, valid); + + return valid; +} + + +///////////////////////////////////////////////////////////////////////// +// +// ButtonController::Private +// +///////////////////////////////////////////////////////////////////////// + +class ButtonController::Private +{ +public: + typedef QList CheckedWidgetList; + + Private() + : okay_(0), apply_(0), cancel_(0), restore_(0), auto_apply_(0), + policy_(ButtonPolicy::IgnorantPolicy) + {} + + /// \return true if all CheckedWidgets are in a valid state. + bool checkWidgets() const + { + bool valid = true; + for (const CheckedLineEdit & w : checked_widgets_) + valid &= w.check(); + return valid; + } + +public: + CheckedWidgetList checked_widgets_; + + QPushButton * okay_; + QPushButton * apply_; + QPushButton * cancel_; + QPushButton * restore_; + QCheckBox * auto_apply_; + + typedef QList Widgets; + Widgets read_only_; + + ButtonPolicy policy_; +}; + + +///////////////////////////////////////////////////////////////////////// +// +// ButtonController +// +///////////////////////////////////////////////////////////////////////// + ButtonController::ButtonController() - : okay_(0), apply_(0), cancel_(0), restore_(0), - policy_(ButtonPolicy::IgnorantPolicy) + : d(new Private) {} +ButtonController::~ButtonController() +{ + delete d; +} + + void ButtonController::setPolicy(ButtonPolicy::Policy policy) { - policy_ = ButtonPolicy(policy); + d->policy_.setPolicy(policy); } @@ -44,7 +155,7 @@ void ButtonController::input(ButtonPolicy::SMInput in) { if (ButtonPolicy::SMI_NOOP == in) return; - policy_.input(in); + d->policy_.input(in); refresh(); } @@ -55,6 +166,12 @@ void ButtonController::apply() } +void ButtonController::autoApply() +{ + input(ButtonPolicy::SMI_AUTOAPPLY); +} + + void ButtonController::cancel() { input(ButtonPolicy::SMI_CANCEL); @@ -81,11 +198,14 @@ void ButtonController::setValid(bool v) bool ButtonController::setReadOnly(bool ro) { - LYXERR(Debug::GUI) << "Setting controller ro: " << ro << std::endl; + LYXERR(Debug::GUI, "Setting controller ro: " << ro); - policy_.input(ro ? + d->policy_.input(ro ? ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE); - refreshReadOnly(); + // refreshReadOnly(); This will enable all widgets in dialogs, no matter if + // they allowed to be enabled, so when you plan to + // reenable this call, read this before: + // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html refresh(); return ro; } @@ -93,124 +213,100 @@ bool ButtonController::setReadOnly(bool ro) void ButtonController::refresh() const { - LYXERR(Debug::GUI) << "Calling BC refresh()" << std::endl; + LYXERR(Debug::GUI, "Calling BC refresh()"); - bool const all_valid = checkWidgets(); + bool const all_valid = d->checkWidgets(); - if (okay_) { + if (d->okay_) { bool const enabled = all_valid && policy().buttonStatus(ButtonPolicy::OKAY); - okay_->setEnabled(enabled); + d->okay_->setEnabled(enabled); } - if (apply_) { + if (d->apply_) { bool const enabled = all_valid && policy().buttonStatus(ButtonPolicy::APPLY); - apply_->setEnabled(enabled); + d->apply_->setEnabled(enabled); } - if (restore_) { + if (d->restore_) { bool const enabled = all_valid && policy().buttonStatus(ButtonPolicy::RESTORE); - restore_->setEnabled(enabled); + d->restore_->setEnabled(enabled); } - if (cancel_) { + if (d->cancel_) { bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL); if (enabled) - cancel_->setText(toqstr(_("Cancel"))); + d->cancel_->setText(qt_("Cancel")); else - cancel_->setText(toqstr(_("Close"))); + d->cancel_->setText(qt_("Close")); } + if (d->auto_apply_) { + bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY); + d->auto_apply_->setEnabled(enabled); + } + } void ButtonController::refreshReadOnly() const { - if (read_only_.empty()) + if (d->read_only_.empty()) return; - bool const enable = !policy().isReadOnly(); - - Widgets::const_iterator end = read_only_.end(); - Widgets::const_iterator iter = read_only_.begin(); - for (; iter != end; ++iter) - setWidgetEnabled(*iter, enable); + for(QWidget * w : d->read_only_) + setWidgetEnabled(w, enable); } -void ButtonController::setWidgetEnabled(QWidget * obj, bool enabled) const +void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label) { - if (QLineEdit * le = qobject_cast(obj)) - le->setReadOnly(!enabled); - else - obj->setEnabled(enabled); - - obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus); + d->checked_widgets_.append(CheckedLineEdit(input, label)); } -void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label) +void ButtonController::setOK(QPushButton * obj) { - checked_widgets.push_back(CheckedLineEdit(input, label)); + d->okay_ = obj; } -bool ButtonController::checkWidgets() const +void ButtonController::setApply(QPushButton * obj) { - bool valid = true; - - CheckedWidgetList::const_iterator it = checked_widgets.begin(); - CheckedWidgetList::const_iterator end = checked_widgets.end(); + d->apply_ = obj; +} - for (; it != end; ++it) - valid &= it->check(); - // return valid status after checking ALL widgets - return valid; +void ButtonController::setAutoApply(QCheckBox * obj) +{ + d->auto_apply_ = obj; } -////////////////////////////////////////////////////////////// -// -// CheckedLineEdit -// -////////////////////////////////////////////////////////////// - -static void setWarningColor(QWidget * widget) +void ButtonController::setCancel(QPushButton * obj) { - QPalette pal = widget->palette(); - pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0)); - widget->setPalette(pal); + d->cancel_ = obj; } -CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label) - : input_(input), label_(label) -{} +void ButtonController::setRestore(QPushButton * obj) +{ + d->restore_ = obj; +} -bool CheckedLineEdit::check() const +void ButtonController::addReadOnly(QWidget * obj) { - QValidator const * validator = input_->validator(); - if (!validator) - return true; - - QString t = input_->text(); - int p = 0; - bool const valid = validator->validate(t, p) == QValidator::Acceptable; + d->read_only_.push_back(obj); +} - // Visual feedback. - if (valid) - input_->setPalette(QPalette()); - else - setWarningColor(input_); +ButtonPolicy const & ButtonController::policy() const +{ + return d->policy_; +} - if (!label_) { - if (valid) - label_->setPalette(QPalette()); - else - setWarningColor(label_); - } - return valid; +ButtonPolicy & ButtonController::policy() +{ + return d->policy_; } } // namespace frontend