QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
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}
61
62void QgsTabBarProxyStyle::addStyle( int tabIndex, const QgsAttributeEditorElement::LabelStyle &style )
63{
64 mTabStyles.insert( tabIndex, style );
65}
66
67const QMap<int, QgsAttributeEditorElement::LabelStyle> &QgsTabBarProxyStyle::tabStyles() const
68{
69 return mTabStyles;
70}
71
72
73QgsTabBar::QgsTabBar( QWidget *parent )
74 : QTabBar( parent )
75{
76}
77
78void QgsTabBar::setTabBarStyle( QgsTabBarProxyStyle *tabStyle )
79{
80 mTabBarStyle = tabStyle;
81}
82
83QSize QgsTabBar::tabSizeHint( int index ) const
84{
85 if ( mTabBarStyle->tabStyles().contains( index ) )
86 {
87 const QgsAttributeEditorElement::LabelStyle tabStyle = mTabBarStyle->tabStyles().value( index );
88 if ( tabStyle.overrideFont )
89 {
90 const QSize s = QTabBar::tabSizeHint( index );
91 const QFontMetrics fm( font() );
92 const int w = fm.horizontalAdvance( tabText( index ) );
93 const QFont f = tabStyle.font;
94 const QFontMetrics bfm( f );
95 const int bw = bfm.horizontalAdvance( tabText( index ) );
96 return QSize( s.width() - w + bw, s.height() );
97 }
98 else
99 {
100 return QTabBar::tabSizeHint( index );
101 }
102 }
103 else
104 {
105 return QTabBar::tabSizeHint( index );
106 }
107}
108
109
110
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.