itom  4.1.0
functionCancellationAndObserver.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom and its software development toolkit (SDK).
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  In addition, as a special exception, the Institut fuer Technische
15  Optik (ITO) gives you certain additional rights.
16  These rights are described in the ITO LGPL Exception version 1.0,
17  which can be found in the file LGPL_EXCEPTION.txt in this package.
18 
19  itom is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
22  General Public Licence for more details.
23 
24  You should have received a copy of the GNU Library General Public License
25  along with itom. If not, see <http://www.gnu.org/licenses/>.
26 *********************************************************************** */
27 
28 #ifndef FUNCCANCELLATIONANDOBSERVER_H
29 #define FUNCCANCELLATIONANDOBSERVER_H
30 
31 #include "commonGlobal.h"
32 #include <qscopedpointer.h>
33 #include <qobject.h>
34 
35 
36 
37 #if !defined(Q_MOC_RUN) || defined(ITOMCOMMONQT_MOC) //only moc this file in itomCommonQtLib but not in other libraries or executables linking against this itomCommonQtLib
38 
39 namespace ito
40 {
41 
42 class FunctionCancellationAndObserverPrivate; //forward declaration
43 
56 class ITOMCOMMONQT_EXPORT FunctionCancellationAndObserver : public QObject
57 {
58  Q_OBJECT
59 
60 public:
61  enum CancellationReason
62  {
63  ReasonGeneral = 1,
64  ReasonKeyboardInterrupt = 2 //if the python script execution has been terminated
65  };
66 
68 
76  FunctionCancellationAndObserver(int progressMinimum = 0, int progressMaximum = 100, QObject *parent = NULL);
77 
79  /*
80  \sa ItomSharedSemaphore::deleteSemaphore
81  */
83 
85  bool isCancelled() const;
86 
88  CancellationReason cancellationReason();
89 
91  int progressMinimum() const;
92 
94  int progressMaximum() const;
95 
97  /*
98  This method emits the progressValueChanged signal. It can for instance be connected
99  with a 'setValue' slot of a QProgressBar.
100 
101  The value will be clipped to progressMinimum and progressMaximum.
102  */
103  void setProgressValue(int value);
104 
106  int progressValue() const;
107 
109  /*
110  This method emits the progressTextChanged signal. It can for instance be connected
111  with a 'setText' slot of a QLabel.
112 
113  The text should inform about the step, the long-running method is currently executing.
114  */
115  void setProgressText(const QString &text);
116 
118  QString progressText();
119 
120 Q_SIGNALS:
121  void progressTextChanged(QString text);
122  void progressValueChanged(int value);
123  void cancellationRequested();
124  void resetDone();
125 
126 public Q_SLOTS:
128  /*
129  It is the responsibility of the called function itself to regulariy check the isCancelled() method
130  and stop the function call as soon as possible.
131 
132  The reason ReasonKeyboardInterrupt should only be set by the method PythonEngine::pythonInterruptExecutionThreadSafe
133  to avoid nested errors. In this case, itom.filter will not set an exception, since the keyboardInterrupt exception
134  is automatically raised.
135 
136  Emits the cancellationRequested signal.
137  */
138  void requestCancellation(CancellationReason reason = CancellationReason::ReasonGeneral);
139 
141  /*
142  Emits progressTextChanged and progressValueChanged with the new values and then the resetDone signal.
143  */
144  void reset();
145 
146 private:
147  Q_DISABLE_COPY(FunctionCancellationAndObserver)
148 
149  QScopedPointer<FunctionCancellationAndObserverPrivate> d_ptr;
150  Q_DECLARE_PRIVATE(FunctionCancellationAndObserver);
151 };
152 
153 } //end namespace ito
154 
155 #endif //#if !defined(Q_MOC_RUN) || defined(ITOMCOMMONQT_MOC)
156 
157 #endif
Definition: apiFunctionsGraph.cpp:39
This class can be passed to a long running method (e.g. as QSharedPointer instance) for two reasons: ...
Definition: functionCancellationAndObserver.h:56