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 <QPushButton>
25 ButtonController::ButtonController()
26 : okay_(0), apply_(0), cancel_(0), restore_(0),
27 policy_(ButtonPolicy::IgnorantPolicy)
31 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
33 policy_ = ButtonPolicy(policy);
37 void ButtonController::ok()
39 input(ButtonPolicy::SMI_OKAY);
43 void ButtonController::input(ButtonPolicy::SMInput in)
45 if (ButtonPolicy::SMI_NOOP == in)
52 void ButtonController::apply()
54 input(ButtonPolicy::SMI_APPLY);
58 void ButtonController::cancel()
60 input(ButtonPolicy::SMI_CANCEL);
64 void ButtonController::restore()
66 input(ButtonPolicy::SMI_RESTORE);
70 void ButtonController::hide()
72 input(ButtonPolicy::SMI_HIDE);
76 void ButtonController::setValid(bool v)
78 input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
82 bool ButtonController::setReadOnly(bool ro)
84 LYXERR(Debug::GUI) << "Setting controller ro: " << ro << std::endl;
87 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
88 // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
89 // they allowed to be enabled, so when you plan to
90 // reenable this call, read this before:
91 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
97 void ButtonController::refresh() const
99 LYXERR(Debug::GUI) << "Calling BC refresh()" << std::endl;
101 bool const all_valid = checkWidgets();
105 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
106 okay_->setEnabled(enabled);
110 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
111 apply_->setEnabled(enabled);
115 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
116 restore_->setEnabled(enabled);
119 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
121 cancel_->setText(toqstr(_("Cancel")));
123 cancel_->setText(toqstr(_("Close")));
128 void ButtonController::refreshReadOnly() const
130 if (read_only_.empty())
133 bool const enable = !policy().isReadOnly();
135 Widgets::const_iterator end = read_only_.end();
136 Widgets::const_iterator iter = read_only_.begin();
137 for (; iter != end; ++iter)
138 setWidgetEnabled(*iter, enable);
142 void ButtonController::setWidgetEnabled(QWidget * obj, bool enabled) const
144 if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
145 le->setReadOnly(!enabled);
147 obj->setEnabled(enabled);
149 obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
153 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
155 checked_widgets.push_back(CheckedLineEdit(input, label));
159 bool ButtonController::checkWidgets() const
163 CheckedWidgetList::const_iterator it = checked_widgets.begin();
164 CheckedWidgetList::const_iterator end = checked_widgets.end();
166 for (; it != end; ++it)
167 valid &= it->check();
169 // return valid status after checking ALL widgets
174 //////////////////////////////////////////////////////////////
178 //////////////////////////////////////////////////////////////
180 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
181 : input_(input), label_(label)
185 bool CheckedLineEdit::check() const
187 QValidator const * validator = input_->validator();
191 QString t = input_->text();
193 bool const valid = validator->validate(t, p) == QValidator::Acceptable;
196 setValid(input_, valid);
198 setValid(label_, valid);
203 } // namespace frontend