Skip to content

Commit b7b638c

Browse files
committedOct 16, 2018
Simpler API to link actions to QgsDockWidget
1 parent 51f6fb0 commit b7b638c

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
 

‎python/gui/auto_generated/qgsdockwidget.sip.in

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ any other tabs.
4747
.. seealso:: :py:func:`setUserVisible`
4848

4949
.. seealso:: :py:func:`toggleUserVisible`
50+
%End
51+
52+
void setLinkedAction( QAction *action );
53+
%Docstring
54+
Links an ``action`` to the dock, so that toggling the action will automatically set the dock's visibility
55+
to suit (and changing the dock visibility will update the action's state).
56+
57+
.. seealso:: :py:func:`linkedAction`
58+
59+
.. versionadded:: 3.4
60+
%End
61+
62+
QAction *linkedAction();
63+
%Docstring
64+
Returns the action linked to the dock.
65+
66+
.. seealso:: :py:func:`setLinkedAction`
67+
68+
.. versionadded:: 3.4
5069
%End
5170

5271
public slots:

‎src/gui/qgsdockwidget.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919
#include "qgsdockwidget.h"
20+
#include <QAction>
2021

2122

2223
QgsDockWidget::QgsDockWidget( QWidget *parent, Qt::WindowFlags flags )
@@ -57,6 +58,27 @@ bool QgsDockWidget::isUserVisible() const
5758
return mVisibleAndActive;
5859
}
5960

61+
void QgsDockWidget::setLinkedAction( QAction *action )
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+
77+
QAction *QgsDockWidget::linkedAction()
78+
{
79+
return mAction;
80+
}
81+
6082
void QgsDockWidget::closeEvent( QCloseEvent *e )
6183
{
6284
emit closed();

‎src/gui/qgsdockwidget.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ class GUI_EXPORT QgsDockWidget : public QDockWidget
5757
*/
5858
bool isUserVisible() const;
5959

60+
/**
61+
* Links an \a action to the dock, so that toggling the action will automatically set the dock's visibility
62+
* to suit (and changing the dock visibility will update the action's state).
63+
*
64+
* \see linkedAction()
65+
* \since QGIS 3.4
66+
*/
67+
void setLinkedAction( QAction *action );
68+
69+
/**
70+
* Returns the action linked to the dock.
71+
*
72+
* \see setLinkedAction()
73+
* \since QGIS 3.4
74+
*/
75+
QAction *linkedAction();
76+
6077
public slots:
6178

6279
/**
@@ -131,5 +148,7 @@ class GUI_EXPORT QgsDockWidget : public QDockWidget
131148

132149
bool mVisibleAndActive = false;
133150

151+
QAction *mAction = nullptr;
152+
134153
};
135154
#endif //QGSDOCKWIDGET_H

‎tests/src/gui/testqgsdockwidget.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgsdockwidget.h"
2020
#include <QApplication>
2121
#include <QMainWindow>
22+
#include <QAction>
2223

2324
class TestQgsDockWidget: public QObject
2425
{
@@ -33,6 +34,7 @@ class TestQgsDockWidget: public QObject
3334
void testUserVisible();
3435
void testSetUserVisible();
3536
void testToggleUserVisible();
37+
void testAction();
3638

3739
private:
3840

@@ -220,5 +222,61 @@ void TestQgsDockWidget::testToggleUserVisible()
220222

221223
}
222224

225+
void TestQgsDockWidget::testAction()
226+
{
227+
QMainWindow *w = new QMainWindow();
228+
QApplication::setActiveWindow( w ); //required for focus events
229+
QgsDockWidget *d1 = new QgsDockWidget( w );
230+
QgsDockWidget *d2 = new QgsDockWidget( w );
231+
w->addDockWidget( Qt::RightDockWidgetArea, d1 );
232+
w->addDockWidget( Qt::RightDockWidgetArea, d2 );
233+
w->tabifyDockWidget( d1, d2 );
234+
w->show();
235+
236+
QAction *a1 = new QAction( w );
237+
QAction *a2 = new QAction( w );
238+
239+
QVERIFY( ! d1->linkedAction() );
240+
d1->setLinkedAction( a1 );
241+
d2->setLinkedAction( a2 );
242+
QVERIFY( a1->isCheckable() );
243+
QVERIFY( a2->isCheckable() );
244+
QCOMPARE( d1->linkedAction(), a1 );
245+
QCOMPARE( d2->linkedAction(), a2 );
246+
247+
QVERIFY( d2->isUserVisible() );
248+
QVERIFY( a2->isChecked() );
249+
QVERIFY( !d1->isUserVisible() );
250+
QVERIFY( !a1->isChecked() );
251+
252+
a1->setChecked( true );
253+
QVERIFY( !d2->isUserVisible() );
254+
QVERIFY( !a2->isChecked() );
255+
QVERIFY( d1->isUserVisible() );
256+
QVERIFY( a1->isChecked() );
257+
258+
a1->setChecked( true );
259+
QVERIFY( !d2->isUserVisible() );
260+
QVERIFY( !a2->isChecked() );
261+
QVERIFY( d1->isUserVisible() );
262+
QVERIFY( a1->isChecked() );
263+
264+
a2->setChecked( true );
265+
QVERIFY( d2->isUserVisible() );
266+
QVERIFY( a2->isChecked() );
267+
QVERIFY( !d1->isUserVisible() );
268+
QVERIFY( !a1->isChecked() );
269+
270+
d2->hide();
271+
d1->raise(); //shouldn't be necessary outside of tests
272+
QVERIFY( !a2->isChecked() );
273+
QVERIFY( d1->isUserVisible() );
274+
QVERIFY( a1->isChecked() );
275+
276+
a1->setChecked( false );
277+
QVERIFY( !d1->isUserVisible() );
278+
QVERIFY( !a1->isChecked() );
279+
}
280+
223281
QGSTEST_MAIN( TestQgsDockWidget )
224282
#include "testqgsdockwidget.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.