Skip to content

Commit 22be7ed

Browse files
authored
Merge pull request #3398 from nyalldawson/grad
FEATURE: Open color dialog inside layer style panel
2 parents a3149b1 + 5407ae8 commit 22be7ed

10 files changed

+128
-7
lines changed

python/gui/qgscompoundcolorwidget.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* \note Added in version 2.16
77
*/
88

9-
class QgsCompoundColorWidget : QWidget
9+
class QgsCompoundColorWidget : QgsPanelWidget
1010
{
1111
%TypeHeaderCode
1212
#include <qgscompoundcolorwidget.h>

python/gui/qgspanelwidget.sip

+10-2
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/qgscolorbutton.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ const QPixmap& QgsColorButton::transparentBackground()
9494

9595
void QgsColorButton::showColorDialog()
9696
{
97+
if ( QgsPanelWidget* panel = QgsPanelWidget::findParentPanel( this ) )
98+
{
99+
QgsCompoundColorWidget* colorWidget = new QgsCompoundColorWidget( panel, color() );
100+
colorWidget->setPanelTitle( mColorDialogTitle );
101+
colorWidget->setAllowAlpha( mAllowAlpha );
102+
connect( colorWidget, SIGNAL( currentColorChanged( QColor ) ), this, SLOT( setValidTemporaryColor( QColor ) ) );
103+
connect( colorWidget, SIGNAL( panelAccepted( QgsPanelWidget* ) ), this, SLOT( panelAccepted( QgsPanelWidget* ) ) );
104+
panel->openPanel( colorWidget );
105+
return;
106+
}
107+
97108
QColor newColor;
98109
QSettings settings;
99110

@@ -358,6 +369,22 @@ void QgsColorButton::setValidColor( const QColor& newColor )
358369
}
359370
}
360371

372+
void QgsColorButton::setValidTemporaryColor( const QColor& newColor )
373+
{
374+
if ( newColor.isValid() )
375+
{
376+
setColor( newColor );
377+
}
378+
}
379+
380+
void QgsColorButton::panelAccepted( QgsPanelWidget* widget )
381+
{
382+
if ( QgsCompoundColorWidget* colorWidget = qobject_cast< QgsCompoundColorWidget* >( widget ) )
383+
{
384+
addRecentColor( colorWidget->color() );
385+
}
386+
}
387+
361388
QPixmap QgsColorButton::createMenuIcon( const QColor &color, const bool showChecks )
362389
{
363390
//create an icon pixmap

src/gui/qgscolorbutton.h

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
class QMimeData;
2323
class QgsColorSchemeRegistry;
24+
class QgsPanelWidget;
2425

2526
/** \ingroup gui
2627
* \class QgsColorButton
@@ -419,6 +420,14 @@ class GUI_EXPORT QgsColorButton : public QToolButton
419420
*/
420421
void setValidColor( const QColor& newColor );
421422

423+
/** Sets color for button, if valid. The color is treated as a temporary color, and is not
424+
* added to the recent colors list.
425+
*/
426+
void setValidTemporaryColor( const QColor& newColor );
427+
428+
//! Called when a color widget panel is accepted, and adds the final color to the recent colors list
429+
void panelAccepted( QgsPanelWidget* widget );
430+
422431
/** Adds a color to the recent colors list
423432
* @param color to add to recent colors list
424433
*/

src/gui/qgscompoundcolorwidget.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor& color )
34-
: QWidget( parent )
34+
: QgsPanelWidget( parent )
3535
, mAllowAlpha( true )
3636
, mLastCustomColorIndex( 0 )
3737
, mPickingColor( false )

src/gui/qgscompoundcolorwidget.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define QGSCOMPOUNDCOLORWIDGET_H
1818

1919
#include "qgisgui.h"
20+
#include "qgspanelwidget.h"
2021
#include "ui_qgscompoundcolorwidget.h"
2122

2223
/** \ingroup gui
@@ -26,7 +27,7 @@
2627
* \note Added in version 2.16
2728
*/
2829

29-
class GUI_EXPORT QgsCompoundColorWidget : public QWidget, private Ui::QgsCompoundColorWidgetBase
30+
class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::QgsCompoundColorWidgetBase
3031
{
3132

3233
Q_OBJECT

src/gui/qgspanelwidget.cpp

+13
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

+10-2
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

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
6464
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
6565
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)
6666
ADD_PYTHON_TEST(PyQgsPalLabelingPlacement test_qgspallabeling_placement.py)
67+
ADD_PYTHON_TEST(PyQgsPanelWidget test_qgspanelwidget.py)
6768
ADD_PYTHON_TEST(PyQgsPoint test_qgspoint.py)
6869
ADD_PYTHON_TEST(PyQgsRangeWidgets test_qgsrangewidgets.py)
6970
ADD_PYTHON_TEST(PyQgsRasterFileWriter test_qgsrasterfilewriter.py)
+54
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)