File indexing completed on 2024-04-28 04:20:53

0001 /* SPDX-FileCopyrightText: 2012-2020 The KPhotoAlbum Development Team
0002 
0003    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "StatusIndicator.h"
0007 
0008 #include "JobManager.h"
0009 #include "JobViewer.h"
0010 
0011 #include <KLocalizedString>
0012 #include <QApplication>
0013 #include <QHelpEvent>
0014 #include <QTimer>
0015 #include <QToolTip>
0016 
0017 namespace BackgroundTaskManager
0018 {
0019 
0020 StatusIndicator::StatusIndicator(QWidget *parent)
0021     : KLed(Qt::green, parent)
0022     , m_timer(new QTimer(this))
0023     , m_jobViewer(nullptr)
0024 {
0025     connect(m_timer, &QTimer::timeout, this, &StatusIndicator::flicker);
0026     setCursor(Qt::PointingHandCursor);
0027     connect(JobManager::instance(), SIGNAL(jobStarted(JobInterface *)), this, SLOT(maybeStartFlicker()));
0028 }
0029 
0030 bool StatusIndicator::event(QEvent *event)
0031 {
0032     if (event->type() == QEvent::ToolTip) {
0033         showToolTip(dynamic_cast<QHelpEvent *>(event));
0034         return true;
0035     }
0036     return KLed::event(event);
0037 }
0038 
0039 void StatusIndicator::mouseReleaseEvent(QMouseEvent *)
0040 {
0041     if (!m_jobViewer)
0042         m_jobViewer = new JobViewer;
0043 
0044     m_jobViewer->setVisible(!m_jobViewer->isVisible());
0045 }
0046 
0047 void StatusIndicator::flicker()
0048 {
0049     QColor newColor;
0050 
0051     if (!JobManager::instance()->hasActiveJobs()) {
0052         m_timer->stop();
0053         newColor = palette().mid().color();
0054     } else if (JobManager::instance()->isPaused() && !JobManager::instance()->hasActiveJobs())
0055         newColor = QColor(Qt::yellow).lighter();
0056     else
0057         newColor = (color() == palette().mid().color() ? currentColor() : palette().mid().color());
0058 
0059     setColor(newColor);
0060 }
0061 
0062 void StatusIndicator::maybeStartFlicker()
0063 {
0064     if (!m_timer->isActive())
0065         m_timer->start(500);
0066 }
0067 
0068 QColor StatusIndicator::currentColor() const
0069 {
0070     return JobManager::instance()->isPaused() ? Qt::yellow : Qt::green;
0071 }
0072 
0073 void StatusIndicator::showToolTip(QHelpEvent *event)
0074 {
0075     const int activeCount = JobManager::instance()->activeJobCount();
0076     const int pendingCount = JobManager::instance()->futureJobCount();
0077 
0078     const QString text = i18n("<p>Active jobs: %1<br/>"
0079                               "Pending jobs: %2"
0080                               "<hr/><br/>"
0081                               "Color codes:"
0082                               "<ul><li><b>blinking green</b>: Active background jobs</li>"
0083                               "<li><b>gray</b>: No active jobs</li>"
0084                               "<li><b>solid yellow</b>: Job queue is paused</li>"
0085                               "<li><b>blinking yellow</b>: Job queue is paused for background jobs, but is executing a foreground job "
0086                               "(like extracting a thumbnail for a video file, which is currently shown in the thumbnail viewer)</li></ul></p>",
0087                               activeCount, pendingCount);
0088     QToolTip::showText(event->globalPos(), text);
0089 }
0090 
0091 } // namespace BackgroundTaskManager
0092 // vi:expandtab:tabstop=4 shiftwidth=4:
0093 
0094 #include "moc_StatusIndicator.cpp"