QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
17#include <QDebug>
18#include <QPainter>
19#include <QStyleOption>
20
21#include "moc_qgstabbarproxystyle.cpp"
22
24
25QgsTabBarProxyStyle::QgsTabBarProxyStyle( QTabBar *tabBar )
26 : QgsProxyStyle( tabBar )
27{
28}
29
30void QgsTabBarProxyStyle::drawControl( ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget ) const
31{
32 QTabBar *tabBar { qobject_cast<QTabBar *>( parent() ) };
33
34 if ( tabBar )
35 {
36 if ( element == CE_TabBarTab && mTabStyles.contains( tabBar->tabAt( option->rect.center() ) ) )
37 {
38 if ( const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>( option ) )
39 {
40 painter->save();
41 const QgsAttributeEditorElement::LabelStyle &style { mTabStyles.value( tabBar->tabAt( option->rect.center() ) ) };
42 if ( style.overrideFont )
43 {
44 painter->setFont( style.font );
45 }
46 QStyleOptionTab opt { *tab };
47 if ( style.overrideColor && style.color.isValid() )
48 {
49 opt.palette.setBrush( QPalette::WindowText, style.color );
50 }
51 QProxyStyle::drawControl( element, &opt, painter, widget );
52 painter->restore();
53 return;
54 }
55 }
56 }
57
58 QProxyStyle::drawControl( element, option, painter, widget );
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
A QProxyStyle subclass which correctly sets the base style to match the QGIS application style,...
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.