]> Shamusworld >> Repos - architektonas/blobdiff - src/mirroraction.cpp
Readded click to add dimension to object (for Line).
[architektonas] / src / mirroraction.cpp
index 720692ed85130725ac3c2170206fa2ec251463ea..877017355cdb7c2f46af067edc7a3917700157cd 100644 (file)
 #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 };
 
 
 MirrorAction::MirrorAction(): state(FIRST_POINT), line(NULL),
-       shiftWasPressedOnNextPoint(false), mirror(new Container(Vector()))
+       shiftWasPressedOnNextPoint(false), ctrlWasPressed(false),
+       mirror(new Container(Vector()))
 {
+       ApplicationWindow::drawing->document.CopySelectedContentsTo(mirror);
+       mirror->Save();
 }
 
 
@@ -41,27 +41,34 @@ MirrorAction::~MirrorAction()
 {
        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
        {
-               painter->DrawLine(p1, p2);
-               painter->DrawHandle(p2);
+               Vector reflectedP2 = -(p2 - p1);
+               Point newP2 = p1 + reflectedP2;
+               painter->DrawLine(newP2, p2);
+               painter->DrawHandle(p1);
 
                double absAngle = (Vector(p2 - p1).Angle()) * RADIANS_TO_DEGREES;
-//             double absLength = Vector(position - endpoint).Magnitude();
+
+               // 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));
+
+               if (ctrlWasPressed)
+                       text += " (Copy)";
+
                painter->DrawInformativeText(text);
 
-               mirror->Draw(painter);
+               // Draw the mirror only if there's been a line to mirror around
+               if (p1 != p2)
+                       mirror->Draw(painter);
        }
 }
 
@@ -85,19 +92,8 @@ MirrorAction::~MirrorAction()
        else
        {
                p2 = point;
-
-               mirror->Clear();
-               int itemsSelected = ApplicationWindow::drawing->document.ItemsSelected();
-
-               if (itemsSelected == 0)
-                       return;
-
-               for(int i=0; i<itemsSelected; i++)
-               {
-                       Object * object = ApplicationWindow::drawing->document.SelectedItem(i);
-                       Object * mirrored = object->Mirror(p1, p2);
-                       mirror->Add(mirrored);
-               }
+               mirror->Restore();
+               mirror->Mirror(p1, p2);
        }
 }
 
@@ -111,19 +107,24 @@ MirrorAction::~MirrorAction()
        }
        else if (state == NEXT_POINT)
        {
-               // We create the new object here, and then pass it off to the
-               // DrawingView which stuffs it into the document.
-//             line = new Line(p1, p2);
-               // We don't need no stinkin' sentinels, when we have signals & slots!
-//             emit ObjectReady(line);
+               if (!ctrlWasPressed)
+               {
+                       state = FIRST_POINT;
+                       ApplicationWindow::drawing->document.MirrorSelected(p1, p2);
 
-               p1 = p2;
-               state = NEXT_POINT;
+                       mirror->Clear();
+                       ApplicationWindow::drawing->document.CopySelectedContentsTo(mirror);
+                       mirror->Save();
+               }
+               else
+               {
+                       mirror->CopyContentsTo(&(ApplicationWindow::drawing->document));
+               }
        }
 }
 
 
-/*virtual*/ bool MirrorAction::KeyDown(int key)
+/*virtual*/ void MirrorAction::KeyDown(int key)
 {
        if ((key == Qt::Key_Shift) && (state == NEXT_POINT))
        {
@@ -131,14 +132,17 @@ MirrorAction::~MirrorAction()
                p1Save = p1;
                p1 = p2;
                state = FIRST_POINT;
-               return true;
+               emit(NeedRefresh());
+       }
+       else if (key == Qt::Key_Control)
+       {
+               ctrlWasPressed = true;
+               emit(NeedRefresh());
        }
-
-       return false;
 }
 
 
-/*virtual*/ bool MirrorAction::KeyReleased(int key)
+/*virtual*/ void MirrorAction::KeyReleased(int key)
 {
        if ((key == Qt::Key_Shift) && shiftWasPressedOnNextPoint)
        {
@@ -146,9 +150,12 @@ MirrorAction::~MirrorAction()
                p2 = p1;
                p1 = p1Save;
                state = NEXT_POINT;
-               return true;
+               emit(NeedRefresh());
+       }
+       else if (key == Qt::Key_Control)
+       {
+               ctrlWasPressed = false;
+               emit(NeedRefresh());
        }
-
-       return false;
 }