File indexing completed on 2024-05-05 04:21:14

0001 /* ============================================================
0002  *
0003  * Date        : 2008-04-17
0004  * Description : Sane plugin interface for KDE
0005  *
0006  * Copyright (C) 2008 by Kare Sars <kare dot sars at iki dot fi>
0007  *
0008  * This library is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU Lesser General Public
0010  * License as published by the Free Software Foundation; either
0011  * version 2.1 of the License, or (at your option) version 3, or any
0012  * later version accepted by the membership of KDE e.V. (or its
0013  * successor approved by the membership of KDE e.V.), which shall
0014  * act as a proxy defined in Section 6 of version 3 of the license.
0015  *
0016  * This library is distributed in the hope that it will be useful,
0017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0019  * Lesser General Public License for more details.
0020  *
0021  * You should have received a copy of the GNU Lesser General Public
0022  * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
0023  *
0024  * ============================================================ */
0025 
0026 #include "sanedialog.h"
0027 
0028 #include <KLocalizedString>
0029 #include "kpLogCategories.h"
0030 #include <KMessageBox>
0031 #include <KSharedConfig>
0032 #include <KWindowConfig>
0033 #include <KConfigGroup>
0034 
0035 #include <QPushButton>
0036 
0037 SaneDialog::SaneDialog(QWidget *parent)
0038     : KPageDialog(parent)
0039 {
0040     setFaceType(static_cast<KPageDialog::FaceType> (Plain));
0041     setWindowTitle(i18nc("@title:window", "Acquire Image"));
0042 
0043     buttonBox()->setStandardButtons(QDialogButtonBox::Close);
0044     buttonBox()->button(QDialogButtonBox::Close)->setDefault(true);
0045 
0046 
0047     m_ksanew = new KSaneIface::KSaneWidget(this);
0048     addPage(m_ksanew, QString());
0049 
0050     connect (m_ksanew, &KSaneIface::KSaneWidget::scannedImageReady,
0051              this, &SaneDialog::imageReady);
0052 
0053     m_openDev = QString();
0054 }
0055 
0056 bool SaneDialog::setup()
0057 {
0058     if(!m_ksanew) {
0059         // new failed
0060         return false;
0061     }
0062     if (!m_openDev.isEmpty()) {
0063         return true;
0064     }
0065     // need to select a scanner
0066     m_openDev = m_ksanew->selectDevice(parentWidget());
0067     if (m_openDev.isEmpty()) {
0068        // either no scanner was found or then cancel was pressed.
0069         return false;
0070     }
0071     if (!m_ksanew->openDevice(m_openDev)) {
0072         // could not open the scanner
0073         KMessageBox::error(parentWidget(), i18n("Opening the selected scanner failed."));
0074         m_openDev = QString();
0075         return false;
0076     }
0077 
0078     // restore scan dialog size and all options for the selected device if available
0079     KSharedConfigPtr configPtr = KSharedConfig::openConfig(QStringLiteral("scannersettings"));
0080     KWindowConfig::restoreWindowSize(windowHandle(), KConfigGroup(configPtr, QStringLiteral("ScanDialog")));
0081     QString groupName = m_openDev;
0082     if (configPtr->hasGroup(groupName)) {
0083         KConfigGroup group(configPtr, groupName);
0084         QStringList keys = group.keyList();
0085         for (int i = 0; i < keys.count(); i++) {
0086             m_ksanew->setOptionValue(keys[i], group.readEntry(keys[i]));
0087         }
0088     }
0089 
0090    return true;
0091 }
0092 
0093 SaneDialog::~SaneDialog()
0094 {
0095     if (m_ksanew && !m_openDev.isEmpty()) {
0096         // save scan dialog size and all options for the selected device if available
0097         KSharedConfigPtr configPtr = KSharedConfig::openConfig(QStringLiteral("scannersettings"));
0098         KConfigGroup group(configPtr, QStringLiteral("ScanDialog"));
0099         KWindowConfig::saveWindowSize(windowHandle(), group, KConfigGroup::Persistent);
0100         group = configPtr->group(m_openDev);
0101         QMap<QString, QString> opts;
0102         m_ksanew->getOptionValues(opts);
0103         QMap<QString, QString>::const_iterator i = opts.constBegin();
0104         for (; i != opts.constEnd(); ++i) {
0105             group.writeEntry(i.key(), i.value(), KConfigGroup::Persistent);
0106         }
0107     }
0108 }
0109 
0110 void SaneDialog::imageReady(const QImage &img)
0111 {
0112     Q_EMIT finalImage(img, nextId());
0113 }
0114 
0115 int SaneDialog::nextId()
0116 {
0117     return ++m_currentId;
0118 }
0119 
0120 #include "moc_sanedialog.cpp"