Prevent crash by creating contextmenu only once

This commit is contained in:
Jan Käberich 2021-05-14 15:31:38 +02:00
parent 036837a6ae
commit ee82237993
2 changed files with 68 additions and 61 deletions

View File

@ -475,68 +475,67 @@ void TraceMarker::deltaDeleted()
void TraceMarker::updateContextmenu()
{
if(parent) {
parent->updateContextmenu();
contextmenu = parent->contextmenu;
} else {
if(contextmenu) {
// check if the contextmenu or one of its submenus is currently open
auto *activeWidget = QApplication::activePopupWidget();
while (activeWidget) {
if(activeWidget == contextmenu) {
// contextmenu currently open, do not update
return;
}
activeWidget = activeWidget->parentWidget();
}
delete contextmenu;
}
contextmenu = new QMenu();
auto typemenu = contextmenu->addMenu("Type");
auto typegroup = new QActionGroup(contextmenu);
for(auto t : getSupportedTypes()) {
auto setTypeAction = new QAction(typeToString(t));
setTypeAction->setCheckable(true);
if(t == type) {
setTypeAction->setChecked(true);
}
connect(setTypeAction, &QAction::triggered, [=](){
setType(t);
});
typegroup->addAction(setTypeAction);
typemenu->addAction(setTypeAction);
// do nothing, using contextmenu from parent anyway
return;
}
// check if the contextmenu or one of its submenus is currently open
auto *activeWidget = QApplication::activePopupWidget();
while (activeWidget) {
if(activeWidget == &contextmenu) {
// contextmenu currently open, do not update
qDebug() << "Contextmenu open, skipping update";
return;
}
activeWidget = activeWidget->parentWidget();
}
auto table = contextmenu->addMenu("Data Format in Table");
auto tablegroup = new QActionGroup(contextmenu);
for(auto f : applicableFormats()) {
auto setFormatAction = new QAction(formatToString(f));
setFormatAction->setCheckable(true);
if(f == formatTable) {
setFormatAction->setChecked(true);
}
connect(setFormatAction, &QAction::triggered, [=](){
setTableFormat(f);
});
tablegroup->addAction(setFormatAction);
table->addAction(setFormatAction);
}
contextmenu.clear();
auto graph = contextmenu->addMenu("Show on Graph");
for(auto f : applicableFormats()) {
auto setFormatAction = new QAction(formatToString(f));
setFormatAction->setCheckable(true);
if(formatGraph.count(f)) {
setFormatAction->setChecked(true);
}
connect(setFormatAction, &QAction::triggered, [=](bool checked){
if(checked) {
formatGraph.insert(f);
} else {
formatGraph.erase(f);
}
});
graph->addAction(setFormatAction);
auto typemenu = contextmenu.addMenu("Type");
auto typegroup = new QActionGroup(&contextmenu);
for(auto t : getSupportedTypes()) {
auto setTypeAction = new QAction(typeToString(t));
setTypeAction->setCheckable(true);
if(t == type) {
setTypeAction->setChecked(true);
}
connect(setTypeAction, &QAction::triggered, [=](){
setType(t);
});
typegroup->addAction(setTypeAction);
typemenu->addAction(setTypeAction);
}
auto table = contextmenu.addMenu("Data Format in Table");
auto tablegroup = new QActionGroup(&contextmenu);
for(auto f : applicableFormats()) {
auto setFormatAction = new QAction(formatToString(f));
setFormatAction->setCheckable(true);
if(f == formatTable) {
setFormatAction->setChecked(true);
}
connect(setFormatAction, &QAction::triggered, [=](){
setTableFormat(f);
});
tablegroup->addAction(setFormatAction);
table->addAction(setFormatAction);
}
auto graph = contextmenu.addMenu("Show on Graph");
for(auto f : applicableFormats()) {
auto setFormatAction = new QAction(formatToString(f));
setFormatAction->setCheckable(true);
if(formatGraph.count(f)) {
setFormatAction->setChecked(true);
}
connect(setFormatAction, &QAction::triggered, [=](bool checked){
if(checked) {
formatGraph.insert(f);
} else {
formatGraph.erase(f);
}
});
graph->addAction(setFormatAction);
}
}
@ -749,7 +748,6 @@ void TraceMarker::setTableFormat(TraceMarker::Format f)
}
formatTable = f;
updateContextmenu();
emit dataChanged(this);
}
@ -1050,6 +1048,14 @@ void TraceMarker::adjustSettings(double value)
update();
}
QMenu *TraceMarker::getContextMenu() {
if(parent) {
return parent->getContextMenu();
} else {
return &contextmenu;
}
}
void TraceMarker::update()
{
if(!parentTrace->size()) {

View File

@ -4,6 +4,7 @@
#include <QPixmap>
#include <QObject>
#include "trace.h"
#include <QMenu>
#include <QComboBox>
#include "CustomWidgets/siunitedit.h"
#include "savable.h"
@ -82,7 +83,7 @@ public:
SIUnitEdit* getSettingsEditor();
void adjustSettings(double value);
QMenu *getContextMenu() { return contextmenu;}
QMenu *getContextMenu();
// Updates marker position and data on automatic markers. Should be called whenever the tracedata is complete
void update();
@ -163,7 +164,7 @@ private:
QString suffix;
QString description;
QMenu *contextmenu;
QMenu contextmenu;
TraceMarker *delta;
std::vector<TraceMarker*> helperMarkers;