]> Shamusworld >> Repos - architektonas/blobdiff - src/drawingview.cpp
Fixed add Line tool to respect keyboard override, slight cleanup to code.
[architektonas] / src / drawingview.cpp
index 99c4da9f2779054f5edbb6e607ee4e4f8f44371e..d5dd3a4d8d392c8668ab0302aad6270991d11bab 100644 (file)
 
 #define BACKGROUND_MAX_SIZE    512
 
+enum { ToolMouseDown, ToolMouseMove, ToolMouseUp };
+
 // Class variable
 //Container DrawingView::document(Vector(0, 0));
 
 
 DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
        // The value in the settings file will override this.
-       useAntialiasing(true), numSelected(0),
+       useAntialiasing(true), numSelected(0), numHovered(0), shiftDown(false),
+       ctrlDown(false),
        gridBackground(BACKGROUND_MAX_SIZE, BACKGROUND_MAX_SIZE),
        scale(1.0), offsetX(-10), offsetY(-10),// document(Vector(0, 0)),
        gridPixels(0), collided(false)//, toolAction(NULL)
@@ -333,6 +336,13 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
                painter.DrawCrosshair(oldPoint);
                toolAction->Draw(&painter);
        }
+#else
+       if (Global::tool)
+       {
+               painter.SetPen(QPen(QColor(200, 100, 0, 255), 1.0, Qt::DashLine));
+               painter.DrawCrosshair(oldPoint);
+               ToolDraw(&painter);
+       }
 #endif
 
 #if 1
@@ -540,6 +550,53 @@ void DrawingView::DeleteSelectedItems(void)
 }
 
 
+void DrawingView::ClearSelection(void)
+{
+       std::vector<void *>::iterator i;
+
+       for(i=document.objects.begin(); i!=document.objects.end(); i++)
+               ((Object *)(*i))->selected = false;
+}
+
+
+void DrawingView::AddHoveredToSelection(void)
+{
+       std::vector<void *>::iterator i;
+
+       for(i=document.objects.begin(); i!=document.objects.end(); i++)
+       {
+               if (((Object *)(*i))->hovered)
+                       ((Object *)(*i))->selected = true;
+       }
+}
+
+
+void DrawingView::GetSelection(std::vector<void *> & v)
+{
+       v.empty();
+       std::vector<void *>::iterator i;
+
+       for(i=v.begin(); i!=v.end(); i++)
+       {
+               if (((Object *)(*i))->selected)
+                       v.push_back(*i);
+       }
+}
+
+
+void DrawingView::GetHovered(std::vector<void *> & v)
+{
+       v.empty();
+       std::vector<void *>::iterator i;
+
+       for(i=v.begin(); i!=v.end(); i++)
+       {
+               if (((Object *)(*i))->hovered)
+                       v.push_back(*i);
+       }
+}
+
+
 void DrawingView::resizeEvent(QResizeEvent * /*event*/)
 {
        Global::screenSize = Vector(size().width(), size().height());
@@ -547,6 +604,73 @@ void DrawingView::resizeEvent(QResizeEvent * /*event*/)
 }
 
 
+void DrawingView::ToolMouse(int mode, Point p)
+{
+       if (Global::tool == TTLine)
+               LineHandler(mode, p);
+}
+
+
+void DrawingView::ToolDraw(Painter * painter)
+{
+       if (Global::tool == TTLine)
+       {
+               if (Global::toolState == TSNone)
+               {
+                       painter->DrawHandle(toolPoint[0]);
+               }
+               else if ((Global::toolState == TSPoint2) && shiftDown)
+               {
+                       painter->DrawHandle(toolPoint[1]);
+               }
+               else
+               {
+                       painter->DrawLine(toolPoint[0], toolPoint[1]);
+                       painter->DrawHandle(toolPoint[1]);
+               }
+       }
+}
+
+
+void DrawingView::LineHandler(int mode, Point p)
+{
+       switch (mode)
+       {
+       case ToolMouseDown:
+               if (Global::toolState == TSNone)
+                       toolPoint[0] = p;
+               else
+                       toolPoint[1] = p;
+
+               break;
+       case ToolMouseMove:
+               if (Global::toolState == TSNone)
+                       toolPoint[0] = p;
+               else
+                       toolPoint[1] = p;
+
+               break;
+       case ToolMouseUp:
+               if (Global::toolState == TSNone)
+               {
+                       Global::toolState = TSPoint2;
+                       // Prevent spurious line from drawing...
+                       toolPoint[1] = toolPoint[0];
+               }
+               else if ((Global::toolState == TSPoint2) && shiftDown)
+               {
+                       toolPoint[0] = toolPoint[1];
+               }
+               else
+               {
+                       Line * l = new Line(toolPoint[0], toolPoint[1]);
+                       document.objects.push_back(l);
+                       toolPoint[0] = toolPoint[1];
+               }
+       }
+}
+
+
 void DrawingView::mousePressEvent(QMouseEvent * event)
 {
        if (event->button() == Qt::LeftButton)
@@ -558,6 +682,9 @@ void DrawingView::mousePressEvent(QMouseEvent * event)
                // Do an update if collided with at least *one* object in the document
 //             if (collided)
 //                     update();
+               // Actually, we already know what we're going to click on as all the collision
+               // detection already happened in the mouse move function...!
+//printf("MouseDown: ctrl=%s, numHovered=%i\n", (ctrlDown ? "DOWN" : "up"), numHovered);
 
 #if 0
                if (toolAction)
@@ -572,11 +699,32 @@ void DrawingView::mousePressEvent(QMouseEvent * event)
 
                        toolAction->MouseDown(point);
                }
