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 = nullptr);
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_(nullptr), apply_(nullptr), cancel_(nullptr),
97 restore_(nullptr), auto_apply_(nullptr), default_(nullptr),
98 policy_(ButtonPolicy::IgnorantPolicy)
101 /// \return true if all CheckedWidgets are in a valid state.
102 bool checkWidgets() const
105 for (const CheckedLineEdit & w : checked_widgets_)
111 CheckedWidgetList checked_widgets_;
114 QPushButton * apply_;
115 QPushButton * cancel_;
116 QPushButton * restore_;
117 QCheckBox * auto_apply_;
118 QPushButton * default_;
120 typedef QList<QWidget *> Widgets;
123 ButtonPolicy policy_;
127 /////////////////////////////////////////////////////////////////////////
131 /////////////////////////////////////////////////////////////////////////
133 ButtonController::ButtonController()
138 ButtonController::~ButtonController()
144 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
146 d->policy_.setPolicy(policy);
150 void ButtonController::ok()
152 input(ButtonPolicy::SMI_OKAY);
156 void ButtonController::input(ButtonPolicy::SMInput in)
158 if (ButtonPolicy::SMI_NOOP == in)
160 d->policy_.input(in);
165 void ButtonController::apply()
167 input(ButtonPolicy::SMI_APPLY);
171 void ButtonController::autoApply()
173 input(ButtonPolicy::SMI_AUTOAPPLY);
177 void ButtonController::cancel()
179 input(ButtonPolicy::SMI_CANCEL);
183 void ButtonController::restore()
185 input(ButtonPolicy::SMI_RESTORE);
189 void ButtonController::hide()
191 input(ButtonPolicy::SMI_HIDE);
195 void ButtonController::setValid(bool v)
197 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
201 bool ButtonController::setReadOnly(bool ro)
203 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
205 d->policy_.input(ro ?
206 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
207 // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
208 // they allowed to be enabled, so when you plan to
209 // reenable this call, read this before:
210 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
216 void ButtonController::refresh() const
218 LYXERR(Debug::GUI, "Calling BC refresh()");
220 bool const all_valid = d->checkWidgets();
224 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
225 d->okay_->setEnabled(enabled);
229 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
230 d->apply_->setEnabled(enabled);
234 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
235 d->restore_->setEnabled(enabled);
238 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
240 d->cancel_->setText(qt_("Cancel"));
242 d->cancel_->setText(qt_("Close"));
244 if (d->auto_apply_) {
245 bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
246 d->auto_apply_->setEnabled(enabled);
249 // Somewhere in the chain this can lose default status (#11417)
250 d->default_->setDefault(true);
254 void ButtonController::refreshReadOnly() const
256 if (d->read_only_.empty())
258 bool const enable = !policy().isReadOnly();
259 for(QWidget * w : d->read_only_)
260 setWidgetEnabled(w, enable);
264 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
266 d->checked_widgets_.append(CheckedLineEdit(input, label));
270 void ButtonController::setOK(QPushButton * obj, bool const default_button)
278 void ButtonController::setApply(QPushButton * obj, bool const default_button)
286 void ButtonController::setAutoApply(QCheckBox * obj)
288 d->auto_apply_ = obj;
292 void ButtonController::setCancel(QPushButton * obj, bool const default_button)
300 void ButtonController::setRestore(QPushButton * obj, bool const default_button)
308 void ButtonController::addReadOnly(QWidget * obj)
310 d->read_only_.push_back(obj);
313 ButtonPolicy const & ButtonController::policy() const
319 ButtonPolicy & ButtonController::policy()
324 } // namespace frontend