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 tmp += QStringView(text).left(m_optDesc->size);
31 if (tmp != text) {
32 writeData(tmp.toLatin1().data());
33 Q_EMIT valueChanged(tmp);
34 }
35 return true;
36}
37
38void StringOption::readValue()
39{
40 if (state() == Option::StateHidden) {
41 return;
42 }
43
44 // read that current value
45 QVarLengthArray<unsigned char> data(m_optDesc->size);
46 SANE_Status status;
47 SANE_Int res;
48 status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
49 if (status != SANE_STATUS_GOOD) {
50 return;
51 }
52
53 m_string = QString::fromUtf8(reinterpret_cast<char *>(data.data()));
54
55 Q_EMIT valueChanged(m_string);
56}
57
58QVariant StringOption::value() const
59{
60 return QVariant(m_string);
61}
62
63int StringOption::valueSize() const
64{
65 return static_cast<int>(m_optDesc->size);
66}
67
68QString StringOption::valueAsString() const
69{
70 if (state() == Option::StateHidden) {
71 return QString();
72 }
73 return m_string;
74}
75
76} // namespace KSaneCore
77
78#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 Sat Dec 21 2024 16:56:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.