QGIS API Documentation 3.29.0-Master (ade4f0cf0f)
qgstabwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstabwidget.cpp - QgsTabWidget
3
4 ---------------------
5 begin : 8.9.2016
6 copyright : (C) 2016 by Matthias Kuhn
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16#include "qgstabwidget.h"
17
18#include "qgslogger.h"
19
21 : QTabWidget( parent )
22{
23 QgsTabBar *qgsTabBar = new QgsTabBar( this );
24 setTabBar( qgsTabBar );
25 mTabBarStyle = new QgsTabBarProxyStyle( tabBar() );
26 qgsTabBar->setTabBarStyle( mTabBarStyle );
27 setStyle( mTabBarStyle );
28}
29
30void QgsTabWidget::hideTab( QWidget *tab )
31{
32 QgsDebugMsg( QStringLiteral( "Hide" ) );
33 TabInformation &info = mTabs[ realTabIndex( tab )];
34 if ( info.visible )
35 {
36 mSetTabVisibleFlag = true;
37 removeTab( info.sourceIndex );
38 info.visible = false;
39 mSetTabVisibleFlag = false;
40 }
41}
42
43void QgsTabWidget::showTab( QWidget *tab )
44{
45 QgsDebugMsg( QStringLiteral( "Show" ) );
46 TabInformation &info = mTabs[ realTabIndex( tab )];
47 if ( ! info.visible )
48 {
49 mSetTabVisibleFlag = true;
50 insertTab( info.sourceIndex + 1, info.widget, info.label );
51 info.visible = true;
52 mSetTabVisibleFlag = false;
53 }
54}
55
56void QgsTabWidget::setTabVisible( QWidget *tab, bool visible )
57{
58 if ( visible )
59 showTab( tab );
60 else
61 hideTab( tab );
62}
63
64int QgsTabWidget::realTabIndex( QWidget *widget )
65{
66 int realIndex = 0;
67 const auto constMTabs = mTabs;
68 for ( const TabInformation &info : constMTabs )
69 {
70 if ( info.widget == widget )
71 return realIndex;
72 ++realIndex;
73 }
74 return -1;
75}
76
78{
79 mTabBarStyle->addStyle( tabIndex, labelStyle );
80}
81
83{
84 if ( !mSetTabVisibleFlag )
85 {
86 QWidget *newWidget = widget( index );
87
88 if ( index == 0 )
89 {
90 mTabs.insert( 0, TabInformation( newWidget, tabText( index ) ) );
91 }
92 else
93 {
94 bool inserted = false;
95 QList<TabInformation>::iterator it;
96
97 for ( it = mTabs.begin(); it != mTabs.end(); ++it )
98 {
99 if ( it->sourceIndex == index )
100 {
101 mTabs.insert( it, TabInformation( newWidget, tabText( index ) ) );
102 inserted = true;
103 break;
104 }
105 }
106
107 if ( !inserted )
108 {
109 mTabs.append( TabInformation( newWidget, tabText( index ) ) );
110 }
111 }
112 }
113
114 synchronizeIndexes();
115}
116
118{
119 if ( !mSetTabVisibleFlag )
120 {
121 QList<TabInformation>::iterator it;
122
123 for ( it = mTabs.begin(); it != mTabs.end(); ++it )
124 {
125 if ( it->sourceIndex == index )
126 {
127 mTabs.removeOne( *it );
128 break;
129 }
130 }
131 }
132
133 synchronizeIndexes();
134}
135
136void QgsTabWidget::synchronizeIndexes()
137{
138 QgsDebugMsg( QStringLiteral( "---------" ) );
139 int i = -1;
140 QWidget *nextWidget = widget( 0 );
141
142 QList<TabInformation>::iterator it;
143
144 for ( it = mTabs.begin(); it != mTabs.end(); ++it )
145 {
146 if ( it->widget == nextWidget )
147 {
148 i++;
149 nextWidget = widget( i + 1 );
150 }
151 it->sourceIndex = i;
152 QgsDebugMsg( QStringLiteral( "Tab %1 (%2): %3" ).arg( it->sourceIndex ).arg( it->label ).arg( i ) );
153 }
154}
155
156QgsTabWidget::TabInformation QgsTabWidget::tabInfo( QWidget *widget )
157{
158 const auto constMTabs = mTabs;
159 for ( const TabInformation &info : constMTabs )
160 {
161 if ( info.widget == widget )
162 return info;
163 }
164 return TabInformation();
165}
166
167bool QgsTabWidget::TabInformation::operator ==( const QgsTabWidget::TabInformation &other ) const
168{
169 return other.widget == widget && other.sourceIndex == sourceIndex;
170}
171
172bool QgsTabWidget::TabInformation::operator !=( const TabInformation &other ) const
173{
174 return !( *this == other );
175}
void setTabVisible(QWidget *tab, bool visible)
Control the visibility for the tab with the given widget.
void tabRemoved(int index) override
Is called internally whenever a tab has been removed.
void showTab(QWidget *tab)
Shows the tab with the given widget.
void tabInserted(int index) override
Is called internally whenever a new tab has been inserted.
void hideTab(QWidget *tab)
Hides the tab with the given widget.
void setTabStyle(int tabIndex, const QgsAttributeEditorElement::LabelStyle &labelStyle)
Sets the optional custom labelStyle for the tab identified by tabIndex.
int realTabIndex(QWidget *widget)
Returns the index of the tab with the given widget.
QgsTabWidget(QWidget *parent=nullptr)
Create a new QgsTabWidget with the optionally provided parent.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.