]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/ButtonController.cpp
do what the FIXME suggested
[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 <QPushButton>
20 #include <QLineEdit>
21 #include <QLabel>
22 #include <QValidator>
23
24 namespace lyx {
25 namespace frontend {
26
27 ButtonController::ButtonController()
28         : okay_(0), apply_(0), cancel_(0), restore_(0),
29                 policy_(ButtonPolicy::IgnorantPolicy)
30 {}
31
32
33 void ButtonController::setPolicy(ButtonPolicy::Policy policy)
34 {
35         policy_ = ButtonPolicy(policy);
36 }
37
38
39 void ButtonController::ok()
40 {
41         input(ButtonPolicy::SMI_OKAY);
42 }
43
44
45 void ButtonController::input(ButtonPolicy::SMInput in)
46 {
47         if (ButtonPolicy::SMI_NOOP == in)
48                 return;
49         policy_.input(in);
50         refresh();
51 }
52
53
54 void ButtonController::apply()
55 {
56         input(ButtonPolicy::SMI_APPLY);
57 }
58
59
60 void ButtonController::cancel()
61 {
62         input(ButtonPolicy::SMI_CANCEL);
63 }
64
65
66 void ButtonController::restore()
67 {
68         input(ButtonPolicy::SMI_RESTORE);
69 }
70
71
72 void ButtonController::hide()
73 {
74         input(ButtonPolicy::SMI_HIDE);
75 }
76
77
78 void ButtonController::setValid(bool v)
79 {
80         input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
81 }
82
83
84 bool ButtonController::setReadOnly(bool ro)
85 {
86         LYXERR(Debug::GUI, "Setting controller ro: " << ro);
87
88         policy_.input(ro ?
89                 ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
90         // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
91         //                    they allowed to be enabled, so when you plan to
92         //                    reenable this call, read this before:
93     // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
94         refresh();
95         return ro;
96 }
97
98
99 void ButtonController::refresh() const
100 {
101         LYXERR(Debug::GUI, "Calling BC refresh()");
102
103         bool const all_valid = checkWidgets();
104
105         if (okay_) {
106                 bool const enabled =
107                         all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
108                 okay_->setEnabled(enabled);
109         }
110         if (apply_) {
111                 bool const enabled =
112                         all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
113                 apply_->setEnabled(enabled);
114         }
115         if (restore_) {
116                 bool const enabled =
117                         all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
118                 restore_->setEnabled(enabled);
119         }
120         if (cancel_) {
121                 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
122                 if (enabled)
123                         cancel_->setText(qt_("Cancel"));
124                 else
125                         cancel_->setText(qt_("Close"));
126         }
127 }
128
129
130 void ButtonController::refreshReadOnly() const
131 {
132         if (read_only_.empty())
133                 return;
134
135         bool const enable = !policy().isReadOnly();
136
137         Widgets::const_iterator end = read_only_.end();
138         Widgets::const_iterator iter = read_only_.begin();
139         for (; iter != end; ++iter)
140                 setWidgetEnabled(*iter, enable);
141 }
142
143
144 void ButtonController::setWidgetEnabled(QWidget * obj, bool enabled) const
145 {
146         if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
147                 le->setReadOnly(!enabled);
148         else
149                 obj->setEnabled(enabled);
150
151         obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
152 }
153
154
155 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
156 {
157         checked_widgets.push_back(CheckedLineEdit(input, label));
158 }
159
160
161 bool ButtonController::checkWidgets() const
162 {
163         bool valid = true;
164
165         CheckedWidgetList::const_iterator it  = checked_widgets.begin();
166         CheckedWidgetList::const_iterator end = checked_widgets.end();
167
168         for (; it != end; ++it)
169                 valid &= it->check();
170
171         // return valid status after checking ALL widgets
172         return valid;
173 }
174
175
176 //////////////////////////////////////////////////////////////
177 //
178 // CheckedLineEdit
179 //
180 //////////////////////////////////////////////////////////////
181
182 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
183         : input_(input), label_(label)
184 {}
185
186
187 bool CheckedLineEdit::check() const
188 {
189         QValidator const * validator = input_->validator();
190         if (!validator)
191                 return true;
192
193         QString t = input_->text();
194         int p = 0;
195         bool const valid = validator->validate(t, p) == QValidator::Acceptable;
196
197         // Visual feedback.
198         setValid(input_, valid);
199         if (label_)
200                 setValid(label_, valid);
201
202         return valid;
203 }
204
205 } // namespace frontend
206 } // namespace lyx