Skip to content
Permalink
Browse files

New addTabifiedDockWidget in QGIS App to add dock widgets as tabs

  • Loading branch information
gacarrillor authored and nyalldawson committed May 2, 2020
1 parent 797972a commit 4629f83409f3c186abde508154135407db5fae4c
Showing with 801 additions and 1 deletion.
  1. +89 −0 src/app/qgisapp.cpp
  2. +16 −1 src/app/qgisapp.h
  3. +1 −0 tests/src/app/CMakeLists.txt
  4. +695 −0 tests/src/app/testqgisappdockwidgets.cpp
#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;
@@ -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
@@ -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
@@ -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)

0 comments on commit 4629f83

Please sign in to comment.
You can’t perform that action at this time.