]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.C
587f1630c0a39d67d7bc1be4a8409be8ea0c6ef4
[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 namespace lyx {
33
34
35 using lyx::support::isStrDbl;
36 using lyx::char_type;
37 using lyx::docstring;
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         LyXLength::UNIT const unit = combo->currentLengthItem();
75
76         return LyXLength(length.toDouble(), unit).asString();
77 }
78
79
80 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
81 {
82         QString const length = input->text();
83         if (length.isEmpty())
84                 return LyXLength();
85
86         // don't return unit-from-choice if the input(field) contains a unit
87         if (isValidGlueLength(fromqstr(length)))
88                 return LyXLength(fromqstr(length));
89
90         LyXLength::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
91
92         return LyXLength(length.toDouble(), unit);
93 }
94
95
96 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
97         string const & len, LyXLength::UNIT defaultUnit)
98 {
99         if (len.empty()) {
100                 // no length (UNIT_NONE)
101                 combo->setCurrentItem(defaultUnit);
102                 input->setText("");
103         } else if (!isValidLength(len) && !isStrDbl(len)) {
104                 // use input field only for gluelengths
105                 combo->setCurrentItem(defaultUnit);
106                 input->setText(toqstr(len));
107         } else {
108                 combo->setCurrentItem(LyXLength(len).unit());
109                 input->setText(toqstr(convert<string>(LyXLength(len).value())));
110         }
111 }
112
113
114 void ucs4_to_qstring(lyx::docstring const & str, QString & s)
115 {
116         int i = static_cast<int>(str.size()); 
117         s.resize(i);
118         for (; --i >= 0;)
119                 s[i] = ucs4_to_qchar(str[i]);
120 }
121
122
123 QString const toqstr(docstring const & ucs4)
124 {
125         QString s;
126         ucs4_to_qstring(ucs4, s);
127         return s;
128 }
129
130
131 docstring const qstring_to_ucs4(QString const & qstr)
132 {
133         int const ls = qstr.size();
134         docstring ucs4;
135         for (int i = 0; i < ls; ++i)
136                 ucs4 += static_cast<char_type>(qstr[i].unicode());
137         return ucs4;
138 }
139
140
141 QString const qt_(char const * str, const char *)
142 {
143         return toqstr(_(str));
144 }
145
146
147 QString const qt_(string const & str)
148 {
149         return toqstr(_(str));
150 }
151
152
153 string const fromqstr(QString const & str)
154 {
155         return str.isEmpty()? string(): string(str.toAscii());
156 }
157
158
159 docstring const formatted(docstring const & text, int w)
160 {
161         docstring sout;
162
163         if (text.empty())
164                 return sout;
165
166         docstring::size_type curpos = 0;
167         docstring line;
168
169         for (;;) {
170                 docstring::size_type const nxtpos1 = text.find(' ',  curpos);
171                 docstring::size_type const nxtpos2 = text.find('\n', curpos);
172                 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
173
174                 docstring const word =
175                         nxtpos == docstring::npos ?
176                         text.substr(curpos) :
177                         text.substr(curpos, nxtpos - curpos);
178
179                 bool const newline = (nxtpos2 != docstring::npos &&
180                                       nxtpos2 < nxtpos1);
181
182                 docstring const line_plus_word =
183                         line.empty() ? word : line + lyx::char_type(' ') + word;
184
185                 // FIXME: make w be size_t
186                 if (int(line_plus_word.length()) >= w) {
187                         sout += line + lyx::char_type('\n');
188                         if (newline) {
189                                 sout += word + lyx::char_type('\n');
190                                 line.erase();
191                         } else {
192                                 line = word;
193                         }
194
195                 } else if (newline) {
196                         sout += line_plus_word + lyx::char_type('\n');
197                         line.erase();
198
199                 } else {
200                         if (!line.empty())
201                                 line += lyx::char_type(' ');
202                         line += word;
203                 }
204
205                 if (nxtpos == docstring::npos) {
206                         if (!line.empty())
207                                 sout += line;
208                         break;
209                 }
210
211                 curpos = nxtpos + 1;
212         }
213
214         return sout;
215 }
216
217
218 } // namespace lyx