]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[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;
132
133         return str.ascii() ? str.ascii() : "";
134 }
135
136
137 string const formatted(string const & text, int w)
138 {
139         string sout;
140
141         if (text.empty())
142                 return sout;
143
144         string::size_type curpos = 0;
145         string line;
146
147         for (;;) {
148                 string::size_type const nxtpos1 = text.find(' ',  curpos);
149                 string::size_type const nxtpos2 = text.find('\n', curpos);
150                 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
151
152                 string const word = nxtpos == string::npos ?
153                         text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
154
155                 bool const newline = (nxtpos2 != string::npos &&
156                                       nxtpos2 < nxtpos1);
157
158                 string const line_plus_word =
159                         line.empty() ? word : line + ' ' + word;
160
161                 // FIXME: make w be size_t
162                 if (int(line_plus_word.length()) >= w) {
163                         sout += line + '\n';
164                         if (newline) {
165                                 sout += word + '\n';
166                                 line.erase();
167                         } else {
168                                 line = word;
169                         }
170
171                 } else if (newline) {
172                         sout += line_plus_word + '\n';
173                         line.erase();
174
175                 } else {
176                         if (!line.empty())
177                                 line += ' ';
178                         line += word;
179                 }
180
181                 if (nxtpos == string::npos) {
182                         if (!line.empty())
183                                 sout += line;
184                         break;
185                 }
186
187                 curpos = nxtpos + 1;
188         }
189
190         return sout;
191 }