Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add method to toggle user visibility of QgsDockWidgets
  • Loading branch information
nyalldawson committed Jun 19, 2018
1 parent c95e1c6 commit a2ef677
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/gui/auto_generated/qgsdockwidget.sip.in
Expand Up @@ -45,6 +45,8 @@ Returns true if the dock is both opened and raised to the front (ie not hidden b
any other tabs.

.. seealso:: :py:func:`setUserVisible`

.. seealso:: :py:func:`toggleUserVisible`
%End

public slots:
Expand All @@ -64,6 +66,21 @@ Sets the dock widget as visible to a user, ie both shown and raised to the front
- hiding a dock which is closed has no effect and raises no signals

.. seealso:: :py:func:`isUserVisible`

.. seealso:: :py:func:`toggleUserVisible`
%End

void toggleUserVisible();
%Docstring
Toggles whether the dock is user visible. If the dock is not currently user
visible (i.e. opened and activated as a tab) then the dock will be opened
and raised. If it is currently user visible it will be closed.

.. seealso:: :py:func:`setUserVisible`

.. seealso:: :py:func:`isUserVisible`

.. versionadded:: 3.2
%End

protected:
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsdockwidget.cpp
Expand Up @@ -47,6 +47,11 @@ void QgsDockWidget::setUserVisible( bool visible )
}
}

void QgsDockWidget::toggleUserVisible()
{
setUserVisible( !isUserVisible() );
}

bool QgsDockWidget::isUserVisible() const
{
return mVisibleAndActive;
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgsdockwidget.h
Expand Up @@ -53,6 +53,7 @@ class GUI_EXPORT QgsDockWidget : public QDockWidget
* Returns true if the dock is both opened and raised to the front (ie not hidden by
* any other tabs.
* \see setUserVisible()
* \see toggleUserVisible()
*/
bool isUserVisible() const;

Expand All @@ -70,9 +71,21 @@ class GUI_EXPORT QgsDockWidget : public QDockWidget
* be closed
* - hiding a dock which is closed has no effect and raises no signals
* \see isUserVisible()
* \see toggleUserVisible()
*/
void setUserVisible( bool visible );

/**
* Toggles whether the dock is user visible. If the dock is not currently user
* visible (i.e. opened and activated as a tab) then the dock will be opened
* and raised. If it is currently user visible it will be closed.
*
* \see setUserVisible()
* \see isUserVisible()
* \since QGIS 3.2
*/
void toggleUserVisible();

protected:

void closeEvent( QCloseEvent * ) override;
Expand Down
44 changes: 44 additions & 0 deletions tests/src/gui/testqgsdockwidget.cpp
Expand Up @@ -32,6 +32,7 @@ class TestQgsDockWidget: public QObject
void testSignals();
void testUserVisible();
void testSetUserVisible();
void testToggleUserVisible();

private:

Expand Down Expand Up @@ -176,5 +177,48 @@ void TestQgsDockWidget::testSetUserVisible()

}

void TestQgsDockWidget::testToggleUserVisible()
{
QMainWindow *w = new QMainWindow();
QApplication::setActiveWindow( w ); //required for focus events
QgsDockWidget *d1 = new QgsDockWidget( w );
QgsDockWidget *d2 = new QgsDockWidget( w );
w->addDockWidget( Qt::RightDockWidgetArea, d1 );
w->addDockWidget( Qt::RightDockWidgetArea, d2 );
w->tabifyDockWidget( d1, d2 );
w->show();

QVERIFY( d2->isUserVisible() );
QVERIFY( !d1->isUserVisible() );

d1->toggleUserVisible();
QVERIFY( !d2->isUserVisible() );
QVERIFY( d2->isVisible() );
QVERIFY( d1->isUserVisible() );
QVERIFY( d1->isVisible() );

d2->hide();
d2->toggleUserVisible();
QVERIFY( d2->isUserVisible() );
QVERIFY( d2->isVisible() );
QVERIFY( !d1->isUserVisible() );
QVERIFY( d1->isVisible() );

d2->hide();
d1->raise(); //shouldn't be necessary outside of tests
QVERIFY( !d2->isUserVisible() );
QVERIFY( !d2->isVisible() );
QVERIFY( d1->isUserVisible() );
QVERIFY( d1->isVisible() );

d1->toggleUserVisible();
QVERIFY( !d2->isVisible() );
QVERIFY( !d1->isUserVisible() );
QVERIFY( !d1->isVisible() );

delete w;

}

QGSTEST_MAIN( TestQgsDockWidget )
#include "testqgsdockwidget.moc"

0 comments on commit a2ef677

Please sign in to comment.