Skip to content

Commit

Permalink
New addTabifiedDockWidget in QGIS App to add dock widgets as tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
gacarrillor authored and nyalldawson committed May 3, 2020
1 parent 797972a commit 4629f83
Show file tree
Hide file tree
Showing 4 changed files with 801 additions and 1 deletion.
89 changes: 89 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -16089,6 +16089,95 @@ void QgisApp::triggerCrashHandler()
#endif
}

void QgisApp::addTabifiedDockWidget( Qt::DockWidgetArea area, QDockWidget *dockWidget, const QStringList &tabifyWith, bool raiseTab )
{
QList< QDockWidget * > dockWidgetsInArea;
const auto dockWidgets = findChildren< QDockWidget * >();
for ( QDockWidget *w : dockWidgets )
{
if ( w->isVisible() && dockWidgetArea( w ) == area )
{
dockWidgetsInArea << w;
}
}

addDockWidget( area, dockWidget ); // First add the dock widget, then attempt to tabify
if ( dockWidgetsInArea.length() > 0 )
{
// Get the base dock widget that we'll use to tabify our new dockWidget
QDockWidget *tabifyWithDockWidget = nullptr;
if ( !tabifyWith.isEmpty() )
{
// Iterate the list of object names looking for a
// dock widget to tabify the new one on top of it
bool objectNameFound = false;
for ( int i = 0; i < tabifyWith.size(); i++ )
{
for ( QDockWidget *cw : dockWidgetsInArea )
{
if ( cw->objectName() == tabifyWith.at( i ) )
{
tabifyWithDockWidget = cw;
objectNameFound = true; // Also exit the outer for loop
break;
}
}
if ( objectNameFound )
{
break;
}
}
}
if ( !tabifyWithDockWidget )
{
tabifyWithDockWidget = dockWidgetsInArea.at( 0 ); // Last resort
}

QTabBar *existentTabBar = nullptr;
int currentIndex = -1;
if ( !raiseTab && dockWidgetsInArea.length() > 1 )
{
// Chances are we've already got a tabBar, if so, get
// currentIndex to restore status after inserting our new tab
const QList<QTabBar *> tabBars = findChildren<QTabBar *>( QString(), Qt::FindDirectChildrenOnly );
bool tabBarFound = false;
for ( QTabBar *tabBar : tabBars )
{
for ( int i = 0; i < tabBar->count(); i++ )
{
if ( tabBar->tabText( i ) == tabifyWithDockWidget->windowTitle() )
{
existentTabBar = tabBar;
currentIndex = tabBar->currentIndex();
tabBarFound = true;
break;
}
}
if ( tabBarFound )
{
break;
}
}
}

// Now we can put the new dockWidget on top of tabifyWith
tabifyDockWidget( tabifyWithDockWidget, dockWidget );

// Should we restore dock widgets status?
if ( !raiseTab )
{
if ( existentTabBar )
{
existentTabBar->setCurrentIndex( currentIndex );
}
else
{
tabifyWithDockWidget->raise(); // Single base dock widget, we can just raise it
}
}
}
}

QgsAttributeEditorContext QgisApp::createAttributeEditorContext()
{
QgsAttributeEditorContext context;
Expand Down
17 changes: 16 additions & 1 deletion src/app/qgisapp.h
Expand Up @@ -340,6 +340,22 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void addDockWidget( Qt::DockWidgetArea area, QDockWidget *dockwidget );
void removeDockWidget( QDockWidget *dockwidget );

/**
* Add a dock widget to the given area and tabify it (if other dock widgets
* exist in the same \a area). The new tab will be below other tabs unless
* \a raiseTab is passed as true.
*
* \a tabifyWith is a list of dock widget object names, ordered by
* priority, with which the new dock widget should be tabified. Only the
* first matching object name will be picked. If none of the given object
* names is found in that \a area (or if \a tabifyWith is not given at
* all), the new dock widget will be created anyways, but its location
* within that \a area will be unpredictable.
*
* \since QGIS 3.14
*/
void addTabifiedDockWidget( Qt::DockWidgetArea area, QDockWidget *dockWidget, const QStringList &tabifyWith = QStringList(), bool raiseTab = false );

/**
* Add a toolbar to the main window. Overloaded from QMainWindow.
* After adding the toolbar to the ui (by delegating to the QMainWindow
Expand Down Expand Up @@ -2150,7 +2166,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

QgisAppStyleSheet *mStyleSheetBuilder = nullptr;


// actions for menus and toolbars -----------------

#ifdef Q_OS_MAC
Expand Down
1 change: 1 addition & 0 deletions tests/src/app/CMakeLists.txt
Expand Up @@ -114,6 +114,7 @@ ENDIF ()
ADD_QGIS_TEST(qgisapp testqgisapp.cpp)
ADD_QGIS_TEST(appbrowserproviders testqgsappbrowserproviders.cpp)
ADD_QGIS_TEST(qgisappclipboard testqgisappclipboard.cpp)
ADD_QGIS_TEST(appdockwidgets testqgisappdockwidgets.cpp)
ADD_QGIS_TEST(attributetabletest testqgsattributetable.cpp)
ADD_QGIS_TEST(applocatorfilters testqgsapplocatorfilters.cpp)
ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp)
Expand Down

0 comments on commit 4629f83

Please sign in to comment.