KSane

ksanedevicedialog.cpp
1/*
2 * SPDX-FileCopyrightText: 2007-2008 Kare Sars <kare dot sars at iki dot fi>
3 * SPDX-FileCopyrightText: 2009 Grzegorz Kurtyka <grzegorz dot kurtyka at gmail dot com>
4 * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "ksanedevicedialog.h"
10
11#include <QScrollArea>
12#include <QLabel>
13#include <QDialogButtonBox>
14
15#include <KLocalizedString>
16
17namespace KSaneIface
18{
19
20KSaneDeviceDialog::KSaneDeviceDialog(QWidget *parent)
21 : QDialog(parent)
22{
23 QVBoxLayout *topLayout = new QVBoxLayout(this);
24
25 m_btnGroupDevices = new QButtonGroup(this);
26
27 m_gbDevices = new QGroupBox;
28 QVBoxLayout *layout = new QVBoxLayout;
29 m_btnContainer = new QWidget;
30 m_btnLayout = new QVBoxLayout(m_btnContainer);
31 QScrollArea *area = new QScrollArea;
32
33 m_gbDevices->setLayout(layout);
34
35 QLabel *explanation =
36 new QLabel(i18n("<html>The SANE (Scanner Access Now Easy) system could not find any device.<br>"
37 "Check that the scanner is plugged in and turned on<br>"
38 "or check your systems scanner setup.<br>"
39 "For details about SANE see the "
40 "<a href='http://www.sane-project.org/'>SANE homepage</a>.</html>"));
41 explanation->setOpenExternalLinks(true);
42 int l, t, r, b;
43 layout->getContentsMargins(&l, &t, &r, &b);
44 explanation->setContentsMargins(l, t, r, b);
45
46 layout->addWidget(explanation);
47 m_gbDevices->adjustSize(); // make sure to see the complete explanation text
48 layout->addWidget(area);
49 layout->setContentsMargins(0, 0, 0, 0);
50
51 area->setWidgetResizable(true);
53 area->setWidget(m_btnContainer);
54
55 QDialogButtonBox *bottomButtonBox = new QDialogButtonBox(this);
57 m_btnOk = bottomButtonBox->button(QDialogButtonBox::Ok);
58 m_btnReloadDevices = bottomButtonBox->addButton(i18n("Reload devices list"), QDialogButtonBox::ButtonRole::ActionRole);
59 layout->addWidget(bottomButtonBox);
60
61 connect(bottomButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
62 connect(bottomButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
63 connect(m_btnReloadDevices, &QPushButton::clicked, this, &KSaneDeviceDialog::reloadDevicesList);
64
65 topLayout->addWidget(m_gbDevices);
66 topLayout->addWidget(bottomButtonBox);
67
68 setMinimumHeight(200);
69
70 reloadDevicesList();
71}
72
73KSaneDeviceDialog::~KSaneDeviceDialog()
74{
75 ///@todo wait for thread to finish if its running
76}
77
78void KSaneDeviceDialog::reloadDevicesList()
79{
80 setAvailable(false);
81 qDeleteAll(m_btnGroupDevices->buttons());
82 m_gbDevices->setTitle(i18n("Looking for devices. Please wait."));
83 m_gbDevices->layout()->itemAt(0)->widget()->hide(); // explanation
84 m_btnReloadDevices->setEnabled(false);
85
86 Q_EMIT requestReloadList(KSaneCore::Interface::AllDevices);
87}
88
89void KSaneDeviceDialog::setAvailable(bool isAvailable)
90{
91 m_btnOk->setEnabled(isAvailable);
92 if (isAvailable) {
93 m_selectedDevice = getSelectedName();
94 m_btnOk->setFocus();
95 }
96}
97
98void KSaneDeviceDialog::setDefault(const QString &defaultBackend)
99{
100 m_selectedDevice = defaultBackend;
101}
102
103QString KSaneDeviceDialog::getSelectedName() const
104{
105 QAbstractButton *selectedButton = m_btnGroupDevices->checkedButton();
106 if (selectedButton) {
107 return selectedButton->objectName();
108 }
109 return QString();
110}
111
112void KSaneDeviceDialog::updateDevicesList(const QList<KSaneCore::DeviceInformation*> &list)
113{
114 qDeleteAll(m_btnGroupDevices->buttons());
115
116 if (list.isEmpty()) {
117 m_gbDevices->setTitle(i18n("Sorry. No devices found."));
118 m_gbDevices->layout()->itemAt(0)->widget()->show(); // explanation
119 m_gbDevices->layout()->itemAt(1)->widget()->hide(); // scroll area
120 m_btnReloadDevices->setEnabled(true);
121 return;
122 }
123
124 delete m_btnLayout;
125 m_btnLayout = new QVBoxLayout;
126 m_btnContainer->setLayout(m_btnLayout);
127 m_gbDevices->setTitle(i18n("Found devices:"));
128 m_gbDevices->layout()->itemAt(0)->widget()->hide(); // explanation
129 m_gbDevices->layout()->itemAt(1)->widget()->show(); // scroll area
130
131 for (int i = 0; i < list.size(); ++i) {
132 QRadioButton *b = new QRadioButton(this);
133 b->setObjectName(list.at(i)->name());
134 b->setToolTip(list.at(i)->name());
135 b->setText(QStringLiteral("%1 : %2\n%3")
136 .arg(list.at(i)->vendor(), list.at(i)->model(), list.at(i)->name()));
137
138 m_btnLayout->addWidget(b);
139 m_btnGroupDevices->addButton(b);
140 connect(b, &QRadioButton::clicked, this, &KSaneDeviceDialog::setAvailable);
141 if ((i == 0) || (list.at(i)->name() == m_selectedDevice)) {
142 b->setChecked(true);
143 setAvailable(true);
144 }
145 }
146
147 m_btnLayout->addStretch();
148
149 if (list.size() == 1) {
150 m_btnOk->animateClick(); // 2014-01-21: why animated?
151 }
152
153 m_btnReloadDevices->setEnabled(true);
154}
155
156}
157
158#include "moc_ksanedevicedialog.cpp"
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void setChecked(bool)
void clicked(bool checked)
void setText(const QString &text)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
virtual void accept()
virtual void reject()
QPushButton * addButton(StandardButton button)
QPushButton * button(StandardButton which) const const
void setStandardButtons(StandardButtons buttons)
void setFrameShape(Shape)
void getContentsMargins(int *left, int *top, int *right, int *bottom) const const
virtual QLayoutItem * itemAt(int index) const const=0
virtual QLayout * layout() override
void setContentsMargins(const QMargins &margins)
virtual QWidget * widget() const const
const_reference at(qsizetype i) const const
bool isEmpty() const const
qsizetype size() const const
void setObjectName(QAnyStringView name)
void setWidget(QWidget *widget)
void setWidgetResizable(bool resizable)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void hide()
void setContentsMargins(const QMargins &margins)
void setLayout(QLayout *layout)
void setToolTip(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.