itom  3.0.0
abstractDockWidget.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2016, 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 
29 #include <qmainwindow.h>
30 #include <qdockwidget.h>
31 #include <qmenubar.h>
32 #include <qevent.h>
33 #include <qmap.h>
34 #include <qtoolbar.h>
35 #include <qdebug.h>
36 #include <qstring.h>
37 #include <qaction.h>
38 #include <qshortcut.h>
39 #include <qrect.h>
40 #include <qwidget.h>
41 
42 
43 namespace ito
44 {
45  class AbstractDockWidget : public QDockWidget
46  {
47  Q_OBJECT
48 
49  //these properties are taken from QWidget in order to redirect them either to the QDockWidget or to the main window (depending on dock status)
50  Q_PROPERTY(bool visible READ isVisible WRITE setVisible DESIGNABLE false)
51  Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
52 
53  Q_PROPERTY(QRect geometry READ geometry WRITE setGeometry)
54  Q_PROPERTY(QRect frameGeometry READ frameGeometry)
55  Q_PROPERTY(QRect normalGeometry READ normalGeometry)
56  Q_PROPERTY(int x READ x)
57  Q_PROPERTY(int y READ y)
58  Q_PROPERTY(QPoint pos READ pos WRITE move DESIGNABLE false STORED false)
59  Q_PROPERTY(QSize frameSize READ frameSize)
60  Q_PROPERTY(QSize size READ size WRITE resize DESIGNABLE false STORED false)
61  Q_PROPERTY(int width READ width)
62  Q_PROPERTY(int height READ height)
63  Q_PROPERTY(QRect rect READ rect)
64  Q_PROPERTY(QRect childrenRect READ childrenRect)
65  Q_PROPERTY(QRegion childrenRegion READ childrenRegion)
66  Q_PROPERTY(QString windowTitle READ windowTitle WRITE setWindowTitle)
67 
68  public:
69 
70  enum tFloatingStyle { floatingNone, floatingStandard, floatingWindow };
71  enum tMovingStyle { movingDisabled, movingEnabled };
72  enum tTopLevelStyle { topLevelOverall, topLevelParentOnly, topLevelNothing };
73 
74  struct Toolbar
75  {
76  Toolbar() : section(0), key(""), tb(NULL) {}
77  Qt::ToolBarArea area;
78  int section;
79  QString key;
80  QToolBar *tb;
81  };
82 
83  AbstractDockWidget(bool docked, bool isDockAvailable, tFloatingStyle floatingStyle, tMovingStyle movingStyle, const QString &title = QString(), const QString &objName = QString(), QWidget *parent = 0);
84  virtual ~AbstractDockWidget();
85 
86  inline bool docked() const { return m_docked; }
88  RetVal setAdvancedWindowTitle( QString newCompleteTitle = QString(), bool appendToBasicTitle = true );
89 
90  RetVal setTopLevel( tTopLevelStyle topLevel );
91 
92  void setParent ( QWidget * parent ) { m_overallParent = parent; QDockWidget::setParent(parent); }
93 
94  QWidget *getActiveInstance()
95  {
96  if(!m_docked && m_floatingStyle == floatingWindow)
97  {
98  return m_pWindow;
99  }
100  return this;
101  }
102 
103  //these methods are mainly redirected to QDockWidget or QMainWindow (depending on dock status)
104  #define QWIDGETPROPGETTER(gettername) if(m_docked && m_dockAvailable) { return QDockWidget::gettername(); } else { return m_pWindow->gettername(); }
105 
106  #define QWIDGETPROPSETTER(settername,...) \
107  if (m_docked) \
108  { \
109  QDockWidget::settername(__VA_ARGS__); \
110  } \
111  else \
112  { \
113  if (m_floatingStyle == floatingWindow) \
114  { \
115  m_pWindow->settername(__VA_ARGS__); \
116  QDockWidget::settername(__VA_ARGS__); \
117  } \
118  else \
119  { \
120  QDockWidget::settername(__VA_ARGS__); \
121  } \
122  }
123 
124  #define QWIDGETPROPSETTERSPECIAL(settername,...) \
125  if (m_docked) \
126  { \
127  QDockWidget::settername(__VA_ARGS__); \
128  } \
129  else \
130  { \
131  if (m_floatingStyle == floatingWindow) \
132  { \
133  m_pWindow->settername(__VA_ARGS__); \
134  } \
135  else \
136  { \
137  QDockWidget::settername(__VA_ARGS__); \
138  } \
139  }
140 
141  QRect frameGeometry() const;
142  const QRect &geometry() const;
143  QRect normalGeometry() const;
144 
145  int x() const;
146  int y() const;
147  QPoint pos() const;
148  QSize frameSize() const;
149  QSize size() const;
150  int width() const;
151  int height() const;
152  QRect rect() const;
153  QRect childrenRect() const;
154  QRegion childrenRegion() const;
155 
156  void move(int x, int y);
157  void move(const QPoint &);
158  void resize(int w, int h);
159  void resize(const QSize &);
160  void setGeometry(int x, int y, int w, int h);
161  void setGeometry(const QRect &);
162 
163  QString windowTitle();
164  void setWindowTitle(const QString &title);
165 
166  bool isEnabled() const;
167  bool isVisible() const;
168 
169  void saveState(const QString &iniName) const;
170  void restoreState(const QString &iniName);
171 
173 
174  virtual QSize sizeHint() const;
175  virtual QSize minimumSizeHint() const;
176 
177  public Q_SLOTS:
178  void setEnabled(bool);
179  virtual void setVisible(bool visible);
180 
181  protected:
182 
183  class ShortcutAction : public QObject
184  {
185  public:
186  ShortcutAction(const QString &text, AbstractDockWidget *parent) : QObject(parent), m_action(NULL), m_shortcut(NULL)
187  {
188  m_action = new QAction(text, parent);
189  QString toolTipText = text;
190  toolTipText.replace("&", "");
191  m_action->setToolTip(toolTipText);
192  }
193 
194  ShortcutAction(const QIcon &icon, const QString &text, AbstractDockWidget *parent) : QObject(parent), m_action(NULL), m_shortcut(NULL)
195  {
196  m_action = new QAction(icon, text, parent);
197  QString toolTipText = text;
198  toolTipText.replace("&", "");
199  m_action->setToolTip(toolTipText);
200  }
201 
202  ShortcutAction(const QIcon &icon, const QString &text, AbstractDockWidget *parent, const QKeySequence &key, Qt::ShortcutContext context = Qt::WindowShortcut) : QObject(parent), m_action(NULL), m_shortcut(NULL)
203  {
204  QString text2 = text;
205  QString text3 = text;
206  text3.replace("&", "");
207 
208  //some key sequences do not exist as default on all operating systems
209  if (key.isEmpty() == false)
210  {
211  text2 += "\t" + key.toString(QKeySequence::NativeText);
212  text3 += " (" + key.toString(QKeySequence::NativeText) + ")";
213 
214  m_shortcut = new QShortcut(key, parent->getCanvas());
215  m_shortcut->setContext(context);
216  }
217 
218  m_action = new QAction(icon, text2, parent);
219  m_action->setToolTip(text3);
220  }
221 
222  ~ShortcutAction()
223  {
224  //do not delete action and shortcut here, since it will be deleted by common parent.
225  }
226 
227  void connectTrigger(const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
228  {
229  if(m_action)
230  {
231  QObject::connect(m_action, SIGNAL(triggered()), receiver, method, type);
232  }
233  if(m_shortcut)
234  {
235  QObject::connect(m_shortcut, SIGNAL(activated()), receiver, method, type);
236  }
237  }
238 
239  void setEnabled(bool actionEnabled, bool shortcutEnabled)
240  {
241  if(m_action)
242  {
243  m_action->setEnabled(actionEnabled);
244  if(m_shortcut) m_shortcut->setEnabled(shortcutEnabled);
245  }
246  }
247 
248  void setEnabled(bool enabled)
249  {
250  setEnabled(enabled,enabled);
251  }
252 
253  void setVisible(bool actionVisible, bool shortcutEnabled)
254  {
255  if(m_action)
256  {
257  m_action->setVisible(actionVisible);
258  if(m_shortcut) m_shortcut->setEnabled(shortcutEnabled);
259  }
260  }
261 
262  void setVisible(bool visible)
263  {
264  setVisible(visible,visible);
265  }
266 
267  QAction* action() { return m_action; }
268 
269  private:
270  QAction *m_action;
271  QShortcut *m_shortcut;
272 
273  };
274 
276 
281  bool eventFilter(QObject *obj, QEvent *event)
282  {
283  if (event->type() == QEvent::Close)
284  {
285  closeEvent((QCloseEvent*)event);
286  if(testAttribute(Qt::WA_DeleteOnClose) && event->isAccepted() ) //if window should be closed and dockwidget is assigned with WA_DeleteOnClose, delete this dockwidget first.
287  {
288  deleteLater();
289  }
290 
291  return true;
292  }
293  else
294  {
295  return QObject::eventFilter(obj,event);
296  }
297  };
298 
299  void init();
300 
301  virtual void closeEvent(QCloseEvent *event);
302 
303  virtual void createActions() = 0;
304  virtual void createMenus() = 0;
305  virtual void createToolBars() = 0;
306  virtual void createStatusBar() = 0;
307  virtual void updateActions() {}
308  virtual void updatePythonActions() = 0;
309 
310  Qt::WindowFlags modifyFlags(const Qt::WindowFlags &flags, const Qt::WindowFlags &setFlags, const Qt::WindowFlags &unsetFlags);
311 
312  virtual void windowStateChanged( bool /*windowNotToolbox*/ ) {}
313 
314  void setContentWidget(QWidget *widget);
315  inline QWidget* getContentWidget() const { return m_pWindow->centralWidget(); }
316  inline QMainWindow* getCanvas() { return m_pWindow; }
317 
318  inline bool pythonBusy() const { return m_pythonBusy; }
319  inline bool pythonDebugMode() const { return m_pythonDebugMode; }
320  inline bool pythonInWaitingMode() const { return m_pythonInWaitingMode; }
322  //RetVal addAndRegisterToolBar(QToolBar* tb, QString key);
323  //RetVal unregisterToolBar(QString key);
324  QToolBar* getToolBar(QString key) const;
325  inline QMenuBar* getMenuBar() const { return (m_pWindow == NULL) ? NULL : m_pWindow->menuBar(); }
326 
327  RetVal addToolBar(QToolBar *tb, const QString &key, Qt::ToolBarArea area = Qt::TopToolBarArea, int section = 1);
328  RetVal removeToolBar(const QString &key);
329 
330  QAction *m_actStayOnTop;
331  QAction *m_actStayOnTopOfApp;
332 
333  private:
334 
335  void contextMenuEvent(QContextMenuEvent *e)
336  {
337  e->accept();
338  qDebug() << "context menu of abstractDockWidget clicked. [placeholder for (un)docking feature.] pos: " << e->pos();
339  }
340 
341  QMainWindow *m_pWindow;
342 
343  bool m_docked;
345  tFloatingStyle m_floatingStyle;
346  tMovingStyle m_movingStyle;
347  QString m_basicTitle;
348  QString m_completeTitle;
350  QMap<QString,QToolBar*> m_toolBars;
351  QList<Toolbar> m_toolbars;
352 
357  QToolBar* m_dockToolbar;
358  QAction* m_actDock;
359  QAction* m_actUndock;
360 
361  QWidget* m_overallParent;
362  tTopLevelStyle m_recentTopLevelStyle;
363 
364  QSize m_oldMinSize;
365  QSize m_oldMaxSize;
366  QRect m_lastUndockedSize;
367  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.
368 
369  public slots:
370  virtual void dockedToMainWindow(AbstractDockWidget * /*widget*/){}
371  virtual void removedFromMainWindow(AbstractDockWidget * /*widget*/){}
372  virtual void pythonStateChanged(tPythonTransitions pyTransition);
373 
374  void raiseAndActivate();
376  void setDockSize(int newWidth, int newHeight);
377 
378  void dockWidget();
379  void undockWidget(bool show_it = true);
380 
381  private slots:
382  void mnuStayOnTop(bool checked);
383  void mnuStayOnTopOfApp(bool checked);
384 
385  void returnToOldMinMaxSizes();
386  };
387 
388 } //end namespace ito
389 
390 #endif
QWidget * m_overallParent
Definition: abstractDockWidget.h:361
tFloatingStyle m_floatingStyle
Definition: abstractDockWidget.h:345
virtual void pythonStateChanged(tPythonTransitions pyTransition)
slot invoked if python state changed. Sets the specific member variables according to the python tran...
Definition: abstractDockWidget.cpp:892
virtual void closeEvent(QCloseEvent *event)
closeEvent invoked if this AbstractDockWidget should be closed
Definition: abstractDockWidget.cpp:867
QMap< QString, QToolBar * > m_toolBars
Definition: abstractDockWidget.h:350
void raiseAndActivate()
activates this dock widget or window and raises it on top of all opened windows
Definition: abstractDockWidget.cpp:1195
RetVal addToolBar(QToolBar *tb, const QString &key, Qt::ToolBarArea area=Qt::TopToolBarArea, int section=1)
Definition: abstractDockWidget.cpp:701
bool m_docked
Definition: abstractDockWidget.h:343
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:1006
AbstractDockWidget(bool docked, bool isDockAvailable, tFloatingStyle floatingStyle, tMovingStyle movingStyle, const QString &title=QString(), const QString &objName=QString(), QWidget *parent=0)
constructor
Definition: abstractDockWidget.cpp:68
bool pythonInWaitingMode() const
Definition: abstractDockWidget.h:320
void dockWidget()
docks this dockWidget.
Definition: abstractDockWidget.cpp:935
bool m_pythonInWaitingMode
Definition: abstractDockWidget.h:355
QWidget * getContentWidget() const
Definition: abstractDockWidget.h:315
tMovingStyle m_movingStyle
Definition: abstractDockWidget.h:346
Definition: apiFunctionsGraph.cpp:39
bool eventFilter(QObject *obj, QEvent *event)
eventFilter for m_pWindow
Definition: abstractDockWidget.h:281
QToolBar * getToolBar(QString key) const
returns reference to toolbar with given key-value
Definition: abstractDockWidget.cpp:844
abstract dock widget class which inherits QDockWidget. The content of QDockWidget consists of an inst...
Definition: abstractDockWidget.h:45
void setContentWidget(QWidget *widget)
sets any given QWidget as central widget of QMainWindow and inversely sets this QWidget&#39;s parent to t...
Definition: abstractDockWidget.cpp:598
Definition: abstractDockWidget.h:183
bool m_dockAvailable
Definition: abstractDockWidget.h:344
void synchronizeTopLevelState()
synchronizes the top level state of the dock widget with the floating settings of this abstract dock ...
Definition: abstractDockWidget.cpp:1302
RetVal setAdvancedWindowTitle(QString newCompleteTitle=QString(), bool appendToBasicTitle=true)
changes the title of widget
Definition: abstractDockWidget.cpp:621
QString m_completeTitle
Definition: abstractDockWidget.h:348
void init()
init method, called by constructor
Definition: abstractDockWidget.cpp:149
virtual ~AbstractDockWidget()
destructor
Definition: abstractDockWidget.cpp:129
bool pythonDebugMode() const
Definition: abstractDockWidget.h:319
bool docked() const
Definition: abstractDockWidget.h:86
QString m_basicTitle
Definition: abstractDockWidget.h:347
bool pythonBusy() const
Definition: abstractDockWidget.h:318
Definition: abstractDockWidget.h:74
bool m_pythonDebugMode
Definition: abstractDockWidget.h:354
bool m_pythonBusy
Definition: abstractDockWidget.h:353