]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qt_helpers.C
get rid of broken_header.h and some unneeded tests
[lyx.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 * codec = QTextCodec::codecForLocale();
146         QCString tmpstr = codec->fromUnicode(str);
147         char const * tmpcstr = tmpstr;
148         return tmpcstr;
149 }
150
151
152 string const formatted(string const & text, int w)
153 {
154         string sout;
155
156         if (text.empty())
157                 return sout;
158
159         string::size_type curpos = 0;
160         string line;
161
162         for (;;) {
163                 string::size_type const nxtpos1 = text.find(' ',  curpos);
164                 string::size_type const nxtpos2 = text.find('\n', curpos);
165                 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
166
167                 string const word = nxtpos == string::npos ?
168                         text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
169
170                 bool const newline = (nxtpos2 != string::npos &&
171                                       nxtpos2 < nxtpos1);
172
173                 string const line_plus_word =
174                         line.empty() ? word : line + ' ' + word;
175
176                 // FIXME: make w be size_t
177                 if (int(line_plus_word.length()) >= w) {
178                         sout += line + '\n';
179                         if (newline) {
180                                 sout += word + '\n';
181                                 line.erase();
182                         } else {
183                                 line = word;
184                         }
185
186                 } else if (newline) {
187                         sout += line_plus_word + '\n';
188                         line.erase();
189
190                 } else {
191                         if (!line.empty())
192                                 line += ' ';
193                         line += word;
194                 }
195
196                 if (nxtpos == string::npos) {
197                         if (!line.empty())
198                                 sout += line;
199                         break;
200                 }
201
202                 curpos = nxtpos + 1;
203         }
204
205         return sout;
206 }