2 * \file LengthCombo.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "support/qstring_helpers.h"
17 #include "LengthCombo.h"
19 #include "qt_helpers.h"
27 LengthCombo::LengthCombo(QWidget * parent)
30 for (int i = 0; i < lyx::num_units; i++) {
31 // mu does not make sense usually
32 // so it must be added manually, if needed
33 if (QLatin1String(lyx::unit_name[i]) == "mu")
35 QComboBox::addItem(lyx::qt_(lyx::unit_name_gui[i]),
36 lyx::toqstr(lyx::unit_name[i]));
39 connect(this, SIGNAL(activated(int)),
40 this, SLOT(hasActivated(int)));
44 lyx::Length::UNIT LengthCombo::currentLengthItem() const
46 QString const val = itemData(currentIndex()).toString();
47 return lyx::unitFromString(lyx::fromqstr(val));
51 void LengthCombo::hasActivated(int)
54 selectionChanged(currentLengthItem());
58 void LengthCombo::setCurrentItem(lyx::Length::UNIT unit)
60 setCurrentItem(lyx::toqstr(lyx::stringFromUnit(unit)));
64 void LengthCombo::setCurrentItem(QString const & item)
66 int num = QComboBox::count();
67 for (int i = 0; i < num; i++) {
68 if (QComboBox::itemData(i).toString() == item) {
69 QComboBox::setCurrentIndex(i);
76 void LengthCombo::setCurrentItem(int item)
78 QComboBox::setCurrentIndex(item);
82 void LengthCombo::setEnabled(bool b)
84 QComboBox::setEnabled(b);
88 void LengthCombo::noPercents()
90 int num = QComboBox::count();
91 for (int i = 0; i < num; i++) {
92 if (QComboBox::itemData(i).toString().contains('%')) {
93 QComboBox::removeItem(i);
101 void LengthCombo::removeFontDependent()
103 removeUnit(Length::EM);
104 removeUnit(Length::EX);
105 removeUnit(Length::MU);
109 void LengthCombo::removeUnit(lyx::Length::UNIT unit)
111 QString const val = lyx::toqstr(lyx::stringFromUnit(unit));
112 int num = QComboBox::count();
113 for (int i = 0; i < num; i++) {
114 if (QComboBox::itemData(i).toString() == val) {
115 QComboBox::removeItem(i);
122 void LengthCombo::addUnit(lyx::Length::UNIT unit)
124 QString const val = lyx::toqstr(lyx::stringFromUnit(unit));
125 int num = QComboBox::count();
126 for (int i = 0; i < num; i++) {
127 if (QComboBox::itemData(i).toString() == val) {
128 // already there, nothing to do
132 insertItem(int(unit), lyx::qt_(lyx::unit_name_gui[int(unit)]),
133 lyx::toqstr(lyx::unit_name[int(unit)]));
136 } // namespace frontend
139 #include "moc_LengthCombo.cpp"