]> git.lyx.org Git - features.git/blob - src/frontends/qt4/ButtonController.cpp
872ead95b52a65fdafaaaddfb9a89e817349692e
[features.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(); This will enable all widgets in dialogs, no matter if
89         //                    they allowed to be enabled, so when you plan to
90         //                    reenable this call, read this before:
91     // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
92         refresh();
93         return ro;
94 }
95
96
97 void ButtonController::refresh() const
98 {
99         LYXERR(Debug::GUI) << "Calling BC refresh()" << std::endl;
100
101         bool const all_valid = checkWidgets();
102
103         if (okay_) {
104                 bool const enabled =
105                         all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
106                 okay_->setEnabled(enabled);
107         }
108         if (apply_) {
109                 bool const enabled =
110                         all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
111                 apply_->setEnabled(enabled);
112         }
113         if (restore_) {
114                 bool const enabled =
115                         all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
116                 restore_->setEnabled(enabled);
117         }
118         if (cancel_) {
119                 bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
120                 if (enabled)
121                         cancel_->setText(toqstr(_("Cancel")));
122                 else
123                         cancel_->setText(toqstr(_("Close")));
124         }
125 }
126
127
128 void ButtonController::refreshReadOnly() const
129 {
130         if (read_only_.empty())
131                 return;
132
133         bool const enable = !policy().isReadOnly();
134
135         Widgets::const_iterator end = read_only_.end();
136         Widgets::const_iterator iter = read_only_.begin();
137         for (; iter != end; ++iter)
138                 setWidgetEnabled(*iter, enable);
139 }
140
141
142 void ButtonController::setWidgetEnabled(QWidget * obj, bool enabled) const
143 {
144         if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
145                 le->setReadOnly(!enabled);
146         else
147                 obj->setEnabled(enabled);
148
149         obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
150 }
151
152
153 void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
154 {
155         checked_widgets.push_back(CheckedLineEdit(input, label));
156 }
157
158
159 bool ButtonController::checkWidgets() const
160 {
161         bool valid = true;
162
163         CheckedWidgetList::const_iterator it  = checked_widgets.begin();
164         CheckedWidgetList::const_iterator end = checked_widgets.end();
165
166         for (; it != end; ++it)
167                 valid &= it->check();
168
169         // return valid status after checking ALL widgets
170         return valid;
171 }
172
173
174 //////////////////////////////////////////////////////////////
175 //
176 // CheckedLineEdit
177 //
178 //////////////////////////////////////////////////////////////
179
180 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
181         : input_(input), label_(label)
182 {}
183
184
185 bool CheckedLineEdit::check() const
186 {
187         QValidator const * validator = input_->validator();
188         if (!validator)
189                 return true;
190
191         QString t = input_->text();
192         int p = 0;
193         bool const valid = validator->validate(t, p) == QValidator::Acceptable;
194
195         // Visual feedback.
196         setValid(input_, valid);
197         if (label_)
198                 setValid(label_, valid);
199
200         return valid;
201 }
202
203 } // namespace frontend
204 } // namespace lyx