]> git.lyx.org Git - features.git/blob - src/frontends/qt4/Qt2BC.cpp
all CheckedWidgets are line edits...
[features.git] / src / frontends / qt4 / Qt2BC.cpp
1 /**
2  * \file Qt2BC.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  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Qt2BC.h"
15 #include "BCView.h"
16 #include "ButtonPolicy.h"
17 #include "debug.h"
18 #include "qt_helpers.h"
19
20 #include <QPushButton>
21 #include <QLineEdit>
22 #include <QLabel>
23 #include <QValidator>
24
25
26 namespace lyx {
27 namespace frontend {
28
29
30 Qt2BC::Qt2BC(ButtonController & parent)
31         : BCView(parent), okay_(0), apply_(0), cancel_(0), restore_(0)
32 {}
33
34
35 void Qt2BC::refresh() const
36 {
37         LYXERR(Debug::GUI) << "Calling BC refresh()" << std::endl;
38
39         bool const all_valid = checkWidgets();
40
41         if (okay_) {
42                 bool const enabled =
43                         all_valid && bp().buttonStatus(ButtonPolicy::OKAY);
44                 okay_->setEnabled(enabled);
45         }
46         if (apply_) {
47                 bool const enabled =
48                         all_valid && bp().buttonStatus(ButtonPolicy::APPLY);
49                 apply_->setEnabled(enabled);
50         }
51         if (restore_) {
52                 bool const enabled =
53                         all_valid && bp().buttonStatus(ButtonPolicy::RESTORE);
54                 restore_->setEnabled(enabled);
55         }
56         if (cancel_) {
57                 bool const enabled = bp().buttonStatus(ButtonPolicy::CANCEL);
58                 if (enabled)
59                         cancel_->setText(toqstr(_("Cancel")));
60                 else
61                         cancel_->setText(toqstr(_("Close")));
62         }
63 }
64
65
66 void Qt2BC::refreshReadOnly() const
67 {
68         if (read_only_.empty())
69                 return;
70
71         bool const enable = !bp().isReadOnly();
72
73         Widgets::const_iterator end = read_only_.end();
74         Widgets::const_iterator iter = read_only_.begin();
75         for (; iter != end; ++iter)
76                 setWidgetEnabled(*iter, enable);
77 }
78
79
80 void Qt2BC::setWidgetEnabled(QWidget * obj, bool enabled) const
81 {
82         if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
83                 le->setReadOnly(!enabled);
84         else
85                 obj->setEnabled(enabled);
86
87         obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
88 }
89
90
91 void Qt2BC::addCheckedLineEdit(QLineEdit * input, QWidget * label)
92 {
93         checked_widgets.push_back(CheckedLineEdit(input, label));
94 }
95
96
97 bool Qt2BC::checkWidgets() const
98 {
99         bool valid = true;
100
101         CheckedWidgetList::const_iterator it  = checked_widgets.begin();
102         CheckedWidgetList::const_iterator end = checked_widgets.end();
103
104         for (; it != end; ++it)
105                 valid &= it->check();
106
107         // return valid status after checking ALL widgets
108         return valid;
109 }
110
111
112 //////////////////////////////////////////////////////////////
113 //
114 // CheckedLineEdit
115 //
116 //////////////////////////////////////////////////////////////
117
118 void addCheckedLineEdit(BCView & bcview, QLineEdit * input, QWidget * label)
119 {
120         Qt2BC * bc = static_cast<Qt2BC *>(&bcview);
121         bc->addCheckedLineEdit(input, label);
122 }
123
124
125 static void setWarningColor(QWidget * widget)
126 {
127         QPalette pal = widget->palette();
128         pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
129         widget->setPalette(pal);
130 }
131
132
133
134 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
135         : input_(input), label_(label)
136 {}
137
138
139 bool CheckedLineEdit::check() const
140 {
141         QValidator const * validator = input_->validator();
142         if (!validator)
143                 return true;
144
145         QString t = input_->text();
146         int p = 0;
147         bool const valid = validator->validate(t, p) == QValidator::Acceptable;
148
149         // Visual feedback.
150         if (valid)
151                 input_->setPalette(QPalette());
152         else
153                 setWarningColor(input_);
154
155         if (!label_) {
156                 if (valid)
157                         label_->setPalette(QPalette());
158                 else
159                         setWarningColor(label_);
160         }
161
162         return valid;
163 }
164
165 } // namespace frontend
166 } // namespace lyx