]> git.lyx.org Git - features.git/blob - src/frontends/qt2/qt_helpers.C
f35c89fa2867bf6040363fbe0ae10998bbb00c8d
[features.git] / src / frontends / qt2 / 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/tostr.h"
22
23 #include <qcombobox.h>
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(tostr(LyXLength(len).value())));
113         }
114 }
115
116
117 QString const toqstr(char const * str)
118 {
119         QTextCodec * codec = QTextCodec::codecForLocale();
120
121         return codec->toUnicode(str);
122 }
123
124
125 QString const toqstr(string const & str)
126 {
127         return toqstr(str.c_str());
128 }
129
130
131 QString const qt_(char const * str)
132 {
133         return toqstr(_(str));
134 }
135
136
137 QString const qt_(string const & str)
138 {
139         return toqstr(_(str));
140 }
141
142
143 string const fromqstr(QString const & str)
144 {
145         QTextCodec const * const codec = QTextCodec::codecForLocale();
146         QCString const tmpstr = codec->fromUnicode(str);
147         return tmpstr.isEmpty() ? string() : string(tmpstr);
148 }
149
150
151 string const formatted(string const & text, int w)
152 {
153         string sout;
154
155         if (text.empty())
156                 return sout;
157
158         string::size_type curpos = 0;
159         string line;
160
161         for (;;) {
162                 string::size_type const nxtpos1 = text.find(' ',  curpos);
163                 string::size_type const nxtpos2 = text.find('\n', curpos);
164                 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
165
166                 string const word = nxtpos == string::npos ?
167                         text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
168
169                 bool const newline = (nxtpos2 != string::npos &&
170                                       nxtpos2 < nxtpos1);
171
172                 string const line_plus_word =
173                         line.empty() ? word : line + ' ' + word;
174
175                 // FIXME: make w be size_t
176                 if (int(line_plus_word.length()) >= w) {
177                         sout += line + '\n';
178                         if (newline) {
179                                 sout += word + '\n';
180                                 line.erase();
181                         } else {
182                                 line = word;
183                         }
184
185                 } else if (newline) {
186                         sout += line_plus_word + '\n';
187                         line.erase();
188
189                 } else {
190                         if (!line.empty())
191                                 line += ' ';
192                         line += word;
193                 }
194
195                 if (nxtpos == string::npos) {
196                         if (!line.empty())
197                                 sout += line;
198                         break;
199                 }
200
201                 curpos = nxtpos + 1;
202         }
203
204         return sout;
205 }