]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qt_helpers.C
Get rid of the static_casts.
[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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "support/tostr.h"
14 #include "gettext.h"
15 #include "qt_helpers.h"
16
17 #include "lengthcombo.h"
18
19 #include <qglobal.h>
20 #include <qlineedit.h>
21 #include <qtextcodec.h>
22
23 #include <algorithm>
24
25 using std::pair;
26 using std::make_pair;
27 using std::min;
28
29 string makeFontName(string const & family, string const & foundry)
30 {
31         if (foundry.empty())
32                 return family;
33 #if QT_VERSION  >= 300
34         return family + " [" + foundry + ']';
35 #else
36         return foundry + '-' + family;
37 #endif
38 }
39
40
41 pair<string,string> parseFontName(string const & name)
42 {
43 #if QT_VERSION  >= 300
44         string::size_type const idx = name.find('[');
45         if (idx == string::npos || idx == 0)
46                 return make_pair(name, string());
47         return make_pair(name.substr(0, idx - 1),
48                          name.substr(idx + 1, name.size() - idx - 2));
49 #else
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(idx + 1),
54                          name.substr(0, idx));
55 #endif
56 }
57
58
59 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
60 {
61         QString length = input->text();
62         if (length.isEmpty())
63                 return string();
64
65         // don't return unit-from-choice if the input(field) contains a unit
66         if (isValidGlueLength(fromqstr(length)))
67                 return fromqstr(length);
68
69         LyXLength::UNIT unit = combo->currentLengthItem();
70
71         return LyXLength(length.toDouble(), unit).asString();
72 }
73
74
75 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
76         string const & len, LyXLength::UNIT defaultUnit)
77 {
78         if (len.empty()) {
79                 // no length (UNIT_NONE)
80                 combo->setCurrentItem(defaultUnit);
81                 input->setText("");
82         } else {
83                 combo->setCurrentItem(LyXLength(len).unit());
84                 input->setText(toqstr(tostr(LyXLength(len).value())));
85         }
86 }
87
88
89 QString const toqstr(char const * str)
90 {
91         QTextCodec * codec = QTextCodec::codecForLocale();
92
93         return codec->toUnicode(str);
94 }
95
96
97 QString const toqstr(string const & str)
98 {
99         return toqstr(str.c_str());
100 }
101
102
103 QString const qt_(char const * str)
104 {
105         return toqstr(_(str));
106 }
107
108
109 QString const qt_(string const & str)
110 {
111         return toqstr(_(str));
112 }
113
114
115 string const fromqstr(QString const & str)
116 {
117         QTextCodec * codec = QTextCodec::codecForLocale();
118         QCString tmpstr = codec->fromUnicode(str);
119         char const * tmpcstr = tmpstr;
120         return tmpcstr;
121 }
122
123
124 string const formatted(string const & text, int w)
125 {
126         string sout;
127
128         if (text.empty())
129                 return sout;
130
131         string::size_type curpos = 0;
132         string line;
133
134         for (;;) {
135                 string::size_type const nxtpos1 = text.find(' ',  curpos);
136                 string::size_type const nxtpos2 = text.find('\n', curpos);
137                 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
138
139                 string const word = nxtpos == string::npos ?
140                         text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
141
142                 bool const newline = (nxtpos2 != string::npos &&
143                                       nxtpos2 < nxtpos1);
144
145                 string const line_plus_word =
146                         line.empty() ? word : line + ' ' + word;
147
148                 // FIXME: make w be size_t
149                 if (line_plus_word.length() >= w) {
150                         sout += line + '\n';
151                         if (newline) {
152                                 sout += word + '\n';
153                                 line.erase();
154                         } else {
155                                 line = word;
156                         }
157
158                 } else if (newline) {
159                         sout += line_plus_word + '\n';
160                         line.erase();
161
162                 } else {
163                         if (!line.empty())
164                                 line += ' ';
165                         line += word;
166                 }
167
168                 if (nxtpos == string::npos) {
169                         if (!line.empty())
170                                 sout += line;
171                         break;
172                 }
173
174                 curpos = nxtpos + 1;
175         }
176
177         return sout;
178 }