Skip to content

Commit

Permalink
Gui: Reuse QActions for workbench activation
Browse files Browse the repository at this point in the history
This fixes segfault that can occour due to keeping reference to QAction
that is supposed to change workbench.
  • Loading branch information
kadet1090 committed May 12, 2024
1 parent b1a11a6 commit 2e6af15
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
30 changes: 23 additions & 7 deletions src/Gui/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,14 @@ WorkbenchGroup::WorkbenchGroup ( Command* pcCmd, QObject * parent )
this, &WorkbenchGroup::onWorkbenchActivated);
}

QAction* WorkbenchGroup::getOrCreateAction(const QString& wbName) {
if (!actionByWorkbenchName.contains(wbName)) {
actionByWorkbenchName[wbName] = new QAction;
}

return actionByWorkbenchName[wbName];
}

void WorkbenchGroup::addTo(QWidget *widget)
{
if (widget->inherits("QToolBar")) {
Expand Down Expand Up @@ -657,26 +665,30 @@ void WorkbenchGroup::addTo(QWidget *widget)

void WorkbenchGroup::refreshWorkbenchList()
{
QStringList enabled_wbs_list = DlgSettingsWorkbenchesImp::getEnabledWorkbenches();
QStringList enabledWbNames = DlgSettingsWorkbenchesImp::getEnabledWorkbenches();

// Clear the actions.
for (QAction* action : actions()) {
groupAction()->removeAction(action);
delete action;
}

enabledWbsActions.clear();
disabledWbsActions.clear();

std::string activeWbName = WorkbenchManager::instance()->activeName();

// Create action list of enabled wb
int index = 0;
for (const auto& wbName : enabled_wbs_list) {
for (const auto& wbName : enabledWbNames) {
QString name = Application::Instance->workbenchMenuText(wbName);
QPixmap px = Application::Instance->workbenchIcon(wbName);
QString tip = Application::Instance->workbenchToolTip(wbName);

QAction* action = groupAction()->addAction(name);
QAction* action = getOrCreateAction(wbName);

groupAction()->addAction(action);

action->setText(name);
action->setCheckable(true);
action->setData(QVariant(index)); // set the index
action->setObjectName(wbName);
Expand All @@ -694,13 +706,17 @@ void WorkbenchGroup::refreshWorkbenchList()
}

// Also create action list of disabled wbs
QStringList disabled_wbs_list = DlgSettingsWorkbenchesImp::getDisabledWorkbenches();
for (const auto& wbName : disabled_wbs_list) {
QStringList disabledWbNames = DlgSettingsWorkbenchesImp::getDisabledWorkbenches();
for (const auto& wbName : disabledWbNames) {
QString name = Application::Instance->workbenchMenuText(wbName);
QPixmap px = Application::Instance->workbenchIcon(wbName);
QString tip = Application::Instance->workbenchToolTip(wbName);

QAction* action = groupAction()->addAction(name);
QAction* action = getOrCreateAction(wbName);

groupAction()->addAction(action);

action->setText(name);
action->setCheckable(true);
action->setData(QVariant(index)); // set the index
action->setObjectName(wbName);
Expand Down
10 changes: 8 additions & 2 deletions src/Gui/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <QAction>
#include <QComboBox>
#include <QKeySequence>
#include <QMap>
#include <FCGlobal.h>

namespace Gui
Expand Down Expand Up @@ -188,13 +189,16 @@ class GuiExport WorkbenchGroup : public ActionGroup
{
Q_OBJECT

QAction* getOrCreateAction(const QString& wbName);

public:
/**
* Creates an action for the command \a pcCmd to load the workbench \a name
* when it gets activated.
*/
WorkbenchGroup (Command* pcCmd, QObject * parent);
void addTo (QWidget * widget) override;
WorkbenchGroup(Command* pcCmd, QObject* parent);

void addTo(QWidget * widget) override;
void refreshWorkbenchList();

void slotActivateWorkbench(const char*);
Expand All @@ -212,6 +216,8 @@ protected Q_SLOTS:
QList<QAction*> enabledWbsActions;
QList<QAction*> disabledWbsActions;

QMap<QString, QAction*> actionByWorkbenchName;

Q_DISABLE_COPY(WorkbenchGroup)
};

Expand Down

0 comments on commit 2e6af15

Please sign in to comment.