Skip to content

Commit dfa6b6a

Browse files
committedMar 10, 2018
Correctly set base style for QgsLayerTreeViewProxyStyle
Creates a new QgsProxyStyle subclass of QProxyStyle which automatically sets the base style to match the current application style (creating a new QStyle object, since setting the base style takes ownership). Additionally, QgsProxyStyle correctly parents the style to a parent widget, avoiding leaks since calling QWidget::setStyle doesn't transfer ownership. Fixes incorrect theme used for layer tree view since addition of indicator icons.
1 parent 0c1ceb3 commit dfa6b6a

12 files changed

+139
-16
lines changed
 

‎python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
%Include qgsprojectionselectiontreewidget.sip
178178
%Include qgspropertyassistantwidget.sip
179179
%Include qgspropertyoverridebutton.sip
180+
%Include qgsproxystyle.sip
180181
%Include qgsquerybuilder.sip
181182
%Include qgsrasterformatsaveoptionswidget.sip
182183
%Include qgsrasterlayersaveasdialog.sip

‎python/gui/qgsproxystyle.sip.in

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/qgsproxystyle.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsProxyStyle : QProxyStyle
12+
{
13+
%Docstring
14+
A QProxyStyle subclass which correctly sets the base style to match
15+
the QGIS application style, and handles object lifetime by correctly
16+
parenting to a parent widget.
17+
18+
.. versionadded:: 3.2
19+
%End
20+
21+
%TypeHeaderCode
22+
#include "qgsproxystyle.h"
23+
%End
24+
public:
25+
26+
explicit QgsProxyStyle( QWidget *parent /Transfer/ );
27+
%Docstring
28+
Constructor for QgsProxyStyle. Ownership is transferred to the ``parent`` widget.
29+
30+
The base style for the QProxyStyle will be set to match the current QGIS application style.
31+
%End
32+
};
33+
34+
/************************************************************************
35+
* This file has been generated automatically from *
36+
* *
37+
* src/gui/qgsproxystyle.h *
38+
* *
39+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
40+
************************************************************************/

‎python/gui/symbology/qgscategorizedsymbolrendererwidget.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212

1313

