QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgstabbarproxystyle.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstabbarproxystyle.cpp - QgsTabBarProxyStyle
3 ---------------------
4 begin : 25.3.2022
5 copyright : (C) 2022 by Alessandro Pasotti
6 email : elpaso at itopen dot it
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#include "qgstabbarproxystyle.h"
16#include <QPainter>
17#include <QStyleOption>
18#include <QDebug>
19
20
22
23QgsTabBarProxyStyle::QgsTabBarProxyStyle( QTabBar *tabBar )
24 : QgsProxyStyle( tabBar )
25{
26}
27
28void QgsTabBarProxyStyle::drawControl( ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget ) const
29{
30
31 QTabBar *tabBar { qobject_cast<QTabBar *>( parent() ) };
32
33 if ( tabBar )
34 {
35 if ( element == CE_TabBarTab && mTabStyles.contains( tabBar->tabAt( option->rect.center() ) ) )
36 {
37 if ( const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>( option ) )
38 {
39 painter->save();
40 const QgsAttributeEditorElement::LabelStyle &style { mTabStyles.value( tabBar->tabAt( option->rect.center() ) ) };
41 if ( style.overrideFont )
42 {
43 painter->setFont( style.font );
44 }
45 QStyleOptionTab opt { *tab };
46 if ( style.overrideColor && style.color.isValid( ) )
47 {
48 opt.palette.setBrush( QPalette::WindowText, style.color );
49 }
50 QProxyStyle::drawControl( element, &opt, painter, widget );
51 painter->restore();
52 return;
53 }
54 }
55 }
56
57 QProxyStyle::drawControl( element, option, painter, widget );
58
59}
60
61void QgsTabBarProxyStyle::addStyle( int tabIndex, const QgsAttributeEditorElement::LabelStyle &style )
62{
63 mTabStyles.insert( tabIndex, style );
64}
65
66const QMap<int, QgsAttributeEditorElement::LabelStyle> &QgsTabBarProxyStyle::tabStyles() const
67{
68 return mTabStyles;
69}
70
71
72QgsTabBar::QgsTabBar( QWidget *parent )
73 : QTabBar( parent )
74{
75}
76
77void QgsTabBar::setTabBarStyle( QgsTabBarProxyStyle *tabStyle )
78{
79 mTabBarStyle = tabStyle;
80}
81
82QSize QgsTabBar::tabSizeHint( int index ) const
83{
84 if ( mTabBarStyle->tabStyles().contains( index ) )
85 {
86 const QgsAttributeEditorElement::LabelStyle tabStyle = mTabBarStyle->tabStyles().value( index );
87 if ( tabStyle.overrideFont )
88 {
89 const QSize s = QTabBar::tabSizeHint( index );
90 const QFontMetrics fm( font() );
91 const int w = fm.horizontalAdvance( tabText( index ) );
92 const QFont f = tabStyle.font;
93 const QFontMetrics bfm( f );
94 const int bw = bfm.horizontalAdvance( tabText( index ) );
95 return QSize( s.width() - w + bw, s.height() );
96 }
97 else
98 {
99 return QTabBar::tabSizeHint( index );
100 }
101 }
102 else
103 {
104 return QTabBar::tabSizeHint( index );
105 }
106}
107
108
109
A QProxyStyle subclass which correctly sets the base style to match the QGIS application style,...
Definition: qgsproxystyle.h:31
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.