itom  4.1.0
delayJobRunner.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.
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  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 
22  Further hints:
23  ------------------------
24 
25  This file belongs to the code editor of itom. The code editor is
26  in major parts a fork / rewritten version of the python-based source
27  code editor PyQode from Colin Duquesnoy and others
28  (see https://github.com/pyQode). PyQode itself is licensed under
29  the MIT License (MIT).
30 
31  Some parts of the code editor of itom are also inspired by the
32  source code editor of the Spyder IDE (https://github.com/spyder-ide),
33  also licensed under the MIT License and developed by the Spyder Project
34  Contributors.
35 
36 *********************************************************************** */
37 
38 #ifndef DELAYJOBRUNNER_H
39 #define DELAYJOBRUNNER_H
40 
41 #include <qobject.h>
42 #include <qvariant.h>
43 #include <qtimer.h>
44 #include <qtextobject.h>
45 
46 #define DELAY_JOB_RUNNER(base,T1,T2) ((DelayJobRunner<T1,T2>*)(base))
47 #define DELAY_JOB_RUNNER_ARGTEXTBLOCK(base,T1,T2) ((DelayJobRunnerArgTextBlock<T1,T2>*)(base))
48 #define DELAY_JOB_RUNNER_ARGTEXTCURSOR(base,T1,T2) ((DelayJobRunnerArgTextCursor<T1,T2>*)(base))
49 #define DELAY_JOB_RUNNER_NOARGS(base,T1,T2) ((DelayJobRunnerNoArgs<T1,T2>*)(base))
50 #define DELAY_JOB_RUNNER_GENERICARG(base,T1,T2,T3) ((DelayJobRunnerGenericArg<T1,T2,T3>*)(base))
51 
52 namespace ito {
53 
54 class DelayJobRunnerBase : public QObject
55 {
56  Q_OBJECT
57 public:
58  DelayJobRunnerBase(int delay = 500, QObject *parent = NULL) : QObject(parent), m_delay(delay)
59  {
60  connect(&m_timer, SIGNAL(timeout()), this, SLOT(execRequestedJob()));
61  }
62  virtual ~DelayJobRunnerBase() {}
63 
64  virtual void cancelRequests() = 0;
65 
66  int delay() const { return m_delay; }
67 
68  void setDelay(int delay) { m_delay = delay; }
69 
70 protected:
71  QTimer m_timer;
72  int m_delay;
73 
74 
75 private slots:
76  virtual void execRequestedJob() = 0;
77 };
78 
79 /*
80 Utility class for running job after a certain delay. If a new request is
81 made during this delay, the previous request is dropped and the timer is
82 restarted for the new request.
83 We use this to implement a cooldown effect that prevents jobs from being
84 executed while the IDE is not idle.
85 A job is a simple callable.
86 */
87 template <typename OBJECT, typename FUNC>
89 {
90 
91 
92 public:
93  //-------------------------------------------
94  /*
95  :param delay: Delay to wait before running the job. This delay applies
96  to all requests and cannot be changed afterwards.
97  */
98  DelayJobRunner(int delay = 500, QObject *parent = NULL) :
99  DelayJobRunnerBase(delay, parent),
100  m_func(NULL),
101  m_obj(NULL)
102  {
103  int i = 0;
104  }
105 
106  virtual ~DelayJobRunner() {}
107 
108  void requestJob(OBJECT* obj, FUNC f, const QList<QVariant> &args)
109  {
110  cancelRequests();
111  m_obj = obj;
112  m_func = f;
113  m_args = args;
114  m_timer.start(m_delay);
115  }
116 
117  virtual void cancelRequests()
118  {
119  m_timer.stop();
120  m_obj = NULL;
121  m_func = NULL;
122  m_args.clear();
123  }
124 
125 private:
126  OBJECT* m_obj;
127  FUNC m_func;
128  QList<QVariant> m_args;
129 
130  //-------------------------------------------
131  /*
132  Execute the requested job after the timer has timeout.
133  */
134  virtual void execRequestedJob()
135  {
136  m_timer.stop();
137  if (m_obj)
138  {
139  (m_obj->*m_func)(m_args);
140  }
141  }
142 };
143 
144 /*
145 Utility class for running job after a certain delay. If a new request is
146 made during this delay, the previous request is dropped and the timer is
147 restarted for the new request.
148 We use this to implement a cooldown effect that prevents jobs from being
149 executed while the IDE is not idle.
150 A job is a simple callable.
151 */
152 template <typename OBJECT, typename FUNC>
154 {
155 
156 
157 public:
158  //-------------------------------------------
159  /*
160  :param delay: Delay to wait before running the job. This delay applies
161  to all requests and cannot be changed afterwards.
162  */
163  DelayJobRunnerArgTextBlock(int delay = 500, QObject *parent = NULL) :
164  DelayJobRunnerBase(delay, parent),
165  m_func(NULL),
166  m_obj(NULL)
167  {
168  int i = 0;
169  }
170 
171  virtual ~DelayJobRunnerArgTextBlock() {}
172 
173  void requestJob(OBJECT* obj, FUNC f, const QTextBlock &block)
174  {
175  cancelRequests();
176  m_obj = obj;
177  m_func = f;
178  m_block = block;
179  m_timer.start(m_delay);
180  }
181 
182  virtual void cancelRequests()
183  {
184  m_timer.stop();
185  m_obj = NULL;
186  m_func = NULL;
187  m_block = QTextBlock();
188  }
189 
190 private:
191  OBJECT* m_obj;
192  FUNC m_func;
193  QTextBlock m_block;
194 
195  //-------------------------------------------
196  /*
197  Execute the requested job after the timer has timeout.
198  */
199  virtual void execRequestedJob()
200  {
201  m_timer.stop();
202  if (m_obj)
203  {
204  (m_obj->*m_func)(m_block);
205  }
206  }
207 };
208 
209 
210 
211 /*
212 Utility class for running job after a certain delay. If a new request is
213 made during this delay, the previous request is dropped and the timer is
214 restarted for the new request.
215 We use this to implement a cooldown effect that prevents jobs from being
216 executed while the IDE is not idle.
217 A job is a simple callable.
218 */
219 template <typename OBJECT, typename FUNC>
221 {
222 
223 
224 public:
225  //-------------------------------------------
226  /*
227  :param delay: Delay to wait before running the job. This delay applies
228  to all requests and cannot be changed afterwards.
229  */
230  DelayJobRunnerArgTextCursor(int delay = 500, QObject *parent = NULL) :
231  DelayJobRunnerBase(delay, parent),
232  m_func(NULL),
233  m_obj(NULL)
234  {
235  }
236 
237  virtual ~DelayJobRunnerArgTextCursor() {}
238 
239  void requestJob(OBJECT* obj, FUNC f, const QTextCursor &cursor)
240  {
241  cancelRequests();
242  m_obj = obj;
243  m_func = f;
244  m_cursor = cursor;
245  m_timer.start(m_delay);
246  }
247 
248  virtual void cancelRequests()
249  {
250  m_timer.stop();
251  m_obj = NULL;
252  m_func = NULL;
253  m_cursor = QTextCursor();
254  }
255 
256 private:
257  OBJECT* m_obj;
258  FUNC m_func;
259  QTextCursor m_cursor;
260 
261  //-------------------------------------------
262  /*
263  Execute the requested job after the timer has timeout.
264  */
265  virtual void execRequestedJob()
266  {
267  m_timer.stop();
268  if (m_obj)
269  {
270  (m_obj->*m_func)(m_cursor);
271  }
272  }
273 };
274 
275 
276 
277 /*
278 Utility class for running job after a certain delay. If a new request is
279 made during this delay, the previous request is dropped and the timer is
280 restarted for the new request.
281 We use this to implement a cooldown effect that prevents jobs from being
282 executed while the IDE is not idle.
283 A job is a simple callable.
284 */
285 template <typename OBJECT, typename FUNC, typename ARGTYPE>
287 {
288 
289 
290 public:
291  //-------------------------------------------
292  /*
293  :param delay: Delay to wait before running the job. This delay applies
294  to all requests and cannot be changed afterwards.
295  */
296  DelayJobRunnerGenericArg(int delay = 500, QObject *parent = NULL) :
297  DelayJobRunnerBase(delay, parent),
298  m_func(NULL),
299  m_obj(NULL)
300  {
301  int i = 0;
302  }
303 
304  virtual ~DelayJobRunnerGenericArg() {}
305 
306  void requestJob(OBJECT* obj, FUNC f, const ARGTYPE &arg)
307  {
308  cancelRequests();
309  m_obj = obj;
310  m_func = f;
311  m_arg = arg;
312  m_timer.start(m_delay);
313  }
314 
315  virtual void cancelRequests()
316  {
317  m_timer.stop();
318  m_obj = NULL;
319  m_func = NULL;
320  m_arg = ARGTYPE();
321  }
322 
323 private:
324  OBJECT* m_obj;
325  FUNC m_func;
326  ARGTYPE m_arg;
327 
328  //-------------------------------------------
329  /*
330  Execute the requested job after the timer has timeout.
331  */
332  virtual void execRequestedJob()
333  {
334  m_timer.stop();
335  if (m_obj)
336  {
337  (m_obj->*m_func)(m_arg);
338  }
339  }
340 };
341 
342 
343 
344 template <typename OBJECT, typename FUNC>
346 {
347 
348 
349 public:
350  //-------------------------------------------
351  /*
352  :param delay: Delay to wait before running the job. This delay applies
353  to all requests and cannot be changed afterwards.
354  */
355  DelayJobRunnerNoArgs(int delay = 500, QObject *parent = NULL) :
356  DelayJobRunnerBase(delay, parent),
357  m_func(NULL),
358  m_obj(NULL)
359  {
360  int i = 0;
361  }
362 
363  virtual ~DelayJobRunnerNoArgs() {}
364 
365  void requestJob(OBJECT* obj, FUNC f)
366  {
367  cancelRequests();
368  m_obj = obj;
369  m_func = f;
370  m_timer.start(m_delay);
371  }
372 
373  virtual void cancelRequests()
374  {
375  m_timer.stop();
376  m_obj = NULL;
377  m_func = NULL;
378  }
379 
380 private:
381  OBJECT* m_obj;
382  FUNC m_func;
383 
384  //-------------------------------------------
385  /*
386  Execute the requested job after the timer has timeout.
387  */
388  virtual void execRequestedJob()
389  {
390  m_timer.stop();
391  if (m_obj)
392  {
393  (m_obj->*m_func)();
394  }
395  }
396 };
397 
398 } //end namespace ito
399 
400 
401 #endif
DataObject arg(const DataObject &dObj)
high-level value which calculates the argument value of each element of the input source data object ...
Definition: dataobj.cpp:10131
Definition: delayJobRunner.h:88
Definition: delayJobRunner.h:286
Definition: apiFunctionsGraph.cpp:39
Definition: delayJobRunner.h:54
Definition: delayJobRunner.h:345
Definition: delayJobRunner.h:220
Definition: delayJobRunner.h:153