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 /////////////////////////////////////////////////////////////////////////
34 /////////////////////////////////////////////////////////////////////////
39 CheckedLineEdit(QLineEdit * input, QWidget * label = nullptr);
49 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
50 : input_(input), label_(label)
54 bool CheckedLineEdit::check() const
56 QValidator const * validator = input_->validator();
60 QString t = input_->text();
62 bool const valid = validator->validate(t, p) == QValidator::Acceptable;
65 setValid(input_, valid);
67 setValid(label_, valid);
73 /////////////////////////////////////////////////////////////////////////
75 // ButtonController::Private
77 /////////////////////////////////////////////////////////////////////////
79 class ButtonController::Private
82 typedef QList<CheckedLineEdit> CheckedWidgetList;
86 /// \return true if all CheckedWidgets are in a valid state.
87 bool checkWidgets() const
90 for (const CheckedLineEdit & w : checked_widgets_)
96 CheckedWidgetList checked_widgets_;
98 QPushButton * okay_ = nullptr;
99 QPushButton * apply_ = nullptr;
100 QPushButton * cancel_ = nullptr;
101 QPushButton * restore_ = nullptr;
102 QCheckBox * auto_apply_ = nullptr;
103 QPushButton * default_ = nullptr;
105 typedef QList<QWidget *> Widgets;
108 ButtonPolicy policy_ {ButtonPolicy::IgnorantPolicy};
112 /////////////////////////////////////////////////////////////////////////
116 /////////////////////////////////////////////////////////////////////////
118 ButtonController::ButtonController()
123 ButtonController::~ButtonController()
129 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
131 d->policy_.setPolicy(policy);
135 void ButtonController::ok()
137 input(ButtonPolicy::SMI_OKAY);
141 void ButtonController::input(ButtonPolicy::SMInput in)
143 if (ButtonPolicy::SMI_NOOP == in)
145 d->policy_.input(in);
150 void ButtonController::apply()
152 input(ButtonPolicy::SMI_APPLY);
156 void ButtonController::autoApply()
158 input(ButtonPolicy::SMI_AUTOAPPLY);
162 void ButtonController::cancel()
164 input(ButtonPolicy::SMI_CANCEL);
168 void ButtonController::restore()
170 input(ButtonPolicy::SMI_RESTORE);
174 void ButtonController::hide()
176 input(ButtonPolicy::SMI_HIDE);
180 void ButtonController::setValid(bool v)
182 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
186 bool ButtonController::setReadOnly(bool ro)
188 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
190 d->policy_.input(ro ?
191 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
198 void ButtonController::refresh() const
200 LYXERR(Debug::GUI, "Calling BC refresh()");
202 bool const all_valid = d->checkWidgets();
206 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
207 d->okay_->setEnabled(enabled);
211 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
212 d->apply_->setEnabled(enabled);
216 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
217 d->restore_->setEnabled(enabled);
220 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
222 d->cancel_->setText(qt_("Cancel"));
224 d->cancel_->setText(qt_("Close"));
226 if (d->auto_apply_) {
227 bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
228 d->auto_apply_->setEnabled(enabled);
231 // Somewhere in the chain this can lose default status (#11417)
232 d->default_->setDefault(true);
236 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
238 d->checked_widgets_.append(CheckedLineEdit(input, label));
242 void ButtonController::setOK(QPushButton * obj, bool const default_button)
250 void ButtonController::setApply(QPushButton * obj, bool const default_button)
258 void ButtonController::setAutoApply(QCheckBox * obj)
260 d->auto_apply_ = obj;
264 void ButtonController::setCancel(QPushButton * obj, bool const default_button)
272 void ButtonController::setRestore(QPushButton * obj, bool const default_button)
280 void ButtonController::addReadOnly(QWidget * obj)
282 d->read_only_.push_back(obj);
285 ButtonPolicy const & ButtonController::policy() const
291 ButtonPolicy & ButtonController::policy()
296 } // namespace frontend