]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.C
This commit saves the need to check for lyx::use_gui in a number of places.
[lyx.git] / src / frontends / qt4 / qt_helpers.C
1 /**
2  * \file qt_helpers.C
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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "lengthcombo.h"
15 #include "qt_helpers.h"
16
17 #include "lengthcommon.h"
18 #include "gettext.h"
19
20 #include "support/lstrings.h"
21 #include "support/convert.h"
22
23 #include "debug.h"
24
25 #include <QComboBox>
26 #include <qlineedit.h>
27 #include <qtextcodec.h>
28
29 #include <algorithm>
30
31
32 using lyx::support::isStrDbl;
33 using lyx::char_type;
34 using lyx::docstring;
35
36 using std::vector;
37 using std::make_pair;
38 using std::string;
39 using std::pair;
40 using std::endl;
41
42
43 string makeFontName(string const & family, string const & foundry)
44 {
45         if (foundry.empty())
46                 return family;
47         return family + " [" + foundry + ']';
48 }
49
50
51 pair<string, string> parseFontName(string const & name)
52 {
53         string::size_type const idx = name.find('[');
54         if (idx == string::npos || idx == 0)
55                 return make_pair(name, string());
56         return make_pair(name.substr(0, idx - 1),
57                          name.substr(idx + 1, name.size() - idx - 2));
58 }
59
60
61 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
62 {
63         QString const length = input->text();
64         if (length.isEmpty())
65                 return string();
66
67         // Don't return unit-from-choice if the input(field) contains a unit
68         if (isValidGlueLength(fromqstr(length)))
69                 return fromqstr(length);
70
71         LyXLength::UNIT const unit = combo->currentLengthItem();
72
73         return LyXLength(length.toDouble(), unit).asString();
74 }
75
76
77 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
78 {
79         QString const length = input->text();
80         if (length.isEmpty())
81                 return LyXLength();
82
83         // don't return unit-from-choice if the input(field) contains a unit
84         if (isValidGlueLength(fromqstr(length)))
85                 return LyXLength(fromqstr(length));
86
87         LyXLength::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
88
89         return LyXLength(length.toDouble(), unit);
90 }
91
92
93 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
94         string const & len, LyXLength::UNIT defaultUnit)
95 {
96         if (len.empty()) {
97                 // no length (UNIT_NONE)
98                 combo->setCurrentItem(defaultUnit);
99                 input->setText("");
100         } else if (!isValidLength(len) && !isStrDbl(len)) {
101                 // use input field only for gluelengths
102                 combo->setCurrentItem(defaultUnit);
103                 input->setText(toqstr(len));
104         } else {
105                 combo->setCurrentItem(LyXLength(len).unit());
106                 input->setText(toqstr(convert<string>(LyXLength(len).value())));
107         }
108 }
109
110
111 QString const toqstr(char const * str)
112 {
113         return QString::fromUtf8(str);
114 }
115
116
117 QString const toqstr(string const & str)
118 {
119         return toqstr(str.c_str());
120 }
121
122
123 void ucs4_to_qstring(char_type const * str, size_t ls, QString & s)
124 {
125         s.reserve(ls);
126         s.clear();
127
128         for (size_t i = 0; i < ls; ++i)
129                 s.append(ucs4_to_qchar(str[i]));
130 }
131
132
133 void ucs4_to_qstring(lyx::docstring const & str, QString & s)
134 {
135         size_t ls = str.size(); 
136         s.reserve(ls);
137         s.clear();
138
139         for (size_t i = 0; i < ls; ++i)
140                 s.append(ucs4_to_qchar(str[i]));
141 }
142
143
144 QString const toqstr(docstring const & ucs4)
145 {
146         QString s;
147         ucs4_to_qstring(ucs4, s);
148
149         return s;
150 }
151
152
153 docstring const qstring_to_ucs4(QString const & qstr)
154 {
155         int const ls = qstr.size();
156         docstring ucs4;
157         for (int i = 0; i < ls; ++i)
158                 ucs4 += static_cast<char_type>(qstr[i].unicode());
159
160         return ucs4;
161 }
162
163
164 void qstring_to_ucs4(QString const & qstr, vector<char_type> & ucs4)
165 {
166         int const ls = qstr.size();
167         ucs4.clear();
168         for (int i = 0; i < ls; ++i)
169                 ucs4.push_back(static_cast<lyx::char_type>(qstr[i].unicode()));
170 }
171
172
173 QString const qt_(char const * str)
174 {
175         return toqstr(_(str));
176 }
177
178
179 QString const qt_(string const & str)
180 {
181         return toqstr(_(str));
182 }
183
184
185 string const fromqstr(QString const & str)
186 {
187         return str.isEmpty()? string(): string(str.toAscii());
188 }
189
190
191 docstring const formatted(docstring const & text, int w)
192 {
193         docstring sout;
194
195         if (text.empty())
196                 return sout;
197
198         docstring::size_type curpos = 0;
199         docstring line;
200
201         for (;;) {
202                 docstring::size_type const nxtpos1 = text.find(' ',  curpos);
203                 docstring::size_type const nxtpos2 = text.find('\n', curpos);
204                 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
205
206                 docstring const word =
207                         nxtpos == docstring::npos ?
208                         text.substr(curpos) :
209                         text.substr(curpos, nxtpos - curpos);
210
211                 bool const newline = (nxtpos2 != docstring::npos &&
212                                       nxtpos2 < nxtpos1);
213
214                 docstring const line_plus_word =
215                         line.empty() ? word : line + lyx::char_type(' ') + word;
216
217                 // FIXME: make w be size_t
218                 if (int(line_plus_word.length()) >= w) {
219                         sout += line + lyx::char_type('\n');
220                         if (newline) {
221                                 sout += word + lyx::char_type('\n');
222                                 line.erase();
223                         } else {
224                                 line = word;
225                         }
226
227                 } else if (newline) {
228                         sout += line_plus_word + lyx::char_type('\n');
229                         line.erase();
230
231                 } else {
232                         if (!line.empty())
233                                 line += lyx::char_type(' ');
234                         line += word;
235                 }
236
237                 if (nxtpos == docstring::npos) {
238                         if (!line.empty())
239                                 sout += line;
240                         break;
241                 }
242
243                 curpos = nxtpos + 1;
244         }
245
246         return sout;
247 }