Skip to content

Commit

Permalink
Gui: support toolbar drag&drop on menu bar
Browse files Browse the repository at this point in the history
  • Loading branch information
realthunder committed May 28, 2024
1 parent 8352f98 commit f2f9ced
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 225 deletions.
15 changes: 3 additions & 12 deletions src/Gui/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Action::~Action()
void Action::addTo(QWidget *widget)
{
widget->addAction(_action);
ToolBarManager::getInstance()->checkToolbarIconSize(qobject_cast<QToolBar*>(widget));
ToolBarManager::getInstance()->checkToolBarIconSize(qobject_cast<QToolBar*>(widget));
}

/**
Expand Down Expand Up @@ -218,7 +218,7 @@ void Action::setIcon (const QIcon & icon)
{
_action->setIcon(icon);
if (!icon.isNull())
ToolBarManager::getInstance()->checkToolbarIconSize(_action);
ToolBarManager::getInstance()->checkToolBarIconSize(_action);
setToolTip(_tooltip, _title);
}

Expand Down Expand Up @@ -647,7 +647,7 @@ void ActionGroup::addTo(QWidget *widget)
}
}
tb->setMenu(menu);
ToolBarManager::getInstance()->checkToolbarIconSize(static_cast<QToolBar*>(widget));
ToolBarManager::getInstance()->checkToolBarIconSize(static_cast<QToolBar*>(widget));

QObject::connect(menu, &QMenu::aboutToShow, [this, menu]() {
Q_EMIT aboutToShow(menu);
Expand Down Expand Up @@ -1334,15 +1334,6 @@ void WorkbenchGroup::addTo(QWidget *widget)
else
actTab->setVisible(false);
}
else if (widget->inherits("QMenuBar")) {
auto* box = new WorkbenchComboBox(this, widget);
setupBox(box);
box->setIconSize(QSize(16, 16));
box->addActions(groupAction()->actions());

bool left = WorkbenchSwitcher::isLeftCorner(WorkbenchSwitcher::getValue());
qobject_cast<QMenuBar*>(widget)->setCornerWidget(box, left ? Qt::TopLeftCorner : Qt::TopRightCorner);
}
else if (widget->inherits("QMenu")) {
QMenu* menu = qobject_cast<QMenu*>(widget);
if (!_menu) {
Expand Down
8 changes: 6 additions & 2 deletions src/Gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
# include <QLabel>
# include <QMdiSubWindow>
# include <QMenu>
# include <QMenuBar>
# include <QMessageBox>
# include <QMimeData>
# include <QPainter>
Expand Down Expand Up @@ -823,7 +824,10 @@ void populateMenu(QMenu *menu, MenuType type, bool popup,
relocate = relocate || (toolbar->isFloating()
&& !rectMain.contains(toolbar->mapToGlobal(QPoint(0,0))));

if (parent == mw || parent == mw->statusBar()) {
if (parent == mw
|| parent == mw->statusBar()
|| (parent && (parent->parentWidget() == mw->statusBar()
|| parent->parentWidget() == mw->menuBar()))) {
// Some misbehaved code may force the toolbar to be visible
// while hiding its action, which causes the user to be unable
// to switch it off. We'll just include those actions anyway.
Expand Down Expand Up @@ -2552,7 +2556,7 @@ void MainWindow::childEvent(QChildEvent *e)
// The parent maybe set in the object constructor, in which case we
// can't really tell the exact type of the object (i.e. e->child()). So
// there is no point using qobject_cast to check for QToolBar here.
ToolBarManager::checkToolbar();
ToolBarManager::checkToolBar();
}

QMainWindow::childEvent(e);
Expand Down
37 changes: 0 additions & 37 deletions src/Gui/MenuManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ void MenuManager::setup(MenuItem* menuItems) const
}
}

setupMenuBarCornerWidgets();

// hide all menus which we don't need for the moment
for (QList<QAction*>::Iterator it = actions.begin(); it != actions.end(); ++it) {
(*it)->setVisible(false);
Expand Down Expand Up @@ -366,41 +364,6 @@ void MenuManager::setup(MenuItem* item, QMenu* menu) const
}
}

void MenuManager::setupMenuBarCornerWidgets() const
{
/*Note: currently only workbench selector uses corner widget.*/
QMenuBar* menuBar = getMainWindow()->menuBar();
std::string pos = WorkbenchSwitcher::getValue();

bool showLeftWidget = false;
bool showRightWidget = false;

//Right corner widget
if (WorkbenchSwitcher::isRightCorner(pos)) {
//add workbench selector to menubar right corner widget.
if (!menuBar->cornerWidget(Qt::TopRightCorner)) {
Application::Instance->commandManager().addTo("Std_Workbench", menuBar);
}
showRightWidget = true;
}
//Left corner widget
else if (WorkbenchSwitcher::isLeftCorner(pos)) {
//add workbench selector to menubar left corner widget.
if (!menuBar->cornerWidget(Qt::TopLeftCorner)) {
Application::Instance->commandManager().addTo("Std_Workbench", menuBar);
}
showLeftWidget = true;
}

// Set visibility of corner widget
if (QWidget* right = menuBar->cornerWidget(Qt::TopRightCorner)) {
right->setVisible(showRightWidget);
}
if (QWidget* left = menuBar->cornerWidget(Qt::TopLeftCorner)) {
left->setVisible(showLeftWidget);
}
}

void MenuManager::retranslate() const
{
QMenuBar* menuBar = getMainWindow()->menuBar();
Expand Down
Loading

0 comments on commit f2f9ced

Please sign in to comment.