Skip to content

Commit

Permalink
show error message when screenshot or export file write fails
Browse files Browse the repository at this point in the history
fix for OpenHantek/openhantek#338

Signed-off-by: Martin <Ho-Ro@users.noreply.github.com>
  • Loading branch information
Ho-Ro committed Mar 27, 2023
1 parent dfe0aaa commit 2ea8261
Show file tree
Hide file tree
Showing 12 changed files with 734 additions and 669 deletions.
9 changes: 6 additions & 3 deletions openhantek/src/exporting/exportcsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QFile>
#include <QFileDialog>
#include <QLocale>
#include <QMessageBox>
#include <QTextStream>

ExporterCSV::ExporterCSV() {}
Expand Down Expand Up @@ -40,10 +41,12 @@ QFile *ExporterCSV::getFile() {
if ( fileDialog.exec() != QDialog::Accepted )
return nullptr;

QFile *file = new QFile( fileDialog.selectedFiles().first() );
if ( !file->open( QIODevice::WriteOnly | QIODevice::Text ) )
QFile *csvFile = new QFile( fileDialog.selectedFiles().first() );
if ( !csvFile->open( QIODevice::WriteOnly | QIODevice::Text ) ) {
QMessageBox::critical( nullptr, tr( "Error" ), csvFile->fileName() );
return nullptr;
return file;
}
return csvFile;
}

void ExporterCSV::fillHeaders( QTextStream &csvStream, const ExporterData &dto, const char *sep ) {
Expand Down
5 changes: 4 additions & 1 deletion openhantek/src/exporting/exportjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QFile>
#include <QFileDialog>
#include <QLocale>
#include <QMessageBox>
#include <QTextStream>

ExporterJSON::ExporterJSON() {}
Expand Down Expand Up @@ -42,8 +43,10 @@ QFile *ExporterJSON::getFile() {
return nullptr;

QFile *jsonFile = new QFile( fileDialog.selectedFiles().first() );
if ( !jsonFile->open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( !jsonFile->open( QIODevice::WriteOnly | QIODevice::Text ) ) {
QMessageBox::critical( nullptr, tr( "Error" ), jsonFile->fileName() );
return nullptr;
}
return jsonFile;
}

Expand Down
7 changes: 5 additions & 2 deletions openhantek/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "dsosettings.h"

#include <QDesktopServices>
#include <QDir>
#include <QFileDialog>
#include <QLoggingCategory>
#include <QMessageBox>
Expand Down Expand Up @@ -621,7 +622,8 @@ void MainWindow::screenShot( screenshotType_t screenshotType, bool autoSafe ) {
QStringList filters;
fileName += ".png";
if ( autoSafe ) { // save under default name as PNG without asking
screenshot.save( fileName );
if ( !screenshot.save( fileName ) )
QMessageBox::critical( this, tr( "Error" ), QFileInfo{ fileName }.absoluteFilePath() );
return;
}
filters << tr( "Image (*.png *.jpg)" ) << tr( "Portable Document Format (*.pdf)" );
Expand All @@ -633,7 +635,8 @@ void MainWindow::screenShot( screenshotType_t screenshotType, bool autoSafe ) {

fileName = fileDialog.selectedFiles().first();
if ( filters.indexOf( fileDialog.selectedNameFilter() ) == 0 ) { // save as image
screenshot.save( fileName );
if ( !screenshot.save( fileName ) )
QMessageBox::critical( this, tr( "Error" ), QFileInfo{ fileName }.absoluteFilePath() );
return;
}

Expand Down
Loading

0 comments on commit 2ea8261

Please sign in to comment.