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