]> Devoid-pointer.net GitWeb - anyanka.git/commitdiff
- Loop the dialog until the signal is exported to image or user cancels
authorMichal Malý <madcatxster@prifuk.cz>
Sun, 9 Mar 2014 19:46:39 +0000 (20:46 +0100)
committerMichal Malý <madcatxster@prifuk.cz>
Sun, 9 Mar 2014 19:46:39 +0000 (20:46 +0100)
the action
- Unify behavior of export to image and export to data file

datafileexporter.cpp
datafileexporter.h
datamanager.cpp
datamanager.h
imagedrawer.cpp
imagedrawer.h

index ecf507ddb899001736ff9d47944858068526fd15..b9d01243127d68b9ee5503fe3af30fc4912ddb2c 100644 (file)
@@ -46,23 +46,20 @@ exportData(const WriteList& list, const QString& path, const int formatIdx)
   DataWriterBackend* backend;
 
   if (formatIdx < 0 || formatIdx >= SUPPORTED_FORMATS.length()) {
-    QMessageBox::warning(nullptr, "Data export error", "Invalid format selected");
-    return ReturnCode::E_TRYAGAIN;
+    return ReturnCode::E_INVAL_FORMAT;
   }
 
   if (path.compare("") == 0) {
-    QMessageBox::information(nullptr, "Data export error", "No output file specified.");
-    return ReturnCode::E_TRYAGAIN;
+    return ReturnCode::E_NO_OUTPUT;
   }
   if (dir.exists()) {
-    QMessageBox::information(nullptr, "Data export error", "Selected path points to a directory. Name of output file is required.");
-    return ReturnCode::E_TRYAGAIN;
+    return ReturnCode::E_IS_DIRECTORY;
   }
 
   if (file.exists()) {
     int ret = QMessageBox::question(nullptr, "Data export", "The selected file already exists. Do you wish to overwrite it?");
     if (ret == QMessageBox::No)
-      return ReturnCode::E_TRYAGAIN;
+      return ReturnCode::E_WOULD_OVERWRITE;
     else
       file.remove();
   }
@@ -93,7 +90,6 @@ exportData(const WriteList& list, const QString& path, const int formatIdx)
     return ret;
   }
 
-  //for (int idx = 1; idx < header.size(); idx++) {
   WriteList::const_iterator cit = list.cbegin();
   std::advance(cit, 1);
   for (; cit != list.cend(); cit++) {
index 96b45836ab5ff876805b94790f38dd44e8f590a3..2ac29835c6da215ad903e89e06122d226072de88 100644 (file)
@@ -36,8 +36,11 @@ class DataFileExporter : public QObject
 public:
   enum class ReturnCode {
     SUCCESS,
-    E_TRYAGAIN,
-    E_FAILED
+    E_FAILED,
+    E_INVAL_FORMAT,
+    E_IS_DIRECTORY,
+    E_NO_OUTPUT,
+    E_WOULD_OVERWRITE
   };
   typedef std::unordered_map<std::string, std::pair<QVariantList, std::vector<QVariantList>>> WriteList;
 
index 96cec2aec7586007f528eec07cfedd753efbe113..e0555b43e4f1187ee367d3b7f8477d78ab058994 100644 (file)
@@ -36,6 +36,8 @@ const char DataManager::UNIT_MILLIAU_TEXT[] = {0x6D, 0x41, 0x55, 0x00};
 const char DataManager::UNIT_MILLIVOLTS_TEXT[] = {0x6D, 0x56, 0x00};
 const char DataManager::UNIT_WATTS_TEXT[] = {0x57, 0x00};
 const QStringList DataManager::DATA_DIR_SUFFIX = QStringList() << "*.D";
+const QString DataManager::S_RES_DATAEXPORT("Error while exporting data to file");
+const QString DataManager::S_RES_IMGEXPORT("Error while exporting graph to image");
 
 DataManager::DataManager(QObject* parent) :
   QObject(parent),
@@ -139,8 +141,8 @@ void DataManager::processDataExport(GeneratorFunc genfunc)
   DataFileExporter exporter(this);
   ExportRawDataDialog dlg(DataFileExporter::SUPPORTED_FORMATS);
   DataFileExporter::WriteList writeList;
-  DataFileExporter::ReturnCode ret;
   std::shared_ptr<SingleRunData> sr = m_activeSequence->selectedRun();
+  int ret = 1;
 
   for (const std::string& s : sr->allKeys())
     dlg.addAvailableSignal(QString::fromStdString(s));
@@ -148,17 +150,37 @@ void DataManager::processDataExport(GeneratorFunc genfunc)
   do {
     int dret = dlg.exec();
     if (dret == QDialog::Rejected)
-      return;
+      ret = 0;
     else if (dret == QDialog::Accepted && !dlg.isSignalSelected()) {
-      QMessageBox::information(nullptr, "Data export error", "No signals selected");
-      ret = DataFileExporter::ReturnCode::E_TRYAGAIN;
+      QMessageBox::information(nullptr, S_RES_DATAEXPORT, "No signals selected");
     } else if (dret == QDialog::Accepted && dlg.isSignalSelected()) {
       std::vector<std::string> selKeys = dlg.selectedSignalKeys();
+      DataFileExporter::ReturnCode exRet;
       /* Generate data to export */
       genfunc(writeList, sr, selKeys);
-      ret = exporter.exportData(writeList, dlg.destination(), dlg.format());
+
+      exRet = exporter.exportData(writeList, dlg.destination(), dlg.format());
+      switch (exRet) {
+      case DataFileExporter::ReturnCode::SUCCESS:
+        ret = 0;
+        break;
+      case DataFileExporter::ReturnCode::E_FAILED:
+        QMessageBox::critical(nullptr, S_RES_DATAEXPORT, "Data could not have been exported to file.");
+        break;
+      case DataFileExporter::ReturnCode::E_INVAL_FORMAT:
+        QMessageBox::warning(nullptr, "Data export error", "Invalid format selected");
+        break;
+      case DataFileExporter::ReturnCode::E_IS_DIRECTORY:
+        QMessageBox::information(nullptr, "Data export error", "Selected path points to a directory. Name of output file is required.");
+        break;
+      case DataFileExporter::ReturnCode::E_NO_OUTPUT:
+        QMessageBox::information(nullptr, "Data export error", "No output file specified.");
+        break;
+      default:
+        break;
+      }
     }
-   } while (ret == DataFileExporter::ReturnCode::E_TRYAGAIN);
+   } while (ret == 1);
 }
 
 Signal::Resource DataManager::resourceFromFiletype(AGRE_Filetype filetype)
