Skip to content

Commit

Permalink
New method to find parent QgsPanelWidget for a widget
Browse files Browse the repository at this point in the history
(cherry-picked from a2fe4c4)
  • Loading branch information
nyalldawson committed Aug 17, 2016
1 parent 7e680b0 commit 9bd1e75
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
12 changes: 10 additions & 2 deletions python/gui/qgspanelwidget.sip
Expand Up @@ -60,20 +60,28 @@ class QgsPanelWidget : public QWidget

/**
* The the auto delete property on the widget. True by default.
* When auto delete is enabeld when a panel is removed from the stack
* When auto delete is enabled when a panel is removed from the stack
* it will be deleted.
* @param autoDelete Enable or disable auto delete on the panel.
*/
void setAutoDelete( bool autoDelete );

/**
* The the auto delete property on the widget. True by default.
* When auto delete is enabeld when a panel is removed from the stack
* When auto delete is enabled when a panel is removed from the stack
* it will be deleted.
* @returns The auto delete value for the widget.
*/
bool autoDelete();

/** Traces through the parents of a widget to find if it is contained within a QgsPanelWidget
* widget.
* @param widget widget which may be contained within a panel widget
* @returns parent panel widget if found, otherwise nullptr
* @note added in QGIS 3.0
*/
static QgsPanelWidget* findParentPanel( QWidget* widget );

signals:

/**
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgspanelwidget.cpp
Expand Up @@ -47,6 +47,19 @@ void QgsPanelWidget::setDockMode( bool dockMode )
mDockMode = dockMode;
}

QgsPanelWidget*QgsPanelWidget::findParentPanel( QWidget* widget )
{
QWidget* p = widget;
while ( p )
{
if ( QgsPanelWidget* panel = qobject_cast< QgsPanelWidget* >( p ) )
return panel;

p = p->parentWidget();
}
return nullptr;
}

void QgsPanelWidget::openPanel( QgsPanelWidget* panel )
{
if ( mDockMode )
Expand Down
12 changes: 10 additions & 2 deletions src/gui/qgspanelwidget.h
Expand Up @@ -79,20 +79,28 @@ class GUI_EXPORT QgsPanelWidget : public QWidget

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

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

/** Traces through the parents of a widget to find if it is contained within a QgsPanelWidget
* widget.
* @param widget widget which may be contained within a panel widget
* @returns parent panel widget if found, otherwise nullptr
* @note added in QGIS 3.0
*/
static QgsPanelWidget* findParentPanel( QWidget* widget );

signals:

/**
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -65,6 +65,7 @@ ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)
ADD_PYTHON_TEST(PyQgsPalLabelingPlacement test_qgspallabeling_placement.py)
ADD_PYTHON_TEST(PyQgsPanelWidget test_qgspanelwidget.py)
ADD_PYTHON_TEST(PyQgsPoint test_qgspoint.py)
ADD_PYTHON_TEST(PyQgsRangeWidgets test_qgsrangewidgets.py)
ADD_PYTHON_TEST(PyQgsRasterFileWriter test_qgsrasterfilewriter.py)
Expand Down
54 changes: 54 additions & 0 deletions tests/src/python/test_qgspanelwidget.py
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsPanelWidget.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '16/08/2016'
__copyright__ = 'Copyright 2016, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

from qgis.PyQt.QtWidgets import QWidget
from qgis.gui import QgsPanelWidget
from qgis.testing import start_app, unittest

start_app()


class TestQgsPanelWidget(unittest.TestCase):

def testFindParentPanel(self):
""" test QgsPanelWidget.findParentPanel """

# no widget
self.assertFalse(QgsPanelWidget.findParentPanel(None))

# widget with no parent
w = QWidget()
self.assertFalse(QgsPanelWidget.findParentPanel(w))

# widget with no panel parent
w2 = QWidget(w)
self.assertFalse(QgsPanelWidget.findParentPanel(w2))

# panel widget itself
w3 = QgsPanelWidget()
self.assertEqual(QgsPanelWidget.findParentPanel(w3), w3)

# widget with direct QgsPanelWidget parent
w4 = QWidget(w3)
self.assertEqual(QgsPanelWidget.findParentPanel(w4), w3)

# widget with QgsPanelWidget grandparent
w5 = QWidget(w4)
self.assertEqual(QgsPanelWidget.findParentPanel(w5), w3)


if __name__ == '__main__':
unittest.main()

0 comments on commit 9bd1e75

Please sign in to comment.