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
37class SoNode;
38class QDialog;
39class QMainWindow;
40
41namespace armarx
42{
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
313namespace 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
#define ARMARXCORE_IMPORT_EXPORT
virtual void loadSettings(QSettings *settings)=0
Implement to load the settings that are part of the GUI configuration.
virtual bool onClose()
onClose is called before the DockWidget is closed.
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
virtual QPointer< QDialog > getConfigDialog(QWidget *parent=0)
getConfigDialog returns a pointer to the a configuration widget of this controller.
virtual QPointer< QDialog > getSceneConfigDialog(QWidget *parent=nullptr)
Reimplementing this function and returning a QDialog* will enable a configuration button which opens ...
virtual SoNode * getScene()
Reimplementing this function and returning a SoNode* will show this SoNode in the 3DViewerWidget,...
static ArmarXWidgetControllerPtr createInstance()
std::shared_ptr< RecursiveMutexLock > RecursiveMutexLockPtr
void configAccepted(ArmarXWidgetControllerPtr widget)
std::shared_ptr< RecursiveMutex > RecursiveMutexPtr
virtual QIcon getWidgetIcon() const
Implement this function to supply an icon for the menu.
std::unique_lock< RecursiveMutex > RecursiveMutexLock
std::shared_ptr< std::recursive_mutex > mutex3D
void enableMainWidgetAsync(bool enable)
This function enables/disables the main widget asynchronously (if called from a non qt thread).
void setTipDialog(QPointer< TipDialog > tipDialog)
virtual void configured()
This function must be implemented by the user, if he supplies a config dialog.
virtual QMainWindow * getMainWindow()
Returns the ArmarX MainWindow.
virtual void saveSettings(QSettings *settings)=0
Implement to save the settings as part of the GUI configuration.
static QIcon GetWidgetCategoryIcon()
Implement this function to supply an icon for the menu (if you implemented static QString GetWidgetNa...
QPointer< TipDialog > getTipDialog() const
Returns the default instance for the TipDialog used by all widgets (if not set otherwise).
virtual void setMainWindow(QMainWindow *mainWindow)
virtual QString getWidgetName() const =0
Implement this function to specify the default name of your Widget.
virtual void postDocking()
postDocking is called after the widget has been docked into the main window.
virtual void setMutex3D(RecursiveMutexPtr const &mutex3D)
This mutex is used to protect 3d scene updates. Usually called by the ArmarXGui main window on creati...
bool setInstanceName(QString instanceName)
sets the name of this instance.
static int showMessageBox(const QString &msg)
static QIcon GetWidgetIcon()
Implement this function to supply an icon for the menu (if you implemented static QString GetWidgetNa...
virtual QIcon getWidgetCategoryIcon() const
Implement this function to supply an icon for the category.
virtual QPointer< QWidget > getCustomTitlebarWidget(QWidget *parent=0)
getTitleToolbar returns a pointer to the a toolbar widget of this controller.
void configRejected(ArmarXWidgetControllerPtr widget)
The TipDialog is a dialog to show tips/hints to the user, which are optionally only shown once.
Definition TipDialog.h:48
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< ArmarXWidgetController > ArmarXWidgetControllerPtr
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)