itom  4.1.0
abstractDockWidget.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 *********************************************************************** */
22 
23 #ifndef ABSTRACTDOCKWIDGET_H
24 #define ABSTRACTDOCKWIDGET_H
25 
26 #include "../global.h"
27 #include "../common/sharedStructures.h"
28 #include "shortcutAction.h"
29 
30 #include <qmainwindow.h>
31 #include <qdockwidget.h>
32 #include <qmenubar.h>
33 #include <qevent.h>
34 #include <qmap.h>
35 #include <qtoolbar.h>
36 #include <qdebug.h>
37 #include <qstring.h>
38 #include <qaction.h>
39 #include <qshortcut.h>
40 #include <qrect.h>
41 #include <qwidget.h>
42 
43 
44 namespace ito
45 {
54  class AbstractDockWidget : public QDockWidget
55  {
56  Q_OBJECT
57 
58  //these properties are taken from QWidget in order to redirect them either to the QDockWidget or to the main window (depending on dock status)
59  Q_PROPERTY(bool visible READ isVisible WRITE setVisible DESIGNABLE false)
60  Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
61 
62  Q_PROPERTY(QRect geometry READ geometry WRITE setGeometry)
63  Q_PROPERTY(QRect frameGeometry READ frameGeometry)
64  Q_PROPERTY(QRect normalGeometry READ normalGeometry)
65  Q_PROPERTY(int x READ x)
66  Q_PROPERTY(int y READ y)
67  Q_PROPERTY(QPoint pos READ pos WRITE move DESIGNABLE false STORED false)
68  Q_PROPERTY(QSize frameSize READ frameSize)
69  Q_PROPERTY(QSize size READ size WRITE resize DESIGNABLE false STORED false)
70  Q_PROPERTY(int width READ width)
71  Q_PROPERTY(int height READ height)
72  Q_PROPERTY(QRect rect READ rect)
73  Q_PROPERTY(QRect childrenRect READ childrenRect)
74  Q_PROPERTY(QRegion childrenRegion READ childrenRegion)
75  Q_PROPERTY(QString windowTitle READ windowTitle WRITE setWindowTitle)
76 
77  public:
78 
80 
89  {
93  };
94 
97  {
100  };
101 
103 
110  {
114  };
115 
116  struct Toolbar
117  {
118  Toolbar() : section(0), key(""), tb(NULL) {}
119  Qt::ToolBarArea area;
120  int section;
121  QString key;
122  QToolBar *tb;
123  };
124 
125  AbstractDockWidget(bool docked, bool isDockAvailable, tFloatingStyle floatingStyle, tMovingStyle movingStyle, const QString &title = QString(), const QString &objName = QString(), QWidget *parent = 0);
126  virtual ~AbstractDockWidget();
127 
128  inline bool docked() const { return m_docked; }
129  RetVal setTopLevel( tTopLevelStyle topLevel, bool showWindow = true );
130 
131  void setParent ( QWidget * parent ) { m_overallParent = parent; QDockWidget::setParent(parent); }
132 
133  QWidget *getActiveInstance()
134  {
136  {
137  return m_pWindow;
138  }
139  return this;
140  }
141 
142  //these methods are mainly redirected to QDockWidget or QMainWindow (depending on dock status)
143  #define QWIDGETPROPGETTER(gettername) if(m_docked && m_dockAvailable) { return QDockWidget::gettername(); } else { return m_pWindow->gettername(); }
144 
145  #define QWIDGETPROPSETTER(settername,...) \
146  if (m_docked) \
147  { \
148  QDockWidget::settername(__VA_ARGS__); \
149  } \
150  else \
151  { \
152  if (m_floatingStyle == floatingWindow) \
153  { \
154  m_pWindow->settername(__VA_ARGS__); \
155  QDockWidget::settername(__VA_ARGS__); \
156  } \
157  else \
158  { \
159  QDockWidget::settername(__VA_ARGS__); \
160  } \
161  }
162 
163  #define QWIDGETPROPSETTERSPECIAL(settername,...) \
164  if (m_docked) \
165  { \
166  QDockWidget::settername(__VA_ARGS__); \
167  } \
168  else \
169  { \
170  if (m_floatingStyle == floatingWindow) \
171  { \
172  m_pWindow->settername(__VA_ARGS__); \
173  } \
174  else \
175  { \
176  QDockWidget::settername(__VA_ARGS__); \
177  } \
178  }
179 
180  QRect frameGeometry() const;
181  const QRect &geometry() const;
182  QRect normalGeometry() const;
183 
184  int x() const;
185  int y() const;
186  QPoint pos() const;
187  QSize frameSize() const;
188  QSize size() const;
189  int width() const;
190  int height() const;
191  QRect rect() const;
192  QRect childrenRect() const;
193  QRegion childrenRegion() const;
194 
195  void move(int x, int y);
196  void move(const QPoint &);
197  void resize(int w, int h);
198  void resize(const QSize &);
199  void setGeometry(int x, int y, int w, int h);
200  void setGeometry(const QRect &);
201  void setMinimumSize(const QSize &size);
202 
203  QString windowTitle();
204  void setWindowTitle(const QString &title);
205  bool isEnabled() const;
206  bool isVisible() const;
207 
208  void saveState(const QString &iniName) const;
209  void restoreState(const QString &iniName);
210 
212 
213  virtual QSize sizeHint() const;
214  virtual QSize minimumSizeHint() const;
215 
216  protected:
217 
218  friend class ShortcutAction; //to access canvas of this dock widget
219 
221 
226  bool eventFilter(QObject *obj, QEvent *event)
227  {
228  if (event->type() == QEvent::Close)
229  {
230  closeEvent((QCloseEvent*)event);
231  if(testAttribute(Qt::WA_DeleteOnClose) && event->isAccepted() ) //if window should be closed and dockwidget is assigned with WA_DeleteOnClose, delete this dockwidget first.
232  {
233  deleteLater();
234  }
235 
236  return true;
237  }
238  else
239  {
240  return QObject::eventFilter(obj,event);
241  }
242  };
243 
244  void init();
245 
246  virtual void closeEvent(QCloseEvent *event);
247 
248  virtual void createActions() = 0;
249  virtual void createMenus() = 0;
250  virtual void createToolBars() = 0;
251  virtual void createStatusBar() = 0;
252  virtual void updateActions() {}
253  virtual void updatePythonActions() = 0;
254 
255  Qt::WindowFlags modifyFlags(const Qt::WindowFlags &flags, const Qt::WindowFlags &setFlags, const Qt::WindowFlags &unsetFlags);
256 
257  virtual void windowStateChanged( bool /*windowNotToolbox*/ ) {}
258 
259  void setContentWidget(QWidget *widget);
260  inline QWidget* getContentWidget() const { return m_pWindow->centralWidget(); }
261  inline QMainWindow* getCanvas() { return m_pWindow; }
262 
263  inline bool pythonBusy() const { return m_pythonBusy; }
264  inline bool pythonDebugMode() const { return m_pythonDebugMode; }
265  inline bool pythonInWaitingMode() const { return m_pythonInWaitingMode; }
267  //RetVal addAndRegisterToolBar(QToolBar* tb, QString key);
268  //RetVal unregisterToolBar(QString key);
269  QToolBar* getToolBar(QString key) const;
270  inline QMenuBar* getMenuBar() const { return (m_pWindow == NULL) ? NULL : m_pWindow->menuBar(); }
271 
272  RetVal addToolBar(QToolBar *tb, const QString &key, Qt::ToolBarArea area = Qt::TopToolBarArea, int section = 1);
273  RetVal removeToolBar(const QString &key);
274 
275  QAction *m_actStayOnTop;
276  QAction *m_actStayOnTopOfApp;
277 
278  private:
279 
280  void contextMenuEvent(QContextMenuEvent *e)
281  {
282  e->accept();
283  qDebug() << "context menu of abstractDockWidget clicked. [placeholder for (un)docking feature.] pos: " << e->pos();
284  }
285 
286  QMainWindow *m_pWindow;
287 
288  bool m_docked;
292  QString m_basicTitle;
293  QString m_completeTitle;
295  QMap<QString,QToolBar*> m_toolBars;
296  QList<Toolbar> m_toolbars;
297 
302  QToolBar* m_dockToolbar;
303  QAction* m_actDock;
304  QAction* m_actUndock;
305 
306  QWidget* m_overallParent;
307  tTopLevelStyle m_recentTopLevelStyle;
308 
309  QSize m_oldMinSize;
310  QSize m_oldMaxSize;
311  QRect m_lastUndockedSize;
312  QByteArray m_pendingGeometryState; //if the window has a loaded geometry state from the settings file, but has been hidden at startup, the geometry is saved here and applied once the window becomes visible. Then, this variable is cleared.
313 
314  public Q_SLOTS:
315  void setEnabled(bool);
316  virtual void setVisible(bool visible);
317 
318  virtual void pythonStateChanged(tPythonTransitions pyTransition);
319 
320  void raiseAndActivate();
321  void mini();
322 
323  void setDockSize(int newWidth, int newHeight);
324 
325  void dockWidget();
326  void undockWidget(bool show_it = true);
327 
328  RetVal setAdvancedWindowTitle(QString newCompleteTitle = QString(), bool appendToBasicTitle = true);
329 
330  private slots:
331  void mnuStayOnTop(bool checked);
332  void mnuStayOnTopOfApp(bool checked);
333 
334  void returnToOldMinMaxSizes();
335 
336  Q_SIGNALS:
337  void dockStateChanged(bool docked);
338  };
339 
340 } //end namespace ito
341 
342 #endif
QWidget * m_overallParent
Definition: abstractDockWidget.h:306
tFloatingStyle m_floatingStyle
Definition: abstractDockWidget.h:290
virtual void pythonStateChanged(tPythonTransitions pyTransition)
slot invoked if python state changed. Sets the specific member variables according to the python tran...
Definition: abstractDockWidget.cpp:819
virtual void closeEvent(QCloseEvent *event)
closeEvent invoked if this AbstractDockWidget should be closed
Definition: abstractDockWidget.cpp:794
QMap< QString, QToolBar * > m_toolBars
Definition: abstractDockWidget.h:295
void raiseAndActivate()
activates this dock widget or window and raises it on top of all opened windows
Definition: abstractDockWidget.cpp:1134
RetVal addToolBar(QToolBar *tb, const QString &key, Qt::ToolBarArea area=Qt::TopToolBarArea, int section=1)
Definition: abstractDockWidget.cpp:628
bool m_docked
Definition: abstractDockWidget.h:288
Class for managing status values (like errors or warning)
Definition: retVal.h:54
void undockWidget(bool show_it=true)
undocks this dockWidget.
Definition: abstractDockWidget.cpp:937
AbstractDockWidget(bool docked, bool isDockAvailable, tFloatingStyle floatingStyle, tMovingStyle movingStyle, const QString &title=QString(), const QString &objName=QString(), QWidget *parent=0)
constructor
Definition: abstractDockWidget.cpp:66
bool pythonInWaitingMode() const
Definition: abstractDockWidget.h:265
void dockWidget()
docks this dockWidget.
Definition: abstractDockWidget.cpp:862
bool m_pythonInWaitingMode
Definition: abstractDockWidget.h:300
QWidget * getContentWidget() const
Definition: abstractDockWidget.h:260
tMovingStyle m_movingStyle
Definition: abstractDockWidget.h:291
Definition: apiFunctionsGraph.cpp:39
bool eventFilter(QObject *obj, QEvent *event)
eventFilter for m_pWindow
Definition: abstractDockWidget.h:226
Definition: abstractDockWidget.h:111
Definition: abstractDockWidget.h:91
QToolBar * getToolBar(QString key) const
returns reference to toolbar with given key-value
Definition: abstractDockWidget.cpp:771
abstract dock widget class which inherits QDockWidget. The content of QDockWidget consists of an inst...
Definition: abstractDockWidget.h:54
Definition: abstractDockWidget.h:113
void setContentWidget(QWidget *widget)
sets any given QWidget as central widget of QMainWindow and inversely sets this QWidget's parent to t...
Definition: abstractDockWidget.cpp:525
bool m_dockAvailable
Definition: abstractDockWidget.h:289
void synchronizeTopLevelState()
synchronizes the top level state of the dock widget with the floating settings of this abstract dock ...
Definition: abstractDockWidget.cpp:1241
RetVal setAdvancedWindowTitle(QString newCompleteTitle=QString(), bool appendToBasicTitle=true)
changes the title of widget
Definition: abstractDockWidget.cpp:548
tFloatingStyle
The floating style of a widget, derived from AbstractDockWidget.
Definition: abstractDockWidget.h:88
QString m_completeTitle
Definition: abstractDockWidget.h:293
tTopLevelStyle
The top level style of a widget, derived from AbstractDockWidget.
Definition: abstractDockWidget.h:109
Definition: abstractDockWidget.h:90
void dockStateChanged(bool docked)
emitted if the widget is either docked or undocked from the main window
void init()
init method, called by constructor
Definition: abstractDockWidget.cpp:151
Definition: abstractDockWidget.h:99
virtual ~AbstractDockWidget()
destructor
Definition: abstractDockWidget.cpp:128
bool pythonDebugMode() const
Definition: abstractDockWidget.h:264
Definition: abstractDockWidget.h:92
bool docked() const
Definition: abstractDockWidget.h:128
Definition: abstractDockWidget.h:112
QString m_basicTitle
Definition: abstractDockWidget.h:292
bool pythonBusy() const
Definition: abstractDockWidget.h:263
Definition: abstractDockWidget.h:116
tMovingStyle
The configuration if a docked AbstractDockWidget can be moved from one docking area to another one...
Definition: abstractDockWidget.h:96
bool m_pythonDebugMode
Definition: abstractDockWidget.h:299
bool m_pythonBusy
Definition: abstractDockWidget.h:298
Definition: abstractDockWidget.h:98