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