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