]> Devoid-pointer.net GitWeb - anyanka.git/commitdiff
Take advantage of Qt MVC when handling available signals in raw data
authorMichal Malý <madcatxster@devoid-pointer.net>
Fri, 17 Apr 2015 16:29:11 +0000 (18:29 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Fri, 17 Apr 2015 16:29:11 +0000 (18:29 +0200)
export

datamanager.cpp
gui/exportrawdatadialog.cpp
gui/exportrawdatadialog.h
gui/exportrawdatadialog.ui
helpers.cpp
helpers.h

index f19b996cd037bb02cadf2b3fcaeac79db1d07dd0..2f9f69df07adc80fed65597a6a6de67c6c966f27 100644 (file)
@@ -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<std::string> selKeys = m_exportRawDataDialog.selectedSignalKeys();
+    if (dret == QDialog::Accepted) {
       DataFileExporter::ReturnCode exRet;
+      std::vector<std::string> 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);
 
index 1396720a2eaf2c15579f9fa3410345d9b3502723..34618b799b05f70d63870ef9124c8e8f279aa51e 100644 (file)
 
 #include "exportrawdatadialog.h"
 #include "ui_exportrawdatadialog.h"
+#include <QStandardItem>
 #include <QtWidgets/QFileDialog>
 
+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<std::string> ExportRawDataDialog::selectedSignalKeys() const
 {
   std::vector<std::string> 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;
index c3115e347bbeb4ba4905c157e0a7f31b2dd79a76..af08c5dc84cf8b81da9995f11ca8805a7c84b26a 100644 (file)
@@ -24,6 +24,7 @@
 #ifndef EXPORTRAWDATADIALOG_H
 #define EXPORTRAWDATADIALOG_H
 
+#include <QStandardItemModel>
 #include <QtWidgets/QCheckBox>
 #include <QtWidgets/QDialog>
 #include <QtWidgets/QLabel>
@@ -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<std::string> selectedSignalKeys() const;
-  int signalsCount() const { return m_signalCheckboxes.size(); }
 
 private:
   QString m_lastPath;
-  std::vector<QCheckBox*> m_signalCheckboxes;
   QStringList m_supportedFormats;
+  QStandardItemModel m_availableSignals;
   Ui::ExportRawDataDialog* ui;
 
+  static const int SIGNAL_TAG_ROLE;
+
 public slots:
   void onFormatChanged(const int idx);
 
index bd3c8ab00dd44de8f48b7cf59c29ebf6f367130a..27cba6c092970c284e27748b67607028d02ce83a 100644 (file)
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>400</width>
-    <height>300</height>
+    <height>344</height>
    </rect>
   </property>
   <property name="sizePolicy">
@@ -99,7 +99,7 @@
        </widget>
       </item>
       <item>
-       <layout class="QFormLayout" name="form_availSigs"/>
+       <widget class="QListView" name="qlv_availableSignals"/>
       </item>
      </layout>
     </widget>
index 5a39c5de9ce69a05f7402797b1e6e21128437946..8b915ea8c72f65e58392be2e166368c188459218 100644 (file)
@@ -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;
+}
index 20d978a162001e5f6354d5e74418b5f476b72062..21da5ad7bd6e612b2105d60ab72bb2790baf8154 100644 (file)
--- 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 <typename T> static T average(T* vals, size_t len)
   {