@@ -17678,8 +17678,8 @@ void QgisApp::triggerCrashHandler()
1767817678void QgisApp::addTabifiedDockWidget( Qt::DockWidgetArea area, QDockWidget *dockWidget, const QStringList &tabifyWith, bool raiseTab )
1767917679{
1768017680 QList<QDockWidget *> dockWidgetsInArea;
17681- const auto dockWidgets = findChildren<QDockWidget *>();
17682- for ( QDockWidget *w : dockWidgets )
17681+ const QList<QDockWidget *> allDockWidgets = findChildren<QDockWidget *>();
17682+ for ( QDockWidget *w : allDockWidgets )
1768317683 {
1768417684 if ( w->isVisible() && dockWidgetArea( w ) == area )
1768517685 {
@@ -17688,80 +17688,73 @@ void QgisApp::addTabifiedDockWidget( Qt::DockWidgetArea area, QDockWidget *dockW
1768817688 }
1768917689
1769017690 addDockWidget( area, dockWidget ); // First add the dock widget, then attempt to tabify
17691- if ( dockWidgetsInArea.length() > 0 )
17691+ if ( dockWidgetsInArea.empty() )
17692+ return;
17693+
17694+ // Get the base dock widget that we'll use to tabify our new dockWidget
17695+ QDockWidget *tabifyWithDockWidget = nullptr;
17696+ for ( const QString &targetName : tabifyWith )
1769217697 {
17693- // Get the base dock widget that we'll use to tabify our new dockWidget
17694- QDockWidget *tabifyWithDockWidget = nullptr;
17695- if ( !tabifyWith.isEmpty() )
17696- {
17697- // Iterate the list of object names looking for a
17698- // dock widget to tabify the new one on top of it
17699- bool objectNameFound = false;
17700- for ( int i = 0; i < tabifyWith.size(); i++ )
17701- {
17702- for ( QDockWidget *cw : std::as_const( dockWidgetsInArea ) )
17703- {
17704- if ( cw->objectName() == tabifyWith.at( i ) || cw->property( "dock_uuid" ).toString() == tabifyWith.at( i ) )
17705- {
17706- tabifyWithDockWidget = cw;
17707- objectNameFound = true; // Also exit the outer for loop
17708- break;
17709- }
17710- }
17711- if ( objectNameFound )
17712- {
17713- break;
17714- }
17715- }
17716- }
17717- if ( !tabifyWithDockWidget )
17698+ auto it = std::find_if( dockWidgetsInArea.begin(), dockWidgetsInArea.end(), [&targetName]( QDockWidget *cw ) {
17699+ return cw->objectName() == targetName || cw->property( "dock_uuid" ).toString() == targetName;
17700+ } );
17701+
17702+ if ( it != dockWidgetsInArea.end() )
1771817703 {
17719- tabifyWithDockWidget = dockWidgetsInArea.at( 0 ); // Last resort
17704+ tabifyWithDockWidget = *it;
17705+ break;
1772017706 }
17721- if ( tabifyWithDockWidget == dockWidget )
17722- return;
17707+ }
1772317708
17724- QTabBar *existentTabBar = nullptr;
17725- int currentIndex = -1;
17726- if ( !raiseTab && dockWidgetsInArea.length() > 1 )
17709+ if ( !tabifyWithDockWidget )
17710+ {
17711+ // fallback to the first available dock widget if no matches were found, or if no tabifyWith names were specified
17712+ tabifyWithDockWidget = dockWidgetsInArea.at( 0 );
17713+ }
17714+ if ( tabifyWithDockWidget == dockWidget )
17715+ return;
17716+
17717+ // find the currently active dock widget so that we can restore that if we're not raising the new tab
17718+ QTabBar *existingTabBar = nullptr;
17719+ int currentTabIndex = -1;
17720+ if ( !raiseTab && dockWidgetsInArea.length() > 1 )
17721+ {
17722+ // Chances are we've already got a tabBar, if so, get
17723+ // currentTabIndex to restore status after inserting our new tab
17724+ const QList<QTabBar *> tabBars = findChildren<QTabBar *>( QString(), Qt::FindDirectChildrenOnly );
17725+ bool tabBarFound = false;
17726+ for ( QTabBar *tabBar : tabBars )
1772717727 {
17728- // Chances are we've already got a tabBar, if so, get
17729- // currentIndex to restore status after inserting our new tab
17730- const QList<QTabBar *> tabBars = findChildren<QTabBar *>( QString(), Qt::FindDirectChildrenOnly );
17731- bool tabBarFound = false;
17732- for ( QTabBar *tabBar : tabBars )
17728+ for ( int i = 0; i < tabBar->count(); i++ )
1773317729 {
17734- for ( int i = 0; i < tabBar->count(); i++ )
17735- {
17736- if ( tabBar->tabText( i ) == tabifyWithDockWidget->windowTitle() )
17737- {
17738- existentTabBar = tabBar;
17739- currentIndex = tabBar->currentIndex();
17740- tabBarFound = true;
17741- break;
17742- }
17743- }
17744- if ( tabBarFound )
17730+ if ( tabBar->tabText( i ) == tabifyWithDockWidget->windowTitle() )
1774517731 {
17732+ existingTabBar = tabBar;
17733+ currentTabIndex = tabBar->currentIndex();
17734+ tabBarFound = true;
1774617735 break;
1774717736 }
1774817737 }
17738+ if ( tabBarFound )
17739+ {
17740+ break;
17741+ }
1774917742 }
17743+ }
1775017744
17751- // Now we can put the new dockWidget on top of tabifyWith
17752- tabifyDockWidget( tabifyWithDockWidget, dockWidget );
17745+ // Now we can put the new dockWidget on top of tabifyWith
17746+ tabifyDockWidget( tabifyWithDockWidget, dockWidget );
1775317747
17754- // Should we restore dock widgets status?
17755- if ( !raiseTab )
17748+ // Should we restore dock widgets status?
17749+ if ( !raiseTab )
17750+ {
17751+ if ( existingTabBar )
1775617752 {
17757- if ( existentTabBar )
17758- {
17759- existentTabBar->setCurrentIndex( currentIndex );
17760- }
17761- else
17762- {
17763- tabifyWithDockWidget->raise(); // Single base dock widget, we can just raise it
17764- }
17753+ existingTabBar->setCurrentIndex( currentTabIndex );
17754+ }
17755+ else
17756+ {
17757+ tabifyWithDockWidget->raise(); // Single base dock widget, we can just raise it
1776517758 }
1776617759 }
1776717760}
0 commit comments