Catch exception when importing files and display error message

This commit is contained in:
Jan Käberich 2021-05-12 22:05:50 +02:00
parent 2fac430381
commit 3d4d3c9468
4 changed files with 93 additions and 72 deletions

View File

@ -15,11 +15,17 @@ void InformationBox::ShowMessage(QString title, QString message, QString message
QSettings s;
if(!s.contains(hashToSettingsKey(hash))) {
auto box = new InformationBox(title, message, hash, nullptr);
auto box = new InformationBox(title, message, QMessageBox::Information, hash, nullptr);
box->show();
}
}
void InformationBox::ShowError(QString title, QString message)
{
auto box = new InformationBox(title, message, QMessageBox::Information, 0, nullptr);
box->show();
}
bool InformationBox::AskQuestion(QString title, QString question, bool defaultAnswer, QString messageID)
{
// check if the user still wants to see this message
@ -32,7 +38,7 @@ bool InformationBox::AskQuestion(QString title, QString question, bool defaultAn
QSettings s;
if(!s.contains(hashToSettingsKey(hash))) {
auto box = new InformationBox(title, question, hash, nullptr);
auto box = new InformationBox(title, question, QMessageBox::Question, hash, nullptr);
box->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
int ret = box->exec();
if(ret == QMessageBox::Yes) {
@ -46,17 +52,21 @@ bool InformationBox::AskQuestion(QString title, QString question, bool defaultAn
}
}
InformationBox::InformationBox(QString title, QString message, unsigned int hash, QWidget *parent)
InformationBox::InformationBox(QString title, QString message, Icon icon, unsigned int hash, QWidget *parent)
: QMessageBox(parent),
hash(hash)
{
setWindowTitle(title);
setText(message);
setAttribute(Qt::WA_DeleteOnClose, true);
setIcon(QMessageBox::Information);
setIcon(icon);
auto cb = new QCheckBox("Do not show this message again");
setCheckBox(cb);
if(hash == 0) {
cb->setVisible(false);
}
}
InformationBox::~InformationBox()

View File

@ -8,10 +8,11 @@ class InformationBox : public QMessageBox
Q_OBJECT
public:
static void ShowMessage(QString title, QString message, QString messageID = QString());
static void ShowError(QString title, QString message);
// Display a dialog with yes/no buttons. Returns true if yes is clicked, false otherwise. If the user has selected to never see this message again, defaultAnswer is returned instead
static bool AskQuestion(QString title, QString question, bool defaultAnswer, QString messageID = QString());
private:
InformationBox(QString title, QString message, unsigned int hash, QWidget *parent);
InformationBox(QString title, QString message, QMessageBox::Icon icon, unsigned int hash, QWidget *parent);
~InformationBox();
static QString hashToSettingsKey(unsigned int hash);
unsigned int hash;

View File

@ -2,6 +2,7 @@
#include "Traces/tracecsvexport.h"
#include <QFileDialog>
#include "Traces/traceimportdialog.h"
#include "CustomWidgets/informationbox.h"
TraceWidgetSA::TraceWidgetSA(TraceModel &model, QWidget *parent)
: TraceWidget(model, parent)
@ -19,6 +20,7 @@ void TraceWidgetSA::importDialog()
{
auto filename = QFileDialog::getOpenFileName(nullptr, "Open measurement file", "", "CSV files (*.csv)", nullptr, QFileDialog::DontUseNativeDialog);
if (!filename.isEmpty()) {
try {
std::vector<Trace*> traces;
QString prefix = QString();
auto csv = CSV::fromFile(filename);
@ -35,5 +37,8 @@ void TraceWidgetSA::importDialog()
prefix.append("_");
auto i = new TraceImportDialog(model, traces, prefix);
i->show();
} catch(const std::exception e) {
InformationBox::ShowError("Failed to import file", QString("Attempt to import file ended with error: \"") + e.what()+"\"");
}
}
}

View File

@ -6,6 +6,7 @@
#include "Traces/tracecsvexport.h"
#include "ui_tracewidget.h"
#include "ui_s2pImportOptions.h"
#include "CustomWidgets/informationbox.h"
#include <QMenu>
@ -47,6 +48,7 @@ void TraceWidgetVNA::importDialog()
{
auto filename = QFileDialog::getOpenFileName(nullptr, "Open measurement file", "", "Touchstone files (*.s1p *.s2p *.s3p *.s4p);;CSV files (*.csv)", nullptr, QFileDialog::DontUseNativeDialog);
if (!filename.isEmpty()) {
try {
std::vector<Trace*> traces;
QString prefix = QString();
if(filename.endsWith(".csv")) {
@ -102,5 +104,8 @@ void TraceWidgetVNA::importDialog()
}
});
}
} catch(const std::exception& e) {
InformationBox::ShowError("Failed to import file", QString("Attempt to import file ended with error: \"") + e.what()+"\"");
}
}
}