From 04ea5f24fa516e44caa7c0a53d5355ad2371fb07 Mon Sep 17 00:00:00 2001 From: Mike LaBranche Date: Tue, 19 Jun 2012 10:28:42 -0700 Subject: [PATCH] MixerCurveWidget: first pass refactoring for performance; bugfix for negative values --- .../uavobjectwidgetutils/mixercurvewidget.cpp | 145 ++++++++++++------ .../uavobjectwidgetutils/mixercurvewidget.h | 15 +- 2 files changed, 110 insertions(+), 50 deletions(-) diff --git a/ground/openpilotgcs/src/plugins/uavobjectwidgetutils/mixercurvewidget.cpp b/ground/openpilotgcs/src/plugins/uavobjectwidgetutils/mixercurvewidget.cpp index f21d782f6..f97c9e296 100644 --- a/ground/openpilotgcs/src/plugins/uavobjectwidgetutils/mixercurvewidget.cpp +++ b/ground/openpilotgcs/src/plugins/uavobjectwidgetutils/mixercurvewidget.cpp @@ -32,8 +32,6 @@ #include #include - - /* * Initialize the widget */ @@ -74,11 +72,48 @@ MixerCurveWidget::MixerCurveWidget(QWidget *parent) : QGraphicsView(parent) scene->setSceneRect(plot->boundingRect()); setScene(scene); + initNodes(MixerCurveWidget::NODE_NUMELEM); } MixerCurveWidget::~MixerCurveWidget() { + for (int i=0; i= 0 && index < nodePool.count()) + { + node = nodePool.at(index); + } + else { + node = new Node(this); + nodePool.append(node); + } + return node; +} + +Edge* MixerCurveWidget::getEdge(int index, Node* sourceNode, Node* destNode) +{ + Edge* edge; + + if (index >= 0 && index < edgePool.count()) + { + edge = edgePool.at(index); + edge->setSourceNode(sourceNode); + edge->setDestNode(destNode); + } + else { + edge = new Edge(sourceNode,destNode); + edgePool.append(edge); + } + return edge; } /** @@ -89,51 +124,50 @@ MixerCurveWidget::~MixerCurveWidget() */ void MixerCurveWidget::initCurve(QList points) { - if (points.length() < 2) return; // We need at least 2 points on a curve! + if (nodeList.count() != points.count()) + initNodes(points.count()); + + // finally, set node positions + setCurve(points); +} + +void MixerCurveWidget::initNodes(int numPoints) +{ // First of all, reset the list // TODO: one edge might not get deleted properly, small mem leak maybe... + + if (nodeList.count()) { foreach (Node *node, nodeList ) { QList edges = node->edges(); foreach(Edge *edge, edges) { if (scene()->items().contains(edge)) scene()->removeItem(edge); - else - delete edge; } - scene()->removeItem(node); - delete node; + scene()->removeItem(node); + } + + nodeList.clear(); } - nodeList.clear(); // Create the nodes - qreal w = plot->boundingRect().width()/(points.length()-1); - qreal h = plot->boundingRect().height(); - for (int i=0; iaddItem(node); + + for (int i=0; icurveMax) - val=curveMax; - if (valsetPos(w*i,h-val*h); - node->verticalMove(true); + scene()->addItem(node); } // ... and link them together: - for (int i=0; i<(points.length()-1); i++) { - scene()->addItem(new Edge(nodeList.at(i),nodeList.at(i+1))); + for (int i=0; i<(numPoints-1); i++) { + scene()->addItem(getEdge(i, nodeList.at(i),nodeList.at(i+1))); } - } - /** Returns the current curve settings */ @@ -142,7 +176,8 @@ QList MixerCurveWidget::getCurve() { qreal h = plot->boundingRect().height(); foreach(Node *node, nodeList) { - list.append(((curveMax-curveMin)*(h-node->pos().y())/h)+curveMin); + double val = ((curveMax-curveMin)*(h-node->pos().y())/h)+curveMin; + list.append(val); } return list; @@ -150,11 +185,11 @@ QList MixerCurveWidget::getCurve() { /** Sets a linear graph */ -void MixerCurveWidget::initLinearCurve(quint32 numPoints, double maxValue) +void MixerCurveWidget::initLinearCurve(quint32 numPoints, double maxValue, double minValue) { QList points; for (double i=0; i points) { - if (nodeList.length()<1) - { - initCurve(points); - } - else - { - qreal w = plot->boundingRect().width()/(points.length()-1); - qreal h = plot->boundingRect().height(); - for (int i=0; icurveMax) - val=curveMax; - if (valsetPos(w*i,h-val*h); - } + curveUpdating = true; + + if (nodeList.count() != points.count()) + initNodes(points.count()); + + double min = curveMin + 10; + double max = curveMax + 10; + + qreal w = plot->boundingRect().width()/(points.count()-1); + qreal h = plot->boundingRect().height(); + for (int i=0; i curveMax) + val = curveMax; + + val += 10; + val -= min; + val /= (max - min); + + nodeList.at(i)->setPos(w*i, h - (val*h)); + nodeList.at(i)->verticalMove(true); } + + curveUpdating = false; + + emit curveUpdated(points, (double)0); } @@ -205,8 +250,10 @@ void MixerCurveWidget::resizeEvent(QResizeEvent* event) void MixerCurveWidget::itemMoved(double itemValue) { - QList list = getCurve(); - emit curveUpdated(list, itemValue); + if (!curveUpdating) { + QList list = getCurve(); + emit curveUpdated(list, itemValue); + } } void MixerCurveWidget::setMin(double value) diff --git a/ground/openpilotgcs/src/plugins/uavobjectwidgetutils/mixercurvewidget.h b/ground/openpilotgcs/src/plugins/uavobjectwidgetutils/mixercurvewidget.h index 9a14123a7..539755d2c 100644 --- a/ground/openpilotgcs/src/plugins/uavobjectwidgetutils/mixercurvewidget.h +++ b/ground/openpilotgcs/src/plugins/uavobjectwidgetutils/mixercurvewidget.h @@ -31,6 +31,7 @@ #include #include #include +#include #include "mixercurvepoint.h" #include "mixercurveline.h" #include "uavobjectwidgetutils_global.h" @@ -45,12 +46,14 @@ public: void itemMoved(double itemValue); // Callback when a point is moved, to be updated void initCurve (QList points); QList getCurve(); - void initLinearCurve(quint32 numPoints, double maxValue); + void initLinearCurve(quint32 numPoints, double maxValue = 1, double minValue = 0); void setCurve(QList); void setMin(double value); void setMax(double value); void setRange(double min, double max); + static const int NODE_NUMELEM = 5; + signals: void curveUpdated(QList, double ); @@ -58,9 +61,19 @@ private slots: private: QGraphicsSvgItem *plot; + + QList nodePool; + QList edgePool; QList nodeList; + QList points; + double curveMin; double curveMax; + bool curveUpdating; + + void initNodes(int numPoints); + Node* getNode(int index); + Edge* getEdge(int index, Node* sourceNode, Node* destNode); protected: void showEvent(QShowEvent *event);