Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsTabWidget
This is almost like the QTabWidget but has additional methods for
showing and hiding individual tabs
  • Loading branch information
m-kuhn committed Sep 9, 2016
1 parent 9626f58 commit ac41436
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -161,6 +161,7 @@
%Include qgssourceselectdialog.sip
%Include qgssublayersdialog.sip
%Include qgssvgannotationitem.sip
%Include qgstabwidget.sip
%Include qgstablewidgetitem.sip
%Include qgstextannotationitem.sip
%Include qgstrackedvectorlayertools.sip
Expand Down
69 changes: 69 additions & 0 deletions python/gui/qgstabwidget.sip
@@ -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 );
};
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -299,6 +299,7 @@ SET(QGIS_GUI_SRCS
qgssublayersdialog.cpp
qgssqlcomposerdialog.cpp
qgssvgannotationitem.cpp
qgstabwidget.cpp
qgstablewidgetitem.cpp
qgstextannotationitem.cpp
qgstrackedvectorlayertools.cpp
Expand Down Expand Up @@ -451,6 +452,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsslider.h
qgssqlcomposerdialog.h
qgssublayersdialog.h
qgstabwidget.h
qgstreewidgetitem.h
qgsunitselectionwidget.h
qgsuserinputdockwidget.h
Expand Down
159 changes: 159 additions & 0 deletions src/gui/qgstabwidget.cpp
@@ -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;
}
104 changes: 104 additions & 0 deletions src/gui/qgstabwidget.h
@@ -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
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -81,6 +81,7 @@ ADD_PYTHON_TEST(PyQgsRulebasedRenderer test_qgsrulebasedrenderer.py)
ADD_PYTHON_TEST(PyQgsSingleSymbolRenderer test_qgssinglesymbolrenderer.py)
ADD_PYTHON_TEST(PyQgsShapefileProvider test_provider_shapefile.py)
ADD_PYTHON_TEST(PyQgsTabfileProvider test_provider_tabfile.py)
ADD_PYTHON_TEST(PyQgsTabWidget test_qgstabwidget.py)
ADD_PYTHON_TEST(PyQgsOGRProvider test_provider_ogr.py)
ADD_PYTHON_TEST(PyQgsSearchWidgetToolButton test_qgssearchwidgettoolbutton.py)
ADD_PYTHON_TEST(PyQgsSearchWidgetWrapper test_qgssearchwidgetwrapper.py)
Expand Down

0 comments on commit ac41436

Please sign in to comment.