@@ -380,6 +402,7 @@ void DataManager::onExportGraphToImage()
 {
   ExportGraphToImageDialog dlg(ImageDrawer::supportedImageFormats());
   std::shared_ptr<SingleRunData> sr;
+  int ret = 1;
 
   if (m_activeSequence == nullptr)
     return;
@@ -391,34 +414,47 @@ void DataManager::onExportGraphToImage()
     dlg.addSignal(p.second->fromXAbs(), p.second->fromYAbs(), p.second->toXAbs(), p.second->toYAbs(), p.first);
   }
 
-  if (dlg.exec() == QDialog::Accepted) {
-    const std::string key = dlg.selectedSignal().toStdString();
-    std::shared_ptr<SignalController> ctrl = sr->controllerAt(key);
-    GraphLayers gl = GraphLayers::GRAPH | GraphLayers::SCALE;
-
-    if (ctrl == nullptr) {
-      Logger::log(Logger::Level::CRITICAL, ME_SENDER_STR, "Invalid key " + QString::fromStdString(key) + " passed to image exporter");
-      return;
-    }
-
-    if (dlg.path().length() == 0) {
-      QMessageBox::warning(nullptr, "Error while exporting to image", "Invalid path");
-      return;
-    }
-
-    /*if (data == nullptr) {
-      QMessageBox::warning(nullptr, "Error while exporting to image", "Cannot generate data to draw. Some values are probably invalid.");
-      return;
-    }*/
-
-    if (dlg.includePeaks())
-      gl |= GraphLayers::INTEGRATION;
-
-    ImageDrawer imageDrawer(ctrl);
-    if (!imageDrawer.render(dlg.path() + "." + dlg.imageFormat(), dlg.imageFormat().toLatin1(), dlg.imageWidth(), dlg.imageHeight(), gl))
-      QMessageBox::critical(nullptr, "Error while exporting to image", "Image could not have been written.");
-
-  }
+  do {
+    if (dlg.exec() == QDialog::Accepted) {
+      const std::string key = dlg.selectedSignal().toStdString();
+      std::shared_ptr<SignalController> ctrl = sr->controllerAt(key);
+      GraphLayers gl = GraphLayers::GRAPH | GraphLayers::SCALE;
+      ImageDrawer::ReturnCode drwRet;
+
+      if (ctrl == nullptr) {
+        Logger::log(Logger::Level::CRITICAL, ME_SENDER_STR, "Invalid key " + QString::fromStdString(key) + " passed to image exporter");
+        continue;
+      }
+
+      if (dlg.path().length() == 0) {
+        QMessageBox::warning(nullptr, "Error while exporting to image", "Invalid path");
+        continue;
+      }
+
+      if (dlg.includePeaks())
+        gl |= GraphLayers::INTEGRATION;
+
+      ImageDrawer imageDrawer(ctrl);
+      drwRet = imageDrawer.render(dlg.path() + "." + dlg.imageFormat(), dlg.imageFormat().toLatin1(), dlg.imageWidth(), dlg.imageHeight(), gl);
+      switch (drwRet) {
+      case ImageDrawer::ReturnCode::SUCCESS:
+        ret = 0;
+        break;
+      case ImageDrawer::ReturnCode::E_INVAL_DIMENSIONS:
+        QMessageBox::critical(nullptr, S_RES_IMGEXPORT, "Image width or height is invalid.");
+        break;
+      case ImageDrawer::ReturnCode::E_CANNOT_DRAW:
+        QMessageBox::critical(nullptr, S_RES_IMGEXPORT, "Graph could not have been drawn.");
+        break;
+      case ImageDrawer::ReturnCode::E_CANNOT_WRITE_IMAGE:
+        QMessageBox::critical(nullptr, S_RES_IMGEXPORT, "Cannot write image to disk.");
+        break;
+      default:
+        break;
+      }
+    } else
+      break;
+  } while (ret == 1);
 }
 
 void DataManager::onExportPeaks()
