Skip to content
Permalink
Browse files

New method to find parent QgsPanelWidget for a widget

  • Loading branch information
nyalldawson committed Aug 15, 2016
1 parent 2976646 commit a2fe4c4f594e2e8d29de65f09271d148ac669028
@@ -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:

/**
@@ -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 )
@@ -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:

/**
@@ -64,6 +64,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)
@@ -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 a2fe4c4

Please sign in to comment.
You can’t perform that action at this time.