diff --git a/src/options/ksaneoptcombo.cpp b/src/options/ksaneoptcombo.cpp index 65539bf..c0eea73 100644 --- a/src/options/ksaneoptcombo.cpp +++ b/src/options/ksaneoptcombo.cpp @@ -1,393 +1,393 @@ /* ============================================================ * * This file is part of the KDE project * * Date : 2009-01-21 * Description : Sane interface for KDE * * Copyright (C) 2009 by Kare Sars * Copyright (C) 2014 by Gregor Mitsch: port to KDE5 frameworks * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see . * * ============================================================ */ #include "ksaneoptcombo.h" #include "labeledcombo.h" #include #include #include namespace KSaneIface { static const char tmp_binary[] = "Binary"; KSaneOptCombo::KSaneOptCombo(const SANE_Handle handle, const int index) : KSaneOption(handle, index), m_combo(nullptr) { } void KSaneOptCombo::createWidget(QWidget *parent) { if (m_widget) { return; } m_widget = m_combo = new LabeledCombo(parent, QStringLiteral(""), QStringList()); readOption(); m_widget->setToolTip(sane_i18n(m_optDesc->desc)); - connect(m_combo, static_cast(&LabeledCombo::activated), this, &KSaneOptCombo::comboboxChangedIndex); + connect(m_combo, QOverload::of(&LabeledCombo::activated), this, &KSaneOptCombo::comboboxChangedIndex); readValue(); } void KSaneOptCombo::readValue() { if (state() == STATE_HIDDEN) { return; } // read that current value QVarLengthArray data(m_optDesc->size); SANE_Status status; SANE_Int res; status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res); if (status != SANE_STATUS_GOOD) { return; } m_currentText = getSaneComboString(data.data()); if (m_combo != nullptr) { if (m_combo->currentText() != m_currentText) { m_combo->setCurrentText(m_currentText); emit valueChanged(); } } } void KSaneOptCombo::readOption() { KSaneOption::readOption(); if (!m_combo) { return; } QString saved = m_combo->currentText(); m_strList = genComboStringList(); m_combo->clear(); m_combo->setLabelText(sane_i18n(m_optDesc->title)); m_combo->addItems(m_strList); m_combo->setIcon(QIcon::fromTheme(QStringLiteral("color")), getSaneComboString((unsigned char *)SANE_VALUE_SCAN_MODE_COLOR)); m_combo->setIcon(QIcon::fromTheme(QStringLiteral("gray-scale")), getSaneComboString((unsigned char *)SANE_VALUE_SCAN_MODE_GRAY)); m_combo->setIcon(QIcon::fromTheme(QStringLiteral("black-white")), getSaneComboString((unsigned char *)SANE_VALUE_SCAN_MODE_LINEART)); // The epkowa/epson backend uses "Binary" which is the same as "Lineart" m_combo->setIcon(QIcon::fromTheme(QStringLiteral("black-white")), i18n(tmp_binary)); // set the previous value m_combo->setCurrentText(saved); } QStringList &KSaneOptCombo::genComboStringList() { int i; m_strList.clear(); switch (m_optDesc->type) { case SANE_TYPE_INT: for (i = 1; i <= m_optDesc->constraint.word_list[0]; ++i) { m_strList += getSaneComboString((int)m_optDesc->constraint.word_list[i]); } break; case SANE_TYPE_FIXED: for (i = 1; i <= m_optDesc->constraint.word_list[0]; ++i) { m_strList += getSaneComboString((float)SANE_UNFIX(m_optDesc->constraint.word_list[i])); } break; case SANE_TYPE_STRING: i = 0; while (m_optDesc->constraint.string_list[i] != nullptr) { m_strList += getSaneComboString((unsigned char *)m_optDesc->constraint.string_list[i]); i++; } break; default : m_strList += QStringLiteral("NOT HANDELED"); break; } return m_strList; } QString KSaneOptCombo::getSaneComboString(int ival) { switch (m_optDesc->unit) { case SANE_UNIT_NONE: break; case SANE_UNIT_PIXEL: return i18np("%1 Pixel", "%1 Pixels", ival); case SANE_UNIT_BIT: return i18np("%1 Bit", "%1 Bits", ival); case SANE_UNIT_MM: return i18np("%1 mm", "%1 mm", ival); case SANE_UNIT_DPI: return i18np("%1 DPI", "%1 DPI", ival); case SANE_UNIT_PERCENT: return i18np("%1 %", "%1 %", ival); case SANE_UNIT_MICROSECOND: return i18np("%1 µs", "%1 µs", ival); } return QString::number(ival); } QString KSaneOptCombo::getSaneComboString(float fval) { switch (m_optDesc->unit) { case SANE_UNIT_NONE: break; case SANE_UNIT_PIXEL: return i18ncp("Parameter and Unit", "%1 Pixel", "%1 Pixels", fval); case SANE_UNIT_BIT: return i18ncp("Parameter and Unit", "%1 Bit", "%1 Bits", fval); case SANE_UNIT_MM: return i18nc("Parameter and Unit (Millimeter)", "%1 mm", fval); case SANE_UNIT_DPI: return i18nc("Parameter and Unit (Dots Per Inch)", "%1 DPI", fval); case SANE_UNIT_PERCENT: return i18nc("Parameter and Unit (Percentage)", "%1 %", fval); case SANE_UNIT_MICROSECOND: return i18nc("Parameter and Unit (Microseconds)", "%1 µs", fval); } return QString::number(fval, 'F', 4); } QString KSaneOptCombo::getSaneComboString(unsigned char *data) { QString tmp; if (data == nullptr) { return QString(); } switch (m_optDesc->type) { case SANE_TYPE_INT: return getSaneComboString((int)toSANE_Word(data)); case SANE_TYPE_FIXED: return getSaneComboString((float)SANE_UNFIX(toSANE_Word(data))); case SANE_TYPE_STRING: tmp = i18n(reinterpret_cast(data)); tmp = tmp.simplified(); return tmp; default : break; } return QString(); } void KSaneOptCombo::comboboxChangedIndex(int i) { if (m_combo && (m_combo->currentText() == m_currentText)) { return; } unsigned char data[4]; void *dataPtr; switch (m_optDesc->type) { case SANE_TYPE_INT: case SANE_TYPE_FIXED: fromSANE_Word(data, m_optDesc->constraint.word_list[i + 1]); dataPtr = data; break; case SANE_TYPE_STRING: dataPtr = (void *)m_optDesc->constraint.string_list[i]; break; default: qDebug() << "can not handle type:" << m_optDesc->type; return; } writeData(dataPtr); readValue(); emit valueChanged(); } bool KSaneOptCombo::getMinValue(float &val) { if (state() == STATE_HIDDEN) { return false; } switch (m_optDesc->type) { case SANE_TYPE_INT: val = (float)m_optDesc->constraint.word_list[1]; for (int i = 2; i <= m_optDesc->constraint.word_list[0]; i++) { val = qMin((float)m_optDesc->constraint.word_list[i], val); } break; case SANE_TYPE_FIXED: val = (float)SANE_UNFIX(m_optDesc->constraint.word_list[1]); for (int i = 2; i <= m_optDesc->constraint.word_list[0]; i++) { val = qMin((float)SANE_UNFIX(m_optDesc->constraint.word_list[i]), val); } break; default: qDebug() << "can not handle type:" << m_optDesc->type; return false; } return true; } bool KSaneOptCombo::getValue(float &val) { if (state() == STATE_HIDDEN) { return false; } // read that current value QVarLengthArray data(m_optDesc->size); SANE_Status status; SANE_Int res; status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res); if (status != SANE_STATUS_GOOD) { qDebug() << m_optDesc->name << "sane_control_option returned" << status; return false; } switch (m_optDesc->type) { case SANE_TYPE_INT: val = (float)toSANE_Word(data.data()); return true; case SANE_TYPE_FIXED: val = SANE_UNFIX(toSANE_Word(data.data())); return true; default: qDebug() << "Type" << m_optDesc->type << "not supported!"; break; } return false; } bool KSaneOptCombo::setValue(float value) { unsigned char data[4]; float tmp; float minDiff; int i; int minIndex = 1; switch (m_optDesc->type) { case SANE_TYPE_INT: tmp = (float)m_optDesc->constraint.word_list[minIndex]; minDiff = qAbs(value - tmp); for (i = 2; i <= m_optDesc->constraint.word_list[0]; ++i) { tmp = (float)m_optDesc->constraint.word_list[i]; if (qAbs(value - tmp) < minDiff) { minDiff = qAbs(value - tmp); minIndex = i; } } fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]); writeData(data); readValue(); return (minDiff < 1.0); case SANE_TYPE_FIXED: tmp = (float)SANE_UNFIX(m_optDesc->constraint.word_list[minIndex]); minDiff = qAbs(value - tmp); for (i = 2; i <= m_optDesc->constraint.word_list[0]; ++i) { tmp = (float)SANE_UNFIX(m_optDesc->constraint.word_list[i]); if (qAbs(value - tmp) < minDiff) { minDiff = qAbs(value - tmp); minIndex = i; } } fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]); writeData(data); readValue(); return (minDiff < 1.0); default: qDebug() << "can not handle type:" << m_optDesc->type; break; } return false; } bool KSaneOptCombo::getValue(QString &val) { if (state() == STATE_HIDDEN) { return false; } val = m_currentText; return true; } bool KSaneOptCombo::setValue(const QString &val) { if (state() == STATE_HIDDEN) { return false; } if (val == m_currentText) { return true; } unsigned char data[4]; void* data_ptr = nullptr; SANE_Word fixed; int i; float f; bool ok; QString tmp; switch (m_optDesc->type) { case SANE_TYPE_INT: tmp = val.left(val.indexOf(QLatin1Char(' '))); // strip the unit // accept float formating of the string i = (int)(tmp.toFloat(&ok)); if (ok == false) { return false; } fromSANE_Word(data, i); data_ptr = data; break; case SANE_TYPE_FIXED: tmp = val.left(val.indexOf(QLatin1Char(' '))); // strip the unit f = tmp.toFloat(&ok); if (ok == false) { return false; } fixed = SANE_FIX(f); fromSANE_Word(data, fixed); data_ptr = data; break; case SANE_TYPE_STRING: i = 0; while (m_optDesc->constraint.string_list[i] != nullptr) { tmp = getSaneComboString((unsigned char *)m_optDesc->constraint.string_list[i]); if (val == tmp) { data_ptr = (void *)m_optDesc->constraint.string_list[i]; break; } i++; } if (m_optDesc->constraint.string_list[i] == nullptr) { return false; } break; default: qDebug() << "can only handle SANE_TYPE: INT, FIXED and STRING"; return false; } writeData(data_ptr); readValue(); return true; } bool KSaneOptCombo::hasGui() { return true; } } // NameSpace KSaneIface diff --git a/src/widgets/labeledcombo.cpp b/src/widgets/labeledcombo.cpp index 05665c6..30e24a8 100644 --- a/src/widgets/labeledcombo.cpp +++ b/src/widgets/labeledcombo.cpp @@ -1,98 +1,98 @@ /* ============================================================ * * This file is part of the KDE project * * Date : 2007-09-13 * Description : Sane interface for KDE * * Copyright (C) 2007-2011 by Kare Sars * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see . * * ============================================================ */ #include "labeledcombo.h" #include #include namespace KSaneIface { LabeledCombo::LabeledCombo(QWidget *parent, const QString <ext, const QStringList &list) : KSaneOptionWidget(parent, ltext) { m_combo = new QComboBox(this); m_combo->addItems(list); m_label->setBuddy(m_combo); - connect(m_combo, static_cast(&QComboBox::activated), this, &LabeledCombo::activated); + connect(m_combo, QOverload::of(&QComboBox::activated), this, &LabeledCombo::activated); m_layout->addWidget(m_combo, 0, 1); m_layout->addWidget(new QWidget(this), 0, 2); m_layout->setColumnStretch(1, 0); m_layout->setColumnStretch(2, 50); setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed); } void LabeledCombo::addItems(const QStringList &list) { m_combo->addItems(list); QString tmp; for (int i = 0; i < m_combo->count(); ++i) { tmp = m_combo->itemText(i); m_combo->setItemData(i, tmp, Qt::ToolTipRole); } } void LabeledCombo::setCurrentText(const QString &t) { for (int i = 0; i < m_combo->count(); ++i) { if (m_combo->itemText(i) == t) { m_combo->setCurrentIndex(i); } } } 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); } void LabeledCombo::clear() { m_combo->clear(); } } // NameSpace KSaneIface diff --git a/src/widgets/labeledslider.cpp b/src/widgets/labeledslider.cpp index 8ce9ddc..78abcbf 100644 --- a/src/widgets/labeledslider.cpp +++ b/src/widgets/labeledslider.cpp @@ -1,156 +1,156 @@ /* ============================================================ * * This file is part of the KDE project * * Date : 2007-09-13 * Description : Sane interface for KDE * * Copyright (C) 2007-2011 by Kare Sars * Copyright (C) 2014 by Gregor Mitsch: port to KDE5 frameworks * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program. If not, see . * * ============================================================ */ #include "labeledslider.h" // Qt includes #include #include // KDE includes #include namespace KSaneIface { LabeledSlider::LabeledSlider(QWidget *parent, const QString <ext, int min, int max, int ste) : KSaneOptionWidget(parent, ltext) { m_step = ste; if (m_step == 0) { m_step = 1; } m_slider = new QSlider(this); m_slider->setOrientation(Qt::Horizontal); m_slider->setMinimum(min); m_slider->setMaximum(max); m_slider->setSingleStep(m_step); m_spinb = new KPluralHandlingSpinBox(this); m_spinb->setMinimum(min); m_spinb->setMaximum(max); m_slider->setSingleStep(m_step); m_spinb->setValue(max); //m_spinb->setMinimumWidth(m_spinb->sizeHint().width()+35); m_spinb->setAlignment(Qt::AlignRight); m_spinb->setValue(min); m_spinb->setValue(min); m_label->setBuddy(m_spinb); - connect(m_spinb, static_cast(&QSpinBox::valueChanged), this, &LabeledSlider::syncValues); + connect(m_spinb, QOverload::of(&QSpinBox::valueChanged), this, &LabeledSlider::syncValues); connect(m_slider, &QSlider::valueChanged, this, &LabeledSlider::syncValues); connect(m_slider, &QSlider::sliderReleased, this, &LabeledSlider::fixValue); m_layout->addWidget(m_slider, 0, 2); m_layout->addWidget(m_spinb, 0, 1); m_layout->setColumnStretch(1, 0); m_layout->setColumnStretch(2, 50); } LabeledSlider::~LabeledSlider() { } void LabeledSlider::setSuffix(const KLocalizedString &text) { m_spinb->setSuffix(text); } void LabeledSlider::setValue(int value) { if (value != m_slider->value()) { m_slider->setValue(value); } else if (value != m_spinb->value()) { m_spinb->setValue(value); } } void LabeledSlider::setRange(int min, int max) { //std::cout << "min=" << min << ", max=" << max << std::endl; m_slider->setRange(min, max); m_spinb->setRange(min, max); } void LabeledSlider::setStep(int st) { m_step = st; if (m_step == 0) { m_step = 1; } m_slider->setSingleStep(m_step); m_spinb->setSingleStep(m_step); } void LabeledSlider::syncValues(int value) { if (value != m_spinb->value()) { m_spinb->setValue(value); } else if (value != m_slider->value()) { //ensure that the value m_step is followed also for the m_slider if ((value - m_slider->minimum()) % m_step != 0) { if (value > m_spinb->value()) { m_slider->setValue(m_slider->value() + (m_step - (value - m_spinb->value()))); } else { m_slider->setValue(m_slider->value() - (m_step - (m_spinb->value() - value))); } // this function will be reentered with the signal fom the m_slider } else { m_slider->setValue(value); } } else { emit valueChanged(value); } } void LabeledSlider::fixValue() { //ensure that the value m_step is followed also for the m_slider int rest = (m_slider->value() - m_slider->minimum()) % m_step; if (rest != 0) { if (rest > (m_step / 2)) { m_slider->setValue(m_slider->value() + (m_step - rest)); } else { m_slider->setValue(m_slider->value() - rest); } m_spinb->setValue(m_slider->value()); } } int LabeledSlider::value() const { return (m_slider->value()); } } // NameSpace KSaneIface