QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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#include "moc_qgstabwidget.cpp"
18
19#include "qgslogger.h"
20
22 : QTabWidget( parent )
23{
24 QgsTabBar *qgsTabBar = new QgsTabBar( this );
25 setTabBar( qgsTabBar );
26 mTabBarStyle = new QgsTabBarProxyStyle( tabBar() );
27 qgsTabBar->setTabBarStyle( mTabBarStyle );
28 setStyle( mTabBarStyle );
29}
30
31void QgsTabWidget::hideTab( QWidget *tab )
32{
33 QgsDebugMsgLevel( QStringLiteral( "Hide" ), 3 );
34 TabInformation &info = mTabs[ realTabIndex( tab )];
35 if ( info.visible )
36 {
37 mSetTabVisibleFlag = true;
38 removeTab( info.sourceIndex );
39 info.visible = false;
40 mSetTabVisibleFlag = false;
41 }
42}
43
44void QgsTabWidget::showTab( QWidget *tab )
45{
46 QgsDebugMsgLevel( QStringLiteral( "Show" ), 3 );
47 TabInformation &info = mTabs[ realTabIndex( tab )];
48 if ( ! info.visible )
49 {
50 mSetTabVisibleFlag = true;
51 insertTab( info.sourceIndex + 1, info.widget, info.label );
52 info.visible = true;
53 mSetTabVisibleFlag = false;
54 }
55}
56
57void QgsTabWidget::setTabVisible( QWidget *tab, bool visible )
58{
59 if ( visible )
60 showTab( tab );
61 else
62 hideTab( tab );
63}
64
65int QgsTabWidget::realTabIndex( QWidget *widget )
66{
67 int realIndex = 0;
68 const auto constMTabs = mTabs;
69 for ( const TabInformation &info : constMTabs )
70 {
71 if ( info.widget == widget )
72 return realIndex;
73 ++realIndex;
74 }
75 return -1;
76}
77
79{
80 mTabBarStyle->addStyle( tabIndex, labelStyle );
81}
82
84{
85 if ( !mSetTabVisibleFlag )
86 {
87 QWidget *newWidget = widget( index );
88
89 if ( index == 0 )
90 {
91 mTabs.insert( 0, TabInformation( newWidget, tabText( index ) ) );
92 }
93 else
94 {
95 bool inserted = false;
96 QList<TabInformation>::iterator it;
97
98 for ( it = mTabs.begin(); it != mTabs.end(); ++it )
99 {
100 if ( it->sourceIndex == index )
101 {
102 mTabs.insert( it, TabInformation( newWidget, tabText( index ) ) );
103 inserted = true;
104 break;
105 }
106 }
107
108 if ( !inserted )
109 {
110 mTabs.append( TabInformation( newWidget, tabText( index ) ) );
111 }
112 }
113 }
114
115 synchronizeIndexes();
116}
117
119{
120 if ( !mSetTabVisibleFlag )
121 {
122 QList<TabInformation>::iterator it;
123
124 for ( it = mTabs.begin(); it != mTabs.end(); ++it )
125 {
126 if ( it->sourceIndex == index )
127 {
128 mTabs.removeOne( *it );
129 break;
130 }
131 }
132 }
133
134 synchronizeIndexes();
135}
136
137void QgsTabWidget::synchronizeIndexes()
138{
139 QgsDebugMsgLevel( QStringLiteral( "---------" ), 3 );
140 int i = -1;
141 QWidget *nextWidget = widget( 0 );
142
143 QList<TabInformation>::iterator it;
144
145 for ( it = mTabs.begin(); it != mTabs.end(); ++it )
146 {
147 if ( it->widget == nextWidget )
148 {
149 i++;
150 nextWidget = widget( i + 1 );
151 }
152 it->sourceIndex = i;
153 QgsDebugMsgLevel( QStringLiteral( "Tab %1 (%2): %3" ).arg( it->sourceIndex ).arg( it->label ).arg( i ), 3 );
154 }
155}
156
157QgsTabWidget::TabInformation QgsTabWidget::tabInfo( QWidget *widget )
158{
159 const auto constMTabs = mTabs;
160 for ( const TabInformation &info : constMTabs )
161 {
162 if ( info.widget == widget )
163 return info;
164 }
165 return TabInformation();
166}
167
168bool QgsTabWidget::TabInformation::operator ==( const QgsTabWidget::TabInformation &other ) const
169{
170 return other.widget == widget && other.sourceIndex == sourceIndex;
171}
172
173bool QgsTabWidget::TabInformation::operator !=( const TabInformation &other ) const
174{
175 return !( *this == other );
176}
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.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.