]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.cpp
extract code that sets color of widget from ButtonController and move to qt_helpers
[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.h>
30 #include <qtextcodec.h>
31
32 #include <algorithm>
33
34
35 namespace lyx {
36
37 using support::isStrDbl;
38
39 using std::vector;
40 using std::make_pair;
41 using std::string;
42 using std::pair;
43 using std::endl;
44
45
46 string makeFontName(string const & family, string const & foundry)
47 {
48         if (foundry.empty())
49                 return family;
50         return family + " [" + foundry + ']';
51 }
52
53
54 pair<string, string> parseFontName(string const & name)
55 {
56         string::size_type const idx = name.find('[');
57         if (idx == string::npos || idx == 0)
58                 return make_pair(name, string());
59         return make_pair(name.substr(0, idx - 1),
60                          name.substr(idx + 1, name.size() - idx - 2));
61 }
62
63
64 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
65 {
66         QString const length = input->text();
67         if (length.isEmpty())
68                 return string();
69
70         // Don't return unit-from-choice if the input(field) contains a unit
71         if (isValidGlueLength(fromqstr(length)))
72                 return fromqstr(length);
73
74         Length::UNIT const unit = combo->currentLengthItem();
75
76         return Length(length.toDouble(), unit).asString();
77 }
78
79
80 Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
81 {
82         QString const length = input->text();
83         if (length.isEmpty())
84                 return Length();
85
86         // don't return unit-from-choice if the input(field) contains a unit
87         if (isValidGlueLength(fromqstr(length)))
88                 return Length(fromqstr(length));
89
90         Length::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
91
92         return Length(length.toDouble(), unit);
93 }
94
95
96 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
97                      Length const & len, Length::UNIT /*defaultUnit*/)
98 {
99         combo->setCurrentItem(Length(len).unit());
100         input->setText(QString::number(Length(len).value()));
101 }
102
103
104 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
105         string const & len, Length::UNIT defaultUnit)
106 {
107         if (len.empty()) {
108                 // no length (UNIT_NONE)
109                 combo->setCurrentItem(defaultUnit);
110                 input->setText("");
111         } else if (!isValidLength(len) && !isStrDbl(len)) {
112                 // use input field only for gluelengths
113                 combo->setCurrentItem(defaultUnit);
114                 input->setText(toqstr(len));
115         } else {
116                 lengthToWidgets(input, combo, Length(len), defaultUnit);
117         }
118 }
119
120
121 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
122         Length const & len, Length::UNIT defaultUnit)
123 {
124         if (len.value() == 0)
125                 lengthToWidgets(input, combo, "auto", defaultUnit);
126         else
127                 lengthToWidgets(input, combo, len, defaultUnit);
128 }
129
130
131 //NOTE "CB" here because we probably will want one of these
132 //for labeled sets, as well.
133 void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
134         LengthCombo * lengthCombo)
135 {
136         if (!checkBox->isChecked())
137                 lengthToWidgets(lineEdit, lengthCombo,
138                                 "auto", lengthCombo->currentLengthItem());
139         else if (lineEdit->text() == "auto")
140                 lengthToWidgets(lineEdit, lengthCombo, string(),
141                                 lengthCombo->currentLengthItem());
142 }
143
144
145 void setValid(QWidget * widget, bool valid)
146 {
147         if (valid) {
148                 widget->setPalette(QPalette());
149         } else {
150                 QPalette pal = widget->palette();
151                 pal.setColor(QPalette::Active, QPalette::Foreground, QColor(255, 0, 0));
152                 widget->setPalette(pal);
153         }
154 }
155
156
157 QString const qt_(char const * str, const char *)
158 {
159         return toqstr(_(str));
160 }
161
162
163 QString const qt_(string const & str)
164 {
165         return toqstr(_(str));
166 }
167
168
169 docstring const formatted(docstring const & text, int w)
170 {
171         docstring sout;
172
173         if (text.empty())
174                 return sout;
175
176         docstring::size_type curpos = 0;
177         docstring line;
178
179         for (;;) {
180                 docstring::size_type const nxtpos1 = text.find(' ',  curpos);
181                 docstring::size_type const nxtpos2 = text.find('\n', curpos);
182                 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
183
184                 docstring const word =
185                         nxtpos == docstring::npos ?
186                         text.substr(curpos) :
187                         text.substr(curpos, nxtpos - curpos);
188
189                 bool const newline = (nxtpos2 != docstring::npos &&
190                                       nxtpos2 < nxtpos1);
191
192                 docstring const line_plus_word =
193                         line.empty() ? word : line + char_type(' ') + word;
194
195                 // FIXME: make w be size_t
196                 if (int(line_plus_word.length()) >= w) {
197                         sout += line + char_type('\n');
198                         if (newline) {
199                                 sout += word + char_type('\n');
200                                 line.erase();
201                         } else {
202                                 line = word;
203                         }
204
205                 } else if (newline) {
206                         sout += line_plus_word + char_type('\n');
207                         line.erase();
208
209                 } else {
210                         if (!line.empty())
211                                 line += char_type(' ');
212                         line += word;
213                 }
214
215                 if (nxtpos == docstring::npos) {
216                         if (!line.empty())
217                                 sout += line;
218                         break;
219                 }
220
221                 curpos = nxtpos + 1;
222         }
223
224         return sout;
225 }
226
227 } // namespace lyx