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