Skip to content

Commit

Permalink
Gui: Fix wrong orientation of workbench tab bar after start
Browse files Browse the repository at this point in the history
This replaces old mechanism that was based on storing tab bar
orientation in user settings with one that delays initialization by half
of a second to ensure that toolbar is placed where in right place.
  • Loading branch information
kadet1090 committed May 5, 2024
1 parent 385e080 commit e7c59ca
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/Gui/WorkbenchSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# include <QScreen>
# include <QToolBar>
# include <QLayout>
# include <QTimer>
#endif

#include "Base/Tools.h"
Expand Down Expand Up @@ -75,8 +76,7 @@ void WorkbenchComboBox::refreshList(QList<QAction*> actionList)
{
clear();

ParameterGrp::handle hGrp;
hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches");
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches");

auto itemStyle = static_cast<WorkbenchItemStyle>(hGrp->GetInt("WorkbenchSelectorItem", 0));

Expand Down Expand Up @@ -111,7 +111,6 @@ WorkbenchTabWidget::WorkbenchTabWidget(WorkbenchGroup* aGroup, QWidget* parent)

tabBar = new QTabBar(this);
moreButton = new QToolButton(this);

layout = new QBoxLayout(QBoxLayout::LeftToRight, this);

layout->setContentsMargins(0, 0, 0, 0);
Expand All @@ -128,18 +127,15 @@ WorkbenchTabWidget::WorkbenchTabWidget(WorkbenchGroup* aGroup, QWidget* parent)
moreButton->setObjectName(QString::fromLatin1("WbTabBarMore"));

if (parent->inherits("QToolBar")) {
// set the initial orientation. We cannot do updateLayoutAndTabOrientation(false);
// because on init the toolbar area is always TopToolBarArea.
ParameterGrp::handle hGrp;
hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches");
std::string orientation = hGrp->GetASCII("TabBarOrientation", "North");

setToolBarArea(
orientation == "North" ? Qt::TopToolBarArea :
orientation == "South" ? Qt::BottomToolBarArea :
orientation == "East" ? Qt::LeftToolBarArea :
Qt::RightToolBarArea
);
// when toolbar is created it is not yet placed in its designated area
// therefore we need to wait a bit and then update layout when it is ready
// this is prone to race conditions, but Qt does not supply any event that
// informs us about toolbar changing its placement.
//
// previous implementation saved that information to user settings and
// restored last layout but this creates issues when default workbench has
// different layout than last visited one
QTimer::singleShot(500, [this]() { updateLayout(); });
}

tabBar->setDocumentMode(true);
Expand Down Expand Up @@ -218,8 +214,8 @@ void WorkbenchTabWidget::updateLayout()

auto toolBar = qobject_cast<QToolBar*>(parentWidget());

// if it's not placed in toolbar it must be placed in menubar so alike top toolbar area
if (!toolBar) {
// if it's not placed in toolbar it must be placed in menubar so alike top toolbar area
if (!toolBar || !toolBar->isVisible()) {
setToolBarArea(Qt::TopToolBarArea);
return;
}
Expand Down Expand Up @@ -297,6 +293,10 @@ void WorkbenchTabWidget::handleTabChange(int selectedTabIndex)

void WorkbenchTabWidget::updateWorkbenchList()
{
if (isInitializing) {
return;
}

// As clearing and adding tabs can cause changing current tab in QTabBar.
// This in turn will cause workbench to change, so we need to prevent
// processing of such events until the QTabBar is fully prepared.
Expand Down Expand Up @@ -324,8 +324,7 @@ void WorkbenchTabWidget::updateWorkbenchList()

int WorkbenchTabWidget::addWorkbenchTab(QAction* action, int tabIndex)
{
ParameterGrp::handle hGrp;
hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches");
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches");
auto itemStyle = static_cast<WorkbenchItemStyle>(hGrp->GetInt("WorkbenchSelectorItem", 0));

// if tabIndex is negative we assume that tab must be placed at the end of tabBar (default behavior)
Expand Down Expand Up @@ -360,21 +359,17 @@ int WorkbenchTabWidget::addWorkbenchTab(QAction* action, int tabIndex)

void WorkbenchTabWidget::setToolBarArea(Qt::ToolBarArea area)
{
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Workbenches");

switch (area) {
case Qt::ToolBarArea::LeftToolBarArea:
case Qt::ToolBarArea::RightToolBarArea:
layout->setDirection(direction() == Qt::LeftToRight ? QBoxLayout::TopToBottom : QBoxLayout::BottomToTop);
tabBar->setShape(area == Qt::LeftToolBarArea ? QTabBar::RoundedWest : QTabBar::RoundedEast);
hGrp->SetASCII("TabBarOrientation", area == Qt::LeftToolBarArea ? "West" : "East");
break;

case Qt::ToolBarArea::TopToolBarArea:
case Qt::ToolBarArea::BottomToolBarArea:
layout->setDirection(direction() == Qt::LeftToRight ? QBoxLayout::LeftToRight : QBoxLayout::RightToLeft);
tabBar->setShape(area == Qt::TopToolBarArea ? QTabBar::RoundedNorth : QTabBar::RoundedSouth);
hGrp->SetASCII("TabBarOrientation", area == Qt::TopToolBarArea ? "North" : "South");
break;

default:
Expand Down

0 comments on commit e7c59ca

Please sign in to comment.