]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.C
Patch 1 Log:
[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
127         for (size_t i = 0; i < ls; ++i)
128                 s.append(ucs4_to_qchar(str[i]));
129 }
130
131
132 QString const toqstr(docstring const & ucs4)
133 {
134         QString s;
135         size_t const ls = ucs4.size();
136
137         for (size_t i = 0; i < ls; ++i)
138                 s.append(ucs4_to_qchar(ucs4[i]));
139
140         return s;
141 }
142
143
144 docstring const qstring_to_ucs4(QString const & qstr)
145 {
146         int const ls = qstr.size();
147         docstring ucs4;
148         for (int i = 0; i < ls; ++i)
149                 ucs4 += static_cast<char_type>(qstr[i].unicode());
150
151         return ucs4;
152 }
153
154
155 void qstring_to_ucs4(QString const & qstr, vector<char_type> & ucs4)
156 {
157         int const ls = qstr.size();
158         ucs4.clear();
159         for (int i = 0; i < ls; ++i)
160                 ucs4.push_back(static_cast<lyx::char_type>(qstr[i].unicode()));
161 }
162
163
164 QString const qt_(char const * str)
165 {
166         return toqstr(_(str));
167 }
168
169
170 QString const qt_(string const & str)
171 {
172         return toqstr(_(str));
173 }
174
175
176 string const fromqstr(QString const & str)
177 {
178         return str.isEmpty()? string(): string(str.toAscii());
179 }
180
181
182 docstring const formatted(docstring const & text, int w)
183 {
184         docstring sout;
185
186         if (text.empty())
187                 return sout;
188
189         docstring::size_type curpos = 0;
190         docstring line;
191
192         for (;;) {
193                 docstring::size_type const nxtpos1 = text.find(' ',  curpos);
194                 docstring::size_type const nxtpos2 = text.find('\n', curpos);
195                 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
196
197                 docstring const word =
198                         nxtpos == docstring::npos ?
199                         text.substr(curpos) :
200                         text.substr(curpos, nxtpos - curpos);
201
202                 bool const newline = (nxtpos2 != docstring::npos &&
203                                       nxtpos2 < nxtpos1);
204
205                 docstring const line_plus_word =
206                         line.empty() ? word : line + lyx::char_type(' ') + word;
207
208                 // FIXME: make w be size_t
209                 if (int(line_plus_word.length()) >= w) {
210                         sout += line + lyx::char_type('\n');
211                         if (newline) {
212                                 sout += word + lyx::char_type('\n');
213                                 line.erase();
214                         } else {
215                                 line = word;
216                         }
217
218                 } else if (newline) {
219                         sout += line_plus_word + lyx::char_type('\n');
220                         line.erase();
221
222                 } else {
223                         if (!line.empty())
224                                 line += lyx::char_type(' ');
225                         line += word;
226                 }
227
228                 if (nxtpos == docstring::npos) {
229                         if (!line.empty())
230                                 sout += line;
231                         break;
232                 }
233
234                 curpos = nxtpos + 1;
235         }
236
237         return sout;
238 }