From 7ff74cce04199ee030e76d0856337f9d9d6fc799 Mon Sep 17 00:00:00 2001
From: Ake Hedman <akhe@grodansparadis.com>
Date: Tue, 6 Feb 2024 12:28:24 +0100
Subject: [PATCH] Added edit/add/dup/delete DM action

---
 src/cdlgmdfdm.cpp       | 98 ++++++++++++++++++++++++++++++++++++++---
 src/cdlgmdfdm.ui        |  6 ++-
 src/cdlgmdfdmaction.cpp |  7 +--
 src/cdlgmdfdmaction.ui  |  6 ++-
 4 files changed, 103 insertions(+), 14 deletions(-)

diff --git a/src/cdlgmdfdm.cpp b/src/cdlgmdfdm.cpp
index 0a58db1f..083e0108 100644
--- a/src/cdlgmdfdm.cpp
+++ b/src/cdlgmdfdm.cpp
@@ -125,7 +125,7 @@ CDlgMdfDM::initDialogData(CMDF* pmdf, CMDF_DecisionMatrix* pdm, int index)
   connect(ui->btnDelAction,
           SIGNAL(clicked()),
           this,
-          SLOT(delAction()));
+          SLOT(deleteAction()));
 
   setLevel(pmdf->getLevel());
   ui->comboLevel->setEnabled(false);
@@ -138,14 +138,12 @@ CDlgMdfDM::initDialogData(CMDF* pmdf, CMDF_DecisionMatrix* pdm, int index)
     setSize(8);
     ui->spinRowSize->setEnabled(false);
   }
