File indexing completed on 2024-05-12 08:32:29

0001 /*
0002  * SPDX-FileCopyrightText: 2007-2008 Kare Sars <kare dot sars at iki dot fi>
0003  * SPDX-FileCopyrightText: 2009 Grzegorz Kurtyka <grzegorz dot kurtyka at gmail dot com>
0004  * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "ksanedevicedialog.h"
0010 
0011 #include <QScrollArea>
0012 #include <QLabel>
0013 #include <QDialogButtonBox>
0014 
0015 #include <KLocalizedString>
0016 
0017 namespace KSaneIface
0018 {
0019 
0020 KSaneDeviceDialog::KSaneDeviceDialog(QWidget *parent)
0021     : QDialog(parent)
0022 {
0023     QVBoxLayout *topLayout = new QVBoxLayout(this);
0024 
0025     m_btnGroupDevices = new QButtonGroup(this);
0026 
0027     m_gbDevices = new QGroupBox;
0028     QVBoxLayout *layout = new QVBoxLayout;
0029     m_btnContainer = new QWidget;
0030     m_btnLayout = new QVBoxLayout(m_btnContainer);
0031     QScrollArea *area = new QScrollArea;
0032 
0033     m_gbDevices->setLayout(layout);
0034 
0035     QLabel *explanation =
0036         new QLabel(i18n("<html>The SANE (Scanner Access Now Easy) system could not find any device.<br>"
0037                         "Check that the scanner is plugged in and turned on<br>"
0038                         "or check your systems scanner setup.<br>"
0039                         "For details about SANE see the "
0040                         "<a href='http://www.sane-project.org/'>SANE homepage</a>.</html>"));
0041     explanation->setOpenExternalLinks(true);
0042     int l, t, r, b;
0043     layout->getContentsMargins(&l, &t, &r, &b);
0044     explanation->setContentsMargins(l, t, r, b);
0045 
0046     layout->addWidget(explanation);
0047     m_gbDevices->adjustSize();  // make sure to see the complete explanation text
0048     layout->addWidget(area);
0049     layout->setContentsMargins(0, 0, 0, 0);
0050 
0051     area->setWidgetResizable(true);
0052     area->setFrameShape(QFrame::NoFrame);
0053     area->setWidget(m_btnContainer);
0054 
0055     QDialogButtonBox *bottomButtonBox = new QDialogButtonBox(this);
0056     bottomButtonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0057     m_btnOk = bottomButtonBox->button(QDialogButtonBox::Ok);
0058     m_btnReloadDevices = bottomButtonBox->addButton(i18n("Reload devices list"), QDialogButtonBox::ButtonRole::ActionRole);
0059     layout->addWidget(bottomButtonBox);
0060 
0061     connect(bottomButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0062     connect(bottomButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0063     connect(m_btnReloadDevices, &QPushButton::clicked, this, &KSaneDeviceDialog::reloadDevicesList);
0064 
0065     topLayout->addWidget(m_gbDevices);
0066     topLayout->addWidget(bottomButtonBox);
0067 
0068     setMinimumHeight(200);
0069 
0070     reloadDevicesList();
0071 }
0072 
0073 KSaneDeviceDialog::~KSaneDeviceDialog()
0074 {
0075     ///@todo wait for thread to finish if its running
0076 }
0077 
0078 void KSaneDeviceDialog::reloadDevicesList()
0079 {
0080     setAvailable(false);
0081     qDeleteAll(m_btnGroupDevices->buttons());
0082     m_gbDevices->setTitle(i18n("Looking for devices. Please wait."));
0083     m_gbDevices->layout()->itemAt(0)->widget()->hide();  // explanation
0084     m_btnReloadDevices->setEnabled(false);
0085 
0086     Q_EMIT requestReloadList(KSaneCore::Interface::AllDevices);
0087 }
0088 
0089 void KSaneDeviceDialog::setAvailable(bool isAvailable)
0090 {
0091     m_btnOk->setEnabled(isAvailable);
0092     if (isAvailable) {
0093         m_selectedDevice = getSelectedName();
0094         m_btnOk->setFocus();
0095     }
0096 }
0097 
0098 void KSaneDeviceDialog::setDefault(const QString &defaultBackend)
0099 {
0100     m_selectedDevice = defaultBackend;
0101 }
0102 
0103 QString KSaneDeviceDialog::getSelectedName() const
0104 {
0105     QAbstractButton *selectedButton = m_btnGroupDevices->checkedButton();
0106     if (selectedButton) {
0107         return selectedButton->objectName();
0108     }
0109     return QString();
0110 }
0111 
0112 void KSaneDeviceDialog::updateDevicesList(const QList<KSaneCore::DeviceInformation*> &list)
0113 {
0114     qDeleteAll(m_btnGroupDevices->buttons());
0115 
0116     if (list.isEmpty()) {
0117         m_gbDevices->setTitle(i18n("Sorry. No devices found."));
0118         m_gbDevices->layout()->itemAt(0)->widget()->show();  // explanation
0119         m_gbDevices->layout()->itemAt(1)->widget()->hide();  // scroll area
0120         m_btnReloadDevices->setEnabled(true);
0121         return;
0122     }
0123 
0124     delete m_btnLayout;
0125     m_btnLayout = new QVBoxLayout;
0126     m_btnContainer->setLayout(m_btnLayout);
0127     m_gbDevices->setTitle(i18n("Found devices:"));
0128     m_gbDevices->layout()->itemAt(0)->widget()->hide();  // explanation
0129     m_gbDevices->layout()->itemAt(1)->widget()->show();  // scroll area
0130 
0131     for (int i = 0; i < list.size(); ++i) {
0132         QRadioButton *b = new QRadioButton(this);
0133         b->setObjectName(list.at(i)->name());
0134         b->setToolTip(list.at(i)->name());
0135         b->setText(QStringLiteral("%1 : %2\n%3")
0136                    .arg(list.at(i)->vendor(), list.at(i)->model(), list.at(i)->name()));
0137 
0138         m_btnLayout->addWidget(b);
0139         m_btnGroupDevices->addButton(b);
0140         connect(b, &QRadioButton::clicked, this, &KSaneDeviceDialog::setAvailable);
0141         if ((i == 0) || (list.at(i)->name() == m_selectedDevice)) {
0142             b->setChecked(true);
0143             setAvailable(true);
0144         }
0145     }
0146 
0147     m_btnLayout->addStretch();
0148 
0149     if (list.size() == 1) {
0150         m_btnOk->animateClick(); // 2014-01-21: why animated?
0151     }
0152 
0153     m_btnReloadDevices->setEnabled(true);
0154 }
0155 
0156 }
0157 
0158 #include "moc_ksanedevicedialog.cpp"