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;
85 : okay_(nullptr), apply_(nullptr), cancel_(nullptr),
86 restore_(nullptr), auto_apply_(nullptr), default_(nullptr),
87 policy_(ButtonPolicy::IgnorantPolicy)
90 /// \return true if all CheckedWidgets are in a valid state.
91 bool checkWidgets() const
94 for (const CheckedLineEdit & w : checked_widgets_)
100 CheckedWidgetList checked_widgets_;
103 QPushButton * apply_;
104 QPushButton * cancel_;
105 QPushButton * restore_;
106 QCheckBox * auto_apply_;
107 QPushButton * default_;
109 typedef QList<QWidget *> Widgets;
112 ButtonPolicy policy_;
116 /////////////////////////////////////////////////////////////////////////
120 /////////////////////////////////////////////////////////////////////////
122 ButtonController::ButtonController()
127 ButtonController::~ButtonController()
133 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
135 d->policy_.setPolicy(policy);
139 void ButtonController::ok()
141 input(ButtonPolicy::SMI_OKAY);
145 void ButtonController::input(ButtonPolicy::SMInput in)
147 if (ButtonPolicy::SMI_NOOP == in)
149 d->policy_.input(in);
154 void ButtonController::apply()
156 input(ButtonPolicy::SMI_APPLY);
160 void ButtonController::autoApply()
162 input(ButtonPolicy::SMI_AUTOAPPLY);
166 void ButtonController::cancel()
168 input(ButtonPolicy::SMI_CANCEL);
172 void ButtonController::restore()
174 input(ButtonPolicy::SMI_RESTORE);
178 void ButtonController::hide()
180 input(ButtonPolicy::SMI_HIDE);
184 void ButtonController::setValid(bool v)
186 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
190 bool ButtonController::setReadOnly(bool ro)
192 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
194 d->policy_.input(ro ?
195 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
202 void ButtonController::refresh() const
204 LYXERR(Debug::GUI, "Calling BC refresh()");
206 bool const all_valid = d->checkWidgets();
210 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
211 d->okay_->setEnabled(enabled);
215 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
216 d->apply_->setEnabled(enabled);
220 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
221 d->restore_->setEnabled(enabled);
224 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
226 d->cancel_->setText(qt_("Cancel"));
228 d->cancel_->setText(qt_("Close"));
230 if (d->auto_apply_) {
231 bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
232 d->auto_apply_->setEnabled(enabled);
235 // Somewhere in the chain this can lose default status (#11417)
236 d->default_->setDefault(true);
240 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
242 d->checked_widgets_.append(CheckedLineEdit(input, label));
246 void ButtonController::setOK(QPushButton * obj, bool const default_button)
254 void ButtonController::setApply(QPushButton * obj, bool const default_button)
262 void ButtonController::setAutoApply(QCheckBox * obj)
264 d->auto_apply_ = obj;
268 void ButtonController::setCancel(QPushButton * obj, bool const default_button)
276 void ButtonController::setRestore(QPushButton * obj, bool const default_button)
284 void ButtonController::addReadOnly(QWidget * obj)
286 d->read_only_.push_back(obj);
289 ButtonPolicy const & ButtonController::policy() const
295 ButtonPolicy & ButtonController::policy()
300 } // namespace frontend