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