]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qt_helpers.C
76a56b50f8d9db3d78f1e18960636edc63a9a2e8
[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  
19 #include "qt_helpers.h"
20
21 #include "lengthcombo.h"
22  
23 #include <qglobal.h>
24 #include <qlineedit.h>
25
26 using std::pair;
27 using std::make_pair;
28
29 string makeFontName(string const & family, string const & foundry)
30 {
31         if (foundry.empty())
32                 return family;
33 #if QT_VERSION  >= 300
34         return family + " [" + foundry + ']';
35 #else
36         return foundry + '-' + family;
37 #endif
38 }
39
40
41 pair<string,string> parseFontName(string const & name)
42 {
43 #if QT_VERSION  >= 300
44         string::size_type const idx = name.find('[');
45         if (idx == string::npos || idx == 0)
46                 return make_pair(name, string());
47         return make_pair(name.substr(0, idx - 1),
48                          name.substr(idx + 1, name.size() - idx - 2));
49 #else
50         string::size_type const idx = name.find('-');
51         if (idx == string::npos || idx == 0)
52                 return make_pair(name, string());
53         return make_pair(name.substr(idx + 1),
54                          name.substr(0, idx));
55 #endif
56 }
57
58  
59 string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
60 {
61         QString length = input->text();
62         if (length.isEmpty())
63                 return string();
64
65         // don't return unit-from-choice if the input(field) contains a unit
66         if (isValidGlueLength(length.latin1()))
67                 return length.latin1();
68
69         LyXLength::UNIT unit = combo->currentLengthItem();
70
71         return LyXLength(length.toDouble(), unit).asString();
72 }
73
74
75 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
76         string const & len, LyXLength::UNIT defaultUnit)
77 {
78         if (len.empty()) {
79                 // no length (UNIT_NONE)
80                 combo->setCurrentItem(defaultUnit);
81                 input->setText("");
82         } else {
83                 combo->setCurrentItem(LyXLength(len).unit());
84                 input->setText(tostr(LyXLength(len).value()).c_str());
85         }
86 }