QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
21  : QTabWidget( parent )
22  , mSetTabVisibleFlag( false )
23 {
24 }
25 
27 {
28  QgsDebugMsg( "Hide" );
29  TabInformation& info = mTabs[ realTabIndex( tab )];
30  if ( info.visible )
31  {
32  mSetTabVisibleFlag = true;
33  removeTab( info.sourceIndex );
34  info.visible = false;
35  mSetTabVisibleFlag = false;
36  }
37 }
38 
40 {
41  QgsDebugMsg( "Show" );
42  TabInformation& info = mTabs[ realTabIndex( tab )];
43  if ( ! info.visible )
44  {
45  mSetTabVisibleFlag = true;
46  insertTab( info.sourceIndex + 1, info.widget, info.label );
47  info.visible = true;
48  mSetTabVisibleFlag = false;
49  }
50 }
51 
53 {
54  if ( visible )
55  showTab( tab );
56  else
57  hideTab( tab );
58 }
59 
61 {
62  int realIndex = 0;
63  Q_FOREACH ( const TabInformation& info, mTabs )
64  {
65  if ( info.widget == widget )
66  return realIndex;
67  ++realIndex;
68  }
69  return -1;
70 }
71 
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;
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 
108 {
109  if ( !mSetTabVisibleFlag )
110  {
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( "---------" );
129  int i = -1;
130  QWidget* nextWidget = widget( 0 );
131 
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( QString( "Tab %1 (%2): %3" ).arg( it->sourceIndex ).arg( it->label ).arg( i ) );
143  }
144 }
145 
146 QgsTabWidget::TabInformation QgsTabWidget::tabInfo( QWidget* widget )
147 {
148  Q_FOREACH ( const TabInformation& info, mTabs )
149  {
150  if ( info.widget == widget )
151  return info;
152  }
153  return TabInformation();
154 }
155 
156 bool QgsTabWidget::TabInformation::operator ==( const QgsTabWidget::TabInformation& other )
157 {
158  return other.widget == widget && other.sourceIndex == sourceIndex;
159 }
static unsigned index
void setTabVisible(QWidget *tab, bool visible)
Control the visibility for the tab with the given widget.
QString tabText(int index) const
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QgsTabWidget(QWidget *parent=nullptr)
Create a new QgsTabWidget with the optionally provided parent.
void append(const T &value)
virtual void tabRemoved(int index) override
Is called internally whenever a tab has been removed.
int insertTab(int index, QWidget *page, const QString &label)
QWidget * widget(int index) const
iterator end()
int realTabIndex(QWidget *widget)
Returns the index of the tab with the given widget.
virtual void tabInserted(int index) override
Is called internally whenever a new tab has been inserted.
void insert(int i, const T &value)
void showTab(QWidget *tab)
Shows the tab with the given widget.
void hideTab(QWidget *tab)
Hides the tab with the given widget.
void removeTab(int index)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
bool removeOne(const T &value)
iterator begin()