14+
1415
class QgsCategorizedSymbolRendererWidget : QgsRendererWidget
1516
{
1617

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ SET(QGIS_GUI_SRCS
332332
qgsprojectionselectiontreewidget.cpp
333333
qgspropertyassistantwidget.cpp
334334
qgspropertyoverridebutton.cpp
335+
qgsproxystyle.cpp
335336
qgsquerybuilder.cpp
336337
qgsrasterformatsaveoptionswidget.cpp
337338
qgsrasterlayersaveasdialog.cpp
@@ -500,6 +501,7 @@ SET(QGIS_GUI_MOC_HDRS
500501
qgsprojectionselectiontreewidget.h
501502
qgspropertyassistantwidget.h
502503
qgspropertyoverridebutton.h
504+
qgsproxystyle.h
503505
qgsquerybuilder.h
504506
qgsrasterformatsaveoptionswidget.h
505507
qgsrasterlayersaveasdialog.h

‎src/gui/layertree/qgslayertreeviewitemdelegate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
/// @cond PRIVATE
2626

2727
QgsLayerTreeViewProxyStyle::QgsLayerTreeViewProxyStyle( QgsLayerTreeView *treeView )
28-
: mLayerTreeView( treeView )
28+
: QgsProxyStyle( treeView )
29+
, mLayerTreeView( treeView )
2930
{
30-
setParent( treeView );
3131
}
3232

3333

‎src/gui/layertree/qgslayertreeviewitemdelegate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ SIP_NO_FILE
3333

3434
class QgsLayerTreeView;
3535

36-
#include <QProxyStyle>
36+
#include "qgsproxystyle.h"
3737
#include <QStyledItemDelegate>
3838

3939
/**
4040
* Proxy style to make the item text rect shorter so that indicators fit in without colliding with text
4141
*/
42-
class QgsLayerTreeViewProxyStyle : public QProxyStyle
42+
class QgsLayerTreeViewProxyStyle : public QgsProxyStyle
4343
{
4444
public:
4545
explicit QgsLayerTreeViewProxyStyle( QgsLayerTreeView *treeView );

‎src/gui/qgsproxystyle.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/***************************************************************************
2+
qgsproxystyle.cpp
3+
-----------------
4+
Date : March 2018
5+
Copyright : (C) 2018 by Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsproxystyle.h"
17+
#include <QStyleFactory>
18+
#include <QStyle>
19+
#include <QApplication>
20+
21+
QgsProxyStyle::QgsProxyStyle( QWidget *parent )
22+
: QProxyStyle( nullptr ) // no base style yet - it transfer ownership, so we need a NEW QStyle object for the base style
23+
{
24+
// get application style
25+
QString appStyle = QApplication::style()->objectName();
26+
if ( !appStyle.isEmpty() )
27+
{
28+
if ( QStyle *style = QStyleFactory::create( appStyle ) )
29+
setBaseStyle( style );
30+
}
31+
32+
// set lifetime to match parent widget's
33+
setParent( parent );
34+
}

‎src/gui/qgsproxystyle.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/***************************************************************************
2+
qgsproxystyle.h
3+
---------------
4+
Date : March 2018
5+
Copyright : (C) 2018 by Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSPROXYSTYLE_H
17+
#define QGSPROXYSTYLE_H
18+
19+
#include "qgis_sip.h"
20+
#include "qgsgui.h"
21+
#include <QProxyStyle>
22+
23+
/**
24+
* A QProxyStyle subclass which correctly sets the base style to match
25+
* the QGIS application style, and handles object lifetime by correctly
26+
* parenting to a parent widget.
27+
* \since QGIS 3.2
28+
* \ingroup gui
29+
*/
30+
class GUI_EXPORT QgsProxyStyle : public QProxyStyle
31+
{
32+
Q_OBJECT
33+
34+
public:
35+
36+
/**
37+
* Constructor for QgsProxyStyle. Ownership is transferred to the \a parent widget.
38+
*
39+
* The base style for the QProxyStyle will be set to match the current QGIS application style.
40+
*/
41+
explicit QgsProxyStyle( QWidget *parent SIP_TRANSFER );
42+
};
43+
44+
#endif // QGSPROXYSTYLE_H

‎src/gui/symbology/qgscategorizedsymbolrendererwidget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ void QgsCategorizedSymbolRendererModel::updateSymbology()
364364
}
365365

366366
// ------------------------------ View style --------------------------------
367-
QgsCategorizedSymbolRendererViewStyle::QgsCategorizedSymbolRendererViewStyle( QStyle *style )
368-
: QProxyStyle( style )
367+
QgsCategorizedSymbolRendererViewStyle::QgsCategorizedSymbolRendererViewStyle( QWidget *parent )
368+
: QgsProxyStyle( parent )
369369
{}
370370

371371
void QgsCategorizedSymbolRendererViewStyle::drawPrimitive( PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget ) const
@@ -443,7 +443,7 @@ QgsCategorizedSymbolRendererWidget::QgsCategorizedSymbolRendererWidget( QgsVecto
443443
viewCategories->resizeColumnToContents( 1 );
444444
viewCategories->resizeColumnToContents( 2 );
445445

446-
viewCategories->setStyle( new QgsCategorizedSymbolRendererViewStyle( viewCategories->style() ) );
446+
viewCategories->setStyle( new QgsCategorizedSymbolRendererViewStyle( viewCategories ) );
447447

448448
connect( mModel, &QgsCategorizedSymbolRendererModel::rowsMoved, this, &QgsCategorizedSymbolRendererWidget::rowsMoved );
449449
connect( mModel, &QAbstractItemModel::dataChanged, this, &QgsPanelWidget::widgetChanged );

‎src/gui/symbology/qgscategorizedsymbolrendererwidget.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
#include "qgscategorizedsymbolrenderer.h"
1919
#include "qgis.h"
2020
#include "qgsrendererwidget.h"
21+
#include "qgsproxystyle.h"
2122
#include <QStandardItem>
22-
#include <QProxyStyle>
23+
2324

2425
class QgsCategorizedSymbolRenderer;
2526
class QgsRendererCategory;
@@ -70,12 +71,12 @@ class GUI_EXPORT QgsCategorizedSymbolRendererModel : public QAbstractItemModel
7071
* \ingroup gui
7172
* View style which shows drop indicator line between items
7273
*/
73-
class QgsCategorizedSymbolRendererViewStyle: public QProxyStyle
74+
class QgsCategorizedSymbolRendererViewStyle: public QgsProxyStyle
7475
{
7576
Q_OBJECT
7677

7778
public:
78-
explicit QgsCategorizedSymbolRendererViewStyle( QStyle *style = nullptr );
79+
explicit QgsCategorizedSymbolRendererViewStyle( QWidget *parent );
7980

8081
void drawPrimitive( PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr ) const override;
8182
};

‎src/gui/symbology/qgsgraduatedsymbolrendererwidget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ void QgsGraduatedSymbolRendererModel::updateLabels()
377377
}
378378

379379
// ------------------------------ View style --------------------------------
380-
QgsGraduatedSymbolRendererViewStyle::QgsGraduatedSymbolRendererViewStyle( QStyle *style )
381-
: QProxyStyle( style )
380+
QgsGraduatedSymbolRendererViewStyle::QgsGraduatedSymbolRendererViewStyle( QWidget *parent )
381+
: QgsProxyStyle( parent )
382382
{}
383383

384384
void QgsGraduatedSymbolRendererViewStyle::drawPrimitive( PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget ) const
@@ -483,7 +483,7 @@ QgsGraduatedSymbolRendererWidget::QgsGraduatedSymbolRendererWidget( QgsVectorLay
483483
}
484484

485485

486-
viewGraduated->setStyle( new QgsGraduatedSymbolRendererViewStyle( viewGraduated->style() ) );
486+
viewGraduated->setStyle( new QgsGraduatedSymbolRendererViewStyle( viewGraduated ) );
487487

488488
mGraduatedSymbol = QgsSymbol::defaultSymbol( mLayer->geometryType() );
489489

‎src/gui/symbology/qgsgraduatedsymbolrendererwidget.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "qgsgraduatedsymbolrenderer.h"
2020
#include "qgis.h"
2121
#include "qgsrendererwidget.h"
22+
#include "qgsproxystyle.h"
2223
#include <QStandardItem>
23-
#include <QProxyStyle>
2424

2525
#include "ui_qgsgraduatedsymbolrendererv2widget.h"
2626
#include "qgis_gui.h"
@@ -66,12 +66,12 @@ class GUI_EXPORT QgsGraduatedSymbolRendererModel : public QAbstractItemModel
6666
};
6767

6868
// View style which shows drop indicator line between items
69-
class QgsGraduatedSymbolRendererViewStyle: public QProxyStyle
69+
class QgsGraduatedSymbolRendererViewStyle: public QgsProxyStyle
7070
{
7171
Q_OBJECT
7272

7373
public:
74-
explicit QgsGraduatedSymbolRendererViewStyle( QStyle *style = nullptr );
74+
explicit QgsGraduatedSymbolRendererViewStyle( QWidget *parent );
7575

7676
void drawPrimitive( PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr ) const override;
7777
};

0 commit comments

Comments
 (0)
Please sign in to comment.