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"
18 #include "support/foreach.h"
20 #include <QPushButton>
30 static void setWidgetEnabled(QWidget * obj, bool enabled)
32 if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
33 le->setReadOnly(!enabled);
35 obj->setEnabled(enabled);
37 obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
41 /////////////////////////////////////////////////////////////////////////
45 /////////////////////////////////////////////////////////////////////////
50 CheckedLineEdit(QLineEdit * input, QWidget * label = 0);
60 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
61 : input_(input), label_(label)
65 bool CheckedLineEdit::check() const
67 QValidator const * validator = input_->validator();
71 QString t = input_->text();
73 bool const valid = validator->validate(t, p) == QValidator::Acceptable;
76 setValid(input_, valid);
78 setValid(label_, valid);
84 /////////////////////////////////////////////////////////////////////////
86 // ButtonController::Private
88 /////////////////////////////////////////////////////////////////////////
90 class ButtonController::Private
93 typedef QList<CheckedLineEdit> CheckedWidgetList;
96 : okay_(0), apply_(0), cancel_(0), restore_(0),
97 policy_(ButtonPolicy::IgnorantPolicy)
100 /// \return true if all CheckedWidgets are in a valid state.
101 bool checkWidgets() const
104 foreach (const CheckedLineEdit & w, checked_widgets_)
110 CheckedWidgetList checked_widgets_;
113 QPushButton * apply_;
114 QPushButton * cancel_;
115 QPushButton * restore_;
117 typedef QList<QWidget *> Widgets;
120 ButtonPolicy policy_;
124 /////////////////////////////////////////////////////////////////////////
128 /////////////////////////////////////////////////////////////////////////
130 ButtonController::ButtonController()
135 ButtonController::~ButtonController()
141 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
143 d->policy_.setPolicy(policy);
147 void ButtonController::ok()
149 input(ButtonPolicy::SMI_OKAY);
153 void ButtonController::input(ButtonPolicy::SMInput in)
155 if (ButtonPolicy::SMI_NOOP == in)
157 d->policy_.input(in);
162 void ButtonController::apply()
164 input(ButtonPolicy::SMI_APPLY);
168 void ButtonController::cancel()
170 input(ButtonPolicy::SMI_CANCEL);
174 void ButtonController::restore()
176 input(ButtonPolicy::SMI_RESTORE);
180 void ButtonController::hide()
182 input(ButtonPolicy::SMI_HIDE);
186 void ButtonController::setValid(bool v)
188 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
192 bool ButtonController::setReadOnly(bool ro)
194 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
196 d->policy_.input(ro ?
197 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
198 // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
199 // they allowed to be enabled, so when you plan to
200 // reenable this call, read this before:
201 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
207 void ButtonController::refresh() const
209 LYXERR(Debug::GUI, "Calling BC refresh()");
211 bool const all_valid = d->checkWidgets();
215 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
216 d->okay_->setEnabled(enabled);
220 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
221 d->apply_->setEnabled(enabled);
225 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
226 d->restore_->setEnabled(enabled);
229 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
231 d->cancel_->setText(qt_("Cancel"));
233 d->cancel_->setText(qt_("Close"));
238 void ButtonController::refreshReadOnly() const
240 if (d->read_only_.empty())
243 bool const enable = !policy().isReadOnly();
245 foreach (QWidget * w, d->read_only_)
246 setWidgetEnabled(w, enable);
250 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
252 d->checked_widgets_.append(CheckedLineEdit(input, label));
256 void ButtonController::setOK(QPushButton * obj)
262 void ButtonController::setApply(QPushButton * obj)
268 void ButtonController::setCancel(QPushButton * obj)
274 void ButtonController::setRestore(QPushButton * obj)
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