Skip to content

Commit

Permalink
Merge pull request #3398 from nyalldawson/grad
Browse files Browse the repository at this point in the history
FEATURE: Open color dialog inside layer style panel
  • Loading branch information
nyalldawson committed Aug 16, 2016
2 parents a3149b1 + 5407ae8 commit 22be7ed
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 7 deletions.
2 changes: 1 addition & 1 deletion python/gui/qgscompoundcolorwidget.sip
Expand Up @@ -6,7 +6,7 @@
* \note Added in version 2.16
*/

class QgsCompoundColorWidget : QWidget
class QgsCompoundColorWidget : QgsPanelWidget
{
%TypeHeaderCode
#include <qgscompoundcolorwidget.h>
Expand Down
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
27 changes: 27 additions & 0 deletions src/gui/qgscolorbutton.cpp
Expand Up @@ -94,6 +94,17 @@ const QPixmap& QgsColorButton::transparentBackground()

void QgsColorButton::showColorDialog()
{
if ( QgsPanelWidget* panel = QgsPanelWidget::findParentPanel( this ) )
{
QgsCompoundColorWidget* colorWidget = new QgsCompoundColorWidget( panel, color() );
colorWidget->setPanelTitle( mColorDialogTitle );
colorWidget->setAllowAlpha( mAllowAlpha );
connect( colorWidget, SIGNAL( currentColorChanged( QColor ) ), this, SLOT( setValidTemporaryColor( QColor ) ) );
connect( colorWidget, SIGNAL( panelAccepted( QgsPanelWidget* ) ), this, SLOT( panelAccepted( QgsPanelWidget* ) ) );
panel->openPanel( colorWidget );
return;
}

QColor newColor;
QSettings settings;

Expand Down Expand Up @@ -358,6 +369,22 @@ void QgsColorButton::setValidColor( const QColor& newColor )
}
}

void QgsColorButton::setValidTemporaryColor( const QColor& newColor )
{
if ( newColor.isValid() )
{
setColor( newColor );
}
}

void QgsColorButton::panelAccepted( QgsPanelWidget* widget )
{
if ( QgsCompoundColorWidget* colorWidget = qobject_cast< QgsCompoundColorWidget* >( widget ) )
{
addRecentColor( colorWidget->color() );
}
}

QPixmap QgsColorButton::createMenuIcon( const QColor &color, const bool showChecks )
{
//create an icon pixmap
Expand Down
9 changes: 9 additions & 0 deletions src/gui/qgscolorbutton.h
Expand Up @@ -21,6 +21,7 @@

class QMimeData;
class QgsColorSchemeRegistry;
class QgsPanelWidget;

/** \ingroup gui
* \class QgsColorButton
Expand Down Expand Up @@ -419,6 +420,14 @@ class GUI_EXPORT QgsColorButton : public QToolButton
*/
void setValidColor( const QColor& newColor );

/** Sets color for button, if valid. The color is treated as a temporary color, and is not
* added to the recent colors list.
*/
void setValidTemporaryColor( const QColor& newColor );

//! Called when a color widget panel is accepted, and adds the final color to the recent colors list
void panelAccepted( QgsPanelWidget* widget );

/** Adds a color to the recent colors list
* @param color to add to recent colors list
*/
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgscompoundcolorwidget.cpp
Expand Up @@ -31,7 +31,7 @@


QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor& color )
: QWidget( parent )
: QgsPanelWidget( parent )
, mAllowAlpha( true )
, mLastCustomColorIndex( 0 )
, mPickingColor( false )
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgscompoundcolorwidget.h
Expand Up @@ -17,6 +17,7 @@
#define QGSCOMPOUNDCOLORWIDGET_H

#include "qgisgui.h"
#include "qgspanelwidget.h"
#include "ui_qgscompoundcolorwidget.h"

/** \ingroup gui
Expand All @@ -26,7 +27,7 @@
* \note Added in version 2.16
*/

class GUI_EXPORT QgsCompoundColorWidget : public QWidget, private Ui::QgsCompoundColorWidgetBase
class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::QgsCompoundColorWidgetBase
{

Q_OBJECT
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 @@ -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)
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 22be7ed

Please sign in to comment.