-  else {  
+  else {
     setPage(0);
     ui->spinStartPage->setEnabled(false);
     setSize(pdm->getRowSize());
   }
 
-  
-
   // Render available actions
   renderActions();
 
@@ -301,8 +299,8 @@ CDlgMdfDM::editAction(void)
     int idx = ui->listActions->currentRow();
 
     QListWidgetItem* pitem = ui->listActions->currentItem();
-    printf("data=%d\n",pitem->data(QListWidgetItem::UserType).toUInt());
-    CMDF_Action* paction   = m_pdm->getAction(pitem->data(QListWidgetItem::UserType).toUInt());
+    printf("data=%d\n", pitem->data(QListWidgetItem::UserType).toUInt());
+    CMDF_Action* paction = m_pdm->getAction(pitem->data(QListWidgetItem::UserType).toUInt());
 
     CDlgMdfDmAction dlg(this);
     dlg.initDialogData(m_pmdf, paction);
@@ -324,6 +322,33 @@ CDlgMdfDM::editAction(void)
 void
 CDlgMdfDM::addAction(void)
 {
+  bool ok;
+
+  // Save the selected row
+  int idx = ui->listActions->currentRow();
+
+  QListWidgetItem* pitem = ui->listActions->currentItem();
+  CMDF_Action* paction = new CMDF_Action; // = m_pdm->getAction(pitem->data(QListWidgetItem::UserType).toUInt());
+  if (nullptr == paction) {
+    return;
+  }
+
+
+  CDlgMdfDmAction dlg(this);
+adddlg:  
+
+  dlg.initDialogData(m_pmdf, paction);
+  if (QDialog::Accepted == dlg.exec()) {
+    if (!m_pdm->addAction(paction)) {
+      QMessageBox::warning(this, tr("MDF add new action"), tr("Action with code %1 is already define. Must be unique.").arg(paction->getCode()));      
+    }
+    ui->listActions->clear();
+    renderActions();
+    ui->listActions->setCurrentRow(idx);
+  }
+  else {
+    delete paction;
+  }
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -333,6 +358,44 @@ CDlgMdfDM::addAction(void)
 void
 CDlgMdfDM::dupAction(void)
 {
+  bool ok;
+
+  // Save the selected row
+  int idx = ui->listActions->currentRow();
+
+  QListWidgetItem* pitem = ui->listActions->currentItem();
+  CMDF_Action* paction = m_pdm->getAction(pitem->data(QListWidgetItem::UserType).toUInt());
+  if (nullptr == paction) {
+    return;
+  }
+
+  CMDF_Action* pactionnew = new (CMDF_Action);
+  if (nullptr == pactionnew) {
+    return;
+  }
+
+  pactionnew->setCode(paction->getCode());
+  pactionnew->setName(paction->getName());
+
+adddlg:
+  CDlgMdfDmAction dlg(this);
+  dlg.initDialogData(m_pmdf, pactionnew);
+  // If DM is level I only offset 0 is allowd
+  if (VSCP_LEVEL1 == m_pmdf->getLevel()) {
+    //dlg.setOffsetReadOnly();
+  }
+  if (QDialog::Accepted == dlg.exec()) {
+    if (!m_pdm->addAction(pactionnew)) {
+      QMessageBox::warning(this, tr("MDF add new action"), tr("Action with code %1 is already define. Must be unique.").arg(paction->getCode()));
+      goto adddlg;
+    }
+    ui->listActions->clear();
+    renderActions();
+    ui->listActions->setCurrentRow(idx);
+  }
+  else {
+    delete paction;
+  }
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -342,6 +405,29 @@ CDlgMdfDM::dupAction(void)
 void
 CDlgMdfDM::deleteAction(void)
 {
+  bool ok;
+
+  // Save the selected row
+  int idx = ui->listActions->currentRow();
+
+  QListWidgetItem* pitem = ui->listActions->currentItem();
+  CMDF_Action* paction = m_pdm->getAction(pitem->data(QListWidgetItem::UserType).toUInt());
+  if (nullptr == paction) {
+    return;
+  }
+
+  if (QMessageBox::No == QMessageBox::question(this, 
+                          tr("MDF delete action"), 
+                          tr("Delete action with code %1.").arg(paction->getCode()))) {
+    return;
+  }
+
+  if (!m_pdm->deleteAction(paction)) {
+    QMessageBox::warning(this, tr("MDF add new action"), tr("Failed to remove action with code %1.").arg(paction->getCode()));      
+  }
+
+  renderActions();
+  ui->listActions->setCurrentRow(idx);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/cdlgmdfdm.ui b/src/cdlgmdfdm.ui
index f8f588c6..12723b6d 100644
--- a/src/cdlgmdfdm.ui
+++ b/src/cdlgmdfdm.ui
@@ -131,7 +131,11 @@
     <item row="5" column="1">
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
-       <widget class="QListWidget" name="listActions"/>
+       <widget class="QListWidget" name="listActions">
+        <property name="sortingEnabled">
+         <bool>true</bool>
+        </property>
+       </widget>
       </item>
       <item>
        <layout class="QVBoxLayout" name="verticalLayout">
diff --git a/src/cdlgmdfdmaction.cpp b/src/cdlgmdfdmaction.cpp
index 13b31a17..f229119a 100644
--- a/src/cdlgmdfdmaction.cpp
+++ b/src/cdlgmdfdmaction.cpp
@@ -290,17 +290,13 @@ CDlgMdfDmAction::addActionParam(void)
   // Save the selected row
   int idx = ui->listActionParams->currentRow();
 
-  // QListWidgetItem* pitem = ui->listActionParams->currentItem();
-  // printf("data=%d\n", pitem->data(QListWidgetItem::UserType).toUInt());
-  // CMDF_ActionParameter* pactionparam = m_paction->getActionParam(pitem->data(QListWidgetItem::UserType).toUInt());
-
   CMDF_ActionParameter* pactionparam = new (CMDF_ActionParameter);
   if (nullptr == pactionparam) {
     return;
   }
 
-adddlg:
   CDlgMdfDmActionParam dlg(this);
+adddlg:  
   dlg.initDialogData(m_pmdf, pactionparam);
   // If DM is level I only offset 0 is allowd
   if (VSCP_LEVEL1 == m_pmdf->getLevel()) {
@@ -333,7 +329,6 @@ CDlgMdfDmAction::dupActionParam(void)
   int idx = ui->listActionParams->currentRow();
 
   QListWidgetItem* pitem = ui->listActionParams->currentItem();
-  // printf("data=%d\n", pitem->data(QListWidgetItem::UserType).toUInt());
   CMDF_ActionParameter* pactionparam = m_paction->getActionParam(pitem->data(QListWidgetItem::UserType).toUInt());
   if (nullptr == pactionparam) {
     return;
diff --git a/src/cdlgmdfdmaction.ui b/src/cdlgmdfdmaction.ui
index b8d8e805..7a0a3215 100644
--- a/src/cdlgmdfdmaction.ui
+++ b/src/cdlgmdfdmaction.ui
@@ -83,7 +83,11 @@
     <item row="2" column="1">
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
-       <widget class="QListWidget" name="listActionParams"/>
+       <widget class="QListWidget" name="listActionParams">
+        <property name="sortingEnabled">
+         <bool>true</bool>
+        </property>
+       </widget>
       </item>
       <item>
        <layout class="QVBoxLayout" name="verticalLayout">