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"
19 #include <QPushButton>
27 ButtonController::ButtonController()
28 : okay_(0), apply_(0), cancel_(0), restore_(0),
29 policy_(ButtonPolicy::IgnorantPolicy)
33 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
35 policy_ = ButtonPolicy(policy);
39 void ButtonController::ok()
41 input(ButtonPolicy::SMI_OKAY);
45 void ButtonController::input(ButtonPolicy::SMInput in)
47 if (ButtonPolicy::SMI_NOOP == in)
54 void ButtonController::apply()
56 input(ButtonPolicy::SMI_APPLY);
60 void ButtonController::cancel()
62 input(ButtonPolicy::SMI_CANCEL);
66 void ButtonController::restore()
68 input(ButtonPolicy::SMI_RESTORE);
72 void ButtonController::hide()
74 input(ButtonPolicy::SMI_HIDE);
78 void ButtonController::setValid(bool v)
80 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
84 bool ButtonController::setReadOnly(bool ro)
86 LYXERR(Debug::GUI, "Setting controller ro: " << ro);
89 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
90 // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
91 // they allowed to be enabled, so when you plan to
92 // reenable this call, read this before:
93 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
99 void ButtonController::refresh() const
101 LYXERR(Debug::GUI, "Calling BC refresh()");
103 bool const all_valid = checkWidgets();
107 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
108 okay_->setEnabled(enabled);
112 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
113 apply_->setEnabled(enabled);
117 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
118 restore_->setEnabled(enabled);
121 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
123 cancel_->setText(qt_("Cancel"));
125 cancel_->setText(qt_("Close"));
130 void ButtonController::refreshReadOnly() const
132 if (read_only_.empty())
135 bool const enable = !policy().isReadOnly();
137 Widgets::const_iterator end = read_only_.end();
138 Widgets::const_iterator iter = read_only_.begin();
139 for (; iter != end; ++iter)
140 setWidgetEnabled(*iter, enable);
144 void ButtonController::setWidgetEnabled(QWidget * obj, bool enabled) const
146 if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
147 le->setReadOnly(!enabled);
149 obj->setEnabled(enabled);
151 obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
155 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
157 checked_widgets.push_back(CheckedLineEdit(input, label));
161 bool ButtonController::checkWidgets() const
165 CheckedWidgetList::const_iterator it = checked_widgets.begin();
166 CheckedWidgetList::const_iterator end = checked_widgets.end();
168 for (; it != end; ++it)
169 valid &= it->check();
171 // return valid status after checking ALL widgets
176 //////////////////////////////////////////////////////////////
180 //////////////////////////////////////////////////////////////
182 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
183 : input_(input), label_(label)
187 bool CheckedLineEdit::check() const
189 QValidator const * validator = input_->validator();
193 QString t = input_->text();
195 bool const valid = validator->validate(t, p) == QValidator::Acceptable;
198 setValid(input_, valid);
200 setValid(label_, valid);
205 } // namespace frontend