KSaneCore

stringoption.cpp
1/*
2 * SPDX-FileCopyrightText: 2009 Kare Sars <kare dot sars at iki dot fi>
3 * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#include "stringoption.h"
9
10#include <QVarLengthArray>
11
12#include <ksanecore_debug.h>
13
14namespace KSaneCore
15{
16
17StringOption::StringOption(const SANE_Handle handle, const int index)
18 : BaseOption(handle, index)
19{
20 m_optionType = Option::TypeString;
21}
22
23bool StringOption::setValue(const QVariant &val)
24{
25 if (state() == Option::StateHidden) {
26 return false;
27 }
28 QString text = val.toString();
29 QString tmp;
30#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
31 tmp += QStringView(text).left(m_optDesc->size);
32#else
33 tmp += text.leftRef(m_optDesc->size);
34#endif
35 if (tmp != text) {
36 writeData(tmp.toLatin1().data());
37 Q_EMIT valueChanged(tmp);
38 }
39 return true;
40}
41
42void StringOption::readValue()
43{
44 if (state() == Option::StateHidden) {
45 return;
46 }
47
48 // read that current value
49 QVarLengthArray<unsigned char> data(m_optDesc->size);
50 SANE_Status status;
51 SANE_Int res;
52 status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
53 if (status != SANE_STATUS_GOOD) {
54 return;
55 }
56
57 m_string = QString::fromUtf8(reinterpret_cast<char *>(data.data()));
58
59 Q_EMIT valueChanged(m_string);
60}
61
62QVariant StringOption::value() const
63{
64 return QVariant(m_string);
65}
66
67int StringOption::valueSize() const
68{
69 return static_cast<int>(m_optDesc->size);
70}
71
72QString StringOption::valueAsString() const
73{
74 if (state() == Option::StateHidden) {
75 return QString();
76 }
77 return m_string;
78}
79
80} // namespace KSaneCore
81
82#include "moc_stringoption.cpp"
Q_SCRIPTABLE CaptureState status()
char * data()
QString fromUtf8(QByteArrayView str)
QByteArray toLatin1() const const
QStringView left(qsizetype length) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.