index ff16b0ef0468daea58848adaef1caaecce40a60b..1f549366346eaa22cb9a4aabe96e106bf4578aaa 100644 (file)
@@ -78,6 +78,9 @@ private:
 
   static const QStringList DATA_DIR_SUFFIX;
 
+  static const QString S_RES_DATAEXPORT;
+  static const QString S_RES_IMGEXPORT;
+
 signals:
   void addToDashboard(SignalView* sw);
   void cleanDashboard();
index 430ee77b3036ecd36ff4aed6a8630cc85b587ed2..6771f666ceb9b8823c0e796a8c7174c9c76c5f78 100644 (file)
@@ -1,5 +1,7 @@
 #include "imagedrawer.h"
 
+#include <QtCore/QFile>
+#include <QtWidgets/QMessageBox>
 #include <QDebug>
 
 ImageDrawer::ImageDrawer(std::shared_ptr<SignalController> controller) :
@@ -9,19 +11,34 @@ ImageDrawer::ImageDrawer(std::shared_ptr<SignalController> controller) :
   m_imageWriter.setCompression(0);
 }
 
-bool ImageDrawer::render(const QString& filename, const QByteArray format, const int width, const int height, const GraphLayers layers)
+ImageDrawer::ReturnCode ImageDrawer::render(const QString& filename, const QByteArray format, const int width, const int height, const GraphLayers layers)
 {
   QImage image;
+  QFile file(filename);
 
-  setDimensions(width, height);
+  if (!setDimensions(width, height))
+    return ReturnCode::E_INVAL_DIMENSIONS;
   if (!draw(SignalController::RELATIVE_MIN, SignalController::RELATIVE_MIN, SignalController::RELATIVE_MAX, SignalController::RELATIVE_MAX, layers))
-    return false;
+    return ReturnCode::E_CANNOT_DRAW;
 
   m_imageWriter.setFileName(filename);
   m_imageWriter.setFormat(format);
   image = m_pixmap->toImage();
+  if (image.isNull())
+    return ReturnCode::E_CANNOT_CREATE_IMAGE;
 
-  return m_imageWriter.write(image);
+  if (file.exists()) {
+    int ret = QMessageBox::question(nullptr, "Data export", "The selected file already exists. Do you wish to overwrite it?");
+    if (ret == QMessageBox::No)
+      return ReturnCode::E_WOULD_OVERWRITE;
+    else
+      file.remove();
+  }
+
+  if (!m_imageWriter.write(image))
+    return ReturnCode::E_CANNOT_WRITE_IMAGE;
+
+  return ReturnCode::SUCCESS;
 }
 
 QList<QByteArray> ImageDrawer::supportedImageFormats()
index b27922e829bc1831dd9777d60d8f045739f93bea..12ba35283c2e97b2605e709828eaf0db51fce4a6 100644 (file)
@@ -7,8 +7,17 @@
 class ImageDrawer : protected SignalDrawer
 {
 public:
+  enum class ReturnCode {
+    SUCCESS,
+    E_INVAL_DIMENSIONS,
+    E_CANNOT_DRAW,
+    E_CANNOT_CREATE_IMAGE,
+    E_CANNOT_WRITE_IMAGE,
+    E_WOULD_OVERWRITE
+  };
+
   explicit ImageDrawer(std::shared_ptr<SignalController> controller);
-  bool render(const QString& filename, const QByteArray format, const int width, const int height, const GraphLayers layers);
+  ReturnCode render(const QString& filename, const QByteArray format, const int width, const int height, const GraphLayers layers);
 
   static QList<QByteArray> supportedImageFormats();