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), default_(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_;
117 QPushButton * default_;
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);
248 // Somewhere in the chain this can lose default status (#11417)
249 d->default_->setDefault(true);
253 void ButtonController::refreshReadOnly() const
255 if (d->read_only_.empty())
257 bool const enable = !policy().isReadOnly();
258 for(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, bool const default_button)
277 void ButtonController::setApply(QPushButton * obj, bool const default_button)
285 void ButtonController::setAutoApply(QCheckBox * obj)
287 d->auto_apply_ = obj;
291 void ButtonController::setCancel(QPushButton * obj, bool const default_button)
299 void ButtonController::setRestore(QPushButton * obj, bool const default_button)
307 void ButtonController::addReadOnly(QWidget * obj)
309 d->read_only_.push_back(obj);
312 ButtonPolicy const & ButtonController::policy() const
318 ButtonPolicy & ButtonController::policy()
323 } // namespace frontend