]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qt_helpers.C
reverse last change
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "support/lstrings.h"
18 #include "gettext.h"
19 #include "qt_helpers.h"
20  
21 #include "lengthcombo.h"
22  
23 #include <qglobal.h>
24 #include <qlineedit.h>
25 #include <qtextcodec.h>
26
27 using std::pair;
28 using std::make_pair;
29
30 string makeFontName(string const & family, string const & foundry)
31 {
32         if (foundry.empty())
33                 return family;
34 #if QT_VERSION  >= 300
35         return family + " [" + foundry + ']';
36 #else
37         return foundry + '-' + family;
38 #endif
39 }
40
41
42 pair<string,string> parseFontName(string const & name)
43 {
44 #if QT_VERSION  >= 300
45         string::size_type const idx = name.find('[');
46         if (idx == string::npos || idx == 0)
47                 return make_pair(name, string());
48         return make_pair(name.substr(0, idx - 1),
49                          name.substr(idx + 1, name.size() - idx - 2));
50 #else
51         string::size_type const idx = name.find('-');
52         if (idx == string::npos || idx == 0)
53                 return make_pair(name, string());
54         return make_pair(name.substr(idx + 1),
55                          name.substr(0, idx));
56 #endif
57 }
58
59  
60 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
61 {
62         QString length = input->text();
63         if (length.isEmpty())
64                 return string();
65
66         // don't return unit-from-choice if the input(field) contains a unit
67         if (isValidGlueLength(fromqstr(length)))
68                 return fromqstr(length);
69
70         LyXLength::UNIT unit = combo->currentLengthItem();
71
72         return LyXLength(length.toDouble(), unit).asString();
73 }
74
75
76 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
77         string const & len, LyXLength::UNIT defaultUnit)
78 {
79         if (len.empty()) {
80                 // no length (UNIT_NONE)
81                 combo->setCurrentItem(defaultUnit);
82                 input->setText("");
83         } else {
84                 combo->setCurrentItem(LyXLength(len).unit());
85                 input->setText(toqstr(tostr(LyXLength(len).value())));
86         }
87 }
88
89
90 QString const toqstr(char const * str)
91 {
92         QTextCodec * codec = QTextCodec::codecForLocale();
93
94         return codec->toUnicode(str);
95 }
96
97
98 QString const toqstr(string const & str)
99 {
100         return toqstr(str.c_str());
101 }
102
103
104 QString const qt_(char const * str)
105 {
106         return toqstr(_(str));
107 }
108
109
110 QString const qt_(string const & str)
111 {
112         return toqstr(_(str));
113 }
114
115
116 string const fromqstr(QString const & str)
117 {
118         QTextCodec * codec = QTextCodec::codecForLocale();
119         QCString tmpstr = codec->fromUnicode(str);
120         char const * tmpcstr = tmpstr;
121         return tmpcstr;
122 }