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