]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qt_helpers.C
renaming in frontends/qt4/ui: s/Q//g
[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  * \author Richard Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "lengthcombo.h"
16 #include "qt_helpers.h"
17
18 #include "lengthcommon.h"
19 #include "gettext.h"
20
21 #include "support/os.h"
22 #include "support/lstrings.h"
23 #include "support/convert.h"
24
25 #include "debug.h"
26
27 #include <QComboBox>
28 #include <QCheckBox>
29 #include <qlineedit.h>
30 #include <qtextcodec.h>
31
32 #include <algorithm>
33
34
35 namespace lyx {
36
37 using support::isStrDbl;
38
39 using std::vector;
40 using std::make_pair;
41 using std::string;
42 using std::pair;
43 using std::endl;
44
45
46 string makeFontName(string const & family, string const & foundry)
47 {
48         if (foundry.empty())
49                 return family;
50         return family + " [" + foundry + ']';
51 }
52
53
54 pair<string, string> parseFontName(string const & name)
55 {
56         string::size_type const idx = name.find('[');
57         if (idx == string::npos || idx == 0)
58                 return make_pair(name, string());
59         return make_pair(name.substr(0, idx - 1),
60                          name.substr(idx + 1, name.size() - idx - 2));
61 }
62
63
64 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
65 {
66         QString const length = input->text();
67         if (length.isEmpty())
68                 return string();
69
70         // Don't return unit-from-choice if the input(field) contains a unit
71         if (isValidGlueLength(fromqstr(length)))
72                 return fromqstr(length);
73
74         LyXLength::UNIT const unit = combo->currentLengthItem();
75
76         return LyXLength(length.toDouble(), unit).asString();
77 }
78
79
80 LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
81 {
82         QString const length = input->text();
83         if (length.isEmpty())
84                 return LyXLength();
85
86         // don't return unit-from-choice if the input(field) contains a unit
87         if (isValidGlueLength(fromqstr(length)))
88                 return LyXLength(fromqstr(length));
89
90         LyXLength::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
91
92         return LyXLength(length.toDouble(), unit);
93 }
94
95
96 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
97         LyXLength const & len, LyXLength::UNIT defaultUnit) 
98 {
99         combo->setCurrentItem(LyXLength(len).unit());
100         input->setText(toqstr(convert<string>(LyXLength(len).value())));
101 }
102
103
104 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
105         string const & len, LyXLength::UNIT defaultUnit)
106 {
107         if (len.empty()) {
108                 // no length (UNIT_NONE)
109                 combo->setCurrentItem(defaultUnit);
110                 input->setText("");
111         } else if (!isValidLength(len) && !isStrDbl(len)) {
112                 // use input field only for gluelengths
113                 combo->setCurrentItem(defaultUnit);
114                 input->setText(toqstr(len));
115         } else {
116                 lengthToWidgets(input, combo, LyXLength(len), defaultUnit);
117         }
118 }
119
120
121 void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo, 
122         LyXLength const & len, LyXLength::UNIT defaultUnit)
123 {
124         if (len.value() == 0) 
125                 lengthToWidgets(input, combo, "auto", defaultUnit);
126         else
127                 lengthToWidgets(input, combo, len, defaultUnit);
128 }
129
130
131 //NOTE "CB" here because we probably will want one of these
132 //for labeled sets, as well.
133 void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit, 
134         LengthCombo * lengthCombo) 
135 {
136         if (!checkBox->isChecked()) 
137                 lengthToWidgets(lineEdit, lengthCombo, "auto", lengthCombo->currentLengthItem());
138         else if (lineEdit->text() == "auto")
139                 lengthToWidgets(lineEdit, lengthCombo, string(""), lengthCombo->currentLengthItem());
140 }
141
142
143 QString const qt_(char const * str, const char *)
144 {
145         return toqstr(_(str));
146 }
147
148
149 QString const qt_(string const & str)
150 {
151         return toqstr(_(str));
152 }
153
154
155 docstring const formatted(docstring const & text, int w)
156 {
157         docstring sout;
158
159         if (text.empty())
160                 return sout;
161
162         docstring::size_type curpos = 0;
163         docstring line;
164
165         for (;;) {
166                 docstring::size_type const nxtpos1 = text.find(' ',  curpos);
167                 docstring::size_type const nxtpos2 = text.find('\n', curpos);
168                 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
169
170                 docstring const word =
171                         nxtpos == docstring::npos ?
172                         text.substr(curpos) :
173                         text.substr(curpos, nxtpos - curpos);
174
175                 bool const newline = (nxtpos2 != docstring::npos &&
176                                       nxtpos2 < nxtpos1);
177
178                 docstring const line_plus_word =
179                         line.empty() ? word : line + lyx::char_type(' ') + word;
180
181                 // FIXME: make w be size_t
182                 if (int(line_plus_word.length()) >= w) {
183                         sout += line + lyx::char_type('\n');
184                         if (newline) {
185                                 sout += word + lyx::char_type('\n');
186                                 line.erase();
187                         } else {
188                                 line = word;
189                         }
190
191                 } else if (newline) {
192                         sout += line_plus_word + lyx::char_type('\n');
193                         line.erase();
194
195                 } else {
196                         if (!line.empty())
197                                 line += lyx::char_type(' ');
198                         line += word;
199                 }
200
201                 if (nxtpos == docstring::npos) {
202                         if (!line.empty())
203                                 sout += line;
204                         break;
205                 }
206
207                 curpos = nxtpos + 1;
208         }
209
210         return sout;
211 }
212
213 } // namespace lyx