QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsproxystyle.cpp
Go to the documentation of this file.
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
18#include "qgis.h"
19#include "qgsimageoperation.h"
20
21#include <QApplication>
22#include <QStyle>
23#include <QStyleFactory>
24#include <QStyleOption>
25#include <QWindow>
26
27#include "moc_qgsproxystyle.cpp"
28
30 : QProxyStyle( nullptr ) // no base style yet - it transfers ownership, so we need a NEW QStyle object for the base style
31{
32 // get application style
33 const QString appStyle = QApplication::style()->objectName();
34 if ( appStyle == QLatin1String( "QgsAppStyle" ) )
35 {
36 setBaseStyle( static_cast<QgsAppStyle *>( QApplication::style() )->clone() );
37 }
38 else if ( !appStyle.isEmpty() )
39 {
40 if ( QStyle *style = QStyleFactory::create( appStyle ) )
41 setBaseStyle( style );
42 }
43
44 // set lifetime to match parent widget's
45 setParent( parent );
46}
47
49
50//
51// QgsAppStyle
52//
53
54QgsAppStyle::QgsAppStyle( const QString &base )
55 : QProxyStyle( nullptr ) // no base style yet - it transfers ownership, so we need a NEW QStyle object for the base style
56 , mBaseStyle( base )
57{
58 if ( !mBaseStyle.isEmpty() )
59 {
60 if ( QStyle *style = QStyleFactory::create( mBaseStyle ) )
61 setBaseStyle( style );
62 }
63
64 setObjectName( QStringLiteral( "QgsAppStyle" ) );
65}
66
67QPixmap QgsAppStyle::generatedIconPixmap( QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt ) const
68{
69 switch ( iconMode )
70 {
71 case QIcon::Disabled:
72 {
73 if ( !pixmap.isNull() )
74 {
75 // override disabled icon style, with something which works better across different light/dark themes.
76 // the default Qt style here only works nicely for light themes.
77 QImage im = pixmap.toImage().convertToFormat( QImage::Format_ARGB32 );
80 return QPixmap::fromImage( im );
81 }
82 return pixmap;
83 }
84
85 case QIcon::Normal:
86 case QIcon::Active:
87 case QIcon::Selected:
88 return pixmap;
89 }
90
91 return QProxyStyle::generatedIconPixmap( iconMode, pixmap, opt );
92}
93
94void QgsAppStyle::polish( QWidget *widget )
95{
96 QProxyStyle::polish( widget );
97#if defined( Q_OS_UNIX ) && !defined( Q_OS_ANDROID )
98 if ( mBaseStyle.contains( QLatin1String( "fusion" ), Qt::CaseInsensitive )
99 || mBaseStyle.contains( QLatin1String( "adwaita" ), Qt::CaseInsensitive ) )
100 {
101 // fix broken inactive window coloring applying to unfocused docks or list/tree widgets
102 // see eg https://github.com/FedoraQt/adwaita-qt/issues/126
103 // the detection used by themes to determine if a widget belongs to an activated window is fragile, which
104 // results in unfocused list/tree views or widget children being shown in the "deactivated window" palette coloring.
105 // Gnome (adwaita) defaults to a coloring which makes widgets looks disabled in this inactive state.
106 // So the best we can do here is force disable the inactive palette coloring to prevent this unwanted behavior.
107 QPalette pal = widget->palette();
108 pal.setColor( QPalette::Inactive, QPalette::Text, pal.color( QPalette::Active, QPalette::Text ) );
109 pal.setColor( QPalette::Inactive, QPalette::Window, pal.color( QPalette::Active, QPalette::Window ) );
110 pal.setColor( QPalette::Inactive, QPalette::WindowText, pal.color( QPalette::Active, QPalette::WindowText ) );
111 pal.setColor( QPalette::Inactive, QPalette::Button, pal.color( QPalette::Active, QPalette::Button ) );
112 pal.setColor( QPalette::Inactive, QPalette::ButtonText, pal.color( QPalette::Active, QPalette::ButtonText ) );
113 widget->setPalette( pal );
114 }
115#endif
116}
117
118QProxyStyle *QgsAppStyle::clone()
119{
120 return new QgsAppStyle( mBaseStyle );
121}
122
static void adjustHueSaturation(QImage &image, double saturation, const QColor &colorizeColor=QColor(), double colorizeStrength=1.0, QgsFeedback *feedback=nullptr)
Alter the hue or saturation of a QImage.
static void multiplyOpacity(QImage &image, double factor, QgsFeedback *feedback=nullptr)
Multiplies opacity of image pixel values by a factor.
QgsProxyStyle(QWidget *parent)
Constructor for QgsProxyStyle.