QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "moc_qgstabbarproxystyle.cpp"
17#include <QPainter>
18#include <QStyleOption>
19#include <QDebug>
20
21
23
24QgsTabBarProxyStyle::QgsTabBarProxyStyle( QTabBar *tabBar )
25 : QgsProxyStyle( tabBar )
26{
27}
28
29void QgsTabBarProxyStyle::drawControl( ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget ) const
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
60void QgsTabBarProxyStyle::addStyle( int tabIndex, const QgsAttributeEditorElement::LabelStyle &style )
61{
62 mTabStyles.insert( tabIndex, style );
63}
64
65const QMap<int, QgsAttributeEditorElement::LabelStyle> &QgsTabBarProxyStyle::tabStyles() const
66{
67 return mTabStyles;
68}
69
70
71QgsTabBar::QgsTabBar( QWidget *parent )
72 : QTabBar( parent )
73{
74}
75
76void QgsTabBar::setTabBarStyle( QgsTabBarProxyStyle *tabStyle )
77{
78 mTabBarStyle = tabStyle;
79}
80
81QSize QgsTabBar::tabSizeHint( int index ) const
82{
83 if ( mTabBarStyle->tabStyles().contains( index ) )
84 {
85 const QgsAttributeEditorElement::LabelStyle tabStyle = mTabBarStyle->tabStyles().value( index );
86 if ( tabStyle.overrideFont )
87 {
88 const QSize s = QTabBar::tabSizeHint( index );
89 const QFontMetrics fm( font() );
90 const int w = fm.horizontalAdvance( tabText( index ) );
91 const QFont f = tabStyle.font;
92 const QFontMetrics bfm( f );
93 const int bw = bfm.horizontalAdvance( tabText( index ) );
94 return QSize( s.width() - w + bw, s.height() );
95 }
96 else
97 {
98 return QTabBar::tabSizeHint( index );
99 }
100 }
101 else
102 {
103 return QTabBar::tabSizeHint( index );
104 }
105}
106
107
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.