File indexing completed on 2024-04-28 05:08:26

0001 /***************************************************************************
0002     Copyright (C) 2005-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
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 
0025 // much of this code is adapted from libkdepim
0026 // which is GPL licensed, Copyright (c) 2004 Till Adam
0027 
0028 #ifndef TELLICO_PROGRESSMANAGER_H
0029 #define TELLICO_PROGRESSMANAGER_H
0030 
0031 #include <QObject>
0032 #include <QHash>
0033 #include <QPointer>
0034 
0035 namespace Tellico {
0036 
0037 class ProgressManager;
0038 
0039 /**
0040  * @author Robby Stephenson
0041  */
0042 class ProgressItem : public QObject {
0043 Q_OBJECT
0044 
0045 friend class ProgressManager;
0046 
0047 public:
0048   class Done {
0049   public:
0050     Done(QObject* object);
0051     ~Done();
0052   private:
0053     QPointer<QObject> m_object;
0054   };
0055 
0056   bool canCancel() const { return m_canCancel; }
0057   const QString& label() const { return m_label; }
0058   void setLabel(const QString& label);
0059 
0060   qulonglong progress() const { return m_progress; }
0061   void setProgress(qulonglong steps);
0062   qulonglong totalSteps() const { return m_total; }
0063   void setTotalSteps(qulonglong steps);
0064   void setDone();
0065 
0066   void cancel();
0067 
0068 Q_SIGNALS:
0069   void signalProgress(Tellico::ProgressItem* item);
0070   void signalDone(Tellico::ProgressItem* item);
0071   void signalCancelled(Tellico::ProgressItem* item);
0072   void signalTotalSteps(Tellico::ProgressItem* item);
0073 
0074 protected:
0075   /* Only to be used by the ProgressManager */
0076   ProgressItem(const QString& label, bool canCancel);
0077   virtual ~ProgressItem();
0078 
0079 private:
0080   QString m_label;
0081   bool m_canCancel;
0082   qulonglong m_progress;
0083   qulonglong m_total;
0084   bool m_cancelled;
0085   bool m_done;
0086 };
0087 
0088 /**
0089  * @author Robby Stephenson
0090  */
0091 class ProgressManager : public QObject {
0092 Q_OBJECT
0093 
0094 public:
0095   virtual ~ProgressManager();
0096 
0097   static ProgressManager* self() { if(!s_self) s_self = new ProgressManager(); return s_self; }
0098 
0099   ProgressItem& newProgressItem(QObject* owner, const QString& label, bool canCancel = false) {
0100     return newProgressItemImpl(owner, label, canCancel);
0101   }
0102 
0103   bool anyCanBeCancelled() const;
0104 
0105 Q_SIGNALS:
0106 //  void signalItemAdded(ProgressItem* item);
0107 //  void signalItemProgress(ProgressItem* item);
0108 //  void signalItemDone(ProgressItem* item);
0109 //  void signalItemCancelled(ProgressItem* item);
0110   void signalTotalProgress(qulonglong progress);
0111 
0112 public Q_SLOTS:
0113   void slotCancelAll();
0114   void setProgress(QObject* owner, qulonglong steps);
0115   void setTotalSteps(QObject* owner, qulonglong steps);
0116   void setDone(QObject* owner);
0117 
0118 private Q_SLOTS:
0119   void slotItemDone(Tellico::ProgressItem* item);
0120   void slotUpdateTotalProgress();
0121 
0122 private:
0123   ProgressManager();
0124   ProgressManager(const ProgressManager&); // no copies
0125 
0126   ProgressItem& newProgressItemImpl(QObject* owner, const QString& label, bool canCancel);
0127   void setDone(ProgressItem* item);
0128 
0129   typedef QHash<QPointer<QObject>, QPointer<ProgressItem> > ProgressMap;
0130   ProgressMap m_items;
0131 
0132   static ProgressManager* s_self;
0133 };
0134 
0135 } // end namespace
0136 
0137 #endif