]> Devoid-pointer.net GitWeb - anyanka.git/commitdiff
Guess output type by file extension
authorMichal Malý <madcatxster@prifuk.cz>
Sat, 15 Mar 2014 17:28:35 +0000 (18:28 +0100)
committerMichal Malý <madcatxster@prifuk.cz>
Sat, 15 Mar 2014 17:28:35 +0000 (18:28 +0100)
14 files changed:
Anyanka.pro
csvdatawriterbackend.cpp
datafileexporter.cpp
datafileexporter.h
datamanager.cpp
gui/exportgraphtoimagedialog.cpp
gui/exportgraphtoimagedialog.h
gui/exportrawdatadialog.cpp
gui/exportrawdatadialog.h
gui/graphview.h
gui/signalview.cpp
helpers.h [moved from mathhelpers.h with 78% similarity]
imagedrawer.cpp
imagedrawer.h

index 23bccc111d2c600a0657aa5817737bcfef9f985b..768d7f2c81b21d536948b065ceda1ba883dca2ac 100644 (file)
@@ -68,7 +68,6 @@ HEADERS  += \
     gui/graphview.h \
     signalcontroller.h \
     metatypes.h \
-    mathhelpers.h \
     integratedpeak.h \
     sequence.h \
     sequenceselectormodel.h \
@@ -88,7 +87,8 @@ HEADERS  += \
     imagedrawer.h \
     signaldrawer.h \
     enumflags.h \
-    jsonserializable.h
+    jsonserializable.h \
+    helpers.h
 
 FORMS += \
     gui/mainwindow.ui \
