File indexing completed on 2024-05-12 05:21:41

0001 /*
0002  * Copyright (C) 2003 by Scott Monachello <smonach@cox.net>
0003  * Copyright (C) 2019  Alexander Potashev <aspotashev@gmail.com>
0004  *
0005  *   This program is free software; you can redistribute it and/or modify
0006  *   it under the terms of the GNU General Public License as published by
0007  *   the Free Software Foundation; either version 2 of the License, or
0008  *   (at your option) any later version.
0009  *
0010  *   This program is distributed in the hope that it will be useful,
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *   GNU General Public License for more details.
0014  *
0015  *   You should have received a copy of the GNU General Public License along
0016  *   with this program; if not, write to the
0017  *      Free Software Foundation, Inc.
0018  *      51 Franklin Street, Fifth Floor
0019  *      Boston, MA  02110-1301  USA.
0020  *
0021  */
0022 
0023 /*
0024  * TrayIcon.
0025  *
0026  * This implements the functionality of the little icon in the kpanel
0027  * tray. Among which are tool tips and the running clock animated icon
0028  */
0029 
0030 #include "tray.h"
0031 
0032 #include <QApplication>
0033 #include <QMenu>
0034 #include <QMovie>
0035 #include <QToolTip>
0036 #include <QScreen>
0037 
0038 #include <KLocalizedString>
0039 
0040 #include "ktt_debug.h"
0041 #include "mainwindow.h"
0042 #include "model/task.h"
0043 #include "timetrackerwidget.h"
0044 
0045 TrayIcon::TrayIcon(MainWindow *parent)
0046     : KStatusNotifierItem(parent)
0047 {
0048     Q_INIT_RESOURCE(pics);
0049 
0050     setObjectName(QStringLiteral("Ktimetracker Tray"));
0051 
0052     m_animation = new QMovie(QStringLiteral(":/pics/active-icon.gif"), QByteArray("GIF"), this);
0053     connect(m_animation, &QMovie::frameChanged, this, &TrayIcon::setActiveIcon);
0054     m_animation->jumpToFrame(0);
0055 
0056     auto *widget = dynamic_cast<TimeTrackerWidget *>(parent->centralWidget());
0057     if (widget) {
0058         QAction *action = widget->action(QStringLiteral("configure_ktimetracker"));
0059         if (action) {
0060             contextMenu()->addAction(action);
0061         }
0062 
0063         action = widget->action(QStringLiteral("stopAll"));
0064         if (action) {
0065             contextMenu()->addAction(action);
0066         }
0067     }
0068 
0069     updateToolTip(QList<Task *>());
0070 }
0071 
0072 void TrayIcon::startClock()
0073 {
0074     m_animation->start();
0075 }
0076 
0077 void TrayIcon::stopClock()
0078 {
0079     m_animation->stop();
0080 }
0081 
0082 void TrayIcon::setActiveIcon(int /*unused*/)
0083 {
0084     setIconByPixmap(QIcon(m_animation->currentPixmap()));
0085 }
0086 
0087 void TrayIcon::updateToolTip(const QList<Task *> &activeTasks)
0088 {
0089     if (activeTasks.isEmpty()) {
0090         this->setToolTip(QStringLiteral("ktimetracker"), QStringLiteral("ktimetracker"), i18n("No active tasks"));
0091         return;
0092     }
0093 
0094     QFontMetrics fm(QToolTip::font());
0095     const QString continued = i18nc("ellipsis to truncate long list of tasks", ", ...");
0096     const int buffer = fm.boundingRect(continued).width();
0097     const int desktopWidth = associatedWindow()->screen()->geometry().width();
0098     const int maxWidth = desktopWidth - buffer;
0099 
0100     QString qTip;
0101     QString s;
0102 
0103     // Build the tool tip with all of the names of the active tasks.
0104     // If at any time the width of the tool tip is larger than the desktop,
0105     // stop building it.
0106 
0107     for (int i = 0; i < activeTasks.count(); ++i) {
0108         Task *task = activeTasks.at(i);
0109         if (i > 0) {
0110             s += i18nc("separator between task names", ", ") + task->name();
0111         } else {
0112             s += task->name();
0113         }
0114 
0115         int width = fm.boundingRect(s).width();
0116         if (width > maxWidth) {
0117             qTip += continued;
0118             break;
0119         }
0120         qTip = s;
0121     }
0122     this->setToolTip(QStringLiteral("ktimetracker"), QStringLiteral("ktimetracker"), qTip);
0123 }
0124 
0125 #include "moc_tray.cpp"