+// TrimAction.cpp: Action class for mirroring 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 08/27/2011 Created this file
+//
+
+#include "trimaction.h"
+#include "applicationwindow.h"
+#include "container.h"
+#include "drawingview.h"
+#include "line.h"
+#include "mathconstants.h"
+#include "painter.h"
+
+
+enum { FIRST_POINT, NEXT_POINT };
+
+
+TrimAction::TrimAction(): state(FIRST_POINT), line(NULL),
+ shiftWasPressedOnNextPoint(false), ctrlWasPressed(false),
+ mirror(new Container(Vector()))
+{
+ ApplicationWindow::drawing->document.CopySelectedContentsTo(mirror);
+ mirror->Save();
+}
+
+
+TrimAction::~TrimAction()
+{
+}
+
+
+/*virtual*/ void TrimAction::Draw(Painter * painter)
+{
+ painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
+
+ if (state == FIRST_POINT)
+ {
+ painter->DrawHandle(p1);
+ }
+ else
+ {
+ Vector reflectedP2 = -(p2 - p1);
+ Point newP2 = p1 + reflectedP2;
+ painter->DrawLine(newP2, 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);
+
+ if (ctrlWasPressed)
+ text += " (Copy)";
+
+ painter->DrawInformativeText(text);
+
+ // Draw the mirror only if there's been a line to mirror around
+ if (p1 != p2)
+ mirror->Draw(painter);
+ }
+}
+
+
+/*virtual*/ void TrimAction::MouseDown(Vector point)
+{
+ // Clear our override...
+ shiftWasPressedOnNextPoint = false;
+
+ if (state == FIRST_POINT)
+ p1 = point;
+ else
+ p2 = point;
+}
+
+
+/*virtual*/ void TrimAction::MouseMoved(Vector point)
+{
+ if (state == FIRST_POINT)
+ p1 = point;
+ else
+ {
+ p2 = point;
+ mirror->Restore();
+ mirror->Mirror(p1, p2);
+ }
+}
+
+
+/*virtual*/ void TrimAction::MouseReleased(void)
+{
+ if (state == FIRST_POINT)
+ {
+ p2 = p1;
+ state = NEXT_POINT;
+ }
+ else if (state == NEXT_POINT)
+ {
+ if (!ctrlWasPressed)
+ {
+ state = FIRST_POINT;
+ ApplicationWindow::drawing->document.MirrorSelected(p1, p2);
+
+ mirror->Clear();
+ ApplicationWindow::drawing->document.CopySelectedContentsTo(mirror);
+ mirror->Save();
+ }
+ else
+ {
+ mirror->CopyContentsTo(&(ApplicationWindow::drawing->document));
+ }
+ }
+}
+
+
+/*virtual*/ void TrimAction::KeyDown(int key)
+{
+ if ((key == Qt::Key_Shift) && (state == NEXT_POINT))
+ {
+ shiftWasPressedOnNextPoint = true;
+ p1Save = p1;
+ p1 = p2;
+ state = FIRST_POINT;
+ emit NeedRefresh();
+ }
+ else if (key == Qt::Key_Control)
+ {
+ ctrlWasPressed = true;
+ emit NeedRefresh();
+ }
+}
+
+
+/*virtual*/ void TrimAction::KeyReleased(int key)
+{
+ if ((key == Qt::Key_Shift) && shiftWasPressedOnNextPoint)
+ {
+ shiftWasPressedOnNextPoint = false;
+ p2 = p1;
+ p1 = p1Save;
+ state = NEXT_POINT;
+ emit NeedRefresh();
+ }
+ else if (key == Qt::Key_Control)
+ {
+ ctrlWasPressed = false;
+ emit NeedRefresh();
+ }
+}
+