Commit cef1e7cd authored by Alexander Stippich's avatar Alexander Stippich
Browse files

add internalValue member to KSaneOption

that gives the untranslated string of a list option. Use it in LabeledCombo to set the icons
parent 0316b3a5
......@@ -103,6 +103,15 @@ QVariantList KSaneOption::valueList() const
}
}
QVariantList KSaneOption::internalValueList() const
{
if (d->option != nullptr) {
return d->option->internalValueList();
} else {
return QVariantList();
}
}
QVariant KSaneOption::value() const
{
if (d->option != nullptr) {
......
......@@ -121,10 +121,18 @@ public:
QVariant stepValue() const;
/** This function returns the list of possible values if the option
* is of type KSaneOptionType::values.
* is of type KSaneOptionType::TypeValueList. The list may contain
* formatted or translated values.
* @return a list with all possible values */
QVariantList valueList() const;
/** This function returns the list of possible values if the option
* is of type KSaneOptionType::TypeValueList. The list contains the raw
* internal values without any formatting or translation.
* @return a list with all possible internal values
* @since 21.12 */
QVariantList internalValueList() const;
/** This function returns an enum specifying whether the values
* of the option have a unit, e.g. mm, px, etc.
* @return unit of value KSaneOptionUnit */
......
......@@ -192,6 +192,11 @@ QVariantList KSaneBaseOption::valueList() const
return QVariantList();
}
QVariantList KSaneBaseOption::internalValueList() const
{
return QVariantList();
}
KSaneOption::KSaneOptionUnit KSaneBaseOption::valueUnit() const
{
if (m_optDesc != nullptr) {
......
......@@ -62,6 +62,7 @@ public:
virtual QVariant stepValue() const;
virtual QVariant value() const;
virtual QVariantList valueList() const;
virtual QVariantList internalValueList() const;
virtual KSaneOption::KSaneOptionUnit valueUnit() const;
virtual int valueSize() const;
virtual QString valueAsString() const;
......
......@@ -84,7 +84,37 @@ QVariantList KSaneListOption::valueList() const
}
break;
default :
list << QStringLiteral("NOT HANDELED");
qCDebug(KSANE_LOG) << "can not handle type:" << m_optDesc->type;
break;
}
return list;
}
QVariantList KSaneListOption::internalValueList() const
{
int i;
QVariantList list;
switch (m_optDesc->type) {
case SANE_TYPE_INT:
for (i = 1; i <= m_optDesc->constraint.word_list[0]; ++i) {
list << static_cast<int>(m_optDesc->constraint.word_list[i]);;
}
break;
case SANE_TYPE_FIXED:
for (i = 1; i <= m_optDesc->constraint.word_list[0]; ++i) {
list << SANE_UNFIX(m_optDesc->constraint.word_list[i]);
}
break;
case SANE_TYPE_STRING:
i = 0;
while (m_optDesc->constraint.string_list[i] != nullptr) {
list << QString::fromLatin1(m_optDesc->constraint.string_list[i]);
i++;
}
break;
default :
qCDebug(KSANE_LOG) << "can not handle type:" << m_optDesc->type;
break;
}
return list;
......
......@@ -27,6 +27,7 @@ public:
QVariant value() const override;
QString valueAsString() const override;
QVariantList valueList() const override;
QVariantList internalValueList() const override;
public Q_SLOTS:
bool setValue(const QVariant &value) override;
......
......@@ -14,18 +14,9 @@
#include <KLocalizedString>
// Sane includes
extern "C"
{
#include <sane/sane.h>
#include <sane/saneopts.h>
}
namespace KSaneIface
{
static const char tmp_binary[] = "Binary";
LabeledCombo::LabeledCombo(QWidget *parent, const QString &ltext, const QStringList &list)
: KSaneOptionWidget(parent, ltext)
{
......@@ -42,22 +33,29 @@ LabeledCombo::LabeledCombo(QWidget *parent, KSaneOption *option)
connect(option, &KSaneOption::valueChanged, this, &LabeledCombo::setValue);
clear();
const QVariantList list = option->valueList();
for (const auto &value : list) {
const QVariantList values = option->valueList();
const QVariantList internalValues = option->internalValueList();
for (int i = 0; i < values.count(); i++) {
const auto &value = values.at(i);
const auto &internalValue = internalValues.at(i);
if (value.type() == static_cast<QVariant::Type>(QMetaType::Int)) {
addItem(getStringWithUnitForInteger(value.toInt()), value);
addItem(getStringWithUnitForInteger(value.toInt()), internalValue);
} else if (value.type() == static_cast<QVariant::Type>(QMetaType::Float)) {
addItem(getStringWithUnitForFloat(value.toFloat()), value);
addItem(getStringWithUnitForFloat(value.toFloat()), internalValue);
} else {
addItem(value.toString(), value);
addItem(value.toString(), internalValue);
if (internalValue == QStringLiteral("Color")) {
m_combo->setItemIcon(i, QIcon::fromTheme(QStringLiteral("color")));
}
if (internalValue == QStringLiteral("Gray")) {
m_combo->setItemIcon(i, QIcon::fromTheme(QStringLiteral("gray-scale")));
}
if (internalValue == QStringLiteral("Lineart") || internalValue == QStringLiteral("Binary")) {
m_combo->setItemIcon(i, QIcon::fromTheme(QStringLiteral("black-white")));
}
}
}
setIcon(QIcon::fromTheme(QStringLiteral("color")), QString::fromUtf8(SANE_VALUE_SCAN_MODE_COLOR));
setIcon(QIcon::fromTheme(QStringLiteral("gray-scale")), QString::fromUtf8(SANE_VALUE_SCAN_MODE_GRAY));
setIcon(QIcon::fromTheme(QStringLiteral("black-white")), QString::fromUtf8(SANE_VALUE_SCAN_MODE_LINEART));
// The epkowa/epson backend uses "Binary" which is the same as "Lineart"
setIcon(QIcon::fromTheme(QStringLiteral("black-white")), i18n(tmp_binary));
QString currentText = option->value().toString();
setCurrentText(currentText);
......@@ -105,17 +103,6 @@ QString LabeledCombo::currentText() const
return m_combo->currentText();
}
bool LabeledCombo::setIcon(const QIcon &icon, const QString &str)
{
for (int i = 0; i < m_combo->count(); ++i) {
if (m_combo->itemText(i) == str) {
m_combo->setItemIcon(i, icon);
return true;
}
}
return false;
}
void LabeledCombo::setCurrentIndex(int i)
{
m_combo->setCurrentIndex(i);
......
......@@ -76,13 +76,6 @@ public Q_SLOTS:
*/
void setCurrentIndex(int);
/**
* add an icon for a string in the combobox
* @param icon the icon to set.
* @param str the string for which the pixmap should be set.
*/
bool setIcon(const QIcon &icon, const QString &str);
void setValue(const QVariant &val);
private Q_SLOTS:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment