File indexing completed on 2025-01-05 03:59:47

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2007-09-06
0007  * Description : a dialog to control camera capture.
0008  *
0009  * SPDX-FileCopyrightText: 2007-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "capturedlg.h"
0016 
0017 // Qt includes
0018 
0019 #include <QTimer>
0020 #include <QDialogButtonBox>
0021 #include <QVBoxLayout>
0022 #include <QPushButton>
0023 
0024 // KDE includes
0025 
0026 #include <kconfiggroup.h>
0027 #include <klocalizedstring.h>
0028 #include <ksharedconfig.h>
0029 
0030 // Local includes
0031 
0032 #include "cameracontroller.h"
0033 #include "capturewidget.h"
0034 #include "dxmlguiwindow.h"
0035 
0036 namespace Digikam
0037 {
0038 
0039 class Q_DECL_HIDDEN CaptureDlg::Private
0040 {
0041 public:
0042 
0043     explicit Private()
0044       : stopPreview  (false),
0045         timer        (nullptr),
0046         buttons      (nullptr),
0047         controller   (nullptr),
0048         captureWidget(nullptr)
0049     {
0050     }
0051 
0052     bool              stopPreview;
0053 
0054     QTimer*           timer;
0055     QDialogButtonBox* buttons;
0056 
0057     CameraController* controller;
0058 
0059     CaptureWidget*    captureWidget;
0060 };
0061 
0062 CaptureDlg::CaptureDlg(QWidget* const parent,
0063                        CameraController* const controller,
0064                        const QString& cameraTitle)
0065     : QDialog(parent),
0066       d      (new Private)
0067 {
0068     d->controller          = controller;
0069 
0070     setWindowTitle(i18nc("@title:window %1: name of the camera", "Capture from %1", cameraTitle));
0071     setModal(true);
0072 
0073     d->buttons             = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0074     d->buttons->button(QDialogButtonBox::Cancel)->setDefault(true);
0075     d->buttons->button(QDialogButtonBox::Ok)->setText(i18nc("@action:button", "Capture"));
0076 
0077     d->captureWidget       = new CaptureWidget(this);
0078 
0079     QVBoxLayout* const vbx = new QVBoxLayout(this);
0080     vbx->addWidget(d->captureWidget);
0081     vbx->addWidget(d->buttons);
0082     setLayout(vbx);
0083 
0084     KConfigGroup group     = KSharedConfig::openConfig()->group(QLatin1String("Capture Tool Dialog"));
0085 
0086     winId();
0087     DXmlGuiWindow::restoreWindowSize(windowHandle(), group);
0088     resize(windowHandle()->size());
0089 
0090     // -------------------------------------------------------------
0091 
0092     connect(d->buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
0093             this, SLOT(slotCapture()));
0094 
0095     connect(d->buttons->button(QDialogButtonBox::Cancel), SIGNAL(clicked()),
0096             this, SLOT(slotCancel()));
0097 
0098     connect(d->buttons->button(QDialogButtonBox::Help), SIGNAL(clicked()),
0099             this, SLOT(slotHelp()));
0100 
0101     connect(d->controller, SIGNAL(signalPreview(QImage)),
0102             this, SLOT(slotPreviewDone(QImage)));
0103 
0104     // -------------------------------------------------------------
0105 
0106     if (d->controller->cameraCaptureImagePreviewSupport())
0107     {
0108         d->timer = new QTimer(this);
0109 
0110         connect(d->timer, SIGNAL(timeout()),
0111                 this, SLOT(slotPreview()));
0112 
0113         d->timer->setSingleShot(true);
0114 
0115         d->timer->start(0);
0116     }
0117 }
0118 
0119 CaptureDlg::~CaptureDlg()
0120 {
0121     // TODO is there a need to call this even separately? As parent is set to this widget in any case, so it should be destroyed?
0122 
0123     delete d->timer;
0124     delete d;
0125 }
0126 
0127 void CaptureDlg::closeEvent(QCloseEvent* e)
0128 {
0129     d->stopPreview = true;
0130 
0131     if (d->timer)
0132     {
0133         d->timer->stop();
0134     }
0135 
0136     KConfigGroup group = KSharedConfig::openConfig()->group(QLatin1String("Capture Tool Dialog"));
0137     DXmlGuiWindow::saveWindowSize(windowHandle(), group);
0138 
0139     e->accept();
0140 }
0141 
0142 void CaptureDlg::slotCancel()
0143 {
0144     d->stopPreview = true;
0145 
0146     if (d->timer)
0147     {
0148         d->timer->stop();
0149     }
0150 
0151     KConfigGroup group = KSharedConfig::openConfig()->group(QLatin1String("Capture Tool Dialog"));
0152     DXmlGuiWindow::saveWindowSize(windowHandle(), group);
0153 
0154     reject();
0155 }
0156 
0157 void CaptureDlg::slotPreview()
0158 {
0159     d->controller->getPreview();
0160 }
0161 
0162 void CaptureDlg::slotCapture()
0163 {
0164     d->stopPreview = true;
0165 
0166     if (d->timer)
0167     {
0168         d->timer->stop();
0169     }
0170 
0171     disconnect(d->controller, SIGNAL(signalPreview(QImage)),
0172                this, SLOT(slotPreviewDone(QImage)));
0173 
0174     KConfigGroup group = KSharedConfig::openConfig()->group(QLatin1String("Capture Tool Dialog"));
0175     DXmlGuiWindow::saveWindowSize(windowHandle(), group);
0176     d->controller->capture();
0177 
0178     accept();
0179 }
0180 
0181 void CaptureDlg::slotPreviewDone(const QImage& preview)
0182 {
0183     d->captureWidget->setPreview(preview);
0184 
0185     if (!d->stopPreview && d->timer)
0186     {
0187         d->timer->start(0);
0188     }
0189 }
0190 
0191 void CaptureDlg::slotHelp()
0192 {
0193     openOnlineDocumentation(QLatin1String("import_tools"), QLatin1String("camera_import"));
0194 }
0195 
0196 } // namespace Digikam
0197 
0198 #include "moc_capturedlg.cpp"