File indexing completed on 2025-03-09 03:52:08

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-10-05
0007  * Description : a presentation tool.
0008  *
0009  * SPDX-FileCopyrightText:      2008 by Valerio Fuoglio <valerio dot fuoglio at gmail dot com>
0010  * SPDX-FileCopyrightText:      2021 by Phuoc Khanh Le <phuockhanhnk94 at gmail dot com>
0011  * SPDX-FileCopyrightText: 2012-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "presentationctrlwidget.h"
0018 
0019 // Qt includes
0020 
0021 #include <QToolButton>
0022 #include <QHBoxLayout>
0023 #include <QPixmap>
0024 #include <QApplication>
0025 #include <QIcon>
0026 #include <QInputDialog>
0027 #include <QMessageBox>
0028 #include <QPointer>
0029 
0030 // KDE includes
0031 
0032 #include <klocalizedstring.h>
0033 
0034 // Local includes
0035 
0036 #include "digikam_debug.h"
0037 
0038 namespace DigikamGenericPresentationPlugin
0039 {
0040 
0041 PresentationCtrlWidget::PresentationCtrlWidget(QWidget* const parent,
0042                                                PresentationContainer* const sharedData)
0043     : QWidget     (parent),
0044       m_canHide   (true),
0045       m_sharedData(sharedData)
0046 {
0047     setupUi(this);
0048     m_playButton->setCheckable(true);
0049     m_slideLabel->setPixmap(QIcon::fromTheme(QLatin1String("view-presentation")).pixmap(64, 64));
0050 
0051     m_prevButton->setText(QString());
0052     m_nextButton->setText(QString());
0053     m_playButton->setText(QString());
0054     m_stopButton->setText(QString());
0055     m_moveToTrash->setText(QString());
0056 
0057     m_prevButton->setIcon(QIcon::fromTheme(QLatin1String("media-skip-backward")));
0058     m_nextButton->setIcon(QIcon::fromTheme(QLatin1String("media-skip-forward")));
0059     m_playButton->setIcon(QIcon::fromTheme(QLatin1String("media-playback-start")));
0060     m_stopButton->setIcon(QIcon::fromTheme(QLatin1String("media-playback-stop")));
0061     m_delayButton->setIcon(QIcon::fromTheme(QLatin1String("appointment-new")));
0062     m_moveToTrash->setIcon(QIcon::fromTheme(QLatin1String("user-trash")));
0063 
0064     connect(m_playButton, SIGNAL(toggled(bool)),
0065             this, SLOT(slotPlayButtonToggled()));
0066 
0067     connect(m_nextButton, SIGNAL(clicked()),
0068             this, SLOT(slotNexPrevClicked()));
0069 
0070     connect(m_prevButton, SIGNAL(clicked()),
0071             this, SLOT(slotNexPrevClicked()));
0072 
0073     connect(m_nextButton, SIGNAL(clicked()),
0074             this, SIGNAL(signalNext()));
0075 
0076     connect(m_prevButton, SIGNAL(clicked()),
0077             this, SIGNAL(signalPrev()));
0078 
0079     connect(m_stopButton, SIGNAL(clicked()),
0080             this, SIGNAL(signalClose()));
0081 
0082     connect(m_delayButton, SIGNAL(clicked()),
0083             this, SLOT(slotChangeDelayButtonPressed()));
0084 
0085     connect(m_moveToTrash, SIGNAL(clicked()),
0086             this, SLOT(slotMoveToTrash()));
0087 
0088     slotPlayButtonToggled();
0089 }
0090 
0091 PresentationCtrlWidget::~PresentationCtrlWidget()
0092 {
0093 }
0094 
0095 bool PresentationCtrlWidget::canHide() const
0096 {
0097     return m_canHide;
0098 }
0099 
0100 bool PresentationCtrlWidget::isPaused() const
0101 {
0102     return m_playButton->isChecked();
0103 }
0104 
0105 void PresentationCtrlWidget::setPaused(bool val)
0106 {
0107     if (val == isPaused())
0108     {
0109         return;
0110     }
0111 
0112     m_playButton->setChecked(val);
0113 
0114     slotPlayButtonToggled();
0115 }
0116 
0117 void PresentationCtrlWidget::setEnabledPlay(bool val)
0118 {
0119     m_playButton->setEnabled(val);
0120 }
0121 
0122 void PresentationCtrlWidget::setEnabledNext(bool val)
0123 {
0124     m_nextButton->setEnabled(val);
0125 }
0126 
0127 void PresentationCtrlWidget::setEnabledPrev(bool val)
0128 {
0129     m_prevButton->setEnabled(val);
0130 }
0131 
0132 void PresentationCtrlWidget::slotPlayButtonToggled()
0133 {
0134     if (m_playButton->isChecked())
0135     {
0136         m_canHide = false;
0137         m_playButton->setIcon(QIcon::fromTheme(QLatin1String("media-playback-start")).pixmap(22));
0138 
0139         Q_EMIT signalPause();
0140     }
0141     else
0142     {
0143         m_canHide = true;
0144         m_playButton->setIcon(QIcon::fromTheme(QLatin1String("media-playback-pause")).pixmap(22));
0145 
0146         Q_EMIT signalPlay();
0147     }
0148 }
0149 
0150 void PresentationCtrlWidget::slotNexPrevClicked()
0151 {
0152     if (!m_playButton->isChecked())
0153     {
0154         m_playButton->setChecked(true);
0155         m_canHide = false;
0156         m_playButton->setIcon(QIcon::fromTheme(QLatin1String("media-playback-start")).pixmap(22));
0157 
0158         Q_EMIT signalPause();
0159     }
0160 }
0161 
0162 void PresentationCtrlWidget::keyPressEvent(QKeyEvent* event)
0163 {
0164     switch (event->key())
0165     {
0166         case (Qt::Key_Space):
0167         {
0168             if (m_playButton->isEnabled())
0169             {
0170                 m_playButton->animateClick();
0171             }
0172 
0173             break;
0174         }
0175 
0176         case (Qt::Key_PageUp):
0177         {
0178             if (m_prevButton->isEnabled())
0179             {
0180                 m_prevButton->animateClick();
0181             }
0182 
0183             break;
0184         }
0185 
0186         case (Qt::Key_PageDown):
0187         {
0188             if (m_nextButton->isEnabled())
0189             {
0190                 m_nextButton->animateClick();
0191             }
0192 
0193             break;
0194         }
0195 
0196         case (Qt::Key_Escape):
0197         {
0198             if (m_stopButton->isEnabled())
0199             {
0200                 m_stopButton->animateClick();
0201             }
0202 
0203             break;
0204         }
0205 
0206         default:
0207         {
0208             break;
0209         }
0210     }
0211 
0212     event->accept();
0213 }
0214 
0215 void PresentationCtrlWidget::slotChangeDelayButtonPressed()
0216 {
0217     bool ok;
0218     bool running = (!isPaused() && m_playButton->isEnabled());
0219     int min      = m_sharedData->useMilliseconds ? 100    : 1;
0220     int max      = m_sharedData->useMilliseconds ? 120000 : 120;
0221     int delay    = m_sharedData->useMilliseconds ? m_sharedData->delay
0222                                                  : m_sharedData->delay / 1000;
0223 
0224     if (running)
0225     {
0226         m_playButton->animateClick();
0227     }
0228 
0229     delay = QInputDialog::getInt(this, i18nc("@title:window", "Specify Delay for Slide Show"),
0230                                  i18n("Delay:"), delay , min, max, min, &ok);
0231 
0232     delay = m_sharedData->useMilliseconds ? delay : delay * 1000;
0233 
0234     if (ok)
0235     {
0236         m_sharedData->delay = delay;
0237     }
0238 
0239     if (running)
0240     {
0241         m_playButton->animateClick();
0242     }
0243 }
0244 
0245 void PresentationCtrlWidget::slotMoveToTrash()
0246 {
0247     bool running = (!isPaused() && m_playButton->isEnabled());
0248 
0249     if (running)
0250     {
0251         m_playButton->animateClick();
0252     }
0253 
0254     QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Question,
0255              i18nc("@title:window", "Delete Image"),
0256              i18n("Do you want to move this image to the trash?"),
0257              QMessageBox::Yes | QMessageBox::No, this);
0258 
0259     msgBox->setDefaultButton(QMessageBox::Yes);
0260 
0261     int ret = msgBox->exec();
0262     delete msgBox;
0263 
0264     if (ret == QMessageBox::Yes)
0265     {
0266         Q_EMIT signalRemoveImageFromList();
0267     }
0268 
0269     if (running)
0270     {
0271         m_playButton->animateClick();
0272     }
0273 }
0274 
0275 } // namespace DigikamGenericPresentationPlugin
0276 
0277 #include "moc_presentationctrlwidget.cpp"