ArmarXWidgetController.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::Gui
17 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
18 * @date 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #pragma once
24 
25 #include <memory>
26 #include <mutex>
27 
28 #include <QPointer>
29 #include <QSettings>
30 #include <QWidget>
31 
32 #include <IceUtil/Shared.h>
33 
35 
36 
37 class SoNode;
38 class QDialog;
39 class QMainWindow;
40 
41 namespace armarx
42 {
43  class ArmarXWidgetController;
44  class TipDialog;
46 
47  /**
48  \class ArmarXWidgetController
49 
50  \ingroup ArmarXGuiBase
51  */
53  public QObject,
54  virtual public Logging,
55  virtual public IceUtil::Shared
56  {
57  Q_OBJECT
58  public:
59  explicit ArmarXWidgetController();
60  ~ArmarXWidgetController() override;
61 
62  /**
63  * @brief getWidget returns a pointer to the a widget of this
64  * controller.
65  *
66  * This widget should be the main widget of this WidgetController.
67  * The standard implementation of this function creates a QWidget on the
68  * first call and assigns the pointer to the widget-member and returns
69  * the member.
70  * @return pointer to the main widget.
71  *
72  * @note This function must <b>always, at any time</b> return a valid
73  * pointer. So if you reimplement it, you should create the widget in this function
74  * on the first call.
75  */
76  virtual QPointer<QWidget> getWidget();
77 
78  /**
79  * @brief getConfigDialog returns a pointer to the a configuration
80  * widget of this controller. It is optional.
81  *
82  * If you need your widget configured, before it is shown, you should
83  * implement this function and return a QDialog derived class.
84  * @param parent Pointer to the object, that should own this dialog.
85  * @return pointer to a QDialog derived class, that should be used to
86  * configure your widget.
87  *
88  * @note When the dialog gets accepted (emit SIGNAL accepted()), the function
89  * configured() is called, which must be implemented as well.
90  *
91  * @see configured(), getWidget()
92  */
93  virtual QPointer<QDialog> getConfigDialog(QWidget* parent = 0);
94 
95  /**
96  * @brief This function must be implemented by the user, if he supplies
97  * a config dialog. This function is then automatically called, when
98  * the dialog was accepted.
99  *
100  * @see getConfigDialog()
101  */
102  virtual void
104  {
105  }
106 
107  /**
108  * @brief getTitleToolbar returns a pointer to the a toolbar
109  * widget of this controller. It is optional. It must return the same instance
110  * on all calls.
111  *
112  * The returned toolbar is displayed in the titlebar of the parent dockwidget.
113  * Use this to show button that should always be visible in the widget, but should
114  * not take up space in the widget. Keep the size of the toolbar to a minimum, so that
115  * it is not wider than the widget itself.
116  */
117  virtual QPointer<QWidget> getCustomTitlebarWidget(QWidget* parent = 0);
118 
119  /**
120  * @brief Implement this function to specify the default name of your Widget.
121  *
122  * Implement the function as:
123  * \code{.cpp}
124  * static QString GetWidgetName()
125  * {
126  * return "my_widget_category.my_widget_name"
127  * }
128  * virtual QString getWidgetName() const override
129  * {
130  * return GetWidgetName();
131  * }
132  * \endcode
133  * If you implement the static GetWidgetName() function, the armarx gui does not need to instantiate
134  * this widget only to get the name.
135  *
136  * Dot-Notation can be used to insert the widget in categories and subcategories
137  * (e.g.: "Statecharts.StatechartEditor"). Categories must not collide with Widget names.
138  * @return Default name of the implemented Widget.
139  */
140  virtual QString getWidgetName() const = 0;
141 
142  /**
143  * @brief Implement this function to supply an icon for the menu.
144  * @return
145  */
146  virtual QIcon getWidgetIcon() const;
147 
148  /**
149  * @brief Implement this function to supply an icon for the menu (if you implemented static QString GetWidgetName()).
150  *
151  * The implementation should look like this:
152  * \code{.cpp}
153  * static QIcon GetWidgetIcon()
154  * {
155  * return QIcon{"my_resource_path"};
156  * }
157  * virtual QIcon getWidgetIcon() const override
158  * {
159  * return GetWidgetIcon();
160  * }
161  * \endcode
162  */
163  static QIcon GetWidgetIcon();
164 
165  /**
166  * @brief Implement this function to supply an icon for the category.
167  * @return
168  */
169  virtual QIcon getWidgetCategoryIcon() const;
170  /**
171  * @brief Implement this function to supply an icon for the menu (if you implemented static QString GetWidgetName()).
172  *
173  * The implementation should look like this:
174  * \code{.cpp}
175  * static QIcon GetWidgetCategoryIcon()
176  * {
177  * return QIcon{"my_resource_path"};
178  * }
179  * virtual QIcon getWidgetCategoryIcon() const override
180  * {
181  * return GetWidgetCategoryIcon();
182  * }
183  * \endcode
184  */
185  static QIcon GetWidgetCategoryIcon();
186 
187  /**
188  * @brief Implement to load the settings that are part of the GUI configuration.
189  * These settings are \b NOT loaded automatically on widget startup, but
190  * when a GUI config is loaded (instead of the optional config dialog).
191  */
192  virtual void loadSettings(QSettings* settings) = 0;
193 
194  /**
195  * @brief Implement to save the settings as part of the GUI configuration.
196  * These settings are \b NOT saved automatically when closing the widget,
197  * but when a GUI config is saved.
198  */
199  virtual void saveSettings(QSettings* settings) = 0;
200 
201  /**
202  * @brief Reimplementing this function and returning a SoNode* will show this SoNode in
203  * the 3DViewerWidget, so that you do not need to implement a viewer yourself.
204  * @return
205  */
206  virtual SoNode*
208  {
209  return 0;
210  }
211 
212  /**
213  * Reimplementing this function and returning a QDialog* will enable a configuration button which opens the returned dialog.
214  *
215  * @param parent The parent widget which should be used as parent of the dialog
216  * @return The dialog to open wehen the configuration button is clicked
217  */
218  virtual QPointer<QDialog> getSceneConfigDialog(QWidget* parent = nullptr);
219 
220  /**
221  * @brief onClose is called before the DockWidget is closed.
222  * @return returns close, if the widget should really be closed
223  */
224  virtual bool
226  {
227  return true;
228  }
229 
230  /**
231  * @brief postDocking is called after the widget has been docked into the main window.
232  * This can be used to initialize OpenGL etc. to avoid strange effects like window plopping out.
233  */
234  virtual void
236  {
237  }
238 
239  /**
240  * @brief sets the name of this instance. Can only be set once.
241  * Only the first call has an effect. Further calls are ignored.
242  *
243  * @return True on first call, else false.
244  */
245  bool setInstanceName(QString instanceName);
246  QString getInstanceName();
247  virtual void setMainWindow(QMainWindow* mainWindow);
248  /**
249  * @brief Returns the ArmarX MainWindow.
250  */
251  virtual QMainWindow* getMainWindow();
252 
253  /**
254  * @brief Returns the default instance for the TipDialog used by all widgets (if not set otherwise).
255  * This dialog has always the checkbox to not show a tip again, which is stored persistently.
256  */
257  QPointer<TipDialog> getTipDialog() const;
258  void setTipDialog(QPointer<TipDialog> tipDialog);
259 
260  // This is all total madness. Why do we put mutexes and locks into shared pointers?
261  using RecursiveMutex = std::recursive_mutex;
262  using RecursiveMutexPtr = std::shared_ptr<RecursiveMutex>;
263  using RecursiveMutexLock = std::unique_lock<RecursiveMutex>;
264  using RecursiveMutexLockPtr = std::shared_ptr<RecursiveMutexLock>;
265  /*!
266  * \brief This mutex is used to protect 3d scene updates. Usually called by the ArmarXGui main window on creation of this controller.
267  * \param mutex3D
268  */
269  virtual void setMutex3D(RecursiveMutexPtr const& mutex3D);
270 
271  template <typename Class>
274  {
275  ArmarXWidgetControllerPtr ptr = new Class();
276  return ptr;
277  }
278 
279  static int showMessageBox(const QString& msg);
280 
281  /**
282  * @brief This function enables/disables the main widget
283  * asynchronously (if called from a non qt thread).
284  * Call this function from onConnectComponent, onDisconnectComponent
285  * if your widget should be disabled if the connection was lost
286  * @param enable true to enable widget, false to disable widget
287  * @see onConnectComponent(), onDisconnectComponent(), getWidget()
288  */
289  void enableMainWidgetAsync(bool enable);
290 
291 
292  virtual void onLockWidget();
293  virtual void onUnlockWidget();
294  signals:
297  public slots:
298  void configAccepted();
299  void configRejected();
300  void enableMainWidget(bool enable);
301 
302  private:
303  QString __instanceName;
304  QPointer<QWidget> __widget;
305  QMainWindow* __appMainWindow;
306  QPointer<TipDialog> tipDialog;
307 
308  protected:
309  std::shared_ptr<std::recursive_mutex> mutex3D;
310  };
311 } // namespace armarx
312 
313 namespace std
314 {
315 
316  ARMARXCORE_IMPORT_EXPORT ostream& operator<<(ostream& stream, const QString& string);
317 
318  ARMARXCORE_IMPORT_EXPORT ostream& operator<<(ostream& stream, const QPointF& point);
319 
320  ARMARXCORE_IMPORT_EXPORT ostream& operator<<(ostream& stream, const QRectF& rect);
321 
322  ARMARXCORE_IMPORT_EXPORT ostream& operator<<(ostream& stream, const QSizeF& rect);
323 
324 } // namespace std
armarx::ArmarXWidgetController::mutex3D
std::shared_ptr< std::recursive_mutex > mutex3D
Definition: ArmarXWidgetController.h:309
armarx::ArmarXWidgetController::configRejected
void configRejected()
Definition: ArmarXWidgetController.cpp:167
armarx::ArmarXWidgetController::createInstance
static ArmarXWidgetControllerPtr createInstance()
Definition: ArmarXWidgetController.h:273
armarx::ArmarXWidgetController::getCustomTitlebarWidget
virtual QPointer< QWidget > getCustomTitlebarWidget(QWidget *parent=0)
getTitleToolbar returns a pointer to the a toolbar widget of this controller.
Definition: ArmarXWidgetController.cpp:71
armarx::ArmarXWidgetController::onUnlockWidget
virtual void onUnlockWidget()
Definition: ArmarXWidgetController.cpp:206
armarx::ArmarXWidgetController::RecursiveMutex
std::recursive_mutex RecursiveMutex
Definition: ArmarXWidgetController.h:261
armarx::ArmarXWidgetController::getScene
virtual SoNode * getScene()
Reimplementing this function and returning a SoNode* will show this SoNode in the 3DViewerWidget,...
Definition: ArmarXWidgetController.h:207
armarx::ArmarXWidgetController::configured
virtual void configured()
This function must be implemented by the user, if he supplies a config dialog.
Definition: ArmarXWidgetController.h:103
armarx::ArmarXWidgetController::loadSettings
virtual void loadSettings(QSettings *settings)=0
Implement to load the settings that are part of the GUI configuration.
armarx::ArmarXWidgetController::getConfigDialog
virtual QPointer< QDialog > getConfigDialog(QWidget *parent=0)
getConfigDialog returns a pointer to the a configuration widget of this controller.
Definition: ArmarXWidgetController.cpp:65
armarx::ArmarXWidgetController
Definition: ArmarXWidgetController.h:52
armarx::ArmarXWidgetController::saveSettings
virtual void saveSettings(QSettings *settings)=0
Implement to save the settings as part of the GUI configuration.
armarx::ArmarXWidgetController::RecursiveMutexLockPtr
std::shared_ptr< RecursiveMutexLock > RecursiveMutexLockPtr
Definition: ArmarXWidgetController.h:264
armarx::ArmarXWidgetController::getInstanceName
QString getInstanceName()
Definition: ArmarXWidgetController.cpp:124
armarx::ArmarXWidgetController::RecursiveMutexLock
std::unique_lock< RecursiveMutex > RecursiveMutexLock
Definition: ArmarXWidgetController.h:263
armarx::ArmarXWidgetController::enableMainWidget
void enableMainWidget(bool enable)
Definition: ArmarXWidgetController.cpp:173
armarx::ArmarXWidgetController::getMainWindow
virtual QMainWindow * getMainWindow()
Returns the ArmarX MainWindow.
Definition: ArmarXWidgetController.cpp:136
armarx::ArmarXWidgetController::setTipDialog
void setTipDialog(QPointer< TipDialog > tipDialog)
Definition: ArmarXWidgetController.cpp:148
armarx::ArmarXWidgetController::configAccepted
void configAccepted()
Definition: ArmarXWidgetController.cpp:160
armarx::ArmarXWidgetController::getSceneConfigDialog
virtual QPointer< QDialog > getSceneConfigDialog(QWidget *parent=nullptr)
Reimplementing this function and returning a QDialog* will enable a configuration button which opens ...
Definition: ArmarXWidgetController.cpp:101
armarx::ArmarXWidgetController::getTipDialog
QPointer< TipDialog > getTipDialog() const
Returns the default instance for the TipDialog used by all widgets (if not set otherwise).
Definition: ArmarXWidgetController.cpp:142
armarx::ArmarXWidgetController::setMutex3D
virtual void setMutex3D(RecursiveMutexPtr const &mutex3D)
This mutex is used to protect 3d scene updates. Usually called by the ArmarXGui main window on creati...
Definition: ArmarXWidgetController.cpp:154
armarx::ArmarXWidgetController::GetWidgetIcon
static QIcon GetWidgetIcon()
Implement this function to supply an icon for the menu (if you implemented static QString GetWidgetNa...
Definition: ArmarXWidgetController.cpp:83
armarx::ArmarXWidgetController::RecursiveMutexPtr
std::shared_ptr< RecursiveMutex > RecursiveMutexPtr
Definition: ArmarXWidgetController.h:262
armarx::ArmarXWidgetController::postDocking
virtual void postDocking()
postDocking is called after the widget has been docked into the main window.
Definition: ArmarXWidgetController.h:235
armarx::ArmarXWidgetController::GetWidgetCategoryIcon
static QIcon GetWidgetCategoryIcon()
Implement this function to supply an icon for the menu (if you implemented static QString GetWidgetNa...
Definition: ArmarXWidgetController.cpp:95
std::operator<<
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)
armarx::ArmarXWidgetController::setInstanceName
bool setInstanceName(QString instanceName)
sets the name of this instance.
Definition: ArmarXWidgetController.cpp:107
armarx::ArmarXWidgetController::showMessageBox
static int showMessageBox(const QString &msg)
Definition: ArmarXWidgetController.cpp:182
armarx::ArmarXWidgetController::onClose
virtual bool onClose()
onClose is called before the DockWidget is closed.
Definition: ArmarXWidgetController.h:225
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:239
armarx::ArmarXWidgetController::enableMainWidgetAsync
void enableMainWidgetAsync(bool enable)
This function enables/disables the main widget asynchronously (if called from a non qt thread).
Definition: ArmarXWidgetController.cpp:191
std
Definition: Application.h:66
IceUtil::Handle< ArmarXWidgetController >
armarx::ArmarXWidgetController::onLockWidget
virtual void onLockWidget()
Definition: ArmarXWidgetController.cpp:201
armarx::ArmarXWidgetController::getWidgetName
virtual QString getWidgetName() const =0
Implement this function to specify the default name of your Widget.
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
armarx::ArmarXWidgetController::getWidgetCategoryIcon
virtual QIcon getWidgetCategoryIcon() const
Implement this function to supply an icon for the category.
Definition: ArmarXWidgetController.cpp:89
armarx::ArmarXWidgetController::getWidgetIcon
virtual QIcon getWidgetIcon() const
Implement this function to supply an icon for the menu.
Definition: ArmarXWidgetController.cpp:77
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
Logging.h
armarx::ArmarXWidgetController::setMainWindow
virtual void setMainWindow(QMainWindow *mainWindow)
Definition: ArmarXWidgetController.cpp:130
armarx::ArmarXWidgetController::ArmarXWidgetController
ArmarXWidgetController()
Definition: ArmarXWidgetController.cpp:38
armarx::ArmarXWidgetController::~ArmarXWidgetController
~ArmarXWidgetController() override
Definition: ArmarXWidgetController.cpp:43
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27