Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Add QgsTabWidget
This is almost like the QTabWidget but has additional methods for showing and hiding individual tabs
- Loading branch information
Showing
7 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/*************************************************************************** | ||
qgstabwidget.sip - QgsTabWidget | ||
|
||
--------------------- | ||
begin : 8.9.2016 | ||
copyright : (C) 2016 by Matthias Kuhn | ||
email : matthias@opengis.ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
/** \ingroup gui | ||
* The QgsTabWidget class is the same as the QTabWidget but with additional methods to | ||
* temporarily hide/show tabs. | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
class QgsTabWidget : QTabWidget | ||
{ | ||
%TypeHeaderCode | ||
#include <qgstabwidget.h> | ||
%End | ||
|
||
public: | ||
/** | ||
* Create a new QgsTabWidget with the optionally provided parent. | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
QgsTabWidget( QWidget *parent = nullptr ); | ||
|
||
/** | ||
* Hides the tab with the given widget | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
void hideTab( QWidget* tab ); | ||
|
||
/** | ||
* Shows the tab with the given widget | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
void showTab( QWidget* tab ); | ||
|
||
/** | ||
* Control the visibility for the tab with the given widget. | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
void setTabVisible( QWidget* tab, bool visible ); | ||
|
||
/** | ||
* Returns the index of the tab with the given widget. | ||
* This index is not the same as the one provided to insertTab and removeTab | ||
* since these methods are not aware of hidden tabs. | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
int realTabIndex( QWidget* widget ); | ||
|
||
virtual void tabInserted( int index ); | ||
virtual void tabRemoved( int index ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/*************************************************************************** | ||
qgstabwidget.cpp - QgsTabWidget | ||
--------------------- | ||
begin : 8.9.2016 | ||
copyright : (C) 2016 by Matthias Kuhn | ||
email : matthias@opengis.ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#include "qgstabwidget.h" | ||
|
||
#include "qgslogger.h" | ||
|
||
QgsTabWidget::QgsTabWidget( QWidget* parent ) | ||
: QTabWidget( parent ) | ||
, mSetTabVisibleFlag( false ) | ||
{ | ||
} | ||
|
||
void QgsTabWidget::hideTab( QWidget* tab ) | ||
{ | ||
QgsDebugMsg( "Hide" ); | ||
TabInformation& info = mTabs[ realTabIndex( tab )]; | ||
if ( info.visible ) | ||
{ | ||
mSetTabVisibleFlag = true; | ||
removeTab( info.sourceIndex ); | ||
info.visible = false; | ||
mSetTabVisibleFlag = false; | ||
} | ||
} | ||
|
||
void QgsTabWidget::showTab( QWidget* tab ) | ||
{ | ||
QgsDebugMsg( "Show" ); | ||
TabInformation& info = mTabs[ realTabIndex( tab )]; | ||
if ( ! info.visible ) | ||
{ | ||
mSetTabVisibleFlag = true; | ||
insertTab( info.sourceIndex + 1, info.widget, info.label ); | ||
info.visible = true; | ||
mSetTabVisibleFlag = false; | ||
} | ||
} | ||
|
||
void QgsTabWidget::setTabVisible( QWidget* tab, bool visible ) | ||
{ | ||
if ( visible ) | ||
showTab( tab ); | ||
else | ||
hideTab( tab ); | ||
} | ||
|
||
int QgsTabWidget::realTabIndex( QWidget* widget ) | ||
{ | ||
int realIndex = 0; | ||
Q_FOREACH ( const TabInformation& info, mTabs ) | ||
{ | ||
if ( info.widget == widget ) | ||
return realIndex; | ||
++realIndex; | ||
} | ||
return -1; | ||
} | ||
|
||
void QgsTabWidget::tabInserted( int index ) | ||
{ | ||
if ( !mSetTabVisibleFlag ) | ||
{ | ||
QWidget* newWidget = widget( index ); | ||
|
||
if ( index == 0 ) | ||
{ | ||
mTabs.insert( 0, TabInformation( newWidget, tabText( index ) ) ); | ||
} | ||
else | ||
{ | ||
bool inserted = false; | ||
QList<TabInformation>::iterator it; | ||
|
||
for ( it = mTabs.begin(); it != mTabs.end(); ++it ) | ||
{ | ||
if ( it->sourceIndex == index ) | ||
{ | ||
mTabs.insert( it, TabInformation( newWidget, tabText( index ) ) ); | ||
inserted = true; | ||
break; | ||
} | ||
} | ||
|
||
if ( !inserted ) | ||
{ | ||
mTabs.append( TabInformation( newWidget, tabText( index ) ) ); | ||
} | ||
} | ||
} | ||
|
||
synchronizeIndexes(); | ||
} | ||
|
||
void QgsTabWidget::tabRemoved( int index ) | ||
{ | ||
if ( !mSetTabVisibleFlag ) | ||
{ | ||
QList<TabInformation>::iterator it; | ||
|
||
for ( it = mTabs.begin(); it != mTabs.end(); ++it ) | ||
{ | ||
if ( it->sourceIndex == index ) | ||
{ | ||
mTabs.removeOne( *it ); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
synchronizeIndexes(); | ||
} | ||
|
||
void QgsTabWidget::synchronizeIndexes() | ||
{ | ||
QgsDebugMsg( "---------" ); | ||
int i = -1; | ||
QWidget* nextWidget = widget( 0 ); | ||
|
||
QList<TabInformation>::iterator it; | ||
|
||
for ( it = mTabs.begin(); it != mTabs.end(); ++it ) | ||
{ | ||
if ( it->widget == nextWidget ) | ||
{ | ||
i++; | ||
nextWidget = widget( i + 1 ); | ||
} | ||
it->sourceIndex = i; | ||
QgsDebugMsg( QString( "Tab %1 (%2): %3" ).arg( it->sourceIndex ).arg( it->label ).arg( i ) ); | ||
} | ||
} | ||
|
||
QgsTabWidget::TabInformation QgsTabWidget::tabInfo( QWidget* widget ) | ||
{ | ||
Q_FOREACH ( const TabInformation& info, mTabs ) | ||
{ | ||
if ( info.widget == widget ) | ||
return info; | ||
} | ||
return TabInformation(); | ||
} | ||
|
||
bool QgsTabWidget::TabInformation::operator ==( const QgsTabWidget::TabInformation& other ) | ||
{ | ||
return other.widget == widget && other.sourceIndex == sourceIndex; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/*************************************************************************** | ||
qgstabwidget.h - QgsTabWidget | ||
--------------------- | ||
begin : 8.9.2016 | ||
copyright : (C) 2016 by Matthias Kuhn | ||
email : matthias@opengis.ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#ifndef QGSTABWIDGET_H | ||
#define QGSTABWIDGET_H | ||
|
||
#include <QTabWidget> | ||
|
||
/** \ingroup gui | ||
* The QgsTabWidget class is the same as the QTabWidget but with additional methods to | ||
* temporarily hide/show tabs. | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
class GUI_EXPORT QgsTabWidget : public QTabWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
/** | ||
* Create a new QgsTabWidget with the optionally provided parent. | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
QgsTabWidget( QWidget *parent = nullptr ); | ||
|
||
/** | ||
* Hides the tab with the given widget | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
void hideTab( QWidget* tab ); | ||
|
||
/** | ||
* Shows the tab with the given widget | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
void showTab( QWidget* tab ); | ||
|
||
/** | ||
* Control the visibility for the tab with the given widget. | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
void setTabVisible( QWidget* tab, bool visible ); | ||
|
||
/** | ||
* Returns the index of the tab with the given widget. | ||
* This index is not the same as the one provided to insertTab and removeTab | ||
* since these methods are not aware of hidden tabs. | ||
* | ||
* @note Added in QGIS 3.0 | ||
*/ | ||
int realTabIndex( QWidget* widget ); | ||
|
||
virtual void tabInserted( int index ); | ||
virtual void tabRemoved( int index ); | ||
|
||
private: | ||
void synchronizeIndexes(); | ||
|
||
struct TabInformation | ||
{ | ||
TabInformation( QWidget* wdg, const QString& lbl ) | ||
: sourceIndex( -1 ) | ||
, widget( wdg ) | ||
, label( lbl ) | ||
, visible( true ) | ||
{} | ||
|
||
TabInformation() | ||
: sourceIndex( -1 ) | ||
, widget( nullptr ) | ||
, visible( true ) | ||
{} | ||
|
||
bool operator ==( const TabInformation& other ); | ||
|
||
int sourceIndex; | ||
QWidget* widget; | ||
QString label; | ||
bool visible; | ||
}; | ||
|
||
TabInformation tabInfo( QWidget* widget ); | ||
|
||
QList<TabInformation> mTabs; | ||
bool mSetTabVisibleFlag; | ||
}; | ||
|
||
#endif // QGSTABWIDGET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.