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,21 +20,25 @@ void TraceWidgetSA::importDialog()
{
auto filename = QFileDialog::getOpenFileName(nullptr, "Open measurement file", "", "CSV files (*.csv)", nullptr, QFileDialog::DontUseNativeDialog);
if (!filename.isEmpty()) {
std::vector<Trace*> traces;
QString prefix = QString();
auto csv = CSV::fromFile(filename);
traces = Trace::createFromCSV(csv);
// contruct prefix from filename
prefix = filename;
// remove any directory names (keep only the filename itself)
int lastSlash = qMax(prefix.lastIndexOf('/'), prefix.lastIndexOf('\\'));
if(lastSlash != -1) {
prefix.remove(0, lastSlash + 1);
try {
std::vector<Trace*> traces;
QString prefix = QString();
auto csv = CSV::fromFile(filename);
traces = Trace::createFromCSV(csv);
// contruct prefix from filename
prefix = filename;
// remove any directory names (keep only the filename itself)
int lastSlash = qMax(prefix.lastIndexOf('/'), prefix.lastIndexOf('\\'));
if(lastSlash != -1) {
prefix.remove(0, lastSlash + 1);
}
// remove file type
prefix.truncate(prefix.indexOf('.'));
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()+"\"");
}
// remove file type
prefix.truncate(prefix.indexOf('.'));
prefix.append("_");
auto i = new TraceImportDialog(model, traces, prefix);
i->show();
}
}

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,60 +48,64 @@ 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()) {
std::vector<Trace*> traces;
QString prefix = QString();
if(filename.endsWith(".csv")) {
auto csv = CSV::fromFile(filename);
traces = Trace::createFromCSV(csv);
} else {
// must be a touchstone file
auto t = Touchstone::fromFile(filename.toStdString());
traces = Trace::createFromTouchstone(t);
}
// contruct prefix from filename
prefix = filename;
// remove any directory names (keep only the filename itself)
int lastSlash = qMax(prefix.lastIndexOf('/'), prefix.lastIndexOf('\\'));
if(lastSlash != -1) {
prefix.remove(0, lastSlash + 1);
}
// remove file type
prefix.truncate(prefix.indexOf('.'));
prefix.append("_");
auto i = new TraceImportDialog(model, traces, prefix);
i->show();
if(filename.endsWith(".s2p")) {
// potential candidate to process via calibration/de-embedding
connect(i, &TraceImportDialog::importFinsished, [=](const std::vector<Trace*> &traces) {
if(traces.size() == 4) {
// all traces imported, can calculate calibration/de-embedding
bool calAvailable = cal.nPoints() > 0;
bool deembedAvailable = deembed.getOptions().size() > 0;
if(calAvailable || deembedAvailable) {
// check if user wants to apply either one to the imported traces
auto dialog = new QDialog();
auto ui = new Ui::s2pImportOptions;
ui->setupUi(dialog);
ui->applyCal->setEnabled(calAvailable);
ui->deembed->setEnabled(deembedAvailable);
bool applyCal = false;
bool applyDeembed = false;
connect(ui->applyCal, &QCheckBox::toggled, [&](bool checked) {
applyCal = checked;
});
connect(ui->deembed, &QCheckBox::toggled, [&](bool checked) {
applyDeembed = checked;
});
dialog->exec();
if(applyCal) {
cal.correctTraces(*traces[0], *traces[1], *traces[2], *traces[3]);
}
if(applyDeembed) {
deembed.Deembed(*traces[0], *traces[1], *traces[2], *traces[3]);
try {
std::vector<Trace*> traces;
QString prefix = QString();
if(filename.endsWith(".csv")) {
auto csv = CSV::fromFile(filename);
traces = Trace::createFromCSV(csv);
} else {
// must be a touchstone file
auto t = Touchstone::fromFile(filename.toStdString());
traces = Trace::createFromTouchstone(t);
}
// contruct prefix from filename
prefix = filename;
// remove any directory names (keep only the filename itself)
int lastSlash = qMax(prefix.lastIndexOf('/'), prefix.lastIndexOf('\\'));
if(lastSlash != -1) {
prefix.remove(0, lastSlash + 1);
}
// remove file type
prefix.truncate(prefix.indexOf('.'));
prefix.append("_");
auto i = new TraceImportDialog(model, traces, prefix);
i->show();
if(filename.endsWith(".s2p")) {
// potential candidate to process via calibration/de-embedding
connect(i, &TraceImportDialog::importFinsished, [=](const std::vector<Trace*> &traces) {
if(traces.size() == 4) {
// all traces imported, can calculate calibration/de-embedding
bool calAvailable = cal.nPoints() > 0;
bool deembedAvailable = deembed.getOptions().size() > 0;
if(calAvailable || deembedAvailable) {
// check if user wants to apply either one to the imported traces
auto dialog = new QDialog();
auto ui = new Ui::s2pImportOptions;
ui->setupUi(dialog);
ui->applyCal->setEnabled(calAvailable);
ui->deembed->setEnabled(deembedAvailable);
bool applyCal = false;
bool applyDeembed = false;
connect(ui->applyCal, &QCheckBox::toggled, [&](bool checked) {
applyCal = checked;
});
connect(ui->deembed, &QCheckBox::toggled, [&](bool checked) {
applyDeembed = checked;
});
dialog->exec();
if(applyCal) {
cal.correctTraces(*traces[0], *traces[1], *traces[2], *traces[3]);
}
if(applyDeembed) {
deembed.Deembed(*traces[0], *traces[1], *traces[2], *traces[3]);
}
}
}
}
});
});
}
} catch(const std::exception& e) {
InformationBox::ShowError("Failed to import file", QString("Attempt to import file ended with error: \"") + e.what()+"\"");
}
}
}