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