Skip to content

Commit 9bd1e75

Browse files
committedAug 17, 2016
New method to find parent QgsPanelWidget for a widget
(cherry-picked from a2fe4c4)
1 parent 7e680b0 commit 9bd1e75

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed
 

‎python/gui/qgspanelwidget.sip

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,28 @@ class QgsPanelWidget : public QWidget
6060

6161
/**
6262
* The the auto delete property on the widget. True by default.
63-
* When auto delete is enabeld when a panel is removed from the stack
63+
* When auto delete is enabled when a panel is removed from the stack
6464
* it will be deleted.
6565
* @param autoDelete Enable or disable auto delete on the panel.
6666
*/
6767
void setAutoDelete( bool autoDelete );
6868

6969
/**
7070
* The the auto delete property on the widget. True by default.
71-
* When auto delete is enabeld when a panel is removed from the stack
71+
* When auto delete is enabled when a panel is removed from the stack
7272
* it will be deleted.
7373
* @returns The auto delete value for the widget.
7474
*/
7575
bool autoDelete();
7676

77+
/** Traces through the parents of a widget to find if it is contained within a QgsPanelWidget
78+
* widget.
79+
* @param widget widget which may be contained within a panel widget
80+
* @returns parent panel widget if found, otherwise nullptr
81+
* @note added in QGIS 3.0
82+
*/
83+
static QgsPanelWidget* findParentPanel( QWidget* widget );
84+
7785
signals:
7886

7987
/**

‎src/gui/qgspanelwidget.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ void QgsPanelWidget::setDockMode( bool dockMode )
4747
mDockMode = dockMode;
4848
}
4949

50+
QgsPanelWidget*QgsPanelWidget::findParentPanel( QWidget* widget )
51+
{
52+
QWidget* p = widget;
53+
while ( p )
54+
{
55+
if ( QgsPanelWidget* panel = qobject_cast< QgsPanelWidget* >( p ) )
56+
return panel;
57+
58+
p = p->parentWidget();
59+
}
60+
return nullptr;
61+
}
62+
5063
void QgsPanelWidget::openPanel( QgsPanelWidget* panel )
5164
{
5265
if ( mDockMode )

‎src/gui/qgspanelwidget.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,28 @@ class GUI_EXPORT QgsPanelWidget : public QWidget
7979

8080
/**
8181
* The the auto delete property on the widget. True by default.
82-
* When auto delete is enabeld when a panel is removed from the stack
82+
* When auto delete is enabled when a panel is removed from the stack
8383
* it will be deleted.
8484
* @param autoDelete Enable or disable auto delete on the panel.
8585
*/
8686
void setAutoDelete( bool autoDelete ) { mAutoDelete = autoDelete; }
8787

8888
/**
8989
* The the auto delete property on the widget. True by default.
90-
* When auto delete is enabeld when a panel is removed from the stack
90+
* When auto delete is enabled when a panel is removed from the stack
9191
* it will be deleted.
9292
* @returns The auto delete value for the widget.
9393
*/
9494
bool autoDelete() { return mAutoDelete; }
9595

96+
/** Traces through the parents of a widget to find if it is contained within a QgsPanelWidget
97+
* widget.
98+
* @param widget widget which may be contained within a panel widget
99+
* @returns parent panel widget if found, otherwise nullptr
100+
* @note added in QGIS 3.0
101+
*/
102+
static QgsPanelWidget* findParentPanel( QWidget* widget );
103+
96104
signals:
97105

98106
/**

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
6565
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
6666
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)
6767
ADD_PYTHON_TEST(PyQgsPalLabelingPlacement test_qgspallabeling_placement.py)
68+
ADD_PYTHON_TEST(PyQgsPanelWidget test_qgspanelwidget.py)
6869
ADD_PYTHON_TEST(PyQgsPoint test_qgspoint.py)
6970
ADD_PYTHON_TEST(PyQgsRangeWidgets test_qgsrangewidgets.py)
7071
ADD_PYTHON_TEST(PyQgsRasterFileWriter test_qgsrasterfilewriter.py)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsPanelWidget.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '16/08/2016'
11+
__copyright__ = 'Copyright 2016, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
from qgis.PyQt.QtWidgets import QWidget
18+
from qgis.gui import QgsPanelWidget
19+
from qgis.testing import start_app, unittest
20+
21+
start_app()
22+
23+
24+
class TestQgsPanelWidget(unittest.TestCase):
25+
26+
def testFindParentPanel(self):
27+
""" test QgsPanelWidget.findParentPanel """
28+
29+
# no widget
30+
self.assertFalse(QgsPanelWidget.findParentPanel(None))
31+
32+
# widget with no parent
33+
w = QWidget()
34+
self.assertFalse(QgsPanelWidget.findParentPanel(w))
35+
36+
# widget with no panel parent
37+
w2 = QWidget(w)
38+
self.assertFalse(QgsPanelWidget.findParentPanel(w2))
39+
40+
# panel widget itself
41+
w3 = QgsPanelWidget()
42+
self.assertEqual(QgsPanelWidget.findParentPanel(w3), w3)
43+
44+
# widget with direct QgsPanelWidget parent
45+
w4 = QWidget(w3)
46+
self.assertEqual(QgsPanelWidget.findParentPanel(w4), w3)
47+
48+
# widget with QgsPanelWidget grandparent
49+
w5 = QWidget(w4)
50+
self.assertEqual(QgsPanelWidget.findParentPanel(w5), w3)
51+
52+
53+
if __name__ == '__main__':
54+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.