digiKam
workerobject.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2010-04-06
7  * Description : Multithreaded worker object
8  *
9  * Copyright (C) 2010-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
10  * Copyright (C) 2012-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  *
12  * This program is free software; you can redistribute it
13  * and/or modify it under the terms of the GNU General
14  * Public License as published by the Free Software Foundation;
15  * either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * ============================================================ */
24 
25 #ifndef DIGIKAM_WORKER_OBJECT_H
26 #define DIGIKAM_WORKER_OBJECT_H
27 
28 // Qt includes
29 
30 #include <QObject>
31 #include <QThread>
32 
33 // Local includes
34 
35 #include "digikam_export.h"
36 
37 class QEventLoop;
38 
39 namespace Digikam
40 {
41 
42 class WorkerObjectRunnable;
43 
44 class DIGIKAM_EXPORT WorkerObject : public QObject
45 {
46  Q_OBJECT
47 
48 public:
49 
50  enum State
51  {
55  Deactivating
56  };
57 
59  {
62  PhaseOut
63  };
64 
65 public:
66 
79  explicit WorkerObject();
80  ~WorkerObject() override;
81 
82  State state() const;
83 
84  void wait();
85 
93  void setPriority(QThread::Priority priority);
94  QThread::Priority priority() const;
95 
102  bool connectAndSchedule(const QObject* sender,
103  const char* signal,
104  const char* method,
105  Qt::ConnectionType type = Qt::AutoConnection) const;
106 
107  static bool connectAndSchedule(const QObject* sender,
108  const char* signal,
109  const WorkerObject* receiver,
110  const char* method,
111  Qt::ConnectionType type = Qt::AutoConnection);
112 
113  static bool disconnectAndSchedule(const QObject* sender,
114  const char* signal,
115  const WorkerObject* receiver,
116  const char* method);
117 
118 public Q_SLOTS:
119 
125  void schedule();
126 
135  void deactivate(DeactivatingMode mode = FlushSignals);
136 
137 Q_SIGNALS:
138 
139  void started();
140  void finished();
141 
142 protected:
143 
144  bool transitionToRunning();
145  void transitionToInactive();
146 
147  void run();
148  void setEventLoop(QEventLoop* loop);
149  void addRunnable(WorkerObjectRunnable* loop);
150  void removeRunnable(WorkerObjectRunnable* loop);
151 
166  void shutDown();
167 
173  virtual void aboutToQuitLoop();
174 
182  virtual void aboutToDeactivate();
183 
184  bool event(QEvent* e) override;
185 
186 private:
187 
188  friend class WorkerObjectRunnable;
189  friend class ThreadManager;
190 
191 private:
192 
193  // Disable.
194  WorkerObject(const WorkerObject&) = delete;
195  WorkerObject& operator=(const WorkerObject&) = delete;
196  WorkerObject(QObject*) = delete;
197 
198 private:
199 
200  class Private;
201  Private* const d;
202 };
203 
204 } // namespace Digikam
205 
206 #endif // DIGIKAM_WORKER_OBJECT_H
Definition: threadmanager.h:43
Definition: workerobject.h:45
DeactivatingMode
Definition: workerobject.h:59
@ FlushSignals
Already sent signals are cleared.
Definition: workerobject.h:60
@ KeepSignals
The thread is stopped, but already sent signals remain in the queue.
Definition: workerobject.h:61
State
Definition: workerobject.h:51
@ Scheduled
Definition: workerobject.h:53
@ Inactive
Definition: workerobject.h:52
@ Running
Definition: workerobject.h:54
Definition: datefolderview.cpp:43
Definition: abstractalbumtreeview_p.h:85