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

View File

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