QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
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
75void QgsTabBar::setTabBarStyle( QgsTabBarProxyStyle *tabStyle )
76{
77 mTabBarStyle = tabStyle;
78}
79
80QSize QgsTabBar::tabSizeHint( int index ) const
81{
82 if ( mTabBarStyle->tabStyles().contains( index ) )
83 {
84 const QgsAttributeEditorElement::LabelStyle tabStyle = mTabBarStyle->tabStyles().value( index );
85 if ( tabStyle.overrideFont )
86 {
87 const QSize s = QTabBar::tabSizeHint( index );
88 const QFontMetrics fm( font() );
89 const int w = fm.horizontalAdvance( tabText( index ) );
90 const QFont f = tabStyle.font;
91 const QFontMetrics bfm( f );
92 const int bw = bfm.horizontalAdvance( tabText( index ) );
93 return QSize( s.width() - w + bw, s.height() );
94 }
95 else
96 {
97 return QTabBar::tabSizeHint( index );
98 }
99 }
100 else
101 {
102 return QTabBar::tabSizeHint( index );
103 }
104}
105
106
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.