| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef GUI_PROGRESSBAR_H |
| 24 |
#define GUI_PROGRESSBAR_H |
| 25 |
|
| 26 |
#include <QProgressBar> |
| 27 |
#ifdef QT_WINEXTRAS_LIB |
| 28 |
# include <QWinTaskbarButton> |
| 29 |
# include <QWinTaskbarProgress> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <Base/Sequencer.h> |
| 33 |
|
| 34 |
|
| 35 |
namespace Gui |
| 36 |
{ |
| 37 |
|
| 38 |
struct SequencerBarPrivate; |
| 39 |
struct ProgressBarPrivate; |
| 40 |
class ProgressBar; |
| 41 |
|
| 42 |
/** |
| 43 |
* FreeCAD's progress bar for long operations |
| 44 |
* To see how to use the progress bar have a look at the following examples: |
| 45 |
* |
| 46 |
* \code |
| 47 |
* unsigned long steps = ... |
| 48 |
* Base::SequencerLauncher seq("Starting progress bar", 0); |
| 49 |
* |
| 50 |
* for (unsigned long i=0; i<steps;i++) |
| 51 |
* { |
| 52 |
* seq.next(); |
| 53 |
* // do one step of your algorithm |
| 54 |
* } |
| 55 |
* |
| 56 |
* \endcode |
| 57 |
* |
| 58 |
* The example below shows how to use two nested progresses. |
| 59 |
* |
| 60 |
* \code |
| 61 |
* void function1() |
| 62 |
* { |
| 63 |
* unsigned long steps = ... |
| 64 |
* Base::SequencerLauncger seq("Starting progress bar", 0); |
| 65 |
* |
| 66 |
* for (unsigned long i=0; i<steps;i++) |
| 67 |
* { |
| 68 |
* seq.next(); |
| 69 |
* // do one step of your algorithm |
| 70 |
* } |
| 71 |
* |
| 72 |
* } |
| 73 |
* |
| 74 |
* void function2() |
| 75 |
* { |
| 76 |
* unsigned long steps = ... |
| 77 |
* Base::SequencerLauncher seq("Starting progress bar", 0); |
| 78 |
* |
| 79 |
* for (unsigned long i=0; i<steps;i++) |
| 80 |
* { |
| 81 |
* seq.next(); |
| 82 |
* // do one step of your algorithm calling function1 |
| 83 |
* function1(); |
| 84 |
* } |
| 85 |
* } |
| 86 |
* |
| 87 |
* \endcode |
| 88 |
* |
| 89 |
* If the total number of steps cannot be determined before, use 0 instead of to show |
| 90 |
* just a busy indicator instead of percentage steps. |
| 91 |
* @author Werner Mayer |
| 92 |
*/ |
| 93 |
class GuiExport SequencerBar: public Base::SequencerBase |
| 94 |
{ |
| 95 |
public: |
| 96 |
/** Returns the sequencer object. */ |
| 97 |
static SequencerBar* instance(); |
| 98 |
/** This restores the last overridden cursor and release the keyboard while the progress bar |
| 99 |
* is running. This is useful e.g. if a modal dialog appears while a long operation is performed |
| 100 |
* to indicate that the user can click on the dialog. Every pause() must eventually be followed |
| 101 |
* by a corresponding @ref resume(). |
| 102 |
*/ |
| 103 |
void pause() override; |
| 104 |
/** This sets the wait cursor again and grabs the keyboard. @see pause() */ |
| 105 |
void resume() override; |
| 106 |
bool isBlocking() const override; |
| 107 |
/** Returns an instance of the progress bar. It creates one if needed. */ |
| 108 |
QProgressBar* getProgressBar(QWidget* parent = nullptr); |
| 109 |
|
| 110 |
void checkAbort() override; |
| 111 |
|
| 112 |
protected: |
| 113 |
/** Construction */ |
| 114 |
SequencerBar(); |
| 115 |
/** Destruction */ |
| 116 |
~SequencerBar() override; |
| 117 |
|
| 118 |
/** Puts text to the status bar */ |
| 119 |
void setText(const char* pszTxt) override; |
| 120 |
/** Starts the progress bar */ |
| 121 |
void startStep() override; |
| 122 |
/** Increase the progress bar. */ |
| 123 |
void nextStep(bool canAbort) override; |
| 124 |
/** Sets the progress indicator to a certain position. */ |
| 125 |
void setProgress(size_t) override; |
| 126 |
/** Resets the sequencer */ |
| 127 |
void resetData() override; |
| 128 |
void showRemainingTime(); |
| 129 |
|
| 130 |
private: |
| 131 |
/** @name for internal use only */ |
| 132 |
//@{ |
| 133 |
void setValue(int step); |
| 134 |
/** Throws an exception to stop the pending operation. */ |
| 135 |
void abort(); |
| 136 |
//@} |
| 137 |
SequencerBarPrivate* d; |
| 138 |
static SequencerBar* _pclSingleton; |
| 139 |
|
| 140 |
friend class ProgressBar; |
| 141 |
}; |
| 142 |
|
| 143 |
class ProgressBar: public QProgressBar |
| 144 |
{ |
| 145 |
Q_OBJECT |
| 146 |
|
| 147 |
public: |
| 148 |
/** Construction */ |
| 149 |
ProgressBar(SequencerBar* s, QWidget* parent = nullptr); |
| 150 |
/** Destruction */ |
| 151 |
~ProgressBar() override; |
| 152 |
|
| 153 |
/** Handles all incoming events while the progress bar is running. All key and mouse |
| 154 |
* events are ignored to block user input. |
| 155 |
*/ |
| 156 |
bool eventFilter(QObject* o, QEvent* e) override; |
| 157 |
/** Returns the time in milliseconds that must pass before the progress bar appears. |
| 158 |
*/ |
| 159 |
int minimumDuration() const; |
| 160 |
|
| 161 |
private Q_SLOTS: |
| 162 |
void resetEx(); |
| 163 |
void setRangeEx(int minimum, int maximum); |
| 164 |
void setValueEx(int value); |
| 165 |
|
| 166 |
|
| 167 |
public Q_SLOTS: |
| 168 |
/** Sets the time that must pass before the progress bar appears to \a ms. |
| 169 |
*/ |
| 170 |
void setMinimumDuration(int ms); |
| 171 |
|
| 172 |
public: |
| 173 |
bool canAbort() const; |
| 174 |
|
| 175 |
protected: |
| 176 |
void showEvent(QShowEvent*) override; |
| 177 |
void hideEvent(QHideEvent*) override; |
| 178 |
|
| 179 |
protected Q_SLOTS: |
| 180 |
/* Shows the progress bar if it is still hidden after the operation has been started |
| 181 |
* and minimumDuration milliseconds have passed. |
| 182 |
*/ |
| 183 |
void delayedShow(); |
| 184 |
void aboutToShow(); |
| 185 |
void aboutToHide(); |
| 186 |
|
| 187 |
private: |
| 188 |
/** @name for internal use only */ |
| 189 |
//@{ |
| 190 |
void resetObserveEventFilter(); |
| 191 |
/** Gets the events under control */ |
| 192 |
void enterControlEvents(bool); |
| 193 |
/** Loses the control over incoming events*/ |
| 194 |
void leaveControlEvents(bool); |
| 195 |
|
| 196 |
|
| 197 |
//@} |
| 198 |
ProgressBarPrivate* d; |
| 199 |
SequencerBar* sequencer; |
| 200 |
|
| 201 |
#ifdef QT_WINEXTRAS_LIB |
| 202 |
/* Set up the taskbar progress in windows */ |
| 203 |
void setupTaskBarProgress(void); |
| 204 |
QWinTaskbarProgress* m_taskbarProgress; |
| 205 |
QWinTaskbarButton* m_taskbarButton; |
| 206 |
#endif |
| 207 |
friend class SequencerBar; |
| 208 |
}; |
| 209 |
|
| 210 |
} // namespace Gui |
| 211 |
|
| 212 |
#endif // GUI_PROGRESSBAR_H |