]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/ButtonController.cpp
Add forgotten initialization
[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
15 #include "qt_helpers.h"
16
17 #include "support/debug.h"
18
19 #include <QCheckBox>
20 #include <QPushButton>
21 #include <QLineEdit>
22 #include <QLabel>
23 #include <QList>
24 #include <QValidator>
25
26
27 namespace lyx {
28 namespace frontend {
29
30 static void setWidgetEnabled(QWidget * obj, bool enabled)
31 {
32         if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
33                 le->setReadOnly(!enabled);
34         else
35                 obj->setEnabled(enabled);
36
37         obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
38 }
39
40
41 /////////////////////////////////////////////////////////////////////////
42 //
43 // CheckedLineEdit
44 //
45 /////////////////////////////////////////////////////////////////////////
46
47 class CheckedLineEdit
48 {
49 public:
50         CheckedLineEdit(QLineEdit * input, QWidget * label = 0);
51         bool check() const;
52
53 private:
54         // non-owned
55         QLineEdit * input_;
56         QWidget * label_;
57 };
58
59
60 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
61         : input_(input), label_(label)
62 {}
63
64
65 bool CheckedLineEdit::check() const
66 {
67         QValidator const * validator = input_->validator();
68         if (!validator)
69                 return true;
70
71         QString t = input_->text();
72         int p = 0;
73         bool const valid = validator->validate(t, p) == QValidator::Acceptable;
74
75         // Visual feedback.
76         setValid(input_, valid);
77         if (label_)
78                 setValid(label_, valid);
79
80         return valid;
81 }
82
83
84 /////////////////////////////////////////////////////////////////////////
85 //
86 // ButtonController::Private
87 //
88 /////////////////////////////////////////////////////////////////////////
89
90 class ButtonController::Private
91 {
92 public:
93         typedef QList<CheckedLineEdit> CheckedWidgetList;
94
95         Private()
96                 : okay_(0), apply_(0), cancel_(0), restore_(0), auto_apply_(0), default_(0),
97                         policy_(ButtonPolicy::IgnorantPolicy)
98         {}
99
100         /// \return true if all CheckedWidgets are in a valid state.
101         bool checkWidgets() const
102         {
103                 bool valid = true;
104                 for (const CheckedLineEdit & w : checked_widgets_)
105                         valid &= w.check();
106                 return valid;
107         }
108
109 public:
110         CheckedWidgetList checked_widgets_;
111
112         QPushButton * okay_;
113         QPushButton * apply_;
114         QPushButton * cancel_;
115         QPushButton * restore_;
116         QCheckBox * auto_apply_;
117         QPushButton * default_;
118
119         typedef QList<QWidget *> Widgets;
120         Widgets read_only_;
121
122         ButtonPolicy policy_;
123 };
124
125
126 /////////////////////////////////////////////////////////////////////////
127 //
128 // ButtonController
129 //
130 /////////////////////////////////////////////////////////////////////////
131
132 ButtonController::ButtonController()
133         : d(new Private)
134 {}
135
136
137 ButtonController::~ButtonController()
138 {
139         delete d;
140 }
141
142
143 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
144 {
145         d->policy_.setPolicy(policy);
146 }
147
148
149 void ButtonController::ok()
150 {
151         input(ButtonPolicy::SMI_OKAY);
152 }
153
154
155 void ButtonController::input(ButtonPolicy::SMInput in)
156 {
157         if (ButtonPolicy::SMI_NOOP == in)
158                 return;
159         d->policy_.input(in);
160         refresh();
161 }
162
163
164 void ButtonController::apply()
165 {
166         input(ButtonPolicy::SMI_APPLY);
167 }
168
169
170 void ButtonController::autoApply()
171 {
172         input(ButtonPolicy::SMI_AUTOAPPLY);
173 }
174
175
176 void ButtonController::cancel()
177 {
178         input(ButtonPolicy::SMI_CANCEL);
179 }
180
181
182 void ButtonController::restore()
183 {
184         input(ButtonPolicy::SMI_RESTORE);
185 }
186
187
188 void ButtonController::hide()
189 {
190         input(ButtonPolicy::SMI_HIDE);
191 }
192
193
194 void ButtonController::setValid(bool v)
195 {
196         input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
197 }
198
199
200 bool ButtonController::setReadOnly(bool ro)
201 {
202         LYXERR(Debug::GUI, "Setting controller ro: " << ro);
203
204         d->policy_.input(ro ?
205                 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
206         // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
207         //                    they allowed to be enabled, so when you plan to
208         //                    reenable this call, read this before:
209         // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
210         refresh();
211         return ro;
212 }
213
214
215 void ButtonController::refresh() const
216 {
217         LYXERR(Debug::GUI, "Calling BC refresh()");
218
219         bool const all_valid = d->checkWidgets();
220
221         if (d->okay_) {
222                 bool const enabled =
223                         all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
224                 d->okay_->setEnabled(enabled);
225         }
226         if (d->apply_) {
227                 bool const enabled =
228                         all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
229                 d->apply_->setEnabled(enabled);
230         }
231         if (d->restore_) {
232                 bool const enabled =
233                         all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
234                 d->restore_->setEnabled(enabled);
235         }
236         if (d->cancel_) {
237                 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
238                 if (enabled)
239                         d->cancel_->setText(qt_("Cancel"));
240                 else
241                         d->cancel_->setText(qt_("Close"));
242         }
243         if (d->auto_apply_) {
244                 bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
245                 d->auto_apply_->setEnabled(enabled);
246         }
247         if (d->default_)
248                 // Somewhere in the chain this can lose default status (#11417)
249                 d->default_->setDefault(true);
250 }
251
252
253 void ButtonController::refreshReadOnly() const
254 {
255         if (d->read_only_.empty())
256                 return;
257         bool const enable = !policy().isReadOnly();
258         for(QWidget * w : d->read_only_)
259                 setWidgetEnabled(w, enable);
260 }
261
262
263 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
264 {
265         d->checked_widgets_.append(CheckedLineEdit(input, label));
266 }
267
268
269 void ButtonController::setOK(QPushButton * obj, bool const default_button)
270 {
271         d->okay_ = obj;
272         if (default_button)
273                 d->default_ = obj;
274 }
275
276
277 void ButtonController::setApply(QPushButton * obj, bool const default_button)
278 {
279         d->apply_ = obj;
280         if (default_button)
281                 d->default_ = obj;
282 }
283
284
285 void ButtonController::setAutoApply(QCheckBox * obj)
286 {
287         d->auto_apply_ = obj;
288 }
289
290
291 void ButtonController::setCancel(QPushButton * obj, bool const default_button)
292 {
293         d->cancel_ = obj;
294         if (default_button)
295                 d->default_ = obj;
296 }
297
298
299 void ButtonController::setRestore(QPushButton * obj, bool const default_button)
300 {
301         d->restore_ = obj;
302         if (default_button)
303                 d->default_ = obj;
304 }
305
306
307 void ButtonController::addReadOnly(QWidget * obj)
308 {
309         d->read_only_.push_back(obj);
310 }
311
312 ButtonPolicy const & ButtonController::policy() const
313 {
314         return d->policy_;
315 }
316
317
318 ButtonPolicy & ButtonController::policy()
319 {
320         return d->policy_;
321 }
322
323 } // namespace frontend
324 } // namespace lyx