2 * \file ButtonController.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "ButtonController.h"
15 #include "qt_helpers.h"
17 #include "support/debug.h"
20 #include <QPushButton>
30 static void setWidgetEnabled(QWidget * obj, bool enabled)
32 if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
33 le->setReadOnly(!enabled);
35 obj->setEnabled(enabled);
37 obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
41 /////////////////////////////////////////////////////////////////////////
45 /////////////////////////////////////////////////////////////////////////
50 CheckedLineEdit(QLineEdit * input, QWidget * label = 0);
60 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
61 : input_(input), label_(label)
65 bool CheckedLineEdit::check() const
67 QValidator const * validator = input_->validator();
71 QString t = input_->text();
73 bool const valid = validator->validate(t, p) == QValidator::Acceptable;
76 setValid(input_, valid);
78 setValid(label_, valid);
84 /////////////////////////////////////////////////////////////////////////
86 // ButtonController::Private
88 /////////////////////////////////////////////////////////////////////////
90 class ButtonController::Private
93 typedef QList<CheckedLineEdit> CheckedWidgetList;
96 : okay_(0), apply_(0), cancel_(0), restore_(0), auto_apply_(0),
97 policy_(ButtonPolicy::IgnorantPolicy)
100 /// \return true if all CheckedWidgets are in a valid state.
101 bool checkWidgets() const
104 for (const CheckedLineEdit & w : checked_widgets_)
110 CheckedWidgetList checked_widgets_;
113 QPushButton * apply_;
114 QPushButton * cancel_;
115 QPushButton * restore_;
116 QCheckBox * auto_apply_;
118 typedef QList<QWidget *> Widgets;
121 ButtonPolicy policy_;
125 /////////////////////////////////////////////////////////////////////////
129 /////////////////////////////////////////////////////////////////////////
131 ButtonController::ButtonController()
136 ButtonController::~ButtonController()
142 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
144 d->policy_.setPolicy(policy);
148 void ButtonController::ok()
150 input(ButtonPolicy::SMI_OKAY);
154 void ButtonController::input(ButtonPolicy::SMInput in)
156 if (ButtonPolicy::SMI_NOOP == in)
158 d->policy_.input(in);
163 void ButtonController::apply()
165 input(ButtonPolicy::SMI_APPLY);
169 void ButtonController::autoApply()
171 input(ButtonPolicy::SMI_AUTOAPPLY);
175 void ButtonController::cancel()
177 input(ButtonPolicy::SMI_CANCEL);
181 void ButtonController::restore()
183 input(ButtonPolicy::SMI_RESTORE);
187 void ButtonController::hide()
189 input(ButtonPolicy::SMI_HIDE);
193 void ButtonController::setValid(bool v)
195 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
199 bool ButtonController::setReadOnly(bool ro)
201 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
203 d->policy_.input(ro ?
204 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
205 // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
206 // they allowed to be enabled, so when you plan to
207 // reenable this call, read this before:
208 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
214 void ButtonController::refresh() const
216 LYXERR(Debug::GUI, "Calling BC refresh()");
218 bool const all_valid = d->checkWidgets();
222 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
223 d->okay_->setEnabled(enabled);
227 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
228 d->apply_->setEnabled(enabled);
232 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
233 d->restore_->setEnabled(enabled);
236 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
238 d->cancel_->setText(qt_("Cancel"));
240 d->cancel_->setText(qt_("Close"));
242 if (d->auto_apply_) {
243 bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
244 d->auto_apply_->setEnabled(enabled);
250 void ButtonController::refreshReadOnly() const
252 if (d->read_only_.empty())
254 bool const enable = !policy().isReadOnly();
255 for(QWidget * w : d->read_only_)
256 setWidgetEnabled(w, enable);
260 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
262 d->checked_widgets_.append(CheckedLineEdit(input, label));
266 void ButtonController::setOK(QPushButton * obj)
272 void ButtonController::setApply(QPushButton * obj)
278 void ButtonController::setAutoApply(QCheckBox * obj)
280 d->auto_apply_ = obj;
284 void ButtonController::setCancel(QPushButton * obj)
290 void ButtonController::setRestore(QPushButton * obj)
296 void ButtonController::addReadOnly(QWidget * obj)
298 d->read_only_.push_back(obj);
301 ButtonPolicy const & ButtonController::policy() const
307 ButtonPolicy & ButtonController::policy()
312 } // namespace frontend