QGIS API Documentation  3.2.0-Bonn (bc43194)
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( "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( "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  Q_FOREACH ( const TabInformation &info, mTabs )
63  {
64  if ( info.widget == widget )
65  return realIndex;
66  ++realIndex;
67  }
68  return -1;
69 }
70 
71 void QgsTabWidget::tabInserted( int index )
72 {
73  if ( !mSetTabVisibleFlag )
74  {
75  QWidget *newWidget = widget( index );
76 
77  if ( index == 0 )
78  {
79  mTabs.insert( 0, TabInformation( newWidget, tabText( index ) ) );
80  }
81  else
82  {
83  bool inserted = false;
84  QList<TabInformation>::iterator it;
85 
86  for ( it = mTabs.begin(); it != mTabs.end(); ++it )
87  {
88  if ( it->sourceIndex == index )
89  {
90  mTabs.insert( it, TabInformation( newWidget, tabText( index ) ) );
91  inserted = true;
92  break;
93  }
94  }
95 
96  if ( !inserted )
97  {
98  mTabs.append( TabInformation( newWidget, tabText( index ) ) );
99  }
100  }
101  }
102 
103  synchronizeIndexes();
104 }
105 
106 void QgsTabWidget::tabRemoved( int index )
107 {
108  if ( !mSetTabVisibleFlag )
109  {
110  QList<TabInformation>::iterator it;
111 
112  for ( it = mTabs.begin(); it != mTabs.end(); ++it )
113  {
114  if ( it->sourceIndex == index )
115  {
116  mTabs.removeOne( *it );
117  break;
118  }
119  }
120  }
121 
122  synchronizeIndexes();
123 }
124 
125 void QgsTabWidget::synchronizeIndexes()
126 {
127  QgsDebugMsg( "---------" );
128  int i = -1;
129  QWidget *nextWidget = widget( 0 );
130 
131  QList<TabInformation>::iterator it;
132 
133  for ( it = mTabs.begin(); it != mTabs.end(); ++it )
134  {
135  if ( it->widget == nextWidget )
136  {
137  i++;
138  nextWidget = widget( i + 1 );
139  }
140  it->sourceIndex = i;
141  QgsDebugMsg( QString( "Tab %1 (%2): %3" ).arg( it->sourceIndex ).arg( it->label ).arg( i ) );
142  }
143 }
144 
145 QgsTabWidget::TabInformation QgsTabWidget::tabInfo( QWidget *widget )
146 {
147  Q_FOREACH ( const TabInformation &info, mTabs )
148  {
149  if ( info.widget == widget )
150  return info;
151  }
152  return TabInformation();
153 }
154 
155 bool QgsTabWidget::TabInformation::operator ==( const QgsTabWidget::TabInformation &other )
156 {
157  return other.widget == widget && other.sourceIndex == sourceIndex;
158 }
void setTabVisible(QWidget *tab, bool visible)
Control the visibility for the tab with the given widget.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsTabWidget(QWidget *parent=nullptr)
Create a new QgsTabWidget with the optionally provided parent.
void tabRemoved(int index) override
Is called internally whenever a tab has been removed.
int realTabIndex(QWidget *widget)
Returns the index of the tab with the given widget.
void tabInserted(int index) override
Is called internally whenever a new tab has been inserted.
void showTab(QWidget *tab)
Shows the tab with the given widget.
void hideTab(QWidget *tab)
Hides the tab with the given widget.