index 5b4e89f117454d9723c0a32b3dcb2ca817f7789d..1372426d375fa13f8edcf3f5271be667bbf5afae 100644 (file)
@@ -41,7 +41,20 @@ QString CSVDataWriterBackend::header(const QVariantList& data) const
 
 QString CSVDataWriterBackend::filename(const QString& name, const QString& extra) const
 {
-  QString newname = name;
+
+  QString newname;
+  int dotIdx;
+
+  dotIdx = name.lastIndexOf('.');
+  if (dotIdx > 0) {
+    QString suffix = name.mid(dotIdx+1).toLower();
+    if (suffix.compare(FILE_EXTENSION) == 0)
+      newname = name.mid(0, dotIdx-1); /* Cut the suffix */
+    else
+      newname = name;
+  } else
+    newname = name;
+
   if (extra.compare("") != 0)
     return newname + "_" + extra + "." + FILE_EXTENSION;
   else
index 72d8f87afddba058b000a7fb1b6b8cebaf38defc..536152f1ec183e2ef9b3b69f18923892513a25dd 100644 (file)
 
 #include "datafileexporter.h"
 #include "csvdatawriterbackend.h"
+#include "helpers.h"
 #include <QtCore/QDir>
 #include <QtCore/QLocale>
 #include <QtWidgets/QMessageBox>
 
-const QStringList DataFileExporter::SUPPORTED_FORMATS = QStringList() << "Comma separated values (CSV)";
+#include <QDebug>
+
+const std::vector<OutputFormat> DataFileExporter::SUPPORTED_FORMATS = { OutputFormat("Comma separated values", "csv") };
+
+/* Static methods */
+QStringList DataFileExporter::supportedFormats()
+{
+  QStringList list;
+
+  for (const OutputFormat& of : SUPPORTED_FORMATS) {
+    list << of.first;
+  }
+
+  return list;
+}
 
 DataFileExporter::DataFileExporter(QObject* parent) :
   QObject(parent)
@@ -46,7 +61,7 @@ exportData(const WriteList& list, const QString& path, const int formatIdx)
   DataFileExporter::ReturnCode ret;
   DataWriterBackend* backend;
 
-  if (formatIdx < 0 || formatIdx >= SUPPORTED_FORMATS.length()) {
+  if (formatIdx < 0 || formatIdx >= SUPPORTED_FORMATS.size()) {
     return ReturnCode::E_INVAL_FORMAT;
   }
 
@@ -70,6 +85,8 @@ exportData(const WriteList& list, const QString& path, const int formatIdx)
     case OutputFormats::CSV:
       backend = new CSVDataWriterBackend(this);
       break;
+    default:
+      return ReturnCode::E_INVAL_FORMAT;
   }
 
   if (list.size() > 1)
@@ -141,3 +158,24 @@ DataFileExporter::ReturnCode DataFileExporter::writeToFile(QFile& file, const Da
 
   return ReturnCode::SUCCESS;
 }
+
+/** Public slots **/
+
+void DataFileExporter::guessFormatFromSuffix(const QString& name)
+{
+  QString suffix = getFileNameSuffix(name);
+
+  if (suffix.length() == 0)
+    return;
+
+  for (size_t i = 0; i < SUPPORTED_FORMATS.size(); i++) {
+    const OutputFormat& of = SUPPORTED_FORMATS.at(i);
+    if (suffix.compare(of.second) == 0) {
+      qDebug() << "Changing format to idx" << i;
+      emit formatChanged(i);
+      return;
+    }
+  }
+
+  qDebug() << "Cannot guess type from suffix";
+}
index ab99c18ac281a4658eafbe6d7fd60bbe8232ac16..c76657734cf14c9ff4210b390880624ef4a91e04 100644 (file)
@@ -31,6 +31,8 @@
 #include <QtCore/QFile>
 #include <QtCore/QObject>
 
+typedef std::pair<const QString, const QString> OutputFormat;
+
 class DataFileExporter : public QObject
 {
   Q_OBJECT
@@ -48,7 +50,7 @@ public:
   explicit DataFileExporter(QObject* parent = nullptr);
   ReturnCode exportData(const WriteList& header, const QString& path, const int formatIdx);
 
-  static const QStringList SUPPORTED_FORMATS;
+  static QStringList supportedFormats();
 private:
   enum class OutputFormats {
     CSV
@@ -58,9 +60,13 @@ private:
   QString toCSVLine(double time, double value);
   ReturnCode writeToFile(QFile& file, const DataWriterBackend* writer, const std::pair<QVariantList, std::vector<QVariantList>>& data);
 
+  static const std::vector<OutputFormat> SUPPORTED_FORMATS;
+
 signals:
+  void formatChanged(const int idx);
 
 public slots:
+  void guessFormatFromSuffix(const QString& name);
 
 };
 
index 3d624ef8e8c7b925373b13d391a8f7e55c57d241..e3fd1617cecb444a5356055f6e2efc543cb0c741 100644 (file)
@@ -186,11 +186,13 @@ Signal::Equipment DataManager::equipmentFromFiletype(AGRE_Filetype filetype)
 void DataManager::processDataExport(GeneratorFunc genfunc)
 {
   DataFileExporter exporter(this);
-  ExportRawDataDialog dlg(DataFileExporter::SUPPORTED_FORMATS);
+  ExportRawDataDialog dlg(DataFileExporter::supportedFormats());
   DataFileExporter::WriteList writeList;
   std::shared_ptr<SingleRunData> sr = m_activeSequence->selectedRun();
   int ret = 1;
 
+  connect(&dlg, SIGNAL(fileNameChanged(QString)), &exporter, SLOT(guessFormatFromSuffix(QString)));
+  connect(&exporter, SIGNAL(formatChanged(int)), &dlg, SLOT(onFormatChanged(int)));
   for (const std::string& s : sr->allKeys())
     dlg.addAvailableSignal(QString::fromStdString(s));
 
@@ -449,6 +451,12 @@ void DataManager::onExportGraphToImage()
   if (sr == nullptr)
     return;
 
+  connect(&dlg, &ExportGraphToImageDialog::fileNameChanged, [&dlg, this](const QString& name) {
+    int idx = ImageDrawer::guessFormatFromSuffix(name);
+    if (idx > 0)
+      dlg.onFormatChanged(idx);
+  });
+
   for (const std::pair<std::string, std::shared_ptr<SignalController>>& p : sr->allControllers()) {
     GUIViewport vp = p.second->guiViewport();
     dlg.addSignal(vp.absFromTime, vp.absFromValue, vp.absToTime, vp.absToValue, p.first);
index 633c4ffe331a145e5960fb6e9861b60f4e0b9e26..c16bb3f1c902e0bb5ab22da5cd5cdf8971f2b33e 100644 (file)
@@ -42,6 +42,7 @@ ExportGraphToImageDialog::ExportGraphToImageDialog(QList<QByteArray> formats, QW
   connect(ui->qpb_cancel, SIGNAL(clicked()), this, SLOT(reject()));
   connect(ui->qpb_ok, SIGNAL(clicked()), this, SLOT(checkAndAccept()));
   connect(ui->qcbox_signal, SIGNAL(currentIndexChanged(QString)), this, SLOT(changeBounds(QString)));
+  connect(ui->qle_destPath, SIGNAL(textChanged(QString)), this, SLOT(onFileNameChanged(QString)));
 }
 
 void ExportGraphToImageDialog::addSignal(const double fromX, const double fromY, const double toX, const double toY, const std::string& key)
@@ -125,7 +126,13 @@ int ExportGraphToImageDialog::imageWidth() const
   return w;
 }
 
-/* Private slots */
+/** Public slots **/
+void ExportGraphToImageDialog::onFormatChanged(const int idx)
+{
+  ui->qcbox_outputFormat->setCurrentIndex(idx);
+}
+
+/** Private slots **/
 
 void ExportGraphToImageDialog::changeBounds(const QString& key)
 {
@@ -194,6 +201,11 @@ void ExportGraphToImageDialog::onBrowseClicked()
     ui->qle_destPath->setText(fdlg.selectedFiles()[0]);
 }
 
+void ExportGraphToImageDialog::onFileNameChanged(const QString& name)
+{
+  emit fileNameChanged(name);
+}
+
 ExportGraphToImageDialog::~ExportGraphToImageDialog()
 {
   delete ui;
index cc1224f801ba5672644776e99a81e857a7830a39..64b2212fcb286a247ea0a26c3c24e99d09c05f9f 100644 (file)
@@ -59,10 +59,17 @@ private:
 
   Ui::ExportGraphToImageDialog *ui;
 
+public slots:
+  void onFormatChanged(const int idx);
+
 private slots:
   void checkAndAccept();
   void changeBounds(const QString& key);
   void onBrowseClicked();
+  void onFileNameChanged(const QString& name);
+
+signals:
+  void fileNameChanged(const QString& name);
 
 };
 
index f5441a7237d7beaec00ad53578e0bcab12e60e96..bb7230a3d638885604a603a129ad3618dab8f497 100644 (file)
@@ -37,6 +37,7 @@ ExportRawDataDialog::ExportRawDataDialog(const QStringList& supportedFormats, QW
   connect(ui->qpb_browse, SIGNAL(clicked()), this, SLOT(onBrowseClicked()));
   connect(ui->qpb_ok, SIGNAL(clicked()), this, SLOT(accept()));
   connect(ui->qpb_cancel, SIGNAL(clicked()), this, SLOT(reject()));
+  connect(ui->qle_destPath, SIGNAL(textChanged(QString)), this, SLOT(onFileNameChanged(QString)));
 }
 
 /* Public functions */
@@ -79,6 +80,12 @@ std::vector<std::string> ExportRawDataDialog::selectedSignalKeys() const
   return kl;
 }
 
+/* Public slots */
+void ExportRawDataDialog::onFormatChanged(const int idx)
+{
+  ui->qcbox_formats->setCurrentIndex(idx);
+}
+
 /* Private slots */
 
 void ExportRawDataDialog::onBrowseClicked()
@@ -88,6 +95,11 @@ void ExportRawDataDialog::onBrowseClicked()
     ui->qle_destPath->setText(fdlg.selectedFiles()[0]);
 }
 
+void ExportRawDataDialog::onFileNameChanged(const QString& name)
+{
+  emit fileNameChanged(name);
+}
+
 ExportRawDataDialog::~ExportRawDataDialog()
 {
   delete ui;
index 2b5581e9c766e134fcd6e960d916ecb951696795..b232c41d1d628e60c2d5df7db81bec95b89e07f1 100644 (file)
@@ -51,9 +51,15 @@ private:
   QStringList m_supportedFormats;
   Ui::ExportRawDataDialog* ui;
 
+public slots:
+  void onFormatChanged(const int idx);
+
 private slots:
   void onBrowseClicked();
+  void onFileNameChanged(const QString& name);
 
+signals:
+  void fileNameChanged(const QString& name);
 };
 
 #endif // EXPORTRAWDATADIALOG_H
index 2d0bd75a0b35fa4a7ced4d66caa0ccb7b3311b48..9e44c0eb7a0647c9e20067e1791d6f3a3f5649c5 100644 (file)
@@ -26,7 +26,7 @@
 
 #include "gui/graphviewcontextmenu.h"
 #include "signaldrawer.h"
-#include "mathhelpers.h"
+#include "helpers.h"
 #include "metatypes.h"
 #include <QtGui/QMouseEvent>
 #include <QtWidgets/QWidget>
index c1da218058711a57f455e5adb1e067ce47bbcbd4..2b8d620beb46b852e684a6f772dfcef4a17f24a3 100644 (file)
@@ -23,7 +23,7 @@
 
 #include "gui/signalview.h"
 #include "ui_signalview.h"
-#include "mathhelpers.h"
+#include "helpers.h"
 #include <cmath>
 #include <QtGui/QFontMetrics>
 #include <QtGui/QPainter>
similarity index 78%
rename from mathhelpers.h
rename to helpers.h
index 5c2ca7a11e7ba159229d99239f95963dbdd3834b..c3b889a7eb87ae0f8933e1803d1118e90ad17be0 100644 (file)
+++ b/helpers.h
 */
 
 
-#ifndef MATHHELPERS_H
-#define MATHHELPERS_H
+#ifndef HELPERS_H
+#define HELPERS_H
 
-template <typename T> T average(T* vals, size_t len) {
+#include <QtCore/QString>
+
+template <typename T> static T average(T* vals, size_t len) {
   T sum = 0;
   for (size_t i = 0; i < len; i++)
     sum += vals[i];
   return sum / len;
 }
 
-template <typename T> T clamp(T in, T min, T max) {
+template <typename T> static T clamp(T in, T min, T max) {
   if (in < min)
     return min;
   if (in > max)
     return max;
 }
 
+static QString getFileNameSuffix(const QString& name)
+{
+  int dotIdx = name.lastIndexOf('.');
+
+  if (dotIdx < 0)
+    return "";
+
+  return name.mid(dotIdx+1).toLower();
+}
+
+
 #endif // MATHHELPERS_H
index a8806be39f358080cd0e0402c35cb0d13eba9cf5..8f2a676d5c1c69a74532a6e8e6d956b87354f790 100644 (file)
 */
 
 
+#include "helpers.h"
 #include "imagedrawer.h"
 
 #include <QtCore/QFile>
 #include <QtWidgets/QMessageBox>
+
 #include <QDebug>
 
-ImageDrawer::ImageDrawer(std::shared_ptr<SignalController> controller) :
+ImageDrawer::ImageDrawer(std::shared_ptr<SignalController> controller, QObject* parent) :
+  QObject(parent),
   SignalDrawer(controller),
   m_controller(controller)
 {
@@ -40,6 +43,9 @@ ImageDrawer::ReturnCode ImageDrawer::render(const QString& filename, const QByte
   QImage image;
   QFile file(filename);
 
+  if (m_controller == nullptr)
+    return ReturnCode::E_NO_CONTROLLER;
+
   if (!setDimensions(width, height))
     return ReturnCode::E_INVAL_DIMENSIONS;
   if (!draw(fromX, fromY, toX, toY, layers))
@@ -69,3 +75,23 @@ QList<QByteArray> ImageDrawer::supportedImageFormats()
 {
   return QImageWriter::supportedImageFormats();
 }
+
+int ImageDrawer::guessFormatFromSuffix(const QString& name)
+{
+  QList<QByteArray> formats = ImageDrawer::supportedImageFormats();
+  QString suffix = getFileNameSuffix(name);
+
+  if (suffix.length() == 0)
+    return -1;
+
+  for (int i = 0; i < formats.size(); i++) {
+    const QString f = QString(formats.at(i));
+    if (suffix.compare(f) == 0) {
+      qDebug() << "Changing format to idx" << i << ", type" << f;
+      return i;
+    }
+  }
+
+  qDebug() << "Cannot guess type from suffix";
+  return -1;
+}
index 2b1b2370be3c6b1949b79967a986f694b005a62e..f3f91eddcaf8b8fa3689cffdd80f3e54f8e50d96 100644 (file)
 #ifndef IMAGEDRAWER_H
 #define IMAGEDRAWER_H
 
+#include "gui/exportgraphtoimagedialog.h"
 #include "signaldrawer.h"
 #include <QtGui/QImageWriter>
 
-class ImageDrawer : protected SignalDrawer
+class ImageDrawer : public QObject, protected SignalDrawer
 {
+  Q_OBJECT
 public:
   enum class ReturnCode {
     SUCCESS,
-    E_INVAL_DIMENSIONS,
     E_CANNOT_DRAW,
     E_CANNOT_CREATE_IMAGE,
     E_CANNOT_WRITE_IMAGE,
+    E_INVAL_DIMENSIONS,
+    E_NO_CONTROLLER,
     E_WOULD_OVERWRITE
   };
 
-  explicit ImageDrawer(std::shared_ptr<SignalController> controller);
+  explicit ImageDrawer(std::shared_ptr<SignalController> controller, QObject* parent = nullptr);
   ReturnCode render(const QString& filename, const QByteArray format, const int width, const int height,
                     const double fromX, const double fromY, const double toX, const double toY, const GraphLayers layers);
 
+  static int guessFormatFromSuffix(const QString& name);
   static QList<QByteArray> supportedImageFormats();
 
 private:
   std::shared_ptr<SignalController> m_controller;
   QImageWriter m_imageWriter;
 
+public slots:
+
+signals:
+  void formatChanged(const int idx);
 
 };