]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/ButtonController.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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 #include "support/foreach.h"
19
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),
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                 foreach (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
117         typedef QList<QWidget *> Widgets;
118         Widgets read_only_;
119
120         ButtonPolicy policy_;
121 };
122
123
124 /////////////////////////////////////////////////////////////////////////
125 //
126 // ButtonController
127 //
128 /////////////////////////////////////////////////////////////////////////
129
130 ButtonController::ButtonController()
131         : d(new Private)
132 {}
133
134
135 ButtonController::~ButtonController()
136 {
137         delete d;
138 }
139
140
141 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
142 {
143         d->policy_.setPolicy(policy);
144 }
145
146
147 void ButtonController::ok()
148 {
149         input(ButtonPolicy::SMI_OKAY);
150 }
151
152
153 void ButtonController::input(ButtonPolicy::SMInput in)
154 {
155         if (ButtonPolicy::SMI_NOOP == in)
156                 return;
157         d->policy_.input(in);
158         refresh();
159 }
160
161
162 void ButtonController::apply()
163 {
164         input(ButtonPolicy::SMI_APPLY);
165 }
166
167
168 void ButtonController::cancel()
169 {
170         input(ButtonPolicy::SMI_CANCEL);
171 }
172
173
174 void ButtonController::restore()
175 {
176         input(ButtonPolicy::SMI_RESTORE);
177 }
178
179
180 void ButtonController::hide()
181 {
182         input(ButtonPolicy::SMI_HIDE);
183 }
184
185
186 void ButtonController::setValid(bool v)
187 {
188         input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
189 }
190
191
192 bool ButtonController::setReadOnly(bool ro)
193 {
194         LYXERR(Debug::GUI, "Setting controller ro: " << ro);
195
196         d->policy_.input(ro ?
197                 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
198         // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
199         //                    they allowed to be enabled, so when you plan to
200         //                    reenable this call, read this before:
201     // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
202         refresh();
203         return ro;
204 }
205
206
207 void ButtonController::refresh() const
208 {
209         LYXERR(Debug::GUI, "Calling BC refresh()");
210
211         bool const all_valid = d->checkWidgets();
212
213         if (d->okay_) {
214                 bool const enabled =
215                         all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
216                 d->okay_->setEnabled(enabled);
217         }
218         if (d->apply_) {
219                 bool const enabled =
220                         all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
221                 d->apply_->setEnabled(enabled);
222         }
223         if (d->restore_) {
224                 bool const enabled =
225                         all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
226                 d->restore_->setEnabled(enabled);
227         }
228         if (d->cancel_) {
229                 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
230                 if (enabled)
231                         d->cancel_->setText(qt_("Cancel"));
232                 else
233                         d->cancel_->setText(qt_("Close"));
234         }
235 }
236
237
238 void ButtonController::refreshReadOnly() const
239 {
240         if (d->read_only_.empty())
241                 return;
242
243         bool const enable = !policy().isReadOnly();
244         
245         foreach (QWidget * w, d->read_only_)
246                 setWidgetEnabled(w, enable);
247 }
248
249
250 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
251 {
252         d->checked_widgets_.append(CheckedLineEdit(input, label));
253 }
254
255
256 void ButtonController::setOK(QPushButton * obj)
257 {
258         d->okay_ = obj;
259 }
260
261
262 void ButtonController::setApply(QPushButton * obj)
263 {
264         d->apply_ = obj;
265 }
266
267
268 void ButtonController::setCancel(QPushButton * obj)
269 {
270         d->cancel_ = obj;
271 }
272
273
274 void ButtonController::setRestore(QPushButton * obj)
275 {
276         d->restore_ = obj;
277 }
278
279
280 void ButtonController::addReadOnly(QWidget * obj)
281 {
282         d->read_only_.push_back(obj);
283 }
284
285 ButtonPolicy const & ButtonController::policy() const
286 {
287         return d->policy_;
288 }
289
290
291 ButtonPolicy & ButtonController::policy()
292 {
293         return d->policy_;
294 }
295
296 } // namespace frontend
297 } // namespace lyx