QGpCoreTools/AbstractProgress.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2008-02-20
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef ABSTRACTPROGRESS_H
00029 #define ABSTRACTPROGRESS_H
00030 
00031 #include <time.h>
00032 
00033 #include "QGpCoreToolsDLLExport.h"
00034 #include "Trace.h"
00035 
00036 namespace QGpCoreTools {
00037 
00038 class QGPCORETOOLS_EXPORT AbstractProgress
00039 {
00040 public:
00041   AbstractProgress();
00042   virtual ~AbstractProgress() {}
00043 
00044   virtual void setMaximum(int val);
00045   int maximum() const;
00046 
00047   virtual void setCaption(QString c);
00048   const QString& caption() const;
00049 
00050   inline void setValue(int val);
00051   inline void increaseValue(int val=1);
00052   int value() const {return _value+_deltaValue;}
00053 protected:
00054   virtual void paint(QString caption, int value, int maximum)=0;
00055   QString valueString(int value, int maximum);
00056 
00057   mutable QMutex _mutex;
00058   QString _caption;
00059   int _value;
00060   int _maximum;
00061 private:
00062   inline void show();
00063   void setValueInternal(int val);
00064   void increaseValueInternal();
00065 
00066   QAtomicInt _lastTime;
00067   int _deltaValue;
00068 };
00069 
00070 inline void AbstractProgress::increaseValue(int val)
00071 {
00072   TRACE;
00073   _deltaValue+=val; // Several increments may stack up if they arrive too fast.
00074   int now=time(0);
00075   if(_lastTime.fetchAndStoreOrdered(now)!=now) {
00076     increaseValueInternal();
00077     _deltaValue=0;
00078   }
00079 }
00080 
00081 inline void AbstractProgress::setValue(int val)
00082 {
00083   TRACE;
00084   int now=time(0);
00085   if(_lastTime.fetchAndStoreOrdered(now)!=now || val==_maximum) {
00086     setValueInternal(val);
00087   }
00088 }
00089 
00090 } // namespace QGpCoreTools
00091 
00092 #endif // ABSTRACTPROGRESS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines