SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
qtOperationLauncher.h
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 
11 #ifndef smtk_extension_qtOperationLauncher_h
12 #define smtk_extension_qtOperationLauncher_h
13 
14 #include "smtk/extension/qt/Exports.h"
15 
16 #include "smtk/attribute/Attribute.h"
17 #include "smtk/attribute/Resource.h"
18 
19 #include "smtk/common/ThreadPool.h"
20 
21 #include "smtk/operation/Operation.h"
22 
23 #include <QObject>
24 #include <QString>
25 
26 #include <future>
27 
28 namespace smtk
29 {
30 namespace extension
31 {
32 
33 class Launcher;
34 
37 class SMTKQTEXT_EXPORT ResultHandler : public QObject
38 {
39  Q_OBJECT
40 
41 public:
42  explicit ResultHandler();
43  smtk::operation::Operation::Result waitForResult();
44  std::shared_future<smtk::operation::Operation::Result>& future() { return m_future; }
45 
46 Q_SIGNALS:
49  void resultReady(smtk::operation::Operation::Result result);
50 
51 private:
52  friend class qtOperationLauncher;
53  friend class Launcher;
54  std::shared_future<smtk::operation::Operation::Result> m_future;
55 };
56 
63 class SMTKQTEXT_EXPORT qtOperationLauncher : public QObject
64 {
65  Q_OBJECT
66 
67 public:
68  static constexpr const char* const type_name = "smtk::extension::qtOperationLauncher";
69 
73  std::shared_ptr<ResultHandler> operator()(const smtk::operation::Operation::Ptr& operation);
74 
76  int activeOperations() const { return m_operationCount; }
77 
86  static bool setBusyCursorBehavior(bool enabled);
87 
88 Q_SIGNALS:
91  void operationHasResult(QString parametersName, QString resultName, QPrivateSignal);
92 
93 private:
95  smtk::operation::Operation::Result run(smtk::operation::Operation::Ptr operation);
96 
98  void decrementCount(smtk::operation::Operation::Result result);
99 
101 
107  std::atomic<int> m_operationCount{ 0 };
109  static bool s_busyCursorEnabled;
110 };
111 
112 namespace qt
113 {
118 class Launcher
119 {
120 public:
121  Launcher()
122  : m_launcher(new qtOperationLauncher)
123  {
124  }
125 
126  Launcher(const Launcher&)
127  : m_launcher(new qtOperationLauncher)
128  {
129  }
130 
131  Launcher(Launcher&& other) noexcept
132  : m_launcher{ other.m_launcher }
133  {
134  other.m_launcher = nullptr;
135  }
136 
137  Launcher& operator=(Launcher&& other) noexcept
138  {
139  if (&other != this)
140  {
141  delete m_launcher;
142  m_launcher = other.m_launcher;
143  other.m_launcher = nullptr;
144  }
145  return *this;
146  }
147 
148  ~Launcher() { delete m_launcher; }
149 
150  std::shared_future<smtk::operation::Operation::Result> operator()(
151  const smtk::operation::Operation::Ptr& operation)
152  {
153  auto resHandler = (*m_launcher)(operation);
154  return resHandler->future();
155  }
156 
157  qtOperationLauncher* get() const { return m_launcher; }
158 
159 private:
160  qtOperationLauncher* m_launcher;
161 };
162 } // namespace qt
163 } // namespace extension
164 } // namespace smtk
165 
166 #endif
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::extension::qtOperationLauncher::activeOperations
int activeOperations() const
Return the number of operations launched whose results have not been processed yet.
Definition: qtOperationLauncher.h:76
smtk::extension::qtOperationLauncher
An operation launcher that emits a signal containing the operation's result after the operation has s...
Definition: qtOperationLauncher.h:63
smtk::extension::ResultHandler
This class will be responsible for notifying the original caller of an operation that the result from...
Definition: qtOperationLauncher.h:37
smtk::common::ThreadPool< smtk::operation::Operation::Result >
smtk::extension::qt::Launcher
qtOperationLauncher has all of the Qt-integrated functionality we require; smtk's operation launchers...
Definition: qtOperationLauncher.h:118