File indexing completed on 2025-03-02 04:16:29

0001 /*
0002  * SPDX-FileCopyrightText: 2007-2011 Kare Sars <kare.sars@iki .fi>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "labeledcombo.h"
0008 
0009 #include <QLabel>
0010 #include <QComboBox>
0011 
0012 //KDE includes
0013 
0014 #include <KLocalizedString>
0015 
0016 namespace KSaneIface
0017 {
0018 
0019 LabeledCombo::LabeledCombo(QWidget *parent, const QString &ltext, const QStringList &list)
0020     : KSaneOptionWidget(parent, ltext)
0021 {
0022     initCombo(list);
0023 }
0024 
0025 LabeledCombo::LabeledCombo(QWidget *parent, KSaneCore::Option *option)
0026     : KSaneOptionWidget(parent, option)
0027 {
0028     initCombo(QStringList());
0029     setLabelText(option->title());
0030     setToolTip(option->description());
0031     connect(this, &LabeledCombo::valueChanged, option, &KSaneCore::Option::setValue);
0032     connect(option, &KSaneCore::Option::valueChanged, this, &LabeledCombo::setValue);
0033     clear();
0034 
0035     const QVariantList values = option->valueList();
0036     const QVariantList internalValues = option->internalValueList();
0037     for (int i = 0; i < values.count(); i++) {
0038         const auto &value = values.at(i);
0039         const auto &internalValue = internalValues.at(i);
0040         if (value.type() == static_cast<QVariant::Type>(QMetaType::Int)) {
0041             addItem(getStringWithUnitForInteger(value.toInt()), internalValue);
0042         } else if (value.type() == static_cast<QVariant::Type>(QMetaType::Float)) {
0043             addItem(getStringWithUnitForFloat(value.toFloat()), internalValue);
0044         } else {
0045             addItem(value.toString(), internalValue);
0046             if (internalValue == QStringLiteral("Color")) {
0047                 m_combo->setItemIcon(i, QIcon::fromTheme(QStringLiteral("color")));
0048             }
0049             if (internalValue == QStringLiteral("Gray")) {
0050                 m_combo->setItemIcon(i, QIcon::fromTheme(QStringLiteral("gray-scale")));
0051             }
0052             if (internalValue == QStringLiteral("Lineart") || internalValue == QStringLiteral("Binary")) {
0053                 m_combo->setItemIcon(i, QIcon::fromTheme(QStringLiteral("black-white")));
0054             }
0055         }
0056     }
0057 
0058     QString currentText = option->value().toString();
0059 
0060     setCurrentText(currentText);
0061 }
0062 
0063 void LabeledCombo::initCombo(const QStringList &list)
0064 {
0065     m_combo  = new QComboBox(this);
0066     m_combo->addItems(list);
0067 
0068     m_label->setBuddy(m_combo);
0069 
0070     connect(m_combo, QOverload<int>::of(&QComboBox::activated), this, &LabeledCombo::emitChangedValue);
0071     connect(m_combo, QOverload<int>::of(&QComboBox::activated), this, &LabeledCombo::activated);
0072 
0073     m_layout->addWidget(m_combo, 0, 1);
0074     m_layout->addWidget(new QWidget(this), 0, 2);
0075     m_layout->setColumnStretch(1, 0);
0076     m_layout->setColumnStretch(2, 50);
0077     setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
0078 }
0079 
0080 void LabeledCombo::addItems(const QStringList &list)
0081 {
0082     m_combo->addItems(list);
0083 
0084     QString tmp;
0085     for (int i = 0; i < m_combo->count(); ++i) {
0086         tmp = m_combo->itemText(i);
0087         m_combo->setItemData(i, tmp, Qt::ToolTipRole);
0088     }
0089 }
0090 
0091 void LabeledCombo::setCurrentText(const QString &t)
0092 {
0093     for (int i = 0; i < m_combo->count(); ++i) {
0094         if (m_combo->itemText(i) == t) {
0095             m_combo->setCurrentIndex(i);
0096         }
0097     }
0098 }
0099 
0100 QString LabeledCombo::currentText() const
0101 {
0102     return m_combo->currentText();
0103 }
0104 
0105 void LabeledCombo::setCurrentIndex(int i)
0106 {
0107     m_combo->setCurrentIndex(i);
0108 }
0109 
0110 void LabeledCombo::setValue(const QVariant &val)
0111 {
0112     for (int i = 0; i < m_combo->count(); ++i) {
0113         if (m_combo->itemData(i) == val) {
0114             m_combo->setCurrentIndex(i);
0115             break;
0116         }
0117     }
0118 }
0119 
0120 void LabeledCombo::clear()
0121 {
0122     m_combo->clear();
0123 }
0124 
0125 void LabeledCombo::emitChangedValue(int)
0126 {
0127     Q_EMIT valueChanged(m_combo->currentData());
0128 }
0129 
0130 QVariant LabeledCombo::currentData(int role) const
0131 {
0132     return m_combo->currentData(role);
0133 }
0134 
0135 void LabeledCombo::addItem(const QString &text, const QVariant &userData)
0136 {
0137     m_combo->addItem(text, userData);
0138 }
0139 
0140 int LabeledCombo::count() const
0141 {
0142     return m_combo->count();
0143 }
0144 
0145 int LabeledCombo::currentIndex() const
0146 {
0147     return m_combo->currentIndex();
0148 }
0149 
0150 QString LabeledCombo::getStringWithUnitForInteger(int iValue) const
0151 {
0152     switch (m_option->valueUnit()) {
0153 
0154     case KSaneCore::Option::UnitPixel:
0155         return i18ncp("Parameter and Unit", "%1 Pixel", "%1 Pixels", iValue);
0156         break;
0157     case KSaneCore::Option::UnitBit:
0158         return i18ncp("Parameter and Unit", "%1 Bit", "%1 Bits", iValue);
0159         break;
0160     case KSaneCore::Option::UnitMilliMeter:
0161         return i18nc("Parameter and Unit (Millimeter)", "%1 mm", iValue);
0162         break;
0163     case KSaneCore::Option::UnitDPI:
0164         return i18nc("Parameter and Unit (Dots Per Inch)", "%1 DPI", iValue);
0165         break;
0166     case KSaneCore::Option::UnitPercent:
0167         return i18nc("Parameter and Unit (Percentage)", "%1 %", iValue);
0168         break;
0169     case KSaneCore::Option::UnitMicroSecond:
0170         return i18nc("Parameter and Unit (Microseconds)", "%1 µs", iValue);
0171         break;
0172     case KSaneCore::Option::UnitSecond:
0173         return i18nc("Parameter and Unit (seconds)", "%1 s", iValue);
0174         break;
0175     default:
0176         return i18n("%1", iValue);
0177         break;
0178     }
0179 }
0180 
0181 QString LabeledCombo::getStringWithUnitForFloat(float fValue) const
0182 {
0183     switch (m_option->valueUnit()) {
0184 
0185     case KSaneCore::Option::UnitPixel:
0186         return i18ncp("Parameter and Unit", "%1 Pixel", "%1 Pixels", static_cast<int>(fValue));
0187         break;
0188     case KSaneCore::Option::UnitBit:
0189         return i18ncp("Parameter and Unit", "%1 Bit", "%1 Bits", static_cast<int>(fValue));
0190         break;
0191     case KSaneCore::Option::UnitMilliMeter:
0192         return i18nc("Parameter and Unit (Millimeter)", "%1 mm", fValue);
0193         break;
0194     case KSaneCore::Option::UnitDPI:
0195         return i18nc("Parameter and Unit (Dots Per Inch)", "%1 DPI", fValue);
0196         break;
0197     case KSaneCore::Option::UnitPercent:
0198         return i18nc("Parameter and Unit (Percentage)", "%1 %", fValue);
0199         break;
0200     case KSaneCore::Option::UnitMicroSecond:
0201         return i18nc("Parameter and Unit (Microseconds)", "%1 µs", fValue);
0202         break;
0203     case KSaneCore::Option::UnitSecond:
0204         return i18nc("Parameter and Unit (seconds)", "%1 s", fValue);
0205         break;
0206     default:
0207         return i18n("%1", fValue);
0208         break;
0209     }
0210 }
0211 
0212 }  // NameSpace KSaneIface
0213 
0214 #include "moc_labeledcombo.cpp"