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"
14 #include "GuiApplication.h"
15 #include "PanelStack.h"
17 #include "qt_helpers.h"
19 #include "support/debug.h"
22 #include <QPushButton>
33 /////////////////////////////////////////////////////////////////////////
37 /////////////////////////////////////////////////////////////////////////
42 CheckedLineEdit(QLineEdit * input, QWidget * label = nullptr,
43 int tabindex = -1, QString const panel = QString());
44 /// check the widget and do visual marking
46 /// reset all visual markings for tabs or panel sections
47 void setSectionsValid() const;
58 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label,
59 int tabindex, QString const panel)
60 : input_(input), target_(label), tab_index_(tabindex), panel_name_(panel)
64 bool CheckedLineEdit::check() const
66 if (!input_->isEnabled()) {
67 // we do not check diabled widgets
69 setValid(target_, true);
73 QValidator const * validator = input_->validator();
77 QString t = input_->text();
79 bool const valid = validator->validate(t, p) == QValidator::Acceptable;
82 setValid(input_, valid);
84 if (!valid && !panel_name_.isEmpty() && qobject_cast<PanelStack*>(target_) != nullptr) {
85 qobject_cast<PanelStack*>(target_)->markPanelValid(panel_name_, false);
86 // this is a panel, so stop here.
89 setValid(target_, valid);
90 if (!valid && tab_index_ >= 0 && qobject_cast<QTabWidget*>(target_) != nullptr) {
91 QIcon warn(getPixmap("images/", "emblem-shellescape", "svgz,png"));
92 QTabBar * tb = qobject_cast<QTabWidget*>(target_)->tabBar();
93 tb->setTabIcon(tab_index_, warn);
94 tb->setTabToolTip(tab_index_, qt_("This tab contains invalid input. Please fix!"));
102 void CheckedLineEdit::setSectionsValid() const
104 if (target_ && tab_index_ >= 0 && qobject_cast<QTabWidget*>(target_) != nullptr) {
105 QTabBar * tb = qobject_cast<QTabWidget*>(target_)->tabBar();
106 tb->setTabIcon(tab_index_, QIcon());
107 tb->setTabToolTip(tab_index_, QString());
109 else if (!panel_name_.isEmpty() && qobject_cast<PanelStack*>(target_) != nullptr)
110 qobject_cast<PanelStack*>(target_)->markPanelValid(panel_name_, true);
114 /////////////////////////////////////////////////////////////////////////
116 // ButtonController::Private
118 /////////////////////////////////////////////////////////////////////////
120 class ButtonController::Private
123 typedef QList<CheckedLineEdit> CheckedWidgetList;
127 /// \return true if all CheckedWidgets are in a valid state.
128 bool checkWidgets() const
131 for (const CheckedLineEdit & w : checked_widgets_) {
132 w.setSectionsValid();
134 for (const CheckedLineEdit & w : checked_widgets_)
140 CheckedWidgetList checked_widgets_;
142 QPushButton * okay_ = nullptr;
143 QPushButton * apply_ = nullptr;
144 QPushButton * cancel_ = nullptr;
145 QPushButton * restore_ = nullptr;
146 QCheckBox * auto_apply_ = nullptr;
147 QPushButton * default_ = nullptr;
149 typedef QList<QWidget *> Widgets;
152 ButtonPolicy policy_ {ButtonPolicy::IgnorantPolicy};
156 /////////////////////////////////////////////////////////////////////////
160 /////////////////////////////////////////////////////////////////////////
162 ButtonController::ButtonController()
167 ButtonController::~ButtonController()
173 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
175 d->policy_.setPolicy(policy);
179 void ButtonController::ok()
181 input(ButtonPolicy::SMI_OKAY);
185 void ButtonController::input(ButtonPolicy::SMInput in)
187 if (ButtonPolicy::SMI_NOOP == in)
189 d->policy_.input(in);
194 void ButtonController::apply()
196 input(ButtonPolicy::SMI_APPLY);
200 void ButtonController::autoApply()
202 input(ButtonPolicy::SMI_AUTOAPPLY);
206 void ButtonController::cancel()
208 input(ButtonPolicy::SMI_CANCEL);
212 void ButtonController::restore()
214 input(ButtonPolicy::SMI_RESTORE);
218 void ButtonController::hide()
220 input(ButtonPolicy::SMI_HIDE);
224 void ButtonController::setValid(bool v)
226 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
230 bool ButtonController::setReadOnly(bool ro)
232 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
234 d->policy_.input(ro ?
235 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
242 void ButtonController::refresh() const
244 LYXERR(Debug::GUI, "Calling BC refresh()");
246 bool const all_valid = d->checkWidgets();
250 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
251 d->okay_->setEnabled(enabled);
255 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
256 d->apply_->setEnabled(enabled);
260 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
261 d->restore_->setEnabled(enabled);
264 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
266 d->cancel_->setText(qt_("Cancel"));
268 d->cancel_->setText(qt_("Close"));
270 if (d->auto_apply_) {
271 bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
272 d->auto_apply_->setEnabled(enabled);
275 // Somewhere in the chain this can lose default status (#11417)
276 d->default_->setDefault(true);
280 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * target, int tabindex)
282 d->checked_widgets_.append(CheckedLineEdit(input, target, tabindex));
286 void ButtonController::addCheckedLineEditPanel(QLineEdit * input, QWidget * target, QString const panel)
288 d->checked_widgets_.append(CheckedLineEdit(input, target, -1, panel));
292 void ButtonController::setOK(QPushButton * obj, bool const default_button)
300 void ButtonController::setApply(QPushButton * obj, bool const default_button)
308 void ButtonController::setAutoApply(QCheckBox * obj)
310 d->auto_apply_ = obj;
314 void ButtonController::setCancel(QPushButton * obj, bool const default_button)
322 void ButtonController::setRestore(QPushButton * obj, bool const default_button)
330 void ButtonController::addReadOnly(QWidget * obj)
332 d->read_only_.push_back(obj);
335 ButtonPolicy const & ButtonController::policy() const
341 ButtonPolicy & ButtonController::policy()
346 } // namespace frontend