From ace85fa91c69a55346c9c99a815040a701f5d7ee Mon Sep 17 00:00:00 2001 From: Jorge Cuadrado Date: Fri, 21 Sep 2007 20:45:53 -0500 Subject: [PATCH] Improved PolyLine Tool --- yamf/tools/polyline/polyline.cpp | 136 ++++++++++++++++++++++++++------------- 1 file changed, 91 insertions(+), 45 deletions(-) diff --git a/yamf/tools/polyline/polyline.cpp b/yamf/tools/polyline/polyline.cpp index 0061f0d..8973f44 100644 --- a/yamf/tools/polyline/polyline.cpp +++ b/yamf/tools/polyline/polyline.cpp @@ -53,13 +53,12 @@ namespace Tool { struct PolyLine::Private { - bool begin; + bool begin, moved; QPointF center; QPointF right; - QPointF mirror; - Drawing::Tool::Private::ContourEditor *editor; QPainterPath path; + QPainterPath lastPart; YAMF::Item::Path *item; PaintArea * paintArea; @@ -68,6 +67,9 @@ struct PolyLine::Private QGraphicsLineItem *line1, *line2; QGraphicsPathItem *line3; + + + }; PolyLine::PolyLine(QObject *parent): AbstractTool(parent), d(new Private) @@ -123,6 +125,7 @@ QString PolyLine::id() const void PolyLine::press(const QGraphicsSceneMouseEvent *input, PaintArea *const paintArea) { + d->paintArea = paintArea; QGraphicsScene *scene = paintArea->scene(); if(scene->items().contains(d->line3)) { @@ -143,15 +146,28 @@ void PolyLine::press(const QGraphicsSceneMouseEvent *input, PaintArea *const pai d->editor = new Drawing::Tool::Private::ContourEditor(d->item, scene); connect(d->editor, SIGNAL(nodeClicked()), this, SLOT(nodeChanged())); scene->addItem( d->item ); + + d->begin = true; + d->moved = false; } else { d->begin = false; - d->path.cubicTo(d->right, d->mirror, input->scenePos()); + + if(!d->moved) + { + d->path.cubicTo(d->center, d->center, input->scenePos()); + } + else + { + d->moved = false; + d->path.cubicTo(d->center, d->right, input->scenePos()); + } } d->center = input->scenePos(); + d->item->setPen( brushManager->pen() ); } @@ -162,24 +178,34 @@ void PolyLine::move(const QGraphicsSceneMouseEvent *input, PaintArea *const pain { if(input->buttons() == Qt::NoButton) { -// d->mirror = d->center - ( input->scenePos() - d->center); + QPainterPath lastPart; - QPointF point; - int size = d->item->path().elementCount(); - if(size > 0) + lastPart.moveTo(d->center); + + if(!d->moved) { - point = d->item->path().elementAt(size -1); +// lastPart.lineTo(input->scenePos()); + lastPart.cubicTo(d->center, d->center, input->scenePos()); + } + else + { + lastPart.cubicTo(d->center, d->right, input->scenePos()); } - QPainterPath path; - path.moveTo(point); - path.cubicTo(point, d->right, input->scenePos()); - - d->line3->setPath(path); + d->line3->setPath(lastPart); if(!scene->items().contains(d->line3)) { scene->addItem(d->line3); } + + if(!scene->items().contains(d->line1)) + { + scene->addItem( d->line1 ); + } + if(!scene->items().contains(d->line2)) + { + scene->addItem( d->line2 ); + } } else if(d->editor && d->editor->isSelected()) { @@ -189,43 +215,54 @@ void PolyLine::move(const QGraphicsSceneMouseEvent *input, PaintArea *const pain { paintArea->setDragMode(QGraphicsView::NoDrag); - d->mirror = d->center - ( input->scenePos() - d->center); - if(d->begin) { + d->moved = true; d->right = input->scenePos(); + + d->line2->setLine(QLineF(d->right, d->center)); + if(!scene->items().contains(d->line2)) + { + scene->addItem( d->line2 ); + } } else { - for(int i = d->path.elementCount()-1; i >= 0; i--) + + int index = d->path.elementCount()-3; + + if( d->path.elementAt(index).type == QPainterPath::CurveToElement ) { - if(d->path.elementAt(i).type == QPainterPath::CurveToElement) + if(!d->moved) + { + QPointF point = d->path.elementAt(index+1); + d->path.setElementPositionAt(index, point.x(), point.y() ); + } + + QPointF mirror = d->center - ( input->scenePos() - d->center); + + d->path.setElementPositionAt(index+1, mirror.x(), mirror.y()); + + d->line1->setLine(QLineF(mirror, d->center)); + d->line2->setLine(QLineF(d->right, d->center)); + + + if(!scene->items().contains(d->line1)) + { + scene->addItem( d->line1 ); + } + if(!scene->items().contains(d->line2)) { - d->right = input->scenePos(); - if(d->path.elementAt(i+1).type == QPainterPath::CurveToDataElement) - { - d->path.setElementPositionAt(i+1, d->mirror.x(), d->mirror.y() ); - } - break; + scene->addItem( d->line2 ); } } + + d->moved = true; + d->right = input->scenePos(); + d->item->setPath(d->path); + } - Q_CHECK_PTR(d->item); - d->item->setPath(d->path); - - d->line1->setLine(QLineF(d->mirror, d->center)); - d->line2->setLine(QLineF(d->right, d->center)); - - - if(!scene->items().contains(d->line1)) - { - scene->addItem( d->line1 ); - } - if(!scene->items().contains(d->line2)) - { - scene->addItem( d->line2 ); - } } } } @@ -288,14 +325,16 @@ void PolyLine::photogramChanged(Photogram *const photogram) Q_UNUSED(photogram); if(d->item) { - d->editor->show(); - d->editor->expandAllNodes (); +// FIXME: +// d->path = d->item->path(); +// d->editor->setItem(d->item); +// d->editor->show(); +// d->editor->expandAllNodes(); int size = d->item->path().elementCount(); if(size > 0) { d->center = d->item->path().elementAt(size -1); } - } } @@ -312,17 +351,24 @@ void PolyLine::endItem() void PolyLine::nodeChanged() { + if(d->paintArea) { int position = d->paintArea->currentFrame()->visualIndexOf(d->item); if(position != -1) { - QPainterPath path = d->item->path(); - d->path = path; + int size = d->item->path().elementCount(); + if(size > 0) + { + d->center = d->item->path().elementAt(size -1); + + } + d->path = d->item->path(); + d->editor->restore(); - d->paintArea->addCommand(new Command::EditNodesItem(d->item, path)); + d->paintArea->addCommand(new Command::EditNodesItem(d->item, d->path)); d->editor->save(); } } -- 2.11.4.GIT