]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.cpp
rename/merge LyXLength related stuff
[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 #include "support/convert.h"
24
25 #include "debug.h"
26
27 #include <QComboBox>
28 #include <QCheckBox>
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(toqstr(convert<string>(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 QString const qt_(char const * str, const char *)
146 {
147         return toqstr(_(str));
148 }
149
150
151 QString const qt_(string const & str)
152 {
153         return toqstr(_(str));
154 }
155
156
157 docstring const formatted(docstring const & text, int w)
158 {
159         docstring sout;
160
161         if (text.empty())
162                 return sout;
163
164         docstring::size_type curpos = 0;
165         docstring line;
166
167         for (;;) {
168                 docstring::size_type const nxtpos1 = text.find(' ',  curpos);
169                 docstring::size_type const nxtpos2 = text.find('\n', curpos);
170                 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
171
172                 docstring const word =
173                         nxtpos == docstring::npos ?
174                         text.substr(curpos) :
175                         text.substr(curpos, nxtpos - curpos);
176
177                 bool const newline = (nxtpos2 != docstring::npos &&
178                                       nxtpos2 < nxtpos1);
179
180                 docstring const line_plus_word =
181                         line.empty() ? word : line + char_type(' ') + word;
182
183                 // FIXME: make w be size_t
184                 if (int(line_plus_word.length()) >= w) {
185                         sout += line + char_type('\n');
186                         if (newline) {
187                                 sout += word + char_type('\n');
188                                 line.erase();
189                         } else {
190                                 line = word;
191                         }
192
193                 } else if (newline) {
194                         sout += line_plus_word + char_type('\n');
195                         line.erase();
196
197                 } else {
198                         if (!line.empty())
199                                 line += char_type(' ');
200                         line += word;
201                 }
202
203                 if (nxtpos == docstring::npos) {
204                         if (!line.empty())
205                                 sout += line;
206                         break;
207                 }
208
209                 curpos = nxtpos + 1;
210         }
211
212         return sout;
213 }
214
215 } // namespace lyx