QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
23 QgsTabBarProxyStyle::QgsTabBarProxyStyle( QTabBar *tabBar )
24  : QgsProxyStyle( tabBar )
25 {
26 }
27 
28 void 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 
61 void QgsTabBarProxyStyle::addStyle( int tabIndex, const QgsAttributeEditorElement::LabelStyle &style )
62 {
63  mTabStyles.insert( tabIndex, style );
64 }
65 
66 const QMap<int, QgsAttributeEditorElement::LabelStyle> &QgsTabBarProxyStyle::tabStyles() const
67 {
68  return mTabStyles;
69 }
70 
71 
72 QgsTabBar::QgsTabBar( QWidget *parent )
73  : QTabBar( parent )
74 {
75 }
76 
77 void QgsTabBar::setTabBarStyle( QgsTabBarProxyStyle *tabStyle )
78 {
79  mTabBarStyle = tabStyle;
80 }
81 
82 QSize QgsTabBar::tabSizeHint( int index ) const
83 {
84  if ( mTabBarStyle->tabStyles().contains( index ) )
85  {
86  const QSize s = QTabBar::tabSizeHint( index );
87  const QFontMetrics fm( font() );
88  const int w = fm.horizontalAdvance( tabText( index ) );
89  const QFont f = mTabBarStyle->tabStyles().value( index ).font;
90  const QFontMetrics bfm( f );
91  const int bw = bfm.horizontalAdvance( tabText( index ) );
92  return QSize( s.width() - w + bw, s.height() );
93  }
94  else
95  {
96  return QTabBar::tabSizeHint( index );
97  }
98 }
99 
100 
101 
QgsAttributeEditorElement::LabelStyle
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.
Definition: qgsattributeeditorelement.h:82
qgstabbarproxystyle.h
QgsProxyStyle
A QProxyStyle subclass which correctly sets the base style to match the QGIS application style,...
Definition: qgsproxystyle.h:30