|
| 1 | +/*************************************************************************** |
| 2 | + qgstabwidget.cpp - 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 | +#include "qgstabwidget.h" |
| 17 | + |
| 18 | +#include "qgslogger.h" |
| 19 | + |
| 20 | +QgsTabWidget::QgsTabWidget( QWidget* parent ) |
| 21 | + : QTabWidget( parent ) |
| 22 | + , mSetTabVisibleFlag( false ) |
| 23 | +{ |
| 24 | +} |
| 25 | + |
| 26 | +void QgsTabWidget::hideTab( QWidget* tab ) |
| 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 | + |
| 39 | +void QgsTabWidget::showTab( QWidget* tab ) |
| 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 | + |
| 52 | +void QgsTabWidget::setTabVisible( QWidget* tab, bool visible ) |
| 53 | +{ |
| 54 | + if ( visible ) |
| 55 | + showTab( tab ); |
| 56 | + else |
| 57 | + hideTab( tab ); |
| 58 | +} |
| 59 | + |
| 60 | +int QgsTabWidget::realTabIndex( QWidget* widget ) |
| 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 | + |
| 72 | +void QgsTabWidget::tabInserted( int index ) |
| 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; |
| 85 | + QList<TabInformation>::iterator it; |
| 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 | + |
| 107 | +void QgsTabWidget::tabRemoved( int index ) |
| 108 | +{ |
| 109 | + if ( !mSetTabVisibleFlag ) |
| 110 | + { |
| 111 | + QList<TabInformation>::iterator it; |
| 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 | + |
| 132 | + QList<TabInformation>::iterator it; |
| 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 | +} |
0 commit comments