From d2b14947df9f7d9cd284c201083da75c7d1f4a8e Mon Sep 17 00:00:00 2001 From: elecpower Date: Sun, 3 Jun 2018 17:49:32 +1000 Subject: [PATCH 1/3] Add point to custom curve using mouse click --- companion/src/modeledit/curves.cpp | 59 ++++++++++++++++++++++++++++-- companion/src/modeledit/curves.h | 19 +++++++++- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/companion/src/modeledit/curves.cpp b/companion/src/modeledit/curves.cpp index 78bb9719928..e85e957d3ca 100644 --- a/companion/src/modeledit/curves.cpp +++ b/companion/src/modeledit/curves.cpp @@ -122,8 +122,10 @@ Curves::Curves(QWidget * parent, ModelData & model, GeneralSettings & generalSet ui->curveNameLabel->hide(); } - QGraphicsScene *scene = new QGraphicsScene(ui->curvePreview); + scene = new CustomScene(ui->curvePreview); scene->setItemIndexMethod(QGraphicsScene::NoIndex); + connect(scene, SIGNAL(newPoint(int, int)), this, SLOT(onSceneNewPoint(int, int))); + ui->curvePreview->setScene(scene); int numcurves=firmware->getCapability(NumCurves); int limit; @@ -292,14 +294,13 @@ void Curves::updateCurve() Node * nodex = 0; QColor color; - QGraphicsScene *scene = ui->curvePreview->scene(); scene->clear(); qreal width = scene->sceneRect().width(); qreal height = scene->sceneRect().height(); - qreal centerX = scene->sceneRect().left() + width/2; //center X - qreal centerY = scene->sceneRect().top() + height/2; //center Y + qreal centerX = scene->sceneRect().left() + width/2; + qreal centerY = scene->sceneRect().top() + height/2; QGraphicsSimpleTextItem *ti = scene->addSimpleText(tr("Editing curve %1").arg(currentCurve+1)); ti->setPos(3, 3); @@ -642,3 +643,53 @@ void Curves::ShowContextMenu(const QPoint& pos) // this is a slot } } } + +void Curves::onSceneNewPoint(int x, int y) +{ + if ((model->curves[currentCurve].type == CurveData::CURVE_TYPE_CUSTOM) && (model->curves[currentCurve].count < CPN_MAX_POINTS)) { + int newidx; + int numpoints = model->curves[currentCurve].count; + if (x < model->curves[currentCurve].points[0].x) { + newidx = 0; + } + else if (x > model->curves[currentCurve].points[numpoints - 1].x) { + newidx = numpoints; + } + else { + for (int i=0; icurves[currentCurve].points[i].x) { + newidx = i; + break; + } + } + } + numpoints++; + model->curves[currentCurve].count = numpoints; + for (int idx=(numpoints-1); idx>(newidx); idx--) { + model->curves[currentCurve].points[idx] = model->curves[currentCurve].points[idx-1]; + } + model->curves[currentCurve].points[newidx].x = x; + model->curves[currentCurve].points[newidx].y = y; + update(); + emit modified(); + } +} + +CustomScene::CustomScene(QGraphicsView * view) +{ +} + +void CustomScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) +{ + if (event->button() == Qt::LeftButton && event->modifiers() == Qt::ControlModifier) { + QRectF rect = sceneRect(); + QPointF pos = event->scenePos(); + QPointF p; + p.setX(-100 + ((pos.x() - rect.left()) * 200) / rect.width()); + p.setY(100 + (rect.top() - pos.y()) * 200 / rect.height()); + QGraphicsScene::mouseReleaseEvent(event); + emit newPoint((int)p.x(), (int)p.y()); + } + else + QGraphicsScene::mouseReleaseEvent(event); +} diff --git a/companion/src/modeledit/curves.h b/companion/src/modeledit/curves.h index ebf4ffa4f93..dafe08f999c 100644 --- a/companion/src/modeledit/curves.h +++ b/companion/src/modeledit/curves.h @@ -23,6 +23,8 @@ #include "modeledit.h" #include "eeprominterface.h" +#include +#include enum CopyAction { CURVE_COPY, @@ -43,6 +45,20 @@ struct CurveCreatorTemplate { curveFunction function; }; +class CustomScene : public QGraphicsScene +{ + Q_OBJECT + + public: + CustomScene(QGraphicsView * view); + + signals: + void newPoint(int, int); + + protected: + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * event) override; +}; + class Curves : public ModelPanel { Q_OBJECT @@ -67,6 +83,7 @@ class Curves : public ModelPanel void onNodeUnfocus(); void on_curveType_currentIndexChanged(int index); void on_curveApply_clicked(); + void onSceneNewPoint(int x, int y); protected: virtual void resizeEvent(QResizeEvent *event); @@ -85,7 +102,7 @@ class Curves : public ModelPanel void updateCurvePoints(); bool allowCurveType(int points, CurveData::CurveType type); void setPointY(int i, int x, int y); - + CustomScene * scene; }; #endif // _CURVES_H_ From 852597d2fc7a3ed6be63a916c532d06dfb704d54 Mon Sep 17 00:00:00 2001 From: elecpower Date: Tue, 5 Jun 2018 21:32:31 +1000 Subject: [PATCH 2/3] Add more curve enhancements delete and resize points --- companion/src/modeledit/curves.cpp | 33 ++++++++ companion/src/modeledit/curves.h | 2 + companion/src/modeledit/curves.ui | 129 ++++++++++++++++------------- companion/src/modeledit/node.cpp | 29 +++++-- companion/src/modeledit/node.h | 7 +- 5 files changed, 134 insertions(+), 66 deletions(-) diff --git a/companion/src/modeledit/curves.cpp b/companion/src/modeledit/curves.cpp index e85e957d3ca..d813e56d724 100644 --- a/companion/src/modeledit/curves.cpp +++ b/companion/src/modeledit/curves.cpp @@ -214,6 +214,11 @@ Curves::Curves(QWidget * parent, ModelData & model, GeneralSettings & generalSet addTemplate(tr("Symmetrical f(x)=-f(-x)"), CURVE_COEFF_ENABLE, curveSymmetricalY); addTemplate(tr("Symmetrical f(x)=f(-x)"), CURVE_COEFF_ENABLE | CURVE_YMID_ENABLE, curveSymmetricalX); + ui->pointSize->setValue(10); + ui->pointSize->setMinimum(3); + ui->pointSize->setMaximum(20); + connect(ui->pointSize, SIGNAL(valueChanged(int)), this, SLOT(onPointSizeEdited())); + disableMouseScrolling(); lock = false; @@ -332,6 +337,8 @@ void Curves::updateCurve() nodex = new Node(); nodex->setProperty("index", i); nodex->setColor(colors[currentCurve]); + nodex->setBallSize(ui->pointSize->value()); + nodex->setBallHeight(0); if (model->curves[currentCurve].type == CurveData::CURVE_TYPE_CUSTOM) { if (i>0 && isetFixedX(false); @@ -350,6 +357,7 @@ void Curves::updateCurve() connect(nodex, SIGNAL(moved(int, int)), this, SLOT(onNodeMoved(int, int))); connect(nodex, SIGNAL(focus()), this, SLOT(onNodeFocus())); connect(nodex, SIGNAL(unfocus()), this, SLOT(onNodeUnfocus())); + connect(nodex, SIGNAL(deleteMe()), this, SLOT(onNodeDelete())); scene->addItem(nodex); if (i>0) scene->addItem(new Edge(nodel, nodex)); } @@ -644,6 +652,31 @@ void Curves::ShowContextMenu(const QPoint& pos) // this is a slot } } +void Curves::onPointSizeEdited() +{ + if (!lock) { + update(); + } +} + +void Curves::onNodeDelete() +{ + int index = sender()->property("index").toInt(); + int numpoints = model->curves[currentCurve].count; + if ((model->curves[currentCurve].type == CurveData::CURVE_TYPE_CUSTOM) && (index > 0) && (index < numpoints-1)) { + spny[index]->clearFocus(); + for (int i=index+1; icurves[currentCurve].points[i-1] = model->curves[currentCurve].points[i]; + } + numpoints--; + model->curves[currentCurve].points[numpoints].x = 0; + model->curves[currentCurve].points[numpoints].y = 0; + model->curves[currentCurve].count = numpoints; + update(); + emit modified(); + } +} + void Curves::onSceneNewPoint(int x, int y) { if ((model->curves[currentCurve].type == CurveData::CURVE_TYPE_CUSTOM) && (model->curves[currentCurve].count < CPN_MAX_POINTS)) { diff --git a/companion/src/modeledit/curves.h b/companion/src/modeledit/curves.h index dafe08f999c..a65ba887271 100644 --- a/companion/src/modeledit/curves.h +++ b/companion/src/modeledit/curves.h @@ -84,6 +84,8 @@ class Curves : public ModelPanel void on_curveType_currentIndexChanged(int index); void on_curveApply_clicked(); void onSceneNewPoint(int x, int y); + void onPointSizeEdited(); + void onNodeDelete(); protected: virtual void resizeEvent(QResizeEvent *event); diff --git a/companion/src/modeledit/curves.ui b/companion/src/modeledit/curves.ui index f7c58ee35ed..f03252942f4 100644 --- a/companion/src/modeledit/curves.ui +++ b/companion/src/modeledit/curves.ui @@ -132,18 +132,18 @@ - - 0 - - - 2 - 0 0 + + 0 + + + 2 + @@ -175,17 +175,41 @@ Curve Creator - - - + + + + + Coefficient + + - - + + - Y at X=0 + Curve type + + + + Side + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -202,15 +226,8 @@ - - - - Y at X=100 - - - - - + + 0 @@ -225,6 +242,13 @@ + + + + Y at X=-100 + + + @@ -244,8 +268,15 @@ - - + + + + Y at X=100 + + + + + 0 @@ -267,28 +298,25 @@ - - + + + + + - Side + Y at X=0 - - - - Qt::Vertical - - - - 20 - 40 - + + + + Point size - + - - + + 0 @@ -303,24 +331,13 @@ - - - - Curve type - - - - - - - Y at X=-100 - - + + - - - - Coefficient + + + + Qt::Horizontal diff --git a/companion/src/modeledit/node.cpp b/companion/src/modeledit/node.cpp index 4cdbd1108d3..a93e0fb2cbe 100644 --- a/companion/src/modeledit/node.cpp +++ b/companion/src/modeledit/node.cpp @@ -41,6 +41,7 @@ Node::Node(): fixedX = false; fixedY = false; ballSize = DEFAULT_BALL_SIZE; + ballHeight = DEFAULT_BALL_HEIGHT; } void Node::addEdge(Edge *edge) @@ -138,7 +139,7 @@ QRectF Node::boundingRect() const { qreal adjust = 2; return QRectF(-ballSize/2 - adjust, -ballSize/2 - adjust, - ballSize+BALL_HEIGHT + adjust, ballSize+BALL_HEIGHT + adjust); + ballSize+ballHeight + adjust, ballSize+ballHeight + adjust); } @@ -153,12 +154,12 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid { painter->setPen(Qt::NoPen); painter->setBrush(Qt::darkGray); - painter->drawEllipse(-ballSize/2 + BALL_HEIGHT, -ballSize/2 + BALL_HEIGHT, ballSize, ballSize); + painter->drawEllipse(-ballSize/2 + ballHeight, -ballSize/2 + ballHeight, ballSize, ballSize); - QRadialGradient gradient(-BALL_HEIGHT, -BALL_HEIGHT, ballSize/2); + QRadialGradient gradient(-ballHeight, -ballHeight, ballSize/2); if (option->state & QStyle::State_Sunken) { - gradient.setCenter(BALL_HEIGHT, BALL_HEIGHT); - gradient.setFocalPoint(BALL_HEIGHT, BALL_HEIGHT); + gradient.setCenter(ballHeight, ballHeight); + gradient.setFocalPoint(ballHeight, ballHeight); gradient.setColorAt(1, nodecolor.light(180)); gradient.setColorAt(0, nodecolor.light(120)); } else { @@ -189,10 +190,10 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) if (newX < minX) newX = minX; if (newX > maxX) newX = maxX; - if (!getFixedX()) { + if (!getFixedX()) { newPos.setX(((newX+100)*rect.width()/200+rect.left())); } - + emit moved(newX, newY); return newPos; } @@ -220,11 +221,23 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { update(); bPressed = false; + if (scene()) { QGraphicsItem::mouseReleaseEvent(event); - emit unfocus(); + if (event->button() == Qt::LeftButton && event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) { + emit deleteMe(); + } + else { + emit unfocus(); + } } else { QGraphicsItem::mouseReleaseEvent(event); } } + +void Node::setBallHeight(int height) +{ + ballHeight = height; +} + diff --git a/companion/src/modeledit/node.h b/companion/src/modeledit/node.h index b952a43e569..17aab3015bd 100644 --- a/companion/src/modeledit/node.h +++ b/companion/src/modeledit/node.h @@ -24,7 +24,7 @@ #include #define DEFAULT_BALL_SIZE 10 -#define BALL_HEIGHT 2 +#define DEFAULT_BALL_HEIGHT 2 class Edge; QT_BEGIN_NAMESPACE @@ -64,13 +64,15 @@ class Node : public QGraphicsObject void setMaxX(int val) { maxX = val; } void setPressed(bool pressed) { bPressed = pressed; } bool isPressed() const { return bPressed; } - void setColor(const QColor & color); + void setBallHeight(int height); + int geBallHeight() { return ballHeight; } signals: void moved(int x, int y); void focus(); void unfocus(); + void deleteMe(); protected: QVariant itemChange(GraphicsItemChange change, const QVariant &value); @@ -89,6 +91,7 @@ class Node : public QGraphicsObject QList edgeList; QPointF newPos; QColor nodecolor; + int ballHeight; }; #endif // _NODE_H_ From f0c0b6c2544e9f2259c2191ba987f7cafac05dc6 Mon Sep 17 00:00:00 2001 From: elecpower Date: Wed, 6 Jun 2018 20:56:00 +1000 Subject: [PATCH 3/3] add scene constructor --- companion/src/modeledit/curves.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/companion/src/modeledit/curves.cpp b/companion/src/modeledit/curves.cpp index d813e56d724..c897a7e1e34 100644 --- a/companion/src/modeledit/curves.cpp +++ b/companion/src/modeledit/curves.cpp @@ -708,7 +708,8 @@ void Curves::onSceneNewPoint(int x, int y) } } -CustomScene::CustomScene(QGraphicsView * view) +CustomScene::CustomScene(QGraphicsView * view) : + QGraphicsScene(view) { }