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);
94 void ButtonController::refresh() const
96 LYXERR(Debug::GUI) << "Calling BC refresh()" << std::endl;
98 bool const all_valid = checkWidgets();
102 all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
103 okay_->setEnabled(enabled);
107 all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
108 apply_->setEnabled(enabled);
112 all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
113 restore_->setEnabled(enabled);
116 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
118 cancel_->setText(toqstr(_("Cancel")));
120 cancel_->setText(toqstr(_("Close")));
125 void ButtonController::refreshReadOnly() const
127 if (read_only_.empty())
130 bool const enable = !policy().isReadOnly();
132 Widgets::const_iterator end = read_only_.end();
133 Widgets::const_iterator iter = read_only_.begin();
134 for (; iter != end; ++iter)
135 setWidgetEnabled(*iter, enable);
139 void ButtonController::setWidgetEnabled(QWidget * obj, bool enabled) const
141 if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
142 le->setReadOnly(!enabled);
144 obj->setEnabled(enabled);
146 obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
150 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
152 checked_widgets.push_back(CheckedLineEdit(input, label));
156 bool ButtonController::checkWidgets() const
160 CheckedWidgetList::const_iterator it = checked_widgets.begin();
161 CheckedWidgetList::const_iterator end = checked_widgets.end();
163 for (; it != end; ++it)
164 valid &= it->check();
166 // return valid status after checking ALL widgets
171 //////////////////////////////////////////////////////////////
175 //////////////////////////////////////////////////////////////
177 static void setWarningColor(QWidget * widget)
179 QPalette pal = widget->palette();
180 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
181 widget->setPalette(pal);
185 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
186 : input_(input), label_(label)
190 bool CheckedLineEdit::check() const
192 QValidator const * validator = input_->validator();
196 QString t = input_->text();
198 bool const valid = validator->validate(t, p) == QValidator::Acceptable;
202 input_->setPalette(QPalette());
204 setWarningColor(input_);
208 label_->setPalette(QPalette());
210 setWarningColor(label_);
216 } // namespace frontend