+#else
+               if (Global::tool)
+               {
+                       if (Global::snapToGrid)
+                               point = SnapPointToGrid(point);
+
+                       // Snap to object point if valid...
+                       
+
+//                     ToolMouseDown(point);
+                       ToolMouse(ToolMouseDown, point);
+                       //Also, may want to figure out if hovering over a snap point on an object,
+                       //snap to grid if not.
+                       return;
+               }
 #endif
 
+               if (!ctrlDown)
+                       ClearSelection();
+
+               if (numHovered > 0)
+                       AddHoveredToSelection();
+
 #if 1
                // Didn't hit any object and not using a tool, so do a selection rectangle
-               if (!(collided))// || toolAction))
+               if (!(numHovered || Global::tool))
                {
                        Global::selectionInProgress = true;
                        Global::selection.setTopLeft(QPointF(point.x, point.y));
@@ -620,7 +768,12 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
 
 #if 1
        // Grid processing... (only snap here is left button is down)
-       if ((event->buttons() & Qt::LeftButton) && Global::snapToGrid)
+       // Umm, WHY??
+       // well, it causes problems with selecting lines that aren't close to a grid line!
+       // THAT'S WHY!
+       // But even still, this is a bad approach, we need to not just do this for every
+       // case because it's WRONG to do it that way! !!! FIX !!!
+       if (/*(event->buttons() & Qt::LeftButton) &&*/ Global::snapToGrid)
        {
                point = SnapPointToGrid(point);
        }
@@ -749,8 +902,11 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
 
 #if 0
        // This returns true if we've moved over an object...
-       if (document.PointerMoved(point))
+       if (document.PointerMoved(point)) // <-- This
+       // This is where the object would do automagic dragging & shit. Since we don't
+       // do that anymore, we need a strategy to handle it.
        {
+
 /*
 Now objects handle mouse move snapping as well. The code below mainly works only
 for tools; we need to fix it so that objects work as well...
@@ -820,16 +976,18 @@ selected object last and thus fucks up the algorithm. Need to fix this...
 
                toolAction->MouseMoved(point);
        }
+#else
 #endif
+
        bool needUpdate = false;
 
        // Don't do this kind of checking unless we're not doing a selection rectangle!
-       // Hmm, lines don't stay selected if globally selected... !!! FIX !!!
+       // Hmm, lines don't stay selected if globally selected... !!! FIX !!! [DONE]
        // it's because there were extra state variables, the hit* vars...
        if (!Global::selectionInProgress)
        {
        std::vector<void *>::iterator i;
-       int numHovered = 0;
+       numHovered = 0;
 
        for(i=document.objects.begin(); i!=document.objects.end(); i++)
        {
@@ -892,14 +1050,29 @@ selected object last and thus fucks up the algorithm. Need to fix this...
                default:
                        break;
                }
+
+               if (obj->hovered)
+               {
+                       numHovered++;
+//printf("MouseMove: OBJECT HOVERED (numHovered = %i)\n", numHovered);
+               }
+       }
        }
+//printf("MouseMove: numHovered = %i\n", numHovered);
+
+       // Tool handling...
+       if (Global::tool)
+       {
+               // Need to do snapping, etc. as well
+//             ToolMouseMove(point);
+               ToolMouse(ToolMouseMove, point);
        }
 
        // This is used to draw the tool crosshair...
        oldPoint = point;
 
 //     if (/*document.NeedsUpdate() ||*/ Global::selectionInProgress /*|| toolAction*/)
-       if (needUpdate || Global::selectionInProgress)
+       if (needUpdate || Global::selectionInProgress || Global::tool)
                update();
 }
 
@@ -922,12 +1095,37 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event)
 #if 0
                if (toolAction)
                        toolAction->MouseReleased();
+#else
+               if (Global::tool)
+               {
+                       Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
+//                     ToolMouseUp(point);
+                       ToolMouse(ToolMouseUp, point);
+                       return;
+               }
 #endif
 
                if (Global::selectionInProgress)
                {
                        // Select all the stuff inside of selection
                        Global::selectionInProgress = false;
+
+                       // Clear our vectors
+                       select.empty();
+                       hover.empty();
+
+                       // Scoop 'em up
+                       std::vector<void *>::iterator i;
+
+                       for(i=document.objects.begin(); i!=document.objects.end(); i++)
+                       {
+                               if (((Object *)(*i))->selected)
+                                       select.push_back(*i);
+
+//hmm, this is no good, too late to do any good :-P
+//                             if ((*i)->hovered)
+//                                     hover.push_back(*i);
+                       }
                }
        }
        else if (event->button() == Qt::MiddleButton)
@@ -975,6 +1173,16 @@ void DrawingView::keyPressEvent(QKeyEvent * event)
        if (toolAction)
                toolAction->KeyDown(event->key());
 #endif
+       bool oldShift = shiftDown;
+       bool oldCtrl = ctrlDown;
+
+       if (event->key() == Qt::Key_Shift)
+               shiftDown = true;
+       else if (event->key() == Qt::Key_Control)
+               ctrlDown = true;
+
+       if ((oldShift != shiftDown) || (oldCtrl != ctrlDown))
+               update();
 }
 
 
@@ -984,6 +1192,16 @@ void DrawingView::keyReleaseEvent(QKeyEvent * event)
        if (toolAction)
                toolAction->KeyReleased(event->key());
 #endif
+       bool oldShift = shiftDown;
+       bool oldCtrl = ctrlDown;
+
+       if (event->key() == Qt::Key_Shift)
+               shiftDown = false;
+       else if (event->key() == Qt::Key_Control)
+               ctrlDown = false;
+
+       if ((oldShift != shiftDown) || (oldCtrl != ctrlDown))
+               update();
 }
 
 //