From 5a1bd9b9005800cbc70a0ace5e40e744db009d3b Mon Sep 17 00:00:00 2001
From: tstellanova <tstellanova+github@gmail.com>
Date: Fri, 9 Aug 2013 07:15:03 -0700
Subject: [PATCH] fix comm mgr -> data model wiring

---
 src/uas/UASParameterCommsMgr.cc |  10 ++-
 src/ui/QGCParamWidget.cc        | 152 +++++++++++++++++++++-------------------
 src/ui/QGCParamWidget.h         |   1 +
 3 files changed, 86 insertions(+), 77 deletions(-)

diff --git a/src/uas/UASParameterCommsMgr.cc b/src/uas/UASParameterCommsMgr.cc
index 3c9818d..690851c 100644
--- a/src/uas/UASParameterCommsMgr.cc
+++ b/src/uas/UASParameterCommsMgr.cc
@@ -30,7 +30,7 @@ UASParameterCommsMgr::UASParameterCommsMgr(QObject *parent, UASInterface *uas) :
     connect(this, SIGNAL(parameterChanged(int,QString,QVariant)),
             mav, SLOT(setParameter(int,QString,QVariant)));
 
-    // New parameters from UAS
+    // Received parameter updates from UAS
     connect(mav, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)),
             this, SLOT(receivedParameterUpdate(int,int,int,int,QString,QVariant)));
 
