]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/LengthCombo.cpp
* fix spelling in comments to please John.
[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 (QString(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(hasActivated(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::hasActivated(int)
49 {
50   // emit signal
51         selectionChanged(currentLengthItem());
52 }
53
54
55 void LengthCombo::setCurrentItem(lyx::Length::UNIT unit)
56 {
57         setCurrentItem(lyx::toqstr(lyx::stringFromUnit(unit)));
58 }
59
60
61 void LengthCombo::setCurrentItem(QString const item)
62 {
63         int num = QComboBox::count();
64         for (int i = 0; i < num; i++) {
65                 if (QComboBox::itemData(i).toString() == item) {
66                         QComboBox::setCurrentIndex(i);
67                         break;
68                 }
69         }
70 }
71
72
73 void LengthCombo::setCurrentItem(int item)
74 {
75         QComboBox::setCurrentIndex(item);
76 }
77
78
79 void LengthCombo::setEnabled(bool b)
80 {
81         QComboBox::setEnabled(b);
82 }
83
84
85 void LengthCombo::noPercents()
86 {
87         int num = QComboBox::count();
88         for (int i = 0; i < num; i++) {
89                 if (QComboBox::itemData(i).toString().contains('%')) {
90                         QComboBox::removeItem(i);
91                         --i;
92                         --num;
93                 }
94         }
95 }
96
97
98 void LengthCombo::removeUnit(lyx::Length::UNIT unit)
99 {
100         QString const val = lyx::toqstr(lyx::stringFromUnit(unit));
101         int num = QComboBox::count();
102         for (int i = 0; i < num; i++) {
103                 if (QComboBox::itemData(i).toString() == val) {
104                         QComboBox::removeItem(i);
105                         break;
106                 }
107         }
108 }
109
110
111 void LengthCombo::addUnit(lyx::Length::UNIT unit)
112 {
113         QString const val = lyx::toqstr(lyx::stringFromUnit(unit));
114         int num = QComboBox::count();
115         for (int i = 0; i < num; i++) {
116                 if (QComboBox::itemData(i).toString() == val) {
117                         // already there, nothing to do
118                         return;
119                 }
120         }
121         insertItem(int(unit), lyx::qt_(lyx::unit_name_gui[int(unit)]),
122                    lyx::toqstr(lyx::unit_name[int(unit)]));
123 }
124
125
126 #include "moc_LengthCombo.cpp"