KSane

labeledcombo.cpp
1/*
2 * SPDX-FileCopyrightText: 2007-2011 Kare Sars <kare.sars@iki .fi>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7#include "labeledcombo.h"
8
9#include <QLabel>
10#include <QComboBox>
11
12//KDE includes
13
14#include <KLocalizedString>
15
16namespace KSaneIface
17{
18
19LabeledCombo::LabeledCombo(QWidget *parent, const QString &ltext, const QStringList &list)
20 : KSaneOptionWidget(parent, ltext)
21{
22 initCombo(list);
23}
24
26 : KSaneOptionWidget(parent, option)
27{
28 initCombo(QStringList());
29 setLabelText(option->title());
30 setToolTip(option->description());
31 connect(this, &LabeledCombo::valueChanged, option, &KSaneCore::Option::setValue);
32 connect(option, &KSaneCore::Option::valueChanged, this, &LabeledCombo::setValue);
33 clear();
34
35 const QVariantList values = option->valueList();
36 const QVariantList internalValues = option->internalValueList();
37 for (int i = 0; i < values.count(); i++) {
38 const auto &value = values.at(i);
39 const auto &internalValue = internalValues.at(i);
40 if (value.type() == static_cast<QVariant::Type>(QMetaType::Int)) {
41 addItem(getStringWithUnitForInteger(value.toInt()), internalValue);
42 } else if (value.type() == static_cast<QVariant::Type>(QMetaType::Float)) {
43 addItem(getStringWithUnitForFloat(value.toFloat()), internalValue);
44 } else {
45 addItem(value.toString(), internalValue);
46 if (internalValue == QStringLiteral("Color")) {
47 m_combo->setItemIcon(i, QIcon::fromTheme(QStringLiteral("color")));
48 }
49 if (internalValue == QStringLiteral("Gray")) {
50 m_combo->setItemIcon(i, QIcon::fromTheme(QStringLiteral("gray-scale")));
51 }
52 if (internalValue == QStringLiteral("Lineart") || internalValue == QStringLiteral("Binary")) {
53 m_combo->setItemIcon(i, QIcon::fromTheme(QStringLiteral("black-white")));
54 }
55 }
56 }
57
58 QString currentText = option->value().toString();
59
61}
62
63void LabeledCombo::initCombo(const QStringList &list)
64{
65 m_combo = new QComboBox(this);
66 m_combo->addItems(list);
67
68 m_label->setBuddy(m_combo);
69
70 connect(m_combo, QOverload<int>::of(&QComboBox::activated), this, &LabeledCombo::emitChangedValue);
71 connect(m_combo, QOverload<int>::of(&QComboBox::activated), this, &LabeledCombo::activated);
72
73 m_layout->addWidget(m_combo, 0, 1);
74 m_layout->addWidget(new QWidget(this), 0, 2);
75 m_layout->setColumnStretch(1, 0);
76 m_layout->setColumnStretch(2, 50);
78}
79
81{
82 m_combo->addItems(list);
83
84 QString tmp;
85 for (int i = 0; i < m_combo->count(); ++i) {
86 tmp = m_combo->itemText(i);
87 m_combo->setItemData(i, tmp, Qt::ToolTipRole);
88 }
89}
90
92{
93 for (int i = 0; i < m_combo->count(); ++i) {
94 if (m_combo->itemText(i) == t) {
95 m_combo->setCurrentIndex(i);
96 }
97 }
98}
99
101{
102 return m_combo->currentText();
103}
104
106{
107 m_combo->setCurrentIndex(i);
108}
109
110void LabeledCombo::setValue(const QVariant &val)
111{
112 for (int i = 0; i < m_combo->count(); ++i) {
113 if (m_combo->itemData(i) == val) {
114 m_combo->setCurrentIndex(i);
115 break;
116 }
117 }
118}
119
121{
122 m_combo->clear();
123}
124
125void LabeledCombo::emitChangedValue(int)
126{
127 Q_EMIT valueChanged(m_combo->currentData());
128}
129
131{
132 return m_combo->currentData(role);
133}
134
135void LabeledCombo::addItem(const QString &text, const QVariant &userData)
136{
137 m_combo->addItem(text, userData);
138}
139
141{
142 return m_combo->count();
143}
144
146{
147 return m_combo->currentIndex();
148}
149
150QString LabeledCombo::getStringWithUnitForInteger(int iValue) const
151{
152 switch (m_option->valueUnit()) {
153
154 case KSaneCore::Option::UnitPixel:
155 return i18ncp("Parameter and Unit", "%1 Pixel", "%1 Pixels", iValue);
156 break;
157 case KSaneCore::Option::UnitBit:
158 return i18ncp("Parameter and Unit", "%1 Bit", "%1 Bits", iValue);
159 break;
160 case KSaneCore::Option::UnitMilliMeter:
161 return i18nc("Parameter and Unit (Millimeter)", "%1 mm", iValue);
162 break;
163 case KSaneCore::Option::UnitDPI:
164 return i18nc("Parameter and Unit (Dots Per Inch)", "%1 DPI", iValue);
165 break;
166 case KSaneCore::Option::UnitPercent:
167 return i18nc("Parameter and Unit (Percentage)", "%1 %", iValue);
168 break;
169 case KSaneCore::Option::UnitMicroSecond:
170 return i18nc("Parameter and Unit (Microseconds)", "%1 µs", iValue);
171 break;
172 case KSaneCore::Option::UnitSecond:
173 return i18nc("Parameter and Unit (seconds)", "%1 s", iValue);
174 break;
175 default:
176 return i18n("%1", iValue);
177 break;
178 }
179}
180
181QString LabeledCombo::getStringWithUnitForFloat(float fValue) const
182{
183 switch (m_option->valueUnit()) {
184
185 case KSaneCore::Option::UnitPixel:
186 return i18ncp("Parameter and Unit", "%1 Pixel", "%1 Pixels", static_cast<int>(fValue));
187 break;
188 case KSaneCore::Option::UnitBit:
189 return i18ncp("Parameter and Unit", "%1 Bit", "%1 Bits", static_cast<int>(fValue));
190 break;
191 case KSaneCore::Option::UnitMilliMeter:
192 return i18nc("Parameter and Unit (Millimeter)", "%1 mm", fValue);
193 break;
194 case KSaneCore::Option::UnitDPI:
195 return i18nc("Parameter and Unit (Dots Per Inch)", "%1 DPI", fValue);
196 break;
197 case KSaneCore::Option::UnitPercent:
198 return i18nc("Parameter and Unit (Percentage)", "%1 %", fValue);
199 break;
200 case KSaneCore::Option::UnitMicroSecond:
201 return i18nc("Parameter and Unit (Microseconds)", "%1 µs", fValue);
202 break;
203 case KSaneCore::Option::UnitSecond:
204 return i18nc("Parameter and Unit (seconds)", "%1 s", fValue);
205 break;
206 default:
207 return i18n("%1", fValue);
208 break;
209 }
210}
211
212} // NameSpace KSaneIface
213
214#include "moc_labeledcombo.cpp"
void valueChanged(const QVariant &value)
OptionUnit valueUnit() const
QVariantList internalValueList() const
bool setValue(const QVariant &value)
QVariantList valueList() const
QString title() const
QVariant value() const
QString description() const
Base class for option widgets.
void setCurrentText(const QString &)
If the given string can be found in the combobox, activate that entry.
QString currentText() const
This function is used to read the current string of the combobox.
void addItem(const QString &text, const QVariant &userData=QVariant())
This function forwards the request to the QComboBox equivalent.
void addItems(const QStringList &list)
Add string entries to the combobox.
int count() const
This function forwards the request to the QComboBox equivalent.
void setCurrentIndex(int)
set the current item of the combobox.
QVariant currentData(int role=Qt::UserRole) const
This function forwards the request to the QComboBox equivalent.
int currentIndex() const
This function forwards the request to the QComboBox equivalent.
LabeledCombo(QWidget *parent, const QString &label, const QStringList &list=QStringList())
create a label and combobox combination.
void clear()
Remove all string entries.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString i18ncp(const char *context, const char *singular, const char *plural, const TYPE &arg...)
void activated(int index)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void addItems(const QStringList &texts)
void clear()
void setCurrentIndex(int index)
QVariant itemData(int index, int role) const const
QString itemText(int index) const const
void setItemData(int index, const QVariant &value, int role)
void setItemIcon(int index, const QIcon &icon)
void addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment)
void setColumnStretch(int column, int stretch)
QIcon fromTheme(const QString &name)
void setBuddy(QWidget *buddy)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
ToolTipRole
QString toString() const const
QWidget(QWidget *parent, Qt::WindowFlags f)
void setSizePolicy(QSizePolicy)
void setToolTip(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 12:00:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.