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