]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/LengthCombo.cpp
448737c3d76172d08714622c9f5f1643d0ef99d5
[lyx.git] / src / frontends / qt4 / LengthCombo.cpp
1 /**
2  * \file LengthCombo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  * \author Herbert Voß
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "support/qstring_helpers.h"
16
17 #include "LengthCombo.h"
18
19 #include "qt_helpers.h"
20
21 #include <string>
22
23
24 LengthCombo::LengthCombo(QWidget * parent)
25         : QComboBox(parent)
26 {
27         for (int i = 0; i < lyx::num_units; i++) {
28                 // mu does not make sense usually
29                 // so it must be added manually, if needed
30                 if (lyx::unit_name[i] == "mu")
31                         continue;
32                 QComboBox::addItem(lyx::qt_(lyx::unit_name_gui[i]),
33                         lyx::toqstr(lyx::unit_name[i]));
34         }
35
36         connect(this, SIGNAL(activated(int)),
37                 this, SLOT(has_activated(int)));
38 }
39
40
41 lyx::Length::UNIT LengthCombo::currentLengthItem() const
42 {
43         QString const val = itemData(currentIndex()).toString();
44         return lyx::unitFromString(lyx::fromqstr(val));
45 }
46
47
48 void LengthCombo::has_activated(int)
49 {
50   // emit signal
51         selectionChanged(currentLengthItem());
52 }
53
54
55 void LengthCombo::setCurrentItem(lyx::Length::UNIT unit)
56 {
57         QString const val = lyx::toqstr(lyx::stringFromUnit(unit));
58         int num = QComboBox::count();
59         for (int i = 0; i < num; i++) {
60                 if (QComboBox::itemData(i).toString() == val) {
61                         QComboBox::setCurrentIndex(i);
62                         break;
63                 }
64         }
65 }
66
67
68 void LengthCombo::setCurrentItem(int item)
69 {
70         QComboBox::setCurrentIndex(item);
71 }
72
73
74 void LengthCombo::setEnabled(bool b)
75 {
76         QComboBox::setEnabled(b);
77 }
78
79
80 void LengthCombo::noPercents()
81 {
82         int num = QComboBox::count();
83         for (int i = 0; i < num; i++) {
84                 if (QComboBox::itemData(i).toString().contains('%')) {
85                         QComboBox::removeItem(i);
86                         --i;
87                         --num;
88                 }
89         }
90 }
91
92
93 void LengthCombo::removeItem(lyx::Length::UNIT unit)
94 {
95         QString const val = lyx::toqstr(lyx::stringFromUnit(unit));
96         int num = QComboBox::count();
97         for (int i = 0; i < num; i++) {
98                 if (QComboBox::itemData(i).toString() == val) {
99                         QComboBox::removeItem(i);
100                         break;
101                 }
102         }
103 }
104
105
106 void LengthCombo::removeItem(int item)
107 {
108         QComboBox::removeItem(item);
109 }
110
111
112 void LengthCombo::addItem(lyx::Length::UNIT unit)
113 {
114         QString const val = lyx::toqstr(lyx::stringFromUnit(unit));
115         int num = QComboBox::count();
116         for (int i = 0; i < num; i++) {
117                 if (QComboBox::itemData(i).toString() == val) {
118                         // already there, nothing to do
119                         return;
120                 }
121         }
122         insertItem(int(unit), lyx::qt_(lyx::unit_name_gui[int(unit)]),
123                    lyx::toqstr(lyx::unit_name[int(unit)]));
124 }
125
126
127 void LengthCombo::addItem(QString const item)
128 {
129         QComboBox::addItem(item);
130 }
131
132 #include "moc_LengthCombo.cpp"