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