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 if (!input_->isEnabled()) {
57 // we do not check diabled widgets
59 setValid(label_, true);
63 QValidator const * validator = input_->validator();
67 QString t = input_->text();
69 bool const valid = validator->validate(t, p) == QValidator::Acceptable;
72 setValid(input_, valid);
74 setValid(label_, valid);
80 /////////////////////////////////////////////////////////////////////////
82 // ButtonController::Private
84 /////////////////////////////////////////////////////////////////////////
86 class ButtonController::Private
89 typedef QList<CheckedLineEdit> CheckedWidgetList;
93 /// \return true if all CheckedWidgets are in a valid state.
94 bool checkWidgets() const
97 for (const CheckedLineEdit & w : checked_widgets_)
103 CheckedWidgetList checked_widgets_;
105 QPushButton * okay_ = nullptr;
106 QPushButton * apply_ = nullptr;
107 QPushButton * cancel_ = nullptr;
108 QPushButton * restore_ = nullptr;
109 QCheckBox * auto_apply_ = nullptr;
110 QPushButton * default_ = nullptr;
112 typedef QList<QWidget *> Widgets;
115 ButtonPolicy policy_ {ButtonPolicy::IgnorantPolicy};
119 /////////////////////////////////////////////////////////////////////////
123 /////////////////////////////////////////////////////////////////////////
125 ButtonController::ButtonController()
130 ButtonController::~ButtonController()
136 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
138 d->policy_.setPolicy(policy);
142 void ButtonController::ok()
144 input(ButtonPolicy::SMI_OKAY);
148 void ButtonController::input(ButtonPolicy::SMInput in)
150 if (ButtonPolicy::SMI_NOOP == in)
152 d->policy_.input(in);
157 void ButtonController::apply()
159 input(ButtonPolicy::SMI_APPLY);
163 void ButtonController::autoApply()
165 input(ButtonPolicy::SMI_AUTOAPPLY);
169 void ButtonController::cancel()
171 input(ButtonPolicy::SMI_CANCEL);
175 void ButtonController::restore()
177 input(ButtonPolicy::SMI_RESTORE);
181 void ButtonController::hide()
183 input(ButtonPolicy::SMI_HIDE);
187 void ButtonController::setValid(bool v)
189 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
193 bool ButtonController::setReadOnly(bool ro)
195 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
197 d->policy_.input(ro ?
198 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
205 void ButtonController::refresh() const
207 LYXERR(Debug::GUI, "Calling BC refresh()");
209 bool const all_valid = d->checkWidgets();
213 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
214 d->okay_->setEnabled(enabled);
218 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
219 d->apply_->setEnabled(enabled);
223 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
224 d->restore_->setEnabled(enabled);
227 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
229 d->cancel_->setText(qt_("Cancel"));
231 d->cancel_->setText(qt_("Close"));
233 if (d->auto_apply_) {
234 bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
235 d->auto_apply_->setEnabled(enabled);
238 // Somewhere in the chain this can lose default status (#11417)
239 d->default_->setDefault(true);
243 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
245 d->checked_widgets_.append(CheckedLineEdit(input, label));
249 void ButtonController::setOK(QPushButton * obj, bool const default_button)
257 void ButtonController::setApply(QPushButton * obj, bool const default_button)
265 void ButtonController::setAutoApply(QCheckBox * obj)
267 d->auto_apply_ = obj;
271 void ButtonController::setCancel(QPushButton * obj, bool const default_button)
279 void ButtonController::setRestore(QPushButton * obj, bool const default_button)
287 void ButtonController::addReadOnly(QWidget * obj)
289 d->read_only_.push_back(obj);
292 ButtonPolicy const & ButtonController::policy() const
298 ButtonPolicy & ButtonController::policy()
303 } // namespace frontend