Companion curve enhancements (#5947)
* Add point to custom curve using mouse click * Add more curve enhancements delete and resize points * add scene constructor
This commit is contained in:
committed by
Bertrand Songis
parent
13c5387dc9
commit
d460f005c6
@@ -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;
|
||||
@@ -212,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;
|
||||
@@ -292,14 +299,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);
|
||||
@@ -331,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 && i<numpoints-1) {
|
||||
nodex->setFixedX(false);
|
||||
@@ -349,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));
|
||||
}
|
||||
@@ -642,3 +651,79 @@ 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; i<numpoints; i++) {
|
||||
model->curves[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)) {
|
||||
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; i<numpoints; i++) {
|
||||
if (x < model->curves[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) :
|
||||
QGraphicsScene(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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user