1 /***************************************************************************
2   qgstabwidget.h - QgsTabWidget
3 
4  ---------------------
5  begin                : 8.9.2016
6  copyright            : (C) 2016 by Matthias Kuhn
7  email                : matthias@opengis.ch
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 #ifndef QGSTABWIDGET_H
17 #define QGSTABWIDGET_H
18 
19 #include <QTabWidget>
20 #include "qgis_gui.h"
21 
22 /**
23  * \ingroup gui
24  * \brief The QgsTabWidget class is the same as the QTabWidget but with additional methods to
25  * temporarily hide/show tabs.
26  *
27  * \since QGIS 3.0
28  */
29 class GUI_EXPORT QgsTabWidget : public QTabWidget
30 {
31     Q_OBJECT
32 
33   public:
34 
35     /**
36      * Create a new QgsTabWidget with the optionally provided parent.
37      *
38      * \since QGIS 3.0
39      */
40     QgsTabWidget( QWidget *parent = nullptr );
41 
42     /**
43      * Hides the tab with the given widget
44      *
45      * \since QGIS 3.0
46      */
47     void hideTab( QWidget *tab );
48 
49     /**
50      * Shows the tab with the given widget
51      *
52      * \since QGIS 3.0
53      */
54     void showTab( QWidget *tab );
55 
56     /**
57      * Control the visibility for the tab with the given widget.
58      *
59      * \since QGIS 3.0
60      */
61     void setTabVisible( QWidget *tab, bool visible );
62 
63     /**
64      * Returns the index of the tab with the given widget.
65      * This index is not the same as the one provided to insertTab and removeTab
66      * since these methods are not aware of hidden tabs.
67      *
68      * \since QGIS 3.0
69      */
70     int realTabIndex( QWidget *widget );
71 
72     /**
73      * Is called internally whenever a new tab has been inserted.
74      *
75      * Is used to keep track of currently available and visible tabs.
76      *
77      * \since QGIS 3.0
78      */
79     void tabInserted( int index ) override;
80 
81     /**
82      * Is called internally whenever a tab has been removed.
83      *
84      * Is used to keep track of currently available and visible tabs.
85      *
86      * \since QGIS 3.0
87      */
88     void tabRemoved( int index ) override;
89 
90   private:
91     void synchronizeIndexes();
92 
93     struct TabInformation
94     {
TabInformationTabInformation95       TabInformation( QWidget *wdg, const QString &lbl )
96         : widget( wdg )
97         , label( lbl )
98       {}
99 
100       //! Constructor for TabInformation
101       TabInformation() = default;
102 
103       bool operator ==( const TabInformation &other ) const;
104       bool operator !=( const TabInformation &other ) const;
105 
106       int sourceIndex = -1;
107       QWidget *widget = nullptr;
108       QString label;
109       bool visible = true;
110     };
111 
112     TabInformation tabInfo( QWidget *widget );
113 
114     QList<TabInformation> mTabs;
115     bool mSetTabVisibleFlag = false;
116 };
117 
118 #endif // QGSTABWIDGET_H
119