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(guiApp ? guiApp->getScaledPixmap("images/", "emblem-shellescape")
92 : getPixmap("images/", "emblem-shellescape", "svgz,png"));
93 QTabBar * tb = qobject_cast<QTabWidget*>(target_)->tabBar();
94 tb->setTabIcon(tab_index_, warn);
95 tb->setTabToolTip(tab_index_, qt_("This tab contains invalid input. Please fix!"));
103 void CheckedLineEdit::setSectionsValid() const
105 if (target_ && tab_index_ >= 0 && qobject_cast<QTabWidget*>(target_) != nullptr) {
106 QTabBar * tb = qobject_cast<QTabWidget*>(target_)->tabBar();
107 tb->setTabIcon(tab_index_, QIcon());
108 tb->setTabToolTip(tab_index_, QString());
110 else if (!panel_name_.isEmpty() && qobject_cast<PanelStack*>(target_) != nullptr)
111 qobject_cast<PanelStack*>(target_)->markPanelValid(panel_name_, true);
115 /////////////////////////////////////////////////////////////////////////
117 // ButtonController::Private
119 /////////////////////////////////////////////////////////////////////////
121 class ButtonController::Private
124 typedef QList<CheckedLineEdit> CheckedWidgetList;
128 /// \return true if all CheckedWidgets are in a valid state.
129 bool checkWidgets() const
132 for (const CheckedLineEdit & w : checked_widgets_) {
133 w.setSectionsValid();
135 for (const CheckedLineEdit & w : checked_widgets_)
141 CheckedWidgetList checked_widgets_;
143 QPushButton * okay_ = nullptr;
144 QPushButton * apply_ = nullptr;
145 QPushButton * cancel_ = nullptr;
146 QPushButton * restore_ = nullptr;
147 QCheckBox * auto_apply_ = nullptr;
148 QPushButton * default_ = nullptr;
150 typedef QList<QWidget *> Widgets;
153 ButtonPolicy policy_ {ButtonPolicy::IgnorantPolicy};
157 /////////////////////////////////////////////////////////////////////////
161 /////////////////////////////////////////////////////////////////////////
163 ButtonController::ButtonController()
168 ButtonController::~ButtonController()
174 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
176 d->policy_.setPolicy(policy);
180 void ButtonController::ok()
182 input(ButtonPolicy::SMI_OKAY);
186 void ButtonController::input(ButtonPolicy::SMInput in)
188 if (ButtonPolicy::SMI_NOOP == in)
190 d->policy_.input(in);
195 void ButtonController::apply()
197 input(ButtonPolicy::SMI_APPLY);
201 void ButtonController::autoApply()
203 input(ButtonPolicy::SMI_AUTOAPPLY);
207 void ButtonController::cancel()
209 input(ButtonPolicy::SMI_CANCEL);
213 void ButtonController::restore()
215 input(ButtonPolicy::SMI_RESTORE);
219 void ButtonController::hide()
221 input(ButtonPolicy::SMI_HIDE);
225 void ButtonController::setValid(bool v)
227 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
231 bool ButtonController::setReadOnly(bool ro)
233 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
235 d->policy_.input(ro ?
236 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
243 void ButtonController::refresh() const
245 LYXERR(Debug::GUI, "Calling BC refresh()");
247 bool const all_valid = d->checkWidgets();
251 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
252 d->okay_->setEnabled(enabled);
256 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
257 d->apply_->setEnabled(enabled);
261 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
262 d->restore_->setEnabled(enabled);
265 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
267 d->cancel_->setText(qt_("Cancel"));
269 d->cancel_->setText(qt_("Close"));
271 if (d->auto_apply_) {
272 bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
273 d->auto_apply_->setEnabled(enabled);
276 // Somewhere in the chain this can lose default status (#11417)
277 d->default_->setDefault(true);
281 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * target, int tabindex)
283 d->checked_widgets_.append(CheckedLineEdit(input, target, tabindex));
287 void ButtonController::addCheckedLineEditPanel(QLineEdit * input, QWidget * target, QString const panel)
289 d->checked_widgets_.append(CheckedLineEdit(input, target, -1, panel));
293 void ButtonController::setOK(QPushButton * obj, bool const default_button)
301 void ButtonController::setApply(QPushButton * obj, bool const default_button)
309 void ButtonController::setAutoApply(QCheckBox * obj)
311 d->auto_apply_ = obj;
315 void ButtonController::setCancel(QPushButton * obj, bool const default_button)
323 void ButtonController::setRestore(QPushButton * obj, bool const default_button)
331 void ButtonController::addReadOnly(QWidget * obj)
333 d->read_only_.push_back(obj);
336 ButtonPolicy const & ButtonController::policy() const
342 ButtonPolicy & ButtonController::policy()
347 } // namespace frontend