]> Shamusworld >> Repos - architektonas/blobdiff - src/mirroraction.cpp
Mirror tool now works successfully for all object types. :-D
[architektonas] / src / mirroraction.cpp
index 61b14ac1276be53c1199b3b310db421e0ff07834..af786d7f1dc72393c3698cd09b3af38b69bf1098 100644 (file)
@@ -12,6 +12,9 @@
 //
 
 #include "mirroraction.h"
+#include "applicationwindow.h"
+#include "container.h"
+#include "drawingview.h"
 #include "line.h"
 #include "mathconstants.h"
 #include "painter.h"
@@ -24,8 +27,14 @@ enum { FIRST_POINT, NEXT_POINT };
 
 
 MirrorAction::MirrorAction(): state(FIRST_POINT), line(NULL),
-       shiftWasPressedOnNextPoint(false)
+       shiftWasPressedOnNextPoint(false), mirror(new Container(Vector()))
 {
+//     ApplicationWindow::drawing->document.CopySelectedContentsTo(selected);
+       ApplicationWindow::drawing->document.CopySelectedContentsTo(mirror);
+
+//     for(std::vector<Object *>::iterator i=mirror->objects.begin(); i!=mirror->objects.end(); i++)
+//             (*i)->Save();
+       mirror->Save();
 }
 
 
@@ -46,17 +55,26 @@ MirrorAction::~MirrorAction()
        }
        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));
                painter->DrawInformativeText(text);
+
+               // Draw the mirror only if there's been a line to mirror around
+               if (p1 != p2)
+                       mirror->Draw(painter);
        }
 }
 
@@ -78,7 +96,15 @@ MirrorAction::~MirrorAction()
        if (state == FIRST_POINT)
                p1 = point;
        else
+       {
                p2 = point;
+
+               for(std::vector<Object *>::iterator i=mirror->objects.begin(); i!=mirror->objects.end(); i++)
+               {
+                       (*i)->Restore();
+                       (*i)->Mirror(p1, p2);
+               }
+       }
 }
 
 
@@ -91,14 +117,19 @@ 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);
+               state = FIRST_POINT;
 
-               p1 = p2;
-               state = NEXT_POINT;
+               std::vector<Object *> & objs = ApplicationWindow::drawing->document.objects;
+
+               for(std::vector<Object *>::iterator i=objs.begin(); i!=objs.end(); i++)
+               {
+                       if ((*i)->state == OSSelected)
+                               (*i)->Mirror(p1, p2);
+               }
+
+               mirror->Clear();
+               ApplicationWindow::drawing->document.CopySelectedContentsTo(mirror);
+               mirror->Save();
        }
 }