]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/checkedwidgets.C
8294094cf626826e51fd9903a62d1ab5dc10f6d0
[lyx.git] / src / frontends / qt4 / checkedwidgets.C
1 /**
2  * \file qt2/checkedwidgets.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "checkedwidgets.h"
14
15 #include <QLabel>
16 #include <QLineEdit>
17 #include <QValidator>
18
19 namespace lyx {
20 namespace frontend {
21
22 void addCheckedLineEdit(BCView & bcview,
23                         QLineEdit * input, QLabel * label)
24 {
25         bcview.addCheckedWidget(new CheckedLineEdit(input, label));
26 }
27
28
29 namespace {
30
31 void setWarningColor(QWidget * widget)
32 {
33         // Qt 2.3 does not have
34         // widget->setPaletteForegroundColor(QColor(255, 0, 0));
35         // So copy the appropriate parts of the function here:
36         QPalette pal = widget->palette();
37         pal.setColor(QPalette::Active,
38                      QColorGroup::Foreground,
39                      QColor(255, 0, 0));
40         widget->setPalette(pal);
41 }
42
43
44 void setWidget(bool valid, QLineEdit * input, QLabel * label)
45 {
46         if (valid)
47                 input->unsetPalette();
48         else
49                 setWarningColor(input);
50
51         if (!label)
52                 return;
53
54         if (valid)
55                 label->unsetPalette();
56         else
57                 setWarningColor(label);
58 }
59
60 } // namespace anon
61
62
63 CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QLabel * label)
64         : input_(input), label_(label)
65 {}
66
67
68 bool CheckedLineEdit::check() const
69 {
70         QValidator const * validator = input_->validator();
71         if (!validator)
72                 return true;
73
74         QString t = input_->text();
75         int p = 0;
76         bool const valid = validator->validate(t, p) == QValidator::Acceptable;
77
78         // Visual feedback.
79         setWidget(valid, input_, label_);
80
81         return valid;
82 }
83
84 } // namespace frontend
85 } // namespace lyx