File indexing completed on 2024-10-13 04:15:34

0001 /*
0002  * SPDX-FileCopyrightText: 2009 Kare Sars <kare dot sars at iki dot fi>
0003  * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 #include "stringoption.h"
0009 
0010 #include <QVarLengthArray>
0011 
0012 #include <ksanecore_debug.h>
0013 
0014 namespace KSaneCore
0015 {
0016 
0017 StringOption::StringOption(const SANE_Handle handle, const int index)
0018     : BaseOption(handle, index)
0019 {
0020     m_optionType = Option::TypeString;
0021 }
0022 
0023 bool StringOption::setValue(const QVariant &val)
0024 {
0025     if (state() == Option::StateHidden) {
0026         return false;
0027     }
0028     QString text = val.toString();
0029     QString tmp;
0030 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0031     tmp += QStringView(text).left(m_optDesc->size);
0032 #else
0033     tmp += text.leftRef(m_optDesc->size);
0034 #endif
0035     if (tmp != text) {
0036         writeData(tmp.toLatin1().data());
0037         Q_EMIT valueChanged(tmp);
0038     }
0039     return true;
0040 }
0041 
0042 void StringOption::readValue()
0043 {
0044     if (state() == Option::StateHidden) {
0045         return;
0046     }
0047 
0048     // read that current value
0049     QVarLengthArray<unsigned char> data(m_optDesc->size);
0050     SANE_Status status;
0051     SANE_Int res;
0052     status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
0053     if (status != SANE_STATUS_GOOD) {
0054         return;
0055     }
0056 
0057     m_string = QString::fromUtf8(reinterpret_cast<char *>(data.data()));
0058 
0059     Q_EMIT valueChanged(m_string);
0060 }
0061 
0062 QVariant StringOption::value() const
0063 {
0064     return QVariant(m_string);
0065 }
0066 
0067 int StringOption::valueSize() const
0068 {
0069     return static_cast<int>(m_optDesc->size);
0070 }
0071 
0072 QString StringOption::valueAsString() const
0073 {
0074     if (state() == Option::StateHidden) {
0075         return QString();
0076     }
0077     return m_string;
0078 }
0079 
0080 } // namespace KSaneCore
0081 
0082 #include "moc_stringoption.cpp"