+// actiondrawpolyline.cpp
+//
+// Part of the Architektonas Project
+// by James L. Hammons
+// Copyright (C) 2010 Underground Software
+// See the README and GPLv2 files for licensing and warranty information
+//
+// JLH = James L. Hammons <jlhamm@acm.org>
+//
+// Who When What
+// --- ---------- -----------------------------------------------------------
+// JLH 09/13/2010 Created this file. :-)
+//
+
+#include "actiondrawpolyline.h"
+
+#include "debug.h"
+#include "dialogfactory.h"
+#include "graphicview.h"
+#include "polyline.h"
+
+ActionDrawPolyline::ActionDrawPolyline(EntityContainer & container,
+ GraphicView & graphicView):
+ ActionInterface("Draw polyline", container, graphicView), vertex(false), polyline(NULL)
+{
+// vertex = Vector(false);
+}
+
+ActionDrawPolyline::~ActionDrawPolyline()
+{
+ if (polyline)
+ delete polyline;
+}
+
+/*virtual*/ RS2::ActionType ActionDrawPolyline::rtti()
+{
+ return RS2::ActionDrawPolyline;
+}
+
+void ActionDrawPolyline::trigger()
+{
+ if (polyline)
+ {
+ container->addEntity(polyline);
+
+ if (document)
+ {
+ document->startUndoCycle();
+ document->addUndoable(polyline);
+ document->endUndoCycle();
+ }
+
+ DEBUG->print("ActionDrawPolyline::trigger(): polyline added: %d", polyline->getId());
+ polyline = NULL;
+ graphicView->redraw(); //hm.
+ }
+}
+
+void ActionDrawPolyline::mouseMoveEvent(QMouseEvent * e)
+{
+#if 0
+ if (vertex.valid && polyline)
+ {
+ Vector v = snapPoint(e);
+ Entity * ent = polyline->addVertex(v);
+ ent->setLayerToActive();
+ ent->setPenToActive();
+
+// deleteSnapper();
+// graphicView->drawEntity(ent);
+// drawSnapper();
+
+ vertex = v;
+ DEBUG->print("ActionDrawPolyline::mouseMoveEvent(): line added: %d", ent->getId());
+ }
+#else
+ Vector mouse = graphicView->SnapPoint(e);
+
+ if (getStatus() == SetNextPoint && startPoint.valid)
+ {
+ // We have to draw a line or arc depending on the state of the "Arc"
+ // checkbox.
+ // We'll start with lines...
+ graphicView->preview.clear();
+ Line * line = new Line(&(graphicView->preview), LineData(startPoint, mouse));
+
+ if (polyline)
+ graphicView->preview.addEntity(polyline);
+
+ graphicView->preview.addEntity(line);
+ }
+
+ graphicView->redraw(); //hm.
+#endif
+
+#if 0
+ Vector mouse = graphicView->snapper.snapPoint(e);
+ DEBUG->print("ActionDrawLine::mouseMoveEvent: snap point: OK");
+
+ if (getStatus() == SetEndpoint && data.startpoint.valid)
+ {
+ DEBUG->print("ActionDrawLine::mouseMoveEvent: update preview");
+ // This is lame. Creating a new Line every time the endpoint moves.
+ // Surely we can alter the line entity inside the preview, no?
+#if 0
+ graphicView->preview.clear(); // Remove entities from the container
+ Line * line = new Line(&(graphicView->preview), LineData(data.startpoint, mouse));
+ graphicView->preview.addEntity(line);
+#else
+ // We can assume there's only one line in there, can't we?
+ Line * line = (Line *)graphicView->preview.firstEntity(RS2::ResolveNone);
+
+ if (line)
+ {
+ line->setEndpoint(mouse);
+ }
+ else
+ {
+ line = new Line(&(graphicView->preview), LineData(data.startpoint, mouse));
+ graphicView->preview.addEntity(line);
+ }
+#endif
+ }
+
+ //hm. [ok, this works. :-D]
+ graphicView->redraw();
+#endif
+}
+
+void ActionDrawPolyline::mousePressEvent(QMouseEvent * e)
+{
+ if (e->button() == Qt::LeftButton)
+ {
+// vertex = snapPoint(e);
+// polyline = new Polyline(container, PolylineData(vertex, vertex, 0));
+// polyline->setLayerToActive();
+// polyline->setPenToActive();
+ }
+}
+
+void ActionDrawPolyline::mouseReleaseEvent(QMouseEvent * e)
+{
+ if (e->button() == Qt::LeftButton)
+ {
+ Vector ce(graphicView->SnapPoint(e));
+ coordinateEvent(&ce);
+
+//let's try this...
+ if (!polyline)
+ {
+//printf("ActionDrawPolyline::mouseReleaseEvent(): Creating polyline...\n");
+ polyline = new Polyline(container, PolylineData(ce, ce, 0));
+// vertex = snapPoint(e);
+// polyline = new Polyline(container, PolylineData(vertex, vertex, 0));
+ polyline->setLayerToActive();
+ polyline->setPenToActive();
+ }
+ else
+ {
+//printf("ActionDrawPolyline::mouseReleaseEvent(): Adding vertex to polyline...\n");
+ Entity * ent = polyline->addVertex(ce);
+ ent->setLayerToActive();
+ ent->setPenToActive();
+ }
+ }
+ else if (e->button() == Qt::RightButton)
+ {
+#if 0
+ if (polyline)
+ {
+ delete polyline;
+ polyline = NULL;
+ }
+#else
+// if (getStatus() == SetFirstPoint)
+//ANY right button should trigger (? what about adding twice? won't happen)
+ trigger(); // this will set the polyline to NULL once added to the container
+#endif
+
+ init(getStatus() - 1);
+ graphicView->redraw(); //hm.
+ }
+}
+
+void ActionDrawPolyline::coordinateEvent(Vector * coordinate)
+{
+ DEBUG->print("ActionDrawPolyline::coordinateEvent");
+
+ if (!coordinate)
+ {
+ DEBUG->print("ActionDrawPolyline::coordinateEvent: event was NULL");
+ return;
+ }
+
+// Vector mouse = *e;
+
+ switch (getStatus())
+ {
+ case SetFirstPoint:
+ startPoint = *coordinate;
+// ClearHistory();
+// history.append(new Vector(mouse));
+// start = data.startpoint;
+ setStatus(SetNextPoint);
+ graphicView->moveRelativeZero(*coordinate);
+// trigger();
+ updateMouseButtonHints();
+ break;
+
+ case SetNextPoint:
+// data.endpoint = mouse;
+// history.append(new Vector(mouse));
+// startPoint = endPoint;
+ startPoint = *coordinate;
+// trigger();
+ updateMouseButtonHints();
+ break;
+
+ default:
+ break;
+ }
+
+ DEBUG->print("ActionDrawPolyline::coordinateEvent: OK");
+}
+
+void ActionDrawPolyline::updateMouseButtonHints()
+{
+ switch (getStatus())
+ {
+ case SetFirstPoint:
+ DIALOGFACTORY->updateMouseWidget(tr("Set first point"), tr("Cancel"));
+ break;
+
+ case SetNextPoint:
+ DIALOGFACTORY->updateMouseWidget(tr("Set next point"), tr("Back"));
+ break;
+
+ default:
+ DIALOGFACTORY->updateMouseWidget("", "");
+ break;
+ }
+}
+
+void ActionDrawPolyline::updateMouseCursor()
+{
+ graphicView->setMouseCursor(RS2::CadCursor);
+}
+
+void ActionDrawPolyline::updateToolBar()
+{
+ if (!isFinished())
+ DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
+ else
+ DIALOGFACTORY->requestToolBar(RS2::ToolBarPolylines);
+}
+
+void ActionDrawPolyline::showOptions()
+{
+ DEBUG->print("ActionDrawPolyline::showOptions");
+ ActionInterface::showOptions();
+ DIALOGFACTORY->requestOptions(this, true);
+ DEBUG->print("ActionDrawPolyline::showOptions: OK");
+}
+
+void ActionDrawPolyline::hideOptions()
+{
+ ActionInterface::hideOptions();
+ DIALOGFACTORY->requestOptions(this, false);
+}
+
+void ActionDrawPolyline::close()
+{
+#if 0
+//NOTE: We should grey out the "close" button until the conditions for its use are satisfied.
+// Though I can see how this would be called via cmd line... So I guess it's OK
+ if (history.count() > 2 && start.valid)
+ {
+ data.endpoint = start;
+ trigger();
+ setStatus(SetFirstPoint);
+ graphicView->moveRelativeZero(start);
+ }
+ else
+ DIALOGFACTORY->commandMessage(tr("Cannot close sequence of lines: Not enough entities defined yet."));
+#else
+ DIALOGFACTORY->commandMessage(tr("Close functionality not defined yet..."));
+#endif
+}
+
+void ActionDrawPolyline::undo()
+{
+#if 0
+//NOTE: We should grey out the "undo" button until the conditions for its use are satisfied.
+ if (history.count() > 1)
+ {
+ history.removeLast();
+ graphicView->setCurrentAction(new ActionEditUndo(true, *container, *graphicView));
+ data.startpoint = *history.last();
+ graphicView->moveRelativeZero(data.startpoint);
+ graphicView->preview.clear();
+ graphicView->redraw();
+ }
+ else
+ DIALOGFACTORY->commandMessage(tr("Cannot undo: Not enough entities defined yet."));
+#else
+ DIALOGFACTORY->commandMessage(tr("Undo functionality not defined yet..."));
+#endif
+}