]> Devoid-pointer.net GitWeb - anyanka.git/commitdiff
Take advantage of Qt MVC in supported formats combobox
authorMichal Malý <madcatxster@devoid-pointer.net>
Fri, 17 Apr 2015 20:12:21 +0000 (22:12 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Fri, 17 Apr 2015 20:12:21 +0000 (22:12 +0200)
datafileexporter.cpp
datafileexporter.h
datamanager.cpp
gui/exportrawdatadialog.cpp
gui/exportrawdatadialog.h

index 27eb5a3b72cb77ac73f07d3f415737637a27818c..1e12c487a224bd12ca927ab949e3622b0a7fc3e5 100644 (file)
 #include <QtWidgets/QMessageBox>
 
 
-const std::vector<DataFileExporter::OutputFormat> DataFileExporter::SUPPORTED_FORMATS({DataFileExporter::OutputFormat("Comma separated values", "csv")});
+const std::unordered_map<std::string, QString> DataFileExporter::SUPPORTED_FORMATS({{"csv", "Comma separated values"}});
 
 /* Static methods */
-QStringList DataFileExporter::supportedFormats()
+const std::unordered_map<std::string, QString>& DataFileExporter::supportedFormats()
 {
-  QStringList list;
-
-  for (const OutputFormat& of : SUPPORTED_FORMATS) {
-    list << of.description;
-  }
-
-  return list;
+  return SUPPORTED_FORMATS;
 }
 
 DataFileExporter::DataFileExporter(QObject* parent) :
@@ -51,7 +45,7 @@ DataFileExporter::DataFileExporter(QObject* parent) :
 /* Public methods */
 
 DataFileExporter::ReturnCode DataFileExporter::
-exportData(const WriteList& list, const QString& path, const int formatIdx)
+exportData(const WriteList& list, const QString& path, const std::string& formatTag)
 {
   QDir dir(path);
   QFile file(path);
@@ -60,10 +54,6 @@ exportData(const WriteList& list, const QString& path, const int formatIdx)
   DataFileExporter::ReturnCode ret;
   DataWriterBackend* backend;
 
-  if (formatIdx < 0 || formatIdx >= SUPPORTED_FORMATS.size()) {
-    return ReturnCode::E_INVAL_FORMAT;
-  }
-
   if (path.compare("") == 0) {
     return ReturnCode::E_NO_OUTPUT;
   }
@@ -79,7 +69,7 @@ exportData(const WriteList& list, const QString& path, const int formatIdx)
       file.remove();
   }
 
-  format = formatIdxToFormat(formatIdx);
+  format = formatTagToFormat(formatTag);
   switch (format) {
     case OutputFormats::CSV:
       backend = new CSVDataWriterBackend(this);
@@ -113,14 +103,12 @@ exportData(const WriteList& list, const QString& path, const int formatIdx)
 
 /* Private methods */
 
-DataFileExporter::OutputFormats DataFileExporter::formatIdxToFormat(const int idx)
+DataFileExporter::OutputFormats DataFileExporter::formatTagToFormat(const std::string& tag)
 {
-  switch (idx) {
-    case 0:
-      return OutputFormats::CSV;
-    default:
-      return OutputFormats::INVALID;
-  }
+  if (tag.compare("csv") == 0)
+    return OutputFormats::CSV;
+
+  return OutputFormats::INVALID;
 }
 
 DataFileExporter::ReturnCode DataFileExporter::writeToFile(QFile& file, const DataWriterBackend* backend, const std::pair<QVariantList, std::vector<QVariantList>>& data)
@@ -150,14 +138,14 @@ DataFileExporter::ReturnCode DataFileExporter::writeToFile(QFile& file, const Da
 
 void DataFileExporter::guessFormatFromSuffix(const QString& name)
 {
-  QString suffix = Helpers::getFileNameSuffix(name);
+  std::string suffix = Helpers::getFileNameSuffix(name).toStdString();
 
   if (suffix.length() == 0)
     return;
 
-  for (int idx = 0; idx < SUPPORTED_FORMATS.size(); idx++) {
-    if (suffix.compare(SUPPORTED_FORMATS[idx].suffix) == 0) {
-      emit formatChanged(idx);
+  for (const auto& format : SUPPORTED_FORMATS) {
+    if (suffix.compare(format.first) == 0) {
+      emit formatChanged(QString(format.first.c_str()));
       return;
     }
   }
index 447eb256fd8fd33b5b88f3567e959a6bfc3dadaf..5804ca3309e5b278624f22b00198cbbfa41601f6 100644 (file)
@@ -46,30 +46,22 @@ public:
   typedef std::unordered_map<std::string, std::pair<QVariantList, std::vector<QVariantList>>> WriteList;
 
   explicit DataFileExporter(QObject* parent = nullptr);
-  ReturnCode exportData(const WriteList& header, const QString& path, const int formatIdx);
+  ReturnCode exportData(const WriteList& header, const QString& path, const std::string& formatTag);
 
-  static QStringList supportedFormats();
+  static const std::unordered_map<std::string, QString>& supportedFormats();
 private:
   enum class OutputFormats {
     CSV,
     INVALID
   };
-  struct OutputFormat {
-    OutputFormat(const QString& description, const QString& suffix) :
-      description(description), suffix(suffix) {}
 
-    const QString description;
-    const QString suffix;
-  };
-
-  OutputFormats formatIdxToFormat(const int idx);
-  QString toCSVLine(double time, double value);
+  OutputFormats formatTagToFormat(const std::string& tag);
   ReturnCode writeToFile(QFile& file, const DataWriterBackend* writer, const std::pair<QVariantList, std::vector<QVariantList>>& data);
 
-  static const std::vector<OutputFormat> SUPPORTED_FORMATS;
+  static const std::unordered_map<std::string, QString> SUPPORTED_FORMATS;
 
 signals:
-  void formatChanged(const int idx);
+  void formatChanged(const QString& suffix);
 
 public slots:
   void guessFormatFromSuffix(const QString& name);
index 8757f13cc3fdee7ebf3b009d78a4f5f48ffe5dd8..a6df329168ca6f42717233da7110574b7be44dcc 100644 (file)
@@ -44,9 +44,12 @@ const std::string DataManager::S_SEQ_KEY_SINGLE_RUNS = "Single runs";
 DataManager::DataManager(QObject* parent) :
   QObject(parent),
   m_prevSequenceKey(""),
-  m_exportRawDataDialog(DataFileExporter::supportedFormats()),
+  m_exportRawDataDialog(),
   m_graphToImageExportDialog(ImageDrawer::supportedImageFormats())
 {
+  for (auto it = DataFileExporter::supportedFormats().cbegin(); it != DataFileExporter::supportedFormats().cend(); it++)
+    m_exportRawDataDialog.addSupportedFormat(it->second, it->first);
+
   m_sequences[S_SEQ_KEY_SINGLE_RUNS] = std::shared_ptr<Sequence>(new Sequence());
   m_seqSelModel.setSequences(allSequenceKeys());
   m_sigNameCodec = QTextCodec::codecForName("UTF-8");
@@ -195,7 +198,7 @@ void DataManager::processDataExport(GeneratorFunc genfunc)
 
   m_exportRawDataDialog.removeAllSignals();
   c1 = connect(&m_exportRawDataDialog, SIGNAL(fileNameChanged(QString)), &exporter, SLOT(guessFormatFromSuffix(QString)));
-  c2 = connect(&exporter, SIGNAL(formatChanged(int)), &m_exportRawDataDialog, SLOT(onFormatChanged(int)));
+  c2 = connect(&exporter, SIGNAL(formatChanged(const QString&)), &m_exportRawDataDialog, SLOT(onFormatChanged(const QString&)));
   for (const auto& sm : sr->allSignals())
     m_exportRawDataDialog.addAvailableSignal(sm.second->readableName(), sm.second->rawDescriptiveName());
 
index 983eda51a8b3ef0c6a0e6a78ec0f55926a7e99d7..303fd54c07845040aff65800210ca83e99f7c139 100644 (file)
 #include <QStandardItem>
 #include <QtWidgets/QFileDialog>
 
+const int ExportRawDataDialog::FORMAT_TAG_ROLE = Qt::UserRole + 1;
 const int ExportRawDataDialog::SIGNAL_TAG_ROLE = Qt::UserRole + 1;
 
-ExportRawDataDialog::ExportRawDataDialog(const QStringList& supportedFormats, QWidget* parent) :
+ExportRawDataDialog::ExportRawDataDialog(QWidget* parent) :
   QDialog(parent),
   m_lastPath(""),
-  m_supportedFormats(supportedFormats),
   ui(new Ui::ExportRawDataDialog)
 {
   ui->setupUi(this);
 
-  ui->qcbox_formats->addItems(supportedFormats);
+  ui->qcbox_formats->setModel(&m_supportedFormats);
   ui->qlv_availableSignals->setModel(&m_availableSignals);
   ui->qcbox_formats->setSizeAdjustPolicy(QComboBox::AdjustToContents);
   ui->qlv_availableSignals->setSizeAdjustPolicy(QListView::AdjustToContents);
@@ -59,14 +59,21 @@ void ExportRawDataDialog::addAvailableSignal(const QString& readable_name, const
   m_availableSignals.appendRow(item);
 }
 
+void ExportRawDataDialog::addSupportedFormat(const QString& format_desc, const std::string& tag)
+{
+  QStandardItem* item = new QStandardItem(format_desc);
+  item->setData(QString(tag.c_str()), FORMAT_TAG_ROLE);
+  m_supportedFormats.appendRow(item);
+}
+
 QString ExportRawDataDialog::destination() const
 {
   return ui->qle_destPath->text();
 }
 
-int ExportRawDataDialog::format() const
+std::string ExportRawDataDialog::format() const
 {
-  return ui->qcbox_formats->currentIndex();
+  return ui->qcbox_formats->currentData(FORMAT_TAG_ROLE).toString().toStdString();
 }
 
 void ExportRawDataDialog::removeAllSignals()
@@ -89,9 +96,13 @@ std::vector<std::string> ExportRawDataDialog::selectedSignalKeys() const
 }
 
 /* Public slots */
-void ExportRawDataDialog::onFormatChanged(const int idx)
+void ExportRawDataDialog::onFormatChanged(const QString& suffix)
 {
-  ui->qcbox_formats->setCurrentIndex(idx);
+  for (int idx = 0; idx < m_supportedFormats.rowCount(); idx++) {
+    QString s = m_supportedFormats.item(idx)->data(FORMAT_TAG_ROLE).toString();
+    if (s.compare(suffix) == 0)
+      ui->qcbox_formats->setCurrentIndex(idx);
+  }
 }
 
 /* Private slots */
index af08c5dc84cf8b81da9995f11ca8805a7c84b26a..ed9dba440d1216a9b4d7302c9d9caa9a3554703a 100644 (file)
@@ -38,24 +38,26 @@ class ExportRawDataDialog : public QDialog
   Q_OBJECT
 
 public:
-  explicit ExportRawDataDialog(const QStringList& supportedFormats, QWidget* parent = nullptr);
+  explicit ExportRawDataDialog(QWidget* parent = nullptr);
   ~ExportRawDataDialog();
   void addAvailableSignal(const QString& readable_name, const std::string& tag);
+  void addSupportedFormat(const QString& format_desc, const std::string& tag);
   QString destination() const;
-  int format() const;
+  std::string format() const;
   void removeAllSignals();
   std::vector<std::string> selectedSignalKeys() const;
 
 private:
   QString m_lastPath;
-  QStringList m_supportedFormats;
+  QStandardItemModel m_supportedFormats;
   QStandardItemModel m_availableSignals;
   Ui::ExportRawDataDialog* ui;
 
+  static const int FORMAT_TAG_ROLE;
   static const int SIGNAL_TAG_ROLE;
 
 public slots:
-  void onFormatChanged(const int idx);
+  void onFormatChanged(const QString& suffix);
 
 private slots:
   void onBrowseClicked();