From 7db4981c1fa3678ecd166c0a68da35204037f910 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Fri, 17 Apr 2015 22:12:21 +0200 Subject: [PATCH] Take advantage of Qt MVC in supported formats combobox --- datafileexporter.cpp | 40 +++++++++++++------------------------ datafileexporter.h | 18 +++++------------ datamanager.cpp | 7 +++++-- gui/exportrawdatadialog.cpp | 25 ++++++++++++++++------- gui/exportrawdatadialog.h | 10 ++++++---- 5 files changed, 48 insertions(+), 52 deletions(-) diff --git a/datafileexporter.cpp b/datafileexporter.cpp index 27eb5a3..1e12c48 100644 --- a/datafileexporter.cpp +++ b/datafileexporter.cpp @@ -29,18 +29,12 @@ #include -const std::vector DataFileExporter::SUPPORTED_FORMATS({DataFileExporter::OutputFormat("Comma separated values", "csv")}); +const std::unordered_map DataFileExporter::SUPPORTED_FORMATS({{"csv", "Comma separated values"}}); /* Static methods */ -QStringList DataFileExporter::supportedFormats() +const std::unordered_map& 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>& 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; } } diff --git a/datafileexporter.h b/datafileexporter.h index 447eb25..5804ca3 100644 --- a/datafileexporter.h +++ b/datafileexporter.h @@ -46,30 +46,22 @@ public: typedef std::unordered_map>> 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& 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>& data); - static const std::vector SUPPORTED_FORMATS; + static const std::unordered_map SUPPORTED_FORMATS; signals: - void formatChanged(const int idx); + void formatChanged(const QString& suffix); public slots: void guessFormatFromSuffix(const QString& name); diff --git a/datamanager.cpp b/datamanager.cpp index 8757f13..a6df329 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -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(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()); diff --git a/gui/exportrawdatadialog.cpp b/gui/exportrawdatadialog.cpp index 983eda5..303fd54 100644 --- a/gui/exportrawdatadialog.cpp +++ b/gui/exportrawdatadialog.cpp @@ -26,17 +26,17 @@ #include #include +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 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 */ diff --git a/gui/exportrawdatadialog.h b/gui/exportrawdatadialog.h index af08c5d..ed9dba4 100644 --- a/gui/exportrawdatadialog.h +++ b/gui/exportrawdatadialog.h @@ -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 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(); -- 2.43.5