+// rotateaction.cpp: Action class for rotating selected objects
+//
+// Part of the Architektonas Project
+// (C) 2011 Underground Software
+// See the README and GPLv3 files for licensing and warranty information
+//
+// JLH = James Hammons <jlhamm@acm.org>
+//
+// WHO WHEN WHAT
+// --- ---------- ------------------------------------------------------------
+// JLH 09/03/2011 Created this file
+//
+
+#include "rotateaction.h"
+#include "applicationwindow.h"
+#include "container.h"
+#include "drawingview.h"
+#include "line.h"
+#include "mathconstants.h"
+#include "painter.h"
+//#include "vector.h"
+
+
+//#define FIRST_POINT 0
+//#define NEXT_POINT 1
+enum { FIRST_POINT, NEXT_POINT };
+
+
+RotateAction::RotateAction(): state(FIRST_POINT), line(NULL),
+ shiftWasPressedOnNextPoint(false), spin(new Container(Vector()))
+{
+// ApplicationWindow::drawing->document.CopySelectedContentsTo(selected);
+ ApplicationWindow::drawing->document.CopySelectedContentsTo(spin);
+
+// for(std::vector<Object *>::iterator i=spin->objects.begin(); i!=spin->objects.end(); i++)
+// (*i)->Save();
+ spin->Save();
+}
+
+
+RotateAction::~RotateAction()
+{
+}
+
+
+/*virtual*/ void RotateAction::Draw(Painter * painter)
+{
+ painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
+
+ // I think stuff like crosshairs should be done in the DrawingView, tho
+ // (and it's done there now...)
+ if (state == FIRST_POINT)
+ {
+ painter->DrawHandle(p1);
+ }
+ else
+ {
+// Vector reflectedP2 = -(p2 - p1);
+// Point newP2 = p1 + reflectedP2;
+ painter->DrawLine(p1, p2);
+ painter->DrawHandle(p1);
+
+ double absAngle = (Vector(p2 - p1).Angle()) * RADIANS_TO_DEGREES;
+
+ // Keep the angle between 0 and 180 degrees
+// if (absAngle > 180.0)
+// absAngle -= 180.0;
+
+ QString text = QChar(0x2221) + QObject::tr(": %1");
+ text = text.arg(absAngle);
+// QString text = tr("Length: %1 in.");
+// text = text.arg(Vector::Magnitude(p1, p2));
+ painter->DrawInformativeText(text);
+
+ // Draw the rotated objects only if there's been a line to spin around
+ if (p1 != p2)
+ spin->Draw(painter);
+ }
+}
+
+
+/*virtual*/ void RotateAction::MouseDown(Vector point)
+{
+ // Clear our override...
+ shiftWasPressedOnNextPoint = false;
+
+ if (state == FIRST_POINT)
+ p1 = point;
+ else
+ p2 = point;
+}
+
+
+/*virtual*/ void RotateAction::MouseMoved(Vector point)
+{
+ if (state == FIRST_POINT)
+ p1 = point;
+ else
+ {
+ p2 = point;
+ double angle = Vector(p2, p1).Angle();
+
+ for(std::vector<Object *>::iterator i=spin->objects.begin(); i!=spin->objects.end(); i++)
+ {
+ (*i)->Restore();
+ (*i)->Rotate(p1, angle);
+ }
+ }
+}
+
+
+/*virtual*/ void RotateAction::MouseReleased(void)
+{
+ if (state == FIRST_POINT)
+ {
+ p2 = p1;
+ state = NEXT_POINT;
+ }
+ else if (state == NEXT_POINT)
+ {
+ state = FIRST_POINT;
+
+ std::vector<Object *> & objs = ApplicationWindow::drawing->document.objects;
+ double angle = Vector(p2, p1).Angle();
+
+ for(std::vector<Object *>::iterator i=objs.begin(); i!=objs.end(); i++)
+ {
+ if ((*i)->state == OSSelected)
+ (*i)->Rotate(p1, angle);
+ }
+
+ spin->Clear();
+ ApplicationWindow::drawing->document.CopySelectedContentsTo(spin);
+ spin->Save();
+ }
+}
+
+
+/*virtual*/ bool RotateAction::KeyDown(int key)
+{
+ if ((key == Qt::Key_Shift) && (state == NEXT_POINT))
+ {
+ shiftWasPressedOnNextPoint = true;
+ p1Save = p1;
+ p1 = p2;
+ state = FIRST_POINT;
+ return true;
+ }
+
+ return false;
+}
+
+
+/*virtual*/ bool RotateAction::KeyReleased(int key)
+{
+ if ((key == Qt::Key_Shift) && shiftWasPressedOnNextPoint)
+ {
+ shiftWasPressedOnNextPoint = false;
+ p2 = p1;
+ p1 = p1Save;
+ state = NEXT_POINT;
+ return true;
+ }
+
+ return false;
+}
+