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; QSettings s;
if(!s.contains(hashToSettingsKey(hash))) { 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(); 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) bool InformationBox::AskQuestion(QString title, QString question, bool defaultAnswer, QString messageID)
{ {
// check if the user still wants to see this message // 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; QSettings s;
if(!s.contains(hashToSettingsKey(hash))) { 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); box->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
int ret = box->exec(); int ret = box->exec();
if(ret == QMessageBox::Yes) { 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), : QMessageBox(parent),
hash(hash) hash(hash)
{ {
setWindowTitle(title); setWindowTitle(title);
setText(message); setText(message);
setAttribute(Qt::WA_DeleteOnClose, true); setAttribute(Qt::WA_DeleteOnClose, true);
setIcon(QMessageBox::Information); setIcon(icon);
auto cb = new QCheckBox("Do not show this message again"); auto cb = new QCheckBox("Do not show this message again");
setCheckBox(cb); setCheckBox(cb);
if(hash == 0) {
cb->setVisible(false);
}
} }
InformationBox::~InformationBox() InformationBox::~InformationBox()

View File

@ -8,10 +8,11 @@ class InformationBox : public QMessageBox
Q_OBJECT Q_OBJECT
public: public:
static void ShowMessage(QString title, QString message, QString messageID = QString()); 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 // 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()); static bool AskQuestion(QString title, QString question, bool defaultAnswer, QString messageID = QString());
private: 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(); ~InformationBox();
static QString hashToSettingsKey(unsigned int hash); static QString hashToSettingsKey(unsigned int hash);
unsigned int hash; unsigned int hash;

View File

@ -2,6 +2,7 @@
#include "Traces/tracecsvexport.h" #include "Traces/tracecsvexport.h"
#include <QFileDialog> #include <QFileDialog>
#include "Traces/traceimportdialog.h" #include "Traces/traceimportdialog.h"
#include "CustomWidgets/informationbox.h"
TraceWidgetSA::TraceWidgetSA(TraceModel &model, QWidget *parent) TraceWidgetSA::TraceWidgetSA(TraceModel &model, QWidget *parent)
: TraceWidget(model, 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); auto filename = QFileDialog::getOpenFileName(nullptr, "Open measurement file", "", "CSV files (*.csv)", nullptr, QFileDialog::DontUseNativeDialog);
if (!filename.isEmpty()) { if (!filename.isEmpty()) {
std::vector<Trace*> traces; try {
QString prefix = QString(); std::vector<Trace*> traces;
auto csv = CSV::fromFile(filename); QString prefix = QString();
traces = Trace::createFromCSV(csv); auto csv = CSV::fromFile(filename);
// contruct prefix from filename traces = Trace::createFromCSV(csv);
prefix = filename; // contruct prefix from filename
// remove any directory names (keep only the filename itself) prefix = filename;
int lastSlash = qMax(prefix.lastIndexOf('/'), prefix.lastIndexOf('\\')); // remove any directory names (keep only the filename itself)
if(lastSlash != -1) { int lastSlash = qMax(prefix.lastIndexOf('/'), prefix.lastIndexOf('\\'));
prefix.remove(0, lastSlash + 1); 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 "Traces/tracecsvexport.h"
#include "ui_tracewidget.h" #include "ui_tracewidget.h"
#include "ui_s2pImportOptions.h" #include "ui_s2pImportOptions.h"
#include "CustomWidgets/informationbox.h"
#include <QMenu> #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); auto filename = QFileDialog::getOpenFileName(nullptr, "Open measurement file", "", "Touchstone files (*.s1p *.s2p *.s3p *.s4p);;CSV files (*.csv)", nullptr, QFileDialog::DontUseNativeDialog);
if (!filename.isEmpty()) { if (!filename.isEmpty()) {
std::vector<Trace*> traces; try {
QString prefix = QString(); std::vector<Trace*> traces;
if(filename.endsWith(".csv")) { QString prefix = QString();
auto csv = CSV::fromFile(filename); if(filename.endsWith(".csv")) {
traces = Trace::createFromCSV(csv); auto csv = CSV::fromFile(filename);
} else { traces = Trace::createFromCSV(csv);
// must be a touchstone file } else {
auto t = Touchstone::fromFile(filename.toStdString()); // must be a touchstone file
traces = Trace::createFromTouchstone(t); auto t = Touchstone::fromFile(filename.toStdString());
} traces = Trace::createFromTouchstone(t);
// contruct prefix from filename }
prefix = filename; // contruct prefix from filename
// remove any directory names (keep only the filename itself) prefix = filename;
int lastSlash = qMax(prefix.lastIndexOf('/'), prefix.lastIndexOf('\\')); // remove any directory names (keep only the filename itself)
if(lastSlash != -1) { int lastSlash = qMax(prefix.lastIndexOf('/'), prefix.lastIndexOf('\\'));
prefix.remove(0, lastSlash + 1); if(lastSlash != -1) {
} prefix.remove(0, lastSlash + 1);
// remove file type }
prefix.truncate(prefix.indexOf('.')); // remove file type
prefix.append("_"); prefix.truncate(prefix.indexOf('.'));
auto i = new TraceImportDialog(model, traces, prefix); prefix.append("_");
i->show(); auto i = new TraceImportDialog(model, traces, prefix);
if(filename.endsWith(".s2p")) { i->show();
// potential candidate to process via calibration/de-embedding if(filename.endsWith(".s2p")) {
connect(i, &TraceImportDialog::importFinsished, [=](const std::vector<Trace*> &traces) { // potential candidate to process via calibration/de-embedding
if(traces.size() == 4) { connect(i, &TraceImportDialog::importFinsished, [=](const std::vector<Trace*> &traces) {
// all traces imported, can calculate calibration/de-embedding if(traces.size() == 4) {
bool calAvailable = cal.nPoints() > 0; // all traces imported, can calculate calibration/de-embedding
bool deembedAvailable = deembed.getOptions().size() > 0; bool calAvailable = cal.nPoints() > 0;
if(calAvailable || deembedAvailable) { bool deembedAvailable = deembed.getOptions().size() > 0;
// check if user wants to apply either one to the imported traces if(calAvailable || deembedAvailable) {
auto dialog = new QDialog(); // check if user wants to apply either one to the imported traces
auto ui = new Ui::s2pImportOptions; auto dialog = new QDialog();
ui->setupUi(dialog); auto ui = new Ui::s2pImportOptions;
ui->applyCal->setEnabled(calAvailable); ui->setupUi(dialog);
ui->deembed->setEnabled(deembedAvailable); ui->applyCal->setEnabled(calAvailable);
bool applyCal = false; ui->deembed->setEnabled(deembedAvailable);
bool applyDeembed = false; bool applyCal = false;
connect(ui->applyCal, &QCheckBox::toggled, [&](bool checked) { bool applyDeembed = false;
applyCal = checked; connect(ui->applyCal, &QCheckBox::toggled, [&](bool checked) {
}); applyCal = checked;
connect(ui->deembed, &QCheckBox::toggled, [&](bool checked) { });
applyDeembed = checked; connect(ui->deembed, &QCheckBox::toggled, [&](bool checked) {
}); applyDeembed = checked;
dialog->exec(); });
if(applyCal) { dialog->exec();
cal.correctTraces(*traces[0], *traces[1], *traces[2], *traces[3]); if(applyCal) {
} cal.correctTraces(*traces[0], *traces[1], *traces[2], *traces[3]);
if(applyDeembed) { }
deembed.Deembed(*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()+"\"");
} }
} }
} }