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"
18 #include "support/foreach.h"
21 #include <QPushButton>
31 static void setWidgetEnabled(QWidget * obj, bool enabled)
33 if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
34 le->setReadOnly(!enabled);
36 obj->setEnabled(enabled);
38 obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
42 /////////////////////////////////////////////////////////////////////////
46 /////////////////////////////////////////////////////////////////////////
51 CheckedLineEdit(QLineEdit * input, QWidget * label = 0);
61 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
62 : input_(input), label_(label)
66 bool CheckedLineEdit::check() const
68 QValidator const * validator = input_->validator();
72 QString t = input_->text();
74 bool const valid = validator->validate(t, p) == QValidator::Acceptable;
77 setValid(input_, valid);
79 setValid(label_, valid);
85 /////////////////////////////////////////////////////////////////////////
87 // ButtonController::Private
89 /////////////////////////////////////////////////////////////////////////
91 class ButtonController::Private
94 typedef QList<CheckedLineEdit> CheckedWidgetList;
97 : okay_(0), apply_(0), cancel_(0), restore_(0), auto_apply_(0),
98 policy_(ButtonPolicy::IgnorantPolicy)
101 /// \return true if all CheckedWidgets are in a valid state.
102 bool checkWidgets() const
105 foreach (const CheckedLineEdit & w, checked_widgets_)
111 CheckedWidgetList checked_widgets_;
114 QPushButton * apply_;
115 QPushButton * cancel_;
116 QPushButton * restore_;
117 QCheckBox * auto_apply_;
119 typedef QList<QWidget *> Widgets;
122 ButtonPolicy policy_;
126 /////////////////////////////////////////////////////////////////////////
130 /////////////////////////////////////////////////////////////////////////
132 ButtonController::ButtonController()
137 ButtonController::~ButtonController()
143 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
145 d->policy_.setPolicy(policy);
149 void ButtonController::ok()
151 input(ButtonPolicy::SMI_OKAY);
155 void ButtonController::input(ButtonPolicy::SMInput in)
157 if (ButtonPolicy::SMI_NOOP == in)
159 d->policy_.input(in);
164 void ButtonController::apply()
166 input(ButtonPolicy::SMI_APPLY);
170 void ButtonController::autoApply()
172 input(ButtonPolicy::SMI_AUTOAPPLY);
176 void ButtonController::cancel()
178 input(ButtonPolicy::SMI_CANCEL);
182 void ButtonController::restore()
184 input(ButtonPolicy::SMI_RESTORE);
188 void ButtonController::hide()
190 input(ButtonPolicy::SMI_HIDE);
194 void ButtonController::setValid(bool v)
196 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
200 bool ButtonController::setReadOnly(bool ro)
202 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
204 d->policy_.input(ro ?
205 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
206 // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
207 // they allowed to be enabled, so when you plan to
208 // reenable this call, read this before:
209 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
215 void ButtonController::refresh() const
217 LYXERR(Debug::GUI, "Calling BC refresh()");
219 bool const all_valid = d->checkWidgets();
223 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
224 d->okay_->setEnabled(enabled);
228 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
229 d->apply_->setEnabled(enabled);
233 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
234 d->restore_->setEnabled(enabled);
237 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
239 d->cancel_->setText(qt_("Cancel"));
241 d->cancel_->setText(qt_("Close"));
243 if (d->auto_apply_) {
244 bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
245 d->auto_apply_->setEnabled(enabled);
251 void ButtonController::refreshReadOnly() const
253 if (d->read_only_.empty())
256 bool const enable = !policy().isReadOnly();
258 foreach (QWidget * w, d->read_only_)
259 setWidgetEnabled(w, enable);
263 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
265 d->checked_widgets_.append(CheckedLineEdit(input, label));
269 void ButtonController::setOK(QPushButton * obj)
275 void ButtonController::setApply(QPushButton * obj)
281 void ButtonController::setAutoApply(QCheckBox * obj)
283 d->auto_apply_ = obj;
287 void ButtonController::setCancel(QPushButton * obj)
293 void ButtonController::setRestore(QPushButton * obj)
299 void ButtonController::addReadOnly(QWidget * obj)
301 d->read_only_.push_back(obj);
304 ButtonPolicy const & ButtonController::policy() const
310 ButtonPolicy & ButtonController::policy()
315 } // namespace frontend