QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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
7  email : [email protected]
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 QgsTabWidget::QgsTabWidget( QWidget *parent )
21  : QTabWidget( parent )
22 {
23 }
24 
25 void QgsTabWidget::hideTab( QWidget *tab )
26 {
27  QgsDebugMsg( QStringLiteral( "Hide" ) );
28  TabInformation &info = mTabs[ realTabIndex( tab )];
29  if ( info.visible )
30  {
31  mSetTabVisibleFlag = true;
32  removeTab( info.sourceIndex );
33  info.visible = false;
34  mSetTabVisibleFlag = false;
35  }
36 }
37 
38 void QgsTabWidget::showTab( QWidget *tab )
39 {
40  QgsDebugMsg( QStringLiteral( "Show" ) );
41  TabInformation &info = mTabs[ realTabIndex( tab )];
42  if ( ! info.visible )
43  {
44  mSetTabVisibleFlag = true;
45  insertTab( info.sourceIndex + 1, info.widget, info.label );
46  info.visible = true;
47  mSetTabVisibleFlag = false;
48  }
49 }
50 
51 void QgsTabWidget::setTabVisible( QWidget *tab, bool visible )
52 {
53  if ( visible )
54  showTab( tab );
55  else
56  hideTab( tab );
57 }
58 
59 int QgsTabWidget::realTabIndex( QWidget *widget )
60 {
61  int realIndex = 0;
62  const auto constMTabs = mTabs;
63  for ( const TabInformation &info : constMTabs )
64  {
65  if ( info.widget == widget )
66  return realIndex;
67  ++realIndex;
68  }
69  return -1;
70 }
71 
72 void QgsTabWidget::tabInserted( int index )
73 {
74  if ( !mSetTabVisibleFlag )
75  {
76  QWidget *newWidget = widget( index );
77 
78  if ( index == 0 )
79  {
80  mTabs.insert( 0, TabInformation( newWidget, tabText( index ) ) );
81  }
82  else
83  {
84  bool inserted = false;
85  QList<TabInformation>::iterator it;
86 
87  for ( it = mTabs.begin(); it != mTabs.end(); ++it )
88  {
89  if ( it->sourceIndex == index )
90  {
91  mTabs.insert( it, TabInformation( newWidget, tabText( index ) ) );
92  inserted = true;
93  break;
94  }
95  }
96 
97  if ( !inserted )
98  {
99  mTabs.append( TabInformation( newWidget, tabText( index ) ) );
100  }
101  }
102  }
103 
104  synchronizeIndexes();
105 }
106 
107 void QgsTabWidget::tabRemoved( int index )
108 {
109  if ( !mSetTabVisibleFlag )
110  {
111  QList<TabInformation>::iterator it;
112 
113  for ( it = mTabs.begin(); it != mTabs.end(); ++it )
114  {
115  if ( it->sourceIndex == index )
116  {
117  mTabs.removeOne( *it );
118  break;
119  }
120  }
121  }
122 
123  synchronizeIndexes();
124 }
125 
126 void QgsTabWidget::synchronizeIndexes()
127 {
128  QgsDebugMsg( QStringLiteral( "---------" ) );
129  int i = -1;
130  QWidget *nextWidget = widget( 0 );
131 
132  QList<TabInformation>::iterator it;
133 
134  for ( it = mTabs.begin(); it != mTabs.end(); ++it )
135  {
136  if ( it->widget == nextWidget )
137  {
138  i++;
139  nextWidget = widget( i + 1 );
140  }
141  it->sourceIndex = i;
142  QgsDebugMsg( QStringLiteral( "Tab %1 (%2): %3" ).arg( it->sourceIndex ).arg( it->label ).arg( i ) );
143  }
144 }
145 
146 QgsTabWidget::TabInformation QgsTabWidget::tabInfo( QWidget *widget )
147 {
148  const auto constMTabs = mTabs;
149  for ( const TabInformation &info : constMTabs )
150  {
151  if ( info.widget == widget )
152  return info;
153  }
154  return TabInformation();
155 }
156 
157 bool QgsTabWidget::TabInformation::operator ==( const QgsTabWidget::TabInformation &other ) const
158 {
159  return other.widget == widget && other.sourceIndex == sourceIndex;
160 }
161 
162 bool QgsTabWidget::TabInformation::operator !=( const TabInformation &other ) const
163 {
164  return !( *this == other );
165 }
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.
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