]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/ButtonController.cpp
26279aa0ee7b049ef68ad15610e97e98ac2f1e2b
[lyx.git] / src / frontends / qt4 / ButtonController.cpp
1 /**
2  * \file ButtonController.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Allan Rae
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ButtonController.h"
14 #include "debug.h"
15 #include "qt_helpers.h"
16
17 #include <QPushButton>
18 #include <QLineEdit>
19 #include <QLabel>
20 #include <QValidator>
21
22 namespace lyx {
23 namespace frontend {
24
25 ButtonController::ButtonController()
26         : okay_(0), apply_(0), cancel_(0), restore_(0),
27                 policy_(ButtonPolicy::IgnorantPolicy)
28 {}
29
30
31 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
32 {
33         policy_ = ButtonPolicy(policy);
34 }
35
36
37 void ButtonController::ok()
38 {
39         input(ButtonPolicy::SMI_OKAY);
40 }
41
42
43 void ButtonController::input(ButtonPolicy::SMInput in)
44 {
45         if (ButtonPolicy::SMI_NOOP == in)
46                 return;
47         policy_.input(in);
48         refresh();
49 }
50
51
52 void ButtonController::apply()
53 {
54         input(ButtonPolicy::SMI_APPLY);
55 }
56
57
58 void ButtonController::cancel()
59 {
60         input(ButtonPolicy::SMI_CANCEL);
61 }
62
63
64 void ButtonController::restore()
65 {
66         input(ButtonPolicy::SMI_RESTORE);
67 }
68
69
70 void ButtonController::hide()
71 {
72         input(ButtonPolicy::SMI_HIDE);
73 }
74
75
76 void ButtonController::setValid(bool v)
77 {
78         input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
79 }
80
81
82 bool ButtonController::setReadOnly(bool ro)
83 {
84         LYXERR(Debug::GUI) << "Setting controller ro: " << ro << std::endl;
85
86         policy_.input(ro ?
87                 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
88         refreshReadOnly();
89         refresh();
90         return ro;
91 }
92
93
94 void ButtonController::refresh() const
95 {
96         LYXERR(Debug::GUI) << "Calling BC refresh()" << std::endl;
97
98         bool const all_valid = checkWidgets();
99
100         if (okay_) {
101                 bool const enabled =
102                         all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
103                 okay_->setEnabled(enabled);
104         }
105         if (apply_) {
106                 bool const enabled =
107                         all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
108                 apply_->setEnabled(enabled);
109         }
110         if (restore_) {
111                 bool const enabled =
112                         all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
113                 restore_->setEnabled(enabled);
114         }
115         if (cancel_) {
116                 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
117                 if (enabled)
118                         cancel_->setText(toqstr(_("Cancel")));
119                 else
120                         cancel_->setText(toqstr(_("Close")));
121         }
122 }
123
124
125 void ButtonController::refreshReadOnly() const
126 {
127         if (read_only_.empty())
128                 return;
129
130         bool const enable = !policy().isReadOnly();
131
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);
136 }
137
138
139 void ButtonController::setWidgetEnabled(QWidget * obj, bool enabled) const
140 {
141         if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
142                 le->setReadOnly(!enabled);
143         else
144                 obj->setEnabled(enabled);
145
146         obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
147 }
148
149
150 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
151 {
152         checked_widgets.push_back(CheckedLineEdit(input, label));
153 }
154
155
156 bool ButtonController::checkWidgets() const
157 {
158         bool valid = true;
159
160         CheckedWidgetList::const_iterator it  = checked_widgets.begin();
161         CheckedWidgetList::const_iterator end = checked_widgets.end();
162
163         for (; it != end; ++it)
164                 valid &= it->check();
165
166         // return valid status after checking ALL widgets
167         return valid;
168 }
169
170
171 //////////////////////////////////////////////////////////////
172 //
173 // CheckedLineEdit
174 //
175 //////////////////////////////////////////////////////////////
176
177 static void setWarningColor(QWidget * widget)
178 {
179         QPalette pal = widget->palette();
180         pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
181         widget->setPalette(pal);
182 }
183
184
185 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
186         : input_(input), label_(label)
187 {}
188
189
190 bool CheckedLineEdit::check() const
191 {
192         QValidator const * validator = input_->validator();
193         if (!validator)
194                 return true;
195
196         QString t = input_->text();
197         int p = 0;
198         bool const valid = validator->validate(t, p) == QValidator::Acceptable;
199
200         // Visual feedback.
201         if (valid)
202                 input_->setPalette(QPalette());
203         else
204                 setWarningColor(input_);
205
206         if (!label_) {
207                 if (valid)
208                         label_->setPalette(QPalette());
209                 else
210                         setWarningColor(label_);
211         }
212
213         return valid;
214 }
215
216 } // namespace frontend
217 } // namespace lyx