QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 <QStyleFactory>
19 #include <QStyle>
20 #include <QStyleOption>
21 #include <QApplication>
22 
23 QgsProxyStyle::QgsProxyStyle( QWidget *parent )
24  : QProxyStyle( nullptr ) // no base style yet - it transfers ownership, so we need a NEW QStyle object for the base style
25 {
26  // get application style
27  const QString appStyle = QApplication::style()->objectName();
28  if ( appStyle == QLatin1String( "QgsAppStyle" ) )
29  {
30  setBaseStyle( static_cast< QgsAppStyle * >( QApplication::style() )->clone() );
31  }
32  else if ( !appStyle.isEmpty() )
33  {
34  if ( QStyle *style = QStyleFactory::create( appStyle ) )
35  setBaseStyle( style );
36  }
37 
38  // set lifetime to match parent widget's
39  setParent( parent );
40 }
41 
43 
44 //
45 // QgsAppStyle
46 //
47 
48 QgsAppStyle::QgsAppStyle( const QString &base )
49  : QProxyStyle( nullptr ) // no base style yet - it transfers ownership, so we need a NEW QStyle object for the base style
50  , mBaseStyle( base )
51 {
52  if ( !mBaseStyle.isEmpty() )
53  {
54  if ( QStyle *style = QStyleFactory::create( mBaseStyle ) )
55  setBaseStyle( style );
56  }
57 
58  setObjectName( QStringLiteral( "QgsAppStyle" ) );
59 }
60 
61 QPixmap QgsAppStyle::generatedIconPixmap( QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt ) const
62 {
63  switch ( iconMode )
64  {
65  case QIcon::Disabled:
66  {
67  if ( !pixmap.isNull() )
68  {
69  // override disabled icon style, with something which works better across different light/dark themes.
70  // the default Qt style here only works nicely for light themes.
71  QImage im = pixmap.toImage().convertToFormat( QImage::Format_ARGB32 );
74  return QPixmap::fromImage( im );
75  }
76  break;
77  }
78 
79  case QIcon::Normal:
80  case QIcon::Active:
81  case QIcon::Selected:
82  break;
83 
84  }
85  return QProxyStyle::generatedIconPixmap( iconMode, pixmap, opt );
86 }
87 
88 QProxyStyle *QgsAppStyle::clone()
89 {
90  return new QgsAppStyle( mBaseStyle );
91 }
92 
static void multiplyOpacity(QImage &image, double factor)
Multiplies opacity of image pixel values by a factor.
static void adjustHueSaturation(QImage &image, double saturation, const QColor &colorizeColor=QColor(), double colorizeStrength=1.0)
Alter the hue or saturation of a QImage.
QgsProxyStyle(QWidget *parent)
Constructor for QgsProxyStyle.