File indexing completed on 2024-05-12 05:09:49

0001 /***************************************************************************
0002     Copyright (C) 2005-2009 Robby Stephenson <robby@periapsis.org>
0003     Copyright (C) 2011 Pedro Miguel Carvalho <kde@pmc.com.pt>
0004  ***************************************************************************/
0005 
0006 /***************************************************************************
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or         *
0009  *   modify it under the terms of the GNU General Public License as        *
0010  *   published by the Free Software Foundation; either version 2 of        *
0011  *   the License or (at your option) version 3 or any later version        *
0012  *   accepted by the membership of KDE e.V. (or its successor approved     *
0013  *   by the membership of KDE e.V.), which shall act as a proxy            *
0014  *   defined in Section 14 of version 3 of the license.                    *
0015  *                                                                         *
0016  *   This program is distributed in the hope that it will be useful,       *
0017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0019  *   GNU General Public License for more details.                          *
0020  *                                                                         *
0021  *   You should have received a copy of the GNU General Public License     *
0022  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0023  *                                                                         *
0024  ***************************************************************************/
0025 
0026 #include "statusbar.h"
0027 #include "progress.h"
0028 #include "../progressmanager.h"
0029 #include "../tellico_debug.h"
0030 
0031 #include <KLocalizedString>
0032 #include <KStandardGuiItem>
0033 
0034 #include <QLabel>
0035 #include <QPushButton>
0036 #include <QApplication>
0037 
0038 using Tellico::StatusBar;
0039 StatusBar* StatusBar::s_self = nullptr;
0040 
0041 StatusBar::StatusBar(QWidget* parent_) : QStatusBar(parent_) {
0042   s_self = this;
0043 
0044   // don't care about text and id
0045   m_mainLabel = new QLabel(this);
0046   m_mainLabel->setAlignment(Qt::Alignment(Qt::AlignLeft) | Qt::Alignment(Qt::AlignVCenter));
0047   m_mainLabel->setIndent(4);
0048   insertPermanentWidget(0, m_mainLabel, 3 /*stretch*/);
0049 
0050   m_countLabel = new QLabel(this);
0051   m_countLabel->setAlignment(Qt::Alignment(Qt::AlignLeft) | Qt::Alignment(Qt::AlignVCenter));
0052   m_countLabel->setIndent(4);
0053   insertPermanentWidget(1, m_countLabel, 0);
0054 
0055   m_progress = new GUI::Progress(100, this);
0056   addPermanentWidget(m_progress, 1);
0057 
0058   m_cancelButton = new QPushButton(this);
0059   KGuiItem::assign(m_cancelButton, KStandardGuiItem::cancel());
0060   m_cancelButton->setText(QString());
0061   m_cancelButton->setToolTip(i18n("Cancel"));
0062   addPermanentWidget(m_cancelButton, 0);
0063 
0064   m_progress->hide();
0065   m_cancelButton->hide();
0066 
0067   ProgressManager* pm = ProgressManager::self();
0068   connect(pm, &ProgressManager::signalTotalProgress, this, &StatusBar::slotProgress);
0069   connect(m_cancelButton, &QPushButton::clicked, pm, &ProgressManager::slotCancelAll);
0070 }
0071 
0072 void StatusBar::ensurePolished() const {
0073   QStatusBar::ensurePolished();
0074 
0075   int h = 0;
0076   QList<QWidget*> list = findChildren<QWidget*>();
0077   foreach(QWidget* o, list) {
0078     int _h = o->minimumSizeHint().height();
0079     if(_h > h) {
0080       h = _h;
0081     }
0082   }
0083 
0084   h -= 4; // hint from amarok, it's too big usually
0085 
0086   foreach(QObject* o, list) {
0087     static_cast<QWidget*>(o)->setFixedHeight(h);
0088   }
0089 }
0090 
0091 void StatusBar::clearStatus() {
0092 // always hide progress bar when clearing status
0093   slotProgress(100);
0094   setStatus(i18n("Ready."));
0095 }
0096 
0097 void StatusBar::setStatus(const QString& status_) {
0098   // always add a space for aesthetics
0099   m_mainLabel->setText(status_ + QLatin1Char(' '));
0100 }
0101 
0102 void StatusBar::setCount(const QString& count_) {
0103   m_countLabel->setText(count_ + QLatin1Char(' '));
0104 }
0105 
0106 void StatusBar::slotProgress(qulonglong progress_) {
0107 //  myDebug() << "StatusBar::slotProgress - Progress:" << progress_;
0108   // yes, yes, yes, casting from longlong to int is bad, I know...
0109   m_progress->setValue(progress_);
0110   if(m_progress->isDone()) {
0111     m_progress->hide();
0112     m_cancelButton->hide();
0113   } else if(m_progress->isHidden()) {
0114     m_progress->show();
0115     if(ProgressManager::self()->anyCanBeCancelled()) {
0116       m_cancelButton->show();
0117     }
0118     qApp->processEvents(); // needed so the window gets updated ???
0119   }
0120 }
0121 
0122 void StatusBar::slotUpdate() {
0123 }