]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.cpp
9bfa3955d35c6691f67004a39d4905d6c2e14374
[lyx.git] / src / frontends / qt4 / qt_helpers.cpp
1 /**
2  * \file qt_helpers.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  * \author Jürgen Spitzmüller
8  * \author Richard Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "LengthCombo.h"
16 #include "qt_helpers.h"
17
18 #include "lengthcommon.h"
19 #include "gettext.h"
20
21 #include "support/os.h"
22 #include "support/lstrings.h"
23
24 #include "debug.h"
25
26 #include <QComboBox>
27 #include <QCheckBox>
28 #include <QPalette>
29 #include <QLineEdit>
30
31 #include <algorithm>
32
33
34 namespace lyx {
35
36 using support::isStrDbl;
37
38 using std::vector;
39 using std::string;
40
41
42 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
43 {
44         QString const length = input->text();
45         if (length.isEmpty())
46                 return string();
47
48         // Don't return unit-from-choice if the input(field) contains a unit
49         if (isValidGlueLength(fromqstr(length)))
50                 return fromqstr(length);
51
52         Length::UNIT const unit = combo->currentLengthItem();
53
54         return Length(length.toDouble(), unit).asString();
55 }
56
57
58 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
59 {
60         QString const length = input->text();
61         if (length.isEmpty())
62                 return Length();
63
64         // don't return unit-from-choice if the input(field) contains a unit
65         if (isValidGlueLength(fromqstr(length)))
66                 return Length(fromqstr(length));
67
68         Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
69
70         return Length(length.toDouble(), unit);
71 }
72
73
74 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
75                      Length const & len, Length::UNIT /*defaultUnit*/)
76 {
77         combo->setCurrentItem(len.unit());
78         input->setText(QString::number(Length(len).value()));
79 }
80
81
82 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
83         string const & len, Length::UNIT defaultUnit)
84 {
85         if (len.empty()) {
86                 // no length (UNIT_NONE)
87                 combo->setCurrentItem(defaultUnit);
88                 input->setText("");
89         } else if (!isValidLength(len) && !isStrDbl(len)) {
90                 // use input field only for gluelengths
91                 combo->setCurrentItem(defaultUnit);
92                 input->setText(toqstr(len));
93         } else {
94                 lengthToWidgets(input, combo, Length(len), defaultUnit);
95         }
96 }
97
98
99 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
100         Length const & len, Length::UNIT defaultUnit)
101 {
102         if (len.value() == 0)
103                 lengthToWidgets(input, combo, "auto", defaultUnit);
104         else
105                 lengthToWidgets(input, combo, len, defaultUnit);
106 }
107
108
109 //NOTE "CB" here because we probably will want one of these
110 //for labeled sets, as well.
111 void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
112         LengthCombo * lengthCombo)
113 {
114         if (!checkBox->isChecked())
115                 lengthToWidgets(lineEdit, lengthCombo,
116                                 "auto", lengthCombo->currentLengthItem());
117         else if (lineEdit->text() == "auto")
118                 lengthToWidgets(lineEdit, lengthCombo, string(),
119                                 lengthCombo->currentLengthItem());
120 }
121
122
123 void setValid(QWidget * widget, bool valid)
124 {
125         if (valid) {
126                 widget->setPalette(QPalette());
127         } else {
128                 QPalette pal = widget->palette();
129                 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
130                 widget->setPalette(pal);
131         }
132 }
133
134
135 QString const qt_(char const * str, const char *)
136 {
137         return toqstr(_(str));
138 }
139
140
141 QString const qt_(string const & str)
142 {
143         return toqstr(_(str));
144 }
145
146 } // namespace lyx