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