Debian Codegraph @

src/Base/Sequencer.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25
26 #ifndef BASE_SEQUENCER_H
27 #define BASE_SEQUENCER_H
28
29 #include <cstddef>
30
31 #include <FCGlobal.h>
32
33 namespace Base
34 {
35
36 class AbortException;
37 class SequencerLauncher;
38
39 /**
40 * \brief This class gives the user an indication of the progress of an operation and
41 * it is used to reassure him that the application is still running.
42 *
43 * Here are some code snippets of how to use the sequencer:
44 * \code
45 *
46 * #include <Base/Sequencer.h>
47 *
48 * //first example
49 * Base::SequencerLauncher seq("my text", 10)
50 * for (int i=0; i<10; i++)
51 * {
52 * // do something
53 * seq.next ();
54 * }
55 *
56 * //second example
57 * Base::SequencerLauncher seq("my text", 10)
58 * do
59 * {
60 * // do something
61 * }
62 * while (seq.next());
63 *
64 * \endcode
65 *
66 * The implementation of this class also supports several nested instances
67 * at a time. But note, that only the first instance has an effect. Any further
68 * sequencer instance doesn't influence the total number of iteration steps. This
69 * is simply because it's impossible to get the exact number of iteration steps
70 * for nested instances and thus we have either too few steps estimated then the
71 * sequencer may indicate 100% but the algorithm still running or we have too many
72 * steps estimated so that the an algorithm may stop long before the sequencer
73 * reaches 100%.
74 *
75 * \code
76 * try {
77 * //start the first operation
78 * Base::SequencerLauncher seq1("my text", 10)
79 * for (int i=0; i<10, i++)
80 * {
81 * // do something
82 *
83 * // start the second operation while the first one is still running
84 * Base::SequencerLauncher seq2("another text", 10);
85 * for (int j=0; j<10; j++)
86 * {
87 * // do something different
88 * seq2.next ();
89 * }
90 *
91 * seq1.next ( true ); // allow one to cancel
92 * }
93 * }
94 * catch(const Base::AbortException&){
95 * // cleanup your data if needed
96 * }
97 *
98 * \endcode
99 *
100 * \note If using the sequencer with SequencerLauncher.next(\a true) then you must
101 * take into account that the exception \a AbortException could be thrown, e.g. in
102 * case the ESC button was pressed. So in this case it's always a good idea to use
103 * the sequencer within a try-catch block.
104 *
105 * \note Instances of SequencerLauncher should always be created on the stack.
106 * This is because if an exception somewhere is thrown the destructor is auto-
107 * matically called to clean-up internal data.
108 *
109 * \note It's not supported to create an instance of SequencerBase or a sub-class
110 * in another thread than the main thread. But you can create SequencerLauncher
111 * instances in other threads.
112 *
113 * \author Werner Mayer
114 */
115 class BaseExport SequencerBase
116 {
117 friend class SequencerLauncher;
118
119 public:
120 /**
121 * Returns the last created sequencer instance.
122 * If you create an instance of a class inheriting SequencerBase
123 * this object is retrieved instead.
124 *
125 * This mechanism is very useful to have an own sequencer for each layer of FreeCAD.
126 * For example, if FreeCAD is running in server mode you have/need no GUI layer
127 * and therewith no (graphical) progress bar; in this case ConsoleSequencer is taken.
128 * But in cases FreeCAD is running with GUI the @ref Gui::ProgressBar is taken instead.
129 * @see Sequencer
130 */
131 static SequencerBase& Instance();
132 /** Destruction */
133 virtual ~SequencerBase();
134 /**
135 * Returns true if the running sequencer is blocking any user input.
136 * This might be only of interest of the GUI where the progress bar or dialog
137 * is used from a thread. If started from a thread this method should return
138 * false, otherwise true. The default implementation always returns true.
139 */
140 virtual bool isBlocking() const;
141 /** If \a bLock is true then the sequencer gets locked. startStep() and nextStep()
142 * don't get invoked any more until the sequencer gets unlocked again.
143 * This method returns the previous lock state.
144 */
145 bool setLocked(bool bLock);
146 /** Returns true if the sequencer was locked, false otherwise. */
147 bool isLocked() const;
148 /** Returns true if the sequencer is running, otherwise returns false. */
149 bool isRunning() const;
150 /**
151 * Returns true if the pending operation was canceled.
152 */
153 bool wasCanceled() const;
154
155 /// Check if the operation is aborted by user
156 virtual void checkAbort()
157 {}
158
159 protected:
160 /**
161 * Starts a new operation, returns false if there is already a pending operation,
162 * otherwise it returns true.
163 * In this method startStep() gets invoked that can be reimplemented in sub-classes.
164 */
165 bool start(const char* pszStr, size_t steps);
166 /** Returns the number of steps. */
167 size_t numberOfSteps() const;
168 /** Returns the current state of progress in percent. */
169 int progressInPercent() const;
170 /**
171 * Performs the next step and returns true if the operation is not yet finished.
172 * But note, when 0 was passed to start() as the number of total steps this method
173 * always returns false.
174 *
175 * In this method nextStep() gets invoked that can be reimplemented in sub-classes.
176 * If \a canAbort is true then the operations can be aborted, otherwise (the default)
177 * the operation cannot be aborted. In case it gets aborted an exception AbortException
178 * is thrown.
179 */
180 bool next(bool canAbort = false);
181 /**
182 * Stops the sequencer if all operations are finished. It returns false if
183 * there are still pending operations, otherwise it returns true.
184 */
185 bool stop();
186 /**
187 * Breaks the sequencer if needed. The default implementation does nothing.
188 * Every pause() must eventually be followed by a corresponding @ref resume().
189 * @see Gui::ProgressBar.
190 */
191 virtual void pause();
192 /**
193 * Continues with progress. The default implementation does nothing.
194 * @see pause(), @see Gui::ProgressBar.
195 */
196 virtual void resume();
197 /**
198 * Try to cancel the pending operation(s).
199 * E.g. @ref Gui::ProgressBar calls this method after the ESC button was pressed.
200 */
201 void tryToCancel();
202 /**
203 * If you tried to cancel but then decided to continue the operation.
204 * E.g. in @ref Gui::ProgressBar a dialog appears asking if you really want to
205 * cancel. If you decide to continue this method must be called.
206 */
207 void rejectCancel();
208
209 protected:
210 /** construction */
211 SequencerBase();
212 SequencerBase(const SequencerBase&) = default;
213 SequencerBase(SequencerBase&&) = default;
214 SequencerBase& operator=(const SequencerBase&) = default;
215 SequencerBase& operator=(SequencerBase&&) = default;
216 /**
217 * Sets a text what the pending operation is doing. The default implementation
218 * does nothing.
219 */
220 virtual void setText(const char* pszTxt);
221 /**
222 * This method can be reimplemented in sub-classes to give the user a feedback
223 * when a new sequence starts. The default implementation does nothing.
224 */
225 virtual void startStep();
226 /**
227 * This method can be reimplemented in sub-classes to give the user a feedback
228 * when the next is performed. The default implementation does nothing. If \a canAbort
229 * is true then the pending operation can aborted, otherwise not. Depending on the
230 * re-implementation this method can throw an AbortException if canAbort is true.
231 */
232 virtual void nextStep(bool canAbort);
233 /**
234 * Sets the progress indicator to a certain position.
235 */
236 virtual void setProgress(size_t);
237 /**
238 * Resets internal data.
239 * If you want to reimplement this method, it is very important to call it in
240 * the re-implemented method.
241 */
242 virtual void resetData();
243
244 protected:
245 // NOLINTBEGIN
246 size_t nProgress {0}; /**< Stores the current amount of progress.*/
247 size_t nTotalSteps {0}; /**< Stores the total number of steps */
248 // NOLINTEND
249
250 private:
251 bool _bLocked {false}; /**< Lock/unlock sequencer. */
252 bool _bCanceled {false}; /**< Is set to true if the last pending operation was canceled */
253 int _nLastPercentage {-1}; /**< Progress in percent. */
254 };
255
256 /** This special sequencer might be useful if you want to suppress any indication
257 * of the progress to the user.
258 */
259 class BaseExport EmptySequencer: public Base::SequencerBase
260 {
261 public:
262 /** construction */
263 EmptySequencer() = default;
264 };
265
266 /**
267 * \brief This class writes the progress to the console window.
268 */
269 class BaseExport ConsoleSequencer: public SequencerBase
270 {
271 public:
272 /** construction */
273 ConsoleSequencer() = default;
274
275 protected:
276 /** Starts the sequencer */
277 void startStep() override;
278 /** Writes the current progress to the console window. */
279 void nextStep(bool canAbort) override;
280
281 private:
282 /** Puts text to the console window */
283 void setText(const char* pszTxt) override;
284 /** Resets the sequencer */
285 void resetData() override;
286 };
287
288 /** The SequencerLauncher class is provided for convenience. It allows you to run an instance of the
289 * sequencer by instantiating an object of this class -- most suitable on the stack. So this
290 * mechanism can be used for try-catch-blocks to destroy the object automatically if the C++
291 * exception mechanism cleans up the stack.
292 *
293 * This class has been introduced to simplify the use with the sequencer. In the FreeCAD Gui layer
294 * there is a subclass of SequencerBase called ProgressBar that grabs the keyboard and filters most
295 * of the incoming events. If the programmer uses the API of SequencerBase directly to start an
296 * instance without due diligence with exceptions then a not handled exception could block the whole
297 * application -- the user has to kill the application then.
298 *
299 * Below is an example of a not correctly used sequencer.
300 *
301 * \code
302 *
303 * #include <Base/Sequencer.h>
304 *
305 * void runOperation();
306 *
307 * void myTest()
308 * {
309 * try{
310 * runOperation();
311 * } catch(...) {
312 * // the programmer forgot to stop the sequencer here
313 * // Under circumstances the sequencer never gets stopped so the keyboard never gets
314 * ungrabbed and
315 * // all Gui events still gets filtered.
316 * }
317 * }
318 *
319 * void runOperation()
320 * {
321 * Base::Sequencer().start ("my text", 10);
322 *
323 * for (int i=0; i<10; i++)
324 * {
325 * // do something where an exception be thrown
326 * ...
327 * Base::Sequencer().next ();
328 * }
329 *
330 * Base::Sequencer().stop ();
331 * }
332 *
333 * \endcode
334 *
335 * To avoid such problems the SequencerLauncher class can be used as follows:
336 *
337 * \code
338 *
339 * #include <Base/Sequencer.h>
340 *
341 * void runOperation();
342 *
343 * void myTest()
344 * {
345 * try{
346 * runOperation();
347 * } catch(...) {
348 * // the programmer forgot to halt the sequencer here
349 * // If SequencerLauncher leaves its scope the object gets destructed automatically and
350 * // stops the running sequencer.
351 * }
352 * }
353 *
354 * void runOperation()
355 * {
356 * // create an instance on the stack (not on any terms on the heap)
357 * SequencerLauncher seq("my text", 10);
358 *
359 * for (int i=0; i<10; i++)
360 * {
361 * // do something (e.g. here can be thrown an exception)
362 * ...
363 * seq.next ();
364 * }
365 * }
366 *
367 * \endcode
368 *
369 * @author Werner Mayer
370 */
371 class BaseExport SequencerLauncher
372 {
373 public:
374 SequencerLauncher(const char* pszStr, size_t steps);
375 ~SequencerLauncher();
376 size_t numberOfSteps() const;
377 void setText(const char* pszTxt);
378 bool next(bool canAbort = false);
379 void setProgress(size_t);
380 bool wasCanceled() const;
381
382 SequencerLauncher(const SequencerLauncher&) = delete;
383 SequencerLauncher(SequencerLauncher&&) = delete;
384 void operator=(const SequencerLauncher&) = delete;
385 void operator=(SequencerLauncher&&) = delete;
386 };
387
388 /** Access to the only SequencerBase instance */
389 inline SequencerBase& Sequencer()
390 {
391 return SequencerBase::Instance();
392 }
393
394 } // namespace Base
395
396 #endif // BASE_SEQUENCER_H