QGIS API Documentation  3.23.0-Master (b5237dafc3)
qgsdockwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdockwidget.cpp
3  -----------------
4  begin : June 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #include "qgsdockwidget.h"
20 #include <QAction>
21 
22 
23 QgsDockWidget::QgsDockWidget( QWidget *parent, Qt::WindowFlags flags )
24  : QDockWidget( parent, flags )
25 {
26  connect( this, &QDockWidget::visibilityChanged, this, &QgsDockWidget::handleVisibilityChanged );
27 }
28 
29 QgsDockWidget::QgsDockWidget( const QString &title, QWidget *parent, Qt::WindowFlags flags )
30  : QDockWidget( title, parent, flags )
31 {
32  connect( this, &QDockWidget::visibilityChanged, this, &QgsDockWidget::handleVisibilityChanged );
33 }
34 
35 void QgsDockWidget::setUserVisible( bool visible )
36 {
37  if ( visible )
38  {
39  show();
40  raise();
41  }
42  else
43  {
44  if ( !mVisibleAndActive )
45  return;
46 
47  hide();
48  }
49 }
50 
52 {
54 }
55 
57 {
58  return mVisibleAndActive;
59 }
60 
62 {
63  mAction = action;
64  if ( !mAction->isCheckable() )
65  mAction->setCheckable( true );
66  mAction->setChecked( isUserVisible() );
67  connect( mAction, &QAction::toggled, this, [ = ]( bool visible )
68  {
69  setUserVisible( visible );
70  } );
71  connect( this, &QgsDockWidget::visibilityChanged, mAction, [ = ]( bool visible )
72  {
73  mAction->setChecked( visible );
74  } );
75 }
76 
78 {
79  return mAction;
80 }
81 
82 void QgsDockWidget::closeEvent( QCloseEvent *e )
83 {
84  emit closed();
85  emit closedStateChanged( true );
86  emit openedStateChanged( false );
87  QDockWidget::closeEvent( e );
88 }
89 
90 void QgsDockWidget::showEvent( QShowEvent *e )
91 {
92  emit opened();
93  emit closedStateChanged( false );
94  emit openedStateChanged( true );
95  QDockWidget::showEvent( e );
96 }
97 
98 void QgsDockWidget::handleVisibilityChanged( bool visible )
99 {
100  mVisibleAndActive = visible;
101 }
102 
void closed()
Emitted when dock widget is closed.
QAction * toggleVisibilityAction()
Returns the action linked to the dock.
void closedStateChanged(bool wasClosed)
Emitted when dock widget is closed (or opened).
QgsDockWidget(QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsDockWidget.
bool isUserVisible() const
Returns true if the dock is both opened and raised to the front (ie not hidden by any other tabs.
void setUserVisible(bool visible)
Sets the dock widget as visible to a user, ie both shown and raised to the front.
void toggleUserVisible()
Toggles whether the dock is user visible.
void closeEvent(QCloseEvent *) override
void opened()
Emitted when dock widget is opened.
void showEvent(QShowEvent *event) override
void setToggleVisibilityAction(QAction *action)
Links an action to the dock, so that toggling the action will automatically set the dock's visibility...
void openedStateChanged(bool wasOpened)
Emitted when dock widget is opened (or closed).