]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.C
remove qPixmapFromMimeSource. This caused the inclusion of Qt3 support headers which...
[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         return family + " [" + foundry + ']';
42 }
43
44
45 pair<string,string> parseFontName(string const & name)
46 {
47         string::size_type const idx = name.find('[');
48         if (idx == string::npos || idx == 0)
49                 return make_pair(name, string());
50         return make_pair(name.substr(0, idx - 1),
51                          name.substr(idx + 1, name.size() - idx - 2));
52 }
53
54
55 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
56 {
57         QString length = input->text();
58         if (length.isEmpty())
59                 return string();
60
61         // don't return unit-from-choice if the input(field) contains a unit
62         if (isValidGlueLength(fromqstr(length)))
63                 return fromqstr(length);
64
65         LyXLength::UNIT unit = combo->currentLengthItem();
66
67         return LyXLength(length.toDouble(), unit).asString();
68 }
69
70
71 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
72 {
73         QString length = input->text();
74         if (length.isEmpty())
75                 return LyXLength();
76
77         // don't return unit-from-choice if the input(field) contains a unit
78         if (isValidGlueLength(fromqstr(length)))
79                 return LyXLength(fromqstr(length));
80
81         LyXLength::UNIT unit = unitFromString(fromqstr(combo->currentText()));
82
83         return LyXLength(length.toDouble(), unit);
84 }
85
86
87 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
88         string const & len, LyXLength::UNIT defaultUnit)
89 {
90         if (len.empty()) {
91                 // no length (UNIT_NONE)
92                 combo->setCurrentItem(defaultUnit);
93                 input->setText("");
94         } else if (!isValidLength(len) && !isStrDbl(len)) {
95                 // use input field only for gluelengths
96                 combo->setCurrentItem(defaultUnit);
97                 input->setText(toqstr(len));
98         } else {
99                 combo->setCurrentItem(LyXLength(len).unit());
100                 input->setText(toqstr(convert<string>(LyXLength(len).value())));
101         }
102 }
103
104
105 QString const toqstr(char const * str)
106 {
107         return QString::fromAscii(str);
108 }
109
110
111 QString const toqstr(string const & str)
112 {
113         return toqstr(str.c_str());
114 }
115
116
117 QString const qt_(char const * str)
118 {
119         return toqstr(_(str));
120 }
121
122
123 QString const qt_(string const & str)
124 {
125         return toqstr(_(str));
126 }
127
128
129 string const fromqstr(QString const & str)
130 {
131         return str.isEmpty()? string(): string(str.toAscii());
132 }
133
134
135 string const formatted(string const & text, int w)
136 {
137         string sout;
138
139         if (text.empty())
140                 return sout;
141
142         string::size_type curpos = 0;
143         string line;
144
145         for (;;) {
146                 string::size_type const nxtpos1 = text.find(' ',  curpos);
147                 string::size_type const nxtpos2 = text.find('\n', curpos);
148                 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
149
150                 string const word = nxtpos == string::npos ?
151                         text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
152
153                 bool const newline = (nxtpos2 != string::npos &&
154                                       nxtpos2 < nxtpos1);
155
156                 string const line_plus_word =
157                         line.empty() ? word : line + ' ' + word;
158
159                 // FIXME: make w be size_t
160                 if (int(line_plus_word.length()) >= w) {
161                         sout += line + '\n';
162                         if (newline) {
163                                 sout += word + '\n';
164                                 line.erase();
165                         } else {
166                                 line = word;
167                         }
168
169                 } else if (newline) {
170                         sout += line_plus_word + '\n';
171                         line.erase();
172
173                 } else {
174                         if (!line.empty())
175                                 line += ' ';
176                         line += word;
177                 }
178
179                 if (nxtpos == string::npos) {
180                         if (!line.empty())
181                                 sout += line;
182                         break;
183                 }
184
185                 curpos = nxtpos + 1;
186         }
187
188         return sout;
189 }