1 /***************************************************************************
2     Copyright (C) 2005-2009 Robby Stephenson <robby@periapsis.org>
3     Copyright (C) 2011 Pedro Miguel Carvalho <kde@pmc.com.pt>
4  ***************************************************************************/
5 
6 /***************************************************************************
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or         *
9  *   modify it under the terms of the GNU General Public License as        *
10  *   published by the Free Software Foundation; either version 2 of        *
11  *   the License or (at your option) version 3 or any later version        *
12  *   accepted by the membership of KDE e.V. (or its successor approved     *
13  *   by the membership of KDE e.V.), which shall act as a proxy            *
14  *   defined in Section 14 of version 3 of the license.                    *
15  *                                                                         *
16  *   This program is distributed in the hope that it will be useful,       *
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19  *   GNU General Public License for more details.                          *
20  *                                                                         *
21  *   You should have received a copy of the GNU General Public License     *
22  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
23  *                                                                         *
24  ***************************************************************************/
25 
26 #include "statusbar.h"
27 #include "progress.h"
28 #include "../progressmanager.h"
29 #include "../tellico_debug.h"
30 
31 #include <KLocalizedString>
32 #include <KStandardGuiItem>
33 
34 #include <QLabel>
35 #include <QPushButton>
36 #include <QApplication>
37 
38 using Tellico::StatusBar;
39 StatusBar* StatusBar::s_self = nullptr;
40 
StatusBar(QWidget * parent_)41 StatusBar::StatusBar(QWidget* parent_) : QStatusBar(parent_) {
42   s_self = this;
43 
44   // don't care about text and id
45   m_mainLabel = new QLabel(this);
46   m_mainLabel->setAlignment(Qt::Alignment(Qt::AlignLeft) | Qt::Alignment(Qt::AlignVCenter));
47   m_mainLabel->setIndent(4);
48   insertPermanentWidget(0, m_mainLabel, 3 /*stretch*/);
49 
50   m_countLabel = new QLabel(this);
51   m_countLabel->setAlignment(Qt::Alignment(Qt::AlignLeft) | Qt::Alignment(Qt::AlignVCenter));
52   m_countLabel->setIndent(4);
53   insertPermanentWidget(1, m_countLabel, 0);
54 
55   m_progress = new GUI::Progress(100, this);
56   addPermanentWidget(m_progress, 1);
57 
58   m_cancelButton = new QPushButton(this);
59   KGuiItem::assign(m_cancelButton, KStandardGuiItem::cancel());
60   m_cancelButton->setText(QString());
61   m_cancelButton->setToolTip(i18n("Cancel"));
62   addPermanentWidget(m_cancelButton, 0);
63 
64   m_progress->hide();
65   m_cancelButton->hide();
66 
67   ProgressManager* pm = ProgressManager::self();
68   connect(pm, &ProgressManager::signalTotalProgress, this, &StatusBar::slotProgress);
69   connect(m_cancelButton, &QPushButton::clicked, pm, &ProgressManager::slotCancelAll);
70 }
71 
ensurePolished() const72 void StatusBar::ensurePolished() const {
73   QStatusBar::ensurePolished();
74 
75   int h = 0;
76   QList<QWidget*> list = findChildren<QWidget*>();
77   foreach(QWidget* o, list) {
78     int _h = o->minimumSizeHint().height();
79     if(_h > h) {
80       h = _h;
81     }
82   }
83 
84   h -= 4; // hint from amarok, it's too big usually
85 
86   foreach(QObject* o, list) {
87     static_cast<QWidget*>(o)->setFixedHeight(h);
88   }
89 }
90 
clearStatus()91 void StatusBar::clearStatus() {
92 // always hide progress bar when clearing status
93   slotProgress(100);
94   setStatus(i18n("Ready."));
95 }
96 
setStatus(const QString & status_)97 void StatusBar::setStatus(const QString& status_) {
98   // always add a space for aesthetics
99   m_mainLabel->setText(status_ + QLatin1Char(' '));
100 }
101 
setCount(const QString & count_)102 void StatusBar::setCount(const QString& count_) {
103   m_countLabel->setText(count_ + QLatin1Char(' '));
104 }
105 
slotProgress(qulonglong progress_)106 void StatusBar::slotProgress(qulonglong progress_) {
107 //  myDebug() << "StatusBar::slotProgress - Progress:" << progress_;
108   // yes, yes, yes, casting from longlong to int is bad, I know...
109   m_progress->setValue(progress_);
110   if(m_progress->isDone()) {
111     m_progress->hide();
112     m_cancelButton->hide();
113   } else if(m_progress->isHidden()) {
114     m_progress->show();
115     if(ProgressManager::self()->anyCanBeCancelled()) {
116       m_cancelButton->show();
117     }
118     qApp->processEvents(); // needed so the window gets updated ???
119   }
120 }
121 
slotUpdate()122 void StatusBar::slotUpdate() {
123 }
124