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