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