File indexing completed on 2024-04-28 05:42:03

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
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     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include "stopdlg.h"
0021 #include "ccontextlistener.h"
0022 #include "helpers/stringhelper.h"
0023 #include "settings/kdesvnsettings.h"
0024 
0025 #include <KLocalizedString>
0026 
0027 #include <QApplication>
0028 #include <QDialogButtonBox>
0029 #include <QLabel>
0030 #include <QProgressBar>
0031 #include <QTextBrowser>
0032 #include <QTimer>
0033 #include <QVBoxLayout>
0034 #include <QWidget>
0035 
0036 StopDlg::StopDlg(CContextListener *listener, QWidget *parent, const QString &caption, const QString &text)
0037     : QDialog(parent)
0038     , m_MinDuration(1000)
0039     , mCancelled(false)
0040     , mShown(false)
0041     , m_BarShown(false)
0042     , m_netBarShown(false)
0043     , cstack(nullptr)
0044     , m_bBox(new QDialogButtonBox(QDialogButtonBox::Cancel, this))
0045 {
0046     setWindowTitle(caption);
0047 
0048     m_lastLogLines = 0;
0049     m_lastLog.clear();
0050 
0051     mShowTimer = new QTimer(this);
0052     m_StopTick.start();
0053 
0054     mainLayout = new QVBoxLayout(this);
0055     layout = new QVBoxLayout;
0056     mainLayout->addLayout(layout);
0057     mainLayout->addWidget(m_bBox);
0058     mLabel = new QLabel(text, this);
0059     layout->addWidget(mLabel);
0060     m_ProgressBar = new QProgressBar(this);
0061     m_ProgressBar->setRange(0, 15);
0062     m_ProgressBar->setTextVisible(false);
0063     layout->addWidget(m_ProgressBar);
0064     m_NetBar = new QProgressBar(this);
0065     m_NetBar->setRange(0, 15);
0066     layout->addWidget(m_NetBar);
0067 
0068     mWait = false;
0069     m_LogWindow = nullptr;
0070 
0071     connect(mShowTimer, &QTimer::timeout, this, &StopDlg::slotAutoShow);
0072     connect(m_bBox, &QDialogButtonBox::rejected, this, &StopDlg::slotCancel);
0073     if (listener) {
0074         connect(listener, &CContextListener::tickProgress, this, &StopDlg::slotTick);
0075         connect(listener, &CContextListener::waitShow, this, &StopDlg::slotWait);
0076         connect(listener, &CContextListener::netProgress, this, &StopDlg::slotNetProgres);
0077         connect(this, &StopDlg::sigCancel, listener, &CContextListener::setCanceled);
0078     }
0079     mShowTimer->setSingleShot(true);
0080     mShowTimer->start(m_MinDuration);
0081     setMinimumSize(280, 160);
0082     adjustSize();
0083 }
0084 
0085 void StopDlg::showEvent(QShowEvent *e)
0086 {
0087     if (!cstack) {
0088         cstack = new CursorStack(Qt::BusyCursor);
0089     }
0090     QDialog::showEvent(e);
0091 }
0092 
0093 void StopDlg::hideEvent(QHideEvent *e)
0094 {
0095     delete cstack;
0096     cstack = nullptr;
0097     QDialog::hideEvent(e);
0098 }
0099 
0100 void StopDlg::slotWait(bool how)
0101 {
0102     mWait = how;
0103     if (mShown && mWait) {
0104         hide();
0105         mShown = false;
0106     }
0107 }
0108 
0109 StopDlg::~StopDlg()
0110 {
0111     delete cstack;
0112 }
0113 
0114 void StopDlg::slotAutoShow()
0115 {
0116     bool hasDialogs = false;
0117     QWidget *w = QApplication::activeModalWidget();
0118     if (w && w != this && w != parentWidget()) {
0119         hasDialogs = true;
0120     }
0121     if (hasDialogs) {
0122         hide();
0123     }
0124     if (mShown || mWait || hasDialogs) {
0125         mShowTimer->setSingleShot(true);
0126         if (mWait) {
0127             // qCDebug(KDESVN_LOG) << "Waiting for show"<<endl;
0128             mShowTimer->start(m_MinDuration);
0129         }
0130         mShowTimer->start(m_MinDuration);
0131         return;
0132     }
0133     m_ProgressBar->hide();
0134     m_NetBar->hide();
0135     m_BarShown = false;
0136     m_netBarShown = false;
0137     show();
0138     QCoreApplication::processEvents();
0139     mShown = true;
0140     mShowTimer->setSingleShot(true);
0141     mShowTimer->start(m_MinDuration);
0142 }
0143 
0144 void StopDlg::slotCancel()
0145 {
0146     mCancelled = true;
0147     emit sigCancel(true);
0148 }
0149 
0150 void StopDlg::slotTick()
0151 {
0152     if (m_StopTick.elapsed() > 500) {
0153         if (!m_BarShown) {
0154             m_ProgressBar->show();
0155             m_BarShown = true;
0156         }
0157         if (m_ProgressBar->value() == 15) {
0158             m_ProgressBar->reset();
0159         } else {
0160             m_ProgressBar->setValue(m_ProgressBar->value() + 1);
0161         }
0162         m_StopTick.restart();
0163         QCoreApplication::processEvents();
0164     }
0165 }
0166 
0167 void StopDlg::slotExtraMessage(const QString &msg)
0168 {
0169     ++m_lastLogLines;
0170     if (!m_LogWindow) {
0171         m_LogWindow = new QTextBrowser(this);
0172         layout->addWidget(m_LogWindow);
0173         m_LogWindow->show();
0174         resize(QSize(500, 400).expandedTo(minimumSizeHint()));
0175     }
0176     if (m_lastLogLines >= Kdesvnsettings::self()->cmdline_log_minline() && isHidden()) {
0177         slotAutoShow();
0178     }
0179     m_LogWindow->append(msg);
0180     QCoreApplication::processEvents();
0181 }
0182 
0183 void StopDlg::slotNetProgres(long long int current, long long int max)
0184 {
0185     if (m_StopTick.elapsed() > 300 || (m_BarShown && !m_netBarShown)) {
0186         if (!m_netBarShown) {
0187             m_NetBar->show();
0188             m_netBarShown = true;
0189         }
0190         QString s1 = helpers::ByteToString(current);
0191         if (max > -1 && max != m_NetBar->maximum()) {
0192             QString s2 = helpers::ByteToString(max);
0193             m_NetBar->setFormat(i18n("%p% of %1", s2));
0194             m_NetBar->setRange(0, max);
0195         }
0196         if (max == -1) {
0197             if (m_NetBar->maximum() == -1 || m_NetBar->maximum() < current) {
0198                 m_NetBar->setFormat(i18n("%1 transferred.", s1));
0199                 m_NetBar->setRange(0, current + 1);
0200             } else {
0201                 m_NetBar->setFormat(i18n("%1 of %2", s1, helpers::ByteToString(m_NetBar->maximum())));
0202             }
0203         }
0204         m_NetBar->setValue(current);
0205         m_StopTick.restart();
0206         QCoreApplication::processEvents();
0207     }
0208 }
0209 
0210 #include "moc_stopdlg.cpp"