File indexing completed on 2024-06-16 05:01:31

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002    Copyright (C) 2014 Thomas Lübking <thomas.luebking@gmail.com>
0003 
0004    This file is part of the Trojita Qt IMAP e-mail client,
0005    http://trojita.flaska.net/
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public License as
0009    published by the Free Software Foundation; either version 2 of
0010    the License or (at your option) version 3 or any later version
0011    accepted by the membership of KDE e.V. (or its successor approved
0012    by the membership of KDE e.V.), which shall act as a proxy
0013    defined in Section 14 of version 3 of the license.
0014 
0015    This program is distributed in the hope that it will be useful,
0016    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018    GNU General Public License for more details.
0019 
0020    You should have received a copy of the GNU General Public License
0021    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022 */
0023 
0024 #include "TaskProgressIndicator.h"
0025 #include <QApplication>
0026 #include <QMouseEvent>
0027 #include <QTimer>
0028 #include "Gui/Spinner.h"
0029 #include "Imap/Model/Model.h"
0030 #include "Imap/Model/VisibleTasksModel.h"
0031 
0032 namespace Gui
0033 {
0034 
0035 TaskProgressIndicator::TaskProgressIndicator(QWidget *parent) :
0036     Spinner(parent), m_busyCursorTrigger(new QTimer(this)), m_busy(false)
0037 {
0038     connect(m_busyCursorTrigger, &QTimer::timeout, []() {
0039         QApplication::setOverrideCursor(Qt::BusyCursor);
0040     });
0041     m_busyCursorTrigger->setSingleShot(true);
0042     m_busyCursorTrigger->setInterval(666);
0043 
0044     setType(Spinner::Elastic);
0045     setContext(Spinner::Throbber);
0046 }
0047 
0048 /** @short Connect to the specified IMAP model as the source of the activity information */
0049 void TaskProgressIndicator::setImapModel(Imap::Mailbox::Model *model)
0050 {
0051     if (model) {
0052         m_visibleTasksModel = new Imap::Mailbox::VisibleTasksModel(model, model->taskModel());
0053         connect(m_visibleTasksModel.data(), &QAbstractItemModel::layoutChanged, this, &TaskProgressIndicator::updateActivityIndication);
0054         connect(m_visibleTasksModel.data(), &QAbstractItemModel::modelReset, this, &TaskProgressIndicator::updateActivityIndication);
0055         connect(m_visibleTasksModel.data(), &QAbstractItemModel::rowsInserted, this, &TaskProgressIndicator::updateActivityIndication);
0056         connect(m_visibleTasksModel.data(), &QAbstractItemModel::rowsRemoved, this, &TaskProgressIndicator::updateActivityIndication);
0057     }
0058 }
0059 
0060 /** @short Reflect a possible change in the activity in the GUI */
0061 void TaskProgressIndicator::updateActivityIndication()
0062 {
0063     if (!m_visibleTasksModel)
0064         return;
0065 
0066     bool busy = m_visibleTasksModel->hasChildren();
0067     if (!m_busy && busy) {
0068         start();
0069         if (!m_busyCursorTrigger->isActive()) {
0070             // do NOT restart, we don't want to prolong this delay
0071             m_busyCursorTrigger->start();
0072         }
0073     } else if (m_busy && !busy) {
0074         stop();
0075         if (!m_busyCursorTrigger->isActive()) {
0076             // don't restore unless we've alredy set it
0077             QApplication::restoreOverrideCursor();
0078         }
0079         // don't cause future timeout
0080         m_busyCursorTrigger->stop();
0081     }
0082 
0083     if (busy) {
0084         setToolTip(tr("%n ongoing actions", 0, m_visibleTasksModel->rowCount()));
0085     } else {
0086         setToolTip(tr("IMAP connection idle"));
0087     }
0088 
0089     m_busy = busy;
0090 }
0091 
0092 }