From e2d070bdb85dbbb7460dd23112dd541f0bcb43e8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Fri, 17 Apr 2015 18:29:11 +0200 Subject: [PATCH] Take advantage of Qt MVC when handling available signals in raw data export --- datamanager.cpp | 14 ++++++++----- gui/exportrawdatadialog.cpp | 39 ++++++++++++++++--------------------- gui/exportrawdatadialog.h | 9 +++++---- gui/exportrawdatadialog.ui | 4 ++-- helpers.cpp | 9 +++++++++ helpers.h | 1 + 6 files changed, 43 insertions(+), 33 deletions(-) diff --git a/datamanager.cpp b/datamanager.cpp index f19b996..2f9f69d 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -197,17 +197,21 @@ void DataManager::processDataExport(GeneratorFunc genfunc) c1 = connect(&m_exportRawDataDialog, SIGNAL(fileNameChanged(QString)), &exporter, SLOT(guessFormatFromSuffix(QString))); c2 = connect(&exporter, SIGNAL(formatChanged(int)), &m_exportRawDataDialog, SLOT(onFormatChanged(int))); for (const std::string& s : sr->allKeys()) - m_exportRawDataDialog.addAvailableSignal(QString::fromStdString(s)); + m_exportRawDataDialog.addAvailableSignal(Helpers::signalTagToHumanReadable(s), s); do { int dret = m_exportRawDataDialog.exec(); if (dret == QDialog::Rejected) ret = 0; - else if (dret == QDialog::Accepted && !m_exportRawDataDialog.isSignalSelected()) { - QMessageBox::information(nullptr, S_RES_DATAEXPORT, "No signals selected"); - } else if (dret == QDialog::Accepted && m_exportRawDataDialog.isSignalSelected()) { - std::vector selKeys = m_exportRawDataDialog.selectedSignalKeys(); + if (dret == QDialog::Accepted) { DataFileExporter::ReturnCode exRet; + std::vector selKeys = m_exportRawDataDialog.selectedSignalKeys(); + + if (selKeys.size() < 1) { + QMessageBox::information(nullptr, S_RES_DATAEXPORT, "No signals selected"); + continue; + } + /* Generate data to export */ genfunc(writeList, sr, selKeys); diff --git a/gui/exportrawdatadialog.cpp b/gui/exportrawdatadialog.cpp index 1396720..34618b7 100644 --- a/gui/exportrawdatadialog.cpp +++ b/gui/exportrawdatadialog.cpp @@ -23,8 +23,11 @@ #include "exportrawdatadialog.h" #include "ui_exportrawdatadialog.h" +#include #include +const int ExportRawDataDialog::SIGNAL_TAG_ROLE = Qt::UserRole + 1; + ExportRawDataDialog::ExportRawDataDialog(const QStringList& supportedFormats, QWidget* parent) : QDialog(parent), m_lastPath(""), @@ -34,6 +37,7 @@ ExportRawDataDialog::ExportRawDataDialog(const QStringList& supportedFormats, QW ui->setupUi(this); ui->qcbox_formats->addItems(supportedFormats); + ui->qlv_availableSignals->setModel(&m_availableSignals); connect(ui->qpb_browse, SIGNAL(clicked()), this, SLOT(onBrowseClicked())); connect(ui->qpb_ok, SIGNAL(clicked()), this, SLOT(accept())); @@ -43,11 +47,14 @@ ExportRawDataDialog::ExportRawDataDialog(const QStringList& supportedFormats, QW /* Public functions */ -void ExportRawDataDialog::addAvailableSignal(const QString& name) +void ExportRawDataDialog::addAvailableSignal(const QString& readable_name, const std::string& tag) { - QCheckBox* cbox = new QCheckBox(name, this); - m_signalCheckboxes.push_back(cbox); - ui->form_availSigs->addRow(cbox); + QStandardItem* item = new QStandardItem(); + item->setCheckable(true); + item->setCheckState(Qt::Unchecked); + item->setData(readable_name, Qt::DisplayRole); + item->setData(QString(tag.c_str()), SIGNAL_TAG_ROLE); + m_availableSignals.appendRow(item); } QString ExportRawDataDialog::destination() const @@ -60,32 +67,20 @@ int ExportRawDataDialog::format() const return ui->qcbox_formats->currentIndex(); } -bool ExportRawDataDialog::isSignalSelected() const -{ - for (const QCheckBox* cbox : m_signalCheckboxes) - if (cbox->isChecked()) return true; - - return false; -} - void ExportRawDataDialog::removeAllSignals() { - QLayoutItem* item; - while ((item = ui->form_availSigs->takeAt(0)) != nullptr) { - delete item->widget(); - delete item; - } - m_signalCheckboxes.clear(); + m_availableSignals.clear(); } std::vector ExportRawDataDialog::selectedSignalKeys() const { std::vector kl; - kl.reserve(m_signalCheckboxes.size()); + kl.reserve(m_availableSignals.rowCount()); - for (const QCheckBox* cbox : m_signalCheckboxes) { - if (cbox->isChecked()) - kl.push_back(cbox->text().toStdString()); + for (int idx = 0; idx < m_availableSignals.rowCount(); idx++) { + const QStandardItem* item = m_availableSignals.item(idx); + if (item->checkState() == Qt::Checked) + kl.push_back(item->data(SIGNAL_TAG_ROLE).toString().toStdString()); } return kl; diff --git a/gui/exportrawdatadialog.h b/gui/exportrawdatadialog.h index c3115e3..af08c5d 100644 --- a/gui/exportrawdatadialog.h +++ b/gui/exportrawdatadialog.h @@ -24,6 +24,7 @@ #ifndef EXPORTRAWDATADIALOG_H #define EXPORTRAWDATADIALOG_H +#include #include #include #include @@ -39,20 +40,20 @@ class ExportRawDataDialog : public QDialog public: explicit ExportRawDataDialog(const QStringList& supportedFormats, QWidget* parent = nullptr); ~ExportRawDataDialog(); - void addAvailableSignal(const QString& name); + void addAvailableSignal(const QString& readable_name, const std::string& tag); QString destination() const; int format() const; - bool isSignalSelected() const; void removeAllSignals(); std::vector selectedSignalKeys() const; - int signalsCount() const { return m_signalCheckboxes.size(); } private: QString m_lastPath; - std::vector m_signalCheckboxes; QStringList m_supportedFormats; + QStandardItemModel m_availableSignals; Ui::ExportRawDataDialog* ui; + static const int SIGNAL_TAG_ROLE; + public slots: void onFormatChanged(const int idx); diff --git a/gui/exportrawdatadialog.ui b/gui/exportrawdatadialog.ui index bd3c8ab..27cba6c 100644 --- a/gui/exportrawdatadialog.ui +++ b/gui/exportrawdatadialog.ui @@ -7,7 +7,7 @@ 0 0 400 - 300 + 344 @@ -99,7 +99,7 @@ - + diff --git a/helpers.cpp b/helpers.cpp index 5a39c5d..8b915ea 100644 --- a/helpers.cpp +++ b/helpers.cpp @@ -60,3 +60,12 @@ QString Helpers::getFileNameSuffix(const QString& name) return name.mid(dotIdx+1).toLower(); } + +QString Helpers::signalTagToHumanReadable(const std::string& tag) +{ + QString hr = QString::fromStdString(tag); + + hr.replace("_", " "); + + return hr; +} diff --git a/helpers.h b/helpers.h index 20d978a..21da5ad 100644 --- a/helpers.h +++ b/helpers.h @@ -36,6 +36,7 @@ public: static int dpiY(); static double firstTick(const double from, const double step); static QString getFileNameSuffix(const QString& name); + static QString signalTagToHumanReadable(const std::string& tag); template static T average(T* vals, size_t len) { -- 2.43.5