@@ -177,7 +177,7 @@ void UASParameterCommsMgr::resendReadWriteRequests()
         QList<int>* missingReadParams = transmissionMissingPackets.value(compId, NULL);
         foreach (int paramId, *missingReadParams) {
             if (requestedReadCount < retransmissionBurstRequestSize) {
-                qDebug() << __FILE__ << __LINE__ << "RETRANSMISSION GUARD REQUESTS RETRANSMISSION OF PARAM #" << paramId << "FROM COMPONENT #" << compId;
+                //qDebug() << __FILE__ << __LINE__ << "RETRANSMISSION GUARD REQUESTS RETRANSMISSION OF PARAM #" << paramId << "FROM COMPONENT #" << compId;
                 emit parameterUpdateRequestedById(compId, paramId);
                 setParameterStatusMsg(tr("Requested retransmission of #%1").arg(paramId+1));
                 requestedReadCount++;
@@ -385,6 +385,10 @@ void UASParameterCommsMgr::setParameterStatusMsg(const QString& msg, ParamCommsS
  */
 void UASParameterCommsMgr::receivedParameterUpdate(int uas, int compId, int paramCount, int paramId, QString paramName, QVariant value)
 {
+    Q_UNUSED(uas); //this object is assigned to one UAS only
+
+    //notify the data model that we have an updated param
+    paramDataModel->handleParameterUpdate(compId,paramName,value);
 
     // Missing packets list has to be instantiated for all components
     if (!transmissionMissingPackets.contains(compId)) {
@@ -402,7 +406,7 @@ void UASParameterCommsMgr::receivedParameterUpdate(int uas, int compId, int para
             transmissionListSizeKnown.insert(compId, true);
 
             qDebug() << "Mark all parameters as missing: " << paramCount;
-            for (int i = 0; i < paramCount; ++i) {
+            for (int i = 1; i < paramCount; ++i) { //TODO check: param Id 0 is  "all parameters" ?
                 if (!compXmitMissing->contains(i)) {
                     compXmitMissing->append(i);
                 }
diff --git a/src/ui/QGCParamWidget.cc b/src/ui/QGCParamWidget.cc
index d5f769b..a141ec4 100644
--- a/src/ui/QGCParamWidget.cc
+++ b/src/ui/QGCParamWidget.cc
@@ -54,6 +54,36 @@ QGCParamWidget::QGCParamWidget(UASInterface* uas, QWidget *parent) :
     componentItems(new QMap<int, QTreeWidgetItem*>())
 {
 
+    layoutWidget();
+
+    // Connect signals/slots
+
+    // Listen for edits to the tree UI
+    connect(tree, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
+            this, SLOT(parameterItemChanged(QTreeWidgetItem*,int)));
+
+    // Listen to updated param signals from the data model
+    connect(paramDataModel, SIGNAL(parameterUpdated(int, QString , QVariant )),
+            this, SLOT(handleParameterUpdate(int,QString,QVariant)));
+
+    // Listen for param list reload finished
+    connect(paramCommsMgr, SIGNAL(parameterListUpToDate()),
+            this, SLOT(handleParameterListUpToDate()));
+
+    // Listen to communications status messages so we can display them
+    connect(paramCommsMgr, SIGNAL(parameterStatusMsgUpdated(QString,int)),
+            this, SLOT(handleParamStatusMsgUpdate(QString , int )));
+
+    // Ensure we're receiving the list of params
+    requestAllParamsUpdate();
+
+}
+
+
+
+
+void QGCParamWidget::layoutWidget()
+{
     // Create tree widget
     tree = new QTreeWidget(this);
     statusLabel = new QLabel();
@@ -77,7 +107,6 @@ QGCParamWidget::QGCParamWidget(UASInterface* uas, QWidget *parent) :
     statusLabel->setText(tr("Click refresh to download parameters"));
     horizontalLayout->addWidget(statusLabel, 1, 0, 1, 3);
 
-
     // BUTTONS
     QPushButton* refreshButton = new QPushButton(tr("Get"));
     refreshButton->setToolTip(tr("Load parameters currently in non-permanent memory of aircraft."));
@@ -130,61 +159,35 @@ QGCParamWidget::QGCParamWidget(UASInterface* uas, QWidget *parent) :
     headerItems.append("Value");
     tree->setHeaderLabels(headerItems);
     tree->setColumnCount(2);
+    tree->setColumnWidth(0,120);
+    tree->setColumnWidth(1,120);
     tree->setExpandsOnDoubleClick(true);
 
-    // Connect signals/slots
-//    connect(this, SIGNAL(parameterChanged(int,QString,QVariant)),
-//            mav, SLOT(setParameter(int,QString,QVariant)));
-    connect(tree, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
-            this, SLOT(parameterItemChanged(QTreeWidgetItem*,int)));
-
-//    // New parameters from UAS
-//    connect(uas, SIGNAL(parameterChanged(int,int,int,int,QString,QVariant)), this, SLOT(receivedParameterUpdate(int,int,int,int,QString,QVariant)));
-
-
-//    connect(&retransmissionTimer, SIGNAL(timeout()), this, SLOT(retransmissionGuardTick()));
-//    connect(this, SIGNAL(requestParameter(int,QString)), uas, SLOT(requestParameter(int,QString)));
-//    connect(this, SIGNAL(requestParameter(int,int)), uas, SLOT(requestParameter(int,int)));
-
-
-    connect(paramDataModel, SIGNAL(parameterUpdated(int, QString , QVariant )),
-            this, SLOT(handleParameterUpdate(int,QString,QVariant)));
-
-    // Listen for param list reload finished
-    connect(paramCommsMgr, SIGNAL(parameterListUpToDate()),
-            this, SLOT(handleParameterListUpToDate()));
-
-    connect(paramCommsMgr, SIGNAL(parameterStatusMsgUpdated(QString,int)),
-            this, SLOT(handleParamStatusMsgUpdate(QString , int )));
-
-    // Get parameters
-    if (uas) {
-        requestAllParamsUpdate();
-    }
+    tree->setVisible(true);
 }
 
 
-
-
-
-
-
 void QGCParamWidget::addComponentItem( int compId, QString compName)
 {
+    QString compLine = QString("%1 (#%2)").arg(compName).arg(compId);
+
+    QString ptrStr.sprintf("%8p", this);
+    qDebug() <<  "QGCParamWidget" << ptrStr << "addComponentItem:" << compLine;
+
     if (componentItems->contains(compId)) {
-        // Update existing
-        componentItems->value(compId)->setData(0, Qt::DisplayRole, QString("%1 (#%2)").arg(compName).arg(compId));
+        // Update existing component item
+        componentItems->value(compId)->setData(0, Qt::DisplayRole, compLine);
         //components->value(component)->setData(1, Qt::DisplayRole, QString::number(component));
         componentItems->value(compId)->setFirstColumnSpanned(true);
     } else {
-        // Add new
-        QStringList list(QString("%1 (#%2)").arg(compName).arg(compId));
-        QTreeWidgetItem* comp = new QTreeWidgetItem(list);
-        comp->setFirstColumnSpanned(true);
-        componentItems->insert(compId, comp);
-        // Create grouping and update maps
+        // Add new component item
+        QStringList list(compLine);
+        QTreeWidgetItem* compItem = new QTreeWidgetItem(list);
+        compItem->setFirstColumnSpanned(true);
+        componentItems->insert(compId, compItem);
+        // Create parameter grouping for this component and update maps
         paramGroups.insert(compId, new QMap<QString, QTreeWidgetItem*>());
-        tree->addTopLevelItem(comp);
+        tree->addTopLevelItem(compItem);
         tree->update();
     }
 
@@ -202,7 +205,10 @@ void QGCParamWidget::handleParameterUpdate(int componentId, const QString& param
 
 void QGCParamWidget::handleParameterListUpToDate()
 {
-    tree->collapseAll();
+//    tree->collapseAll();
+
+    //turn off updates while we refresh the entire list
+    tree->setUpdatesEnabled(false);
 
     //rewrite the component item tree after receiving the full list
     QMap<int, QMap<QString, QVariant>*>::iterator i;
@@ -219,44 +225,38 @@ void QGCParamWidget::handleParameterListUpToDate()
 
     // Expand visual tree
     tree->expandItem(tree->topLevelItem(0));
+    tree->setUpdatesEnabled(true);
+    tree->update();
+
 }
 
 
 void QGCParamWidget::updateParameterDisplay(int componentId, QString parameterName, QVariant value)
 {
+//    qDebug() << "QGCParamWidget::updateParameterDisplay" << parameterName;
 
-//    QString ptrStr;
-//    ptrStr.sprintf("%8p", this);
-//    qDebug() <<  "QGCParamWidget " << ptrStr << " got param" <<  parameterName;
 
     // Reference to item in tree
     QTreeWidgetItem* parameterItem = NULL;
 
-    // Get component
+    // Add component item if necessary
     if (!componentItems->contains(componentId)) {
         QString componentName = tr("Component #%1").arg(componentId);
         addComponentItem(componentId, componentName);
     }
 
-    //TODO this should be bubbling up from the model, not vice-versa, right?
-//    // Replace value in data model
-//    paramDataModel->handleParameterUpdate(componentId,parameterName,value);
-
-
     QString splitToken = "_";
     // Check if auto-grouping can work
-    if (parameterName.contains(splitToken))
-    {
+    if (parameterName.contains(splitToken)) {
         QString parent = parameterName.section(splitToken, 0, 0, QString::SectionSkipEmpty);
         QMap<QString, QTreeWidgetItem*>* compParamGroups = paramGroups.value(componentId);
-        if (!compParamGroups->contains(parent))
-        {
+        if (!compParamGroups->contains(parent)) {
             // Insert group item
             QStringList glist;
             glist.append(parent);
-            QTreeWidgetItem* item = new QTreeWidgetItem(glist);
-            compParamGroups->insert(parent, item);
-            componentItems->value(componentId)->addChild(item);
+            QTreeWidgetItem* groupItem = new QTreeWidgetItem(glist);
+            compParamGroups->insert(parent, groupItem);
+            componentItems->value(componentId)->addChild(groupItem);
         }
 
         // Append child to group
@@ -322,26 +322,30 @@ void QGCParamWidget::updateParameterDisplay(int componentId, QString parameterNa
             componentItems->value(componentId)->addChild(parameterItem);
             parameterItem->setFlags(parameterItem->flags() | Qt::ItemIsEditable);
         }
-        //tree->expandAll();
     }
+
     // Reset background color
     parameterItem->setBackground(0, Qt::NoBrush);
     parameterItem->setBackground(1, Qt::NoBrush);
+
+    parameterItem->setTextColor(0, QGC::colorDarkWhite);
+    parameterItem->setTextColor(1, QGC::colorDarkWhite);
+
     // Add tooltip
     QString paramDesc = paramDataModel->getParamDescription(parameterName);
-    QString tooltipFormat;
-    if (paramDataModel->isParamDefaultKnown(parameterName)) {
-        tooltipFormat = tr("Default: %1, %2");
-        double paramDefValue = paramDataModel->getParamDefault(parameterName);
-        tooltipFormat = tooltipFormat.arg(paramDefValue).arg(paramDesc);
-    }
-    else {
-        tooltipFormat = paramDesc;
+    if (!paramDesc.isEmpty()) {
+        QString tooltipFormat;
+        if (paramDataModel->isParamDefaultKnown(parameterName)) {
+            tooltipFormat = tr("Default: %1, %2");
+            double paramDefValue = paramDataModel->getParamDefault(parameterName);
+            tooltipFormat = tooltipFormat.arg(paramDefValue).arg(paramDesc);
+        }
+        else {
+            tooltipFormat = paramDesc;
+        }
+        parameterItem->setToolTip(0, tooltipFormat);
+        parameterItem->setToolTip(1, tooltipFormat);
     }
-    parameterItem->setToolTip(0, tooltipFormat);
-    parameterItem->setToolTip(1, tooltipFormat);
-
-    //paramDataModel->handleParameterUpdate(componentId,parameterName,value);
 
 }
 
diff --git a/src/ui/QGCParamWidget.h b/src/ui/QGCParamWidget.h
index 5a356b7..8579496 100644
--- a/src/ui/QGCParamWidget.h
+++ b/src/ui/QGCParamWidget.h
@@ -51,6 +51,7 @@ public:
 
 protected:
     virtual void setParameterStatusMsg(const QString& msg);
+    virtual void layoutWidget();
 
 signals:
     /** @brief A parameter was changed in the widget, NOT onboard */