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