From a92af08b3ecd7c218c3fe3ecbbc5f10a984ebc0b Mon Sep 17 00:00:00 2001 From: Daniel Guramulta Date: Tue, 28 Jan 2020 10:44:30 +0200 Subject: [PATCH] NetworkAnalyzer: fix file dialog not displaying correct file formats Signed-off-by: Daniel Guramulta --- src/network_analyzer.cpp | 45 +++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/network_analyzer.cpp b/src/network_analyzer.cpp index 2ddcfd9ecb..be449889c5 100644 --- a/src/network_analyzer.cpp +++ b/src/network_analyzer.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #include @@ -572,11 +573,29 @@ NetworkAnalyzer::NetworkAnalyzer(struct iio_context *ctx, Filter *filt, widget->render(&painter); QString date = QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss"); - QString fileNameHint = "Scopy-" + api->objectName() + "-" + date + ".png"; + QString fileNameHint = "Scopy-" + api->objectName() + "-" + date; - QString fileName = QFileDialog::getSaveFileName(this, - tr("Save to"), fileNameHint, tr({"(*.png);;"}), - nullptr, (m_useNativeDialogs ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog)); + const QList imageFormats = + QImageWriter::supportedImageFormats(); + + QStringList filter; + if ( imageFormats.size() > 0 ) { + for ( int i = 0; i < imageFormats.size(); i++ ) { + filter += (imageFormats[i].toUpper() + " " + + tr("Image") + " (*." + imageFormats[i] + ")"); + } + } + + QString selectedFilter = filter[0]; + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save to"), fileNameHint, filter.join(";;"), + &selectedFilter, (m_useNativeDialogs ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog)); + + if (fileName.split(".").size() <= 1) { + // file name w/o extension. Let's append it + QString ext = selectedFilter.split(".")[1].split(")")[0]; + fileName += "." + ext; + } painter.end(); img.invertPixels(QImage::InvertRgb); @@ -815,10 +834,22 @@ void NetworkAnalyzer::showEvent(QShowEvent *event) void NetworkAnalyzer::on_btnExport_clicked() { + QStringList filter; + filter += QString(tr("Comma-separated values files (*.csv)")); + filter += QString(tr("Tab-delimited values files (*.txt)")); + filter += QString(tr("All Files(*)")); + + QString selectedFilter = filter[0]; + QString fileName = QFileDialog::getSaveFileName(this, - tr("Export"), "", tr("Comma-separated values files (*.csv)", - "Tab-delimited values files (*.txt)"), - nullptr, (m_useNativeDialogs ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog)); + tr("Export"), "", filter.join(";;"), + &selectedFilter, (m_useNativeDialogs ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog)); + + if (fileName.split(".").size() <= 1) { + // file name w/o extension. Let's append it + QString ext = selectedFilter.split(".")[1].split(")")[0]; + fileName += "." + ext; + } if (!fileName.isEmpty()) { FileManager fm("Network Analyzer");