]> Shamusworld >> Repos - architektonas/blobdiff - src/drawingview.cpp
Readded click to add dimension to object (for Line).
[architektonas] / src / drawingview.cpp
index 89c63ed429d9a95f71aa245160d09d95c6ca7e4b..acfad409c2f22e685e49c646de30c355039684df 100644 (file)
 
 #define BACKGROUND_MAX_SIZE    512
 
+// 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),
        gridBackground(BACKGROUND_MAX_SIZE, BACKGROUND_MAX_SIZE),
-       scale(1.0), offsetX(-10), offsetY(-10),
-       document(Vector(0, 0)),
-       /*gridSpacing(12.0),*/ gridPixels(0), collided(false), //rotateTool(false),
-//     rx(150.0), ry(150.0),
-//     scrollDrag(false), addLineTool(false), addCircleTool(false),
-//     addDimensionTool(false),
-       toolAction(NULL)
+       scale(1.0), offsetX(-10), offsetY(-10), document(Vector(0, 0)),
+       gridPixels(0), collided(false), toolAction(NULL)
 {
        document.isTopLevelContainer = true;
        setBackgroundRole(QPalette::Base);
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
-       Object::gridSpacing = 12.0;
-//     toolPalette = new ToolWindow();
-//     CreateCursors();
-//     setCursor(cur[TOOLSelect]);
-//     setMouseTracking(true);
+       Object::gridSpacing = 12.0;             // In base units (inch is default)
 
        Line * line = new Line(Vector(5, 5), Vector(50, 40), &document);
        document.Add(line);
@@ -127,7 +121,7 @@ need a thickness parameter similar to the "size" param for dimensions. (And now
 we do! :-)
 
 */
-       SetGridSize(12);
+       SetGridSize(12);        // This is in pixels
 }
 
 
@@ -138,6 +132,7 @@ void DrawingView::SetToolActive(Action * action)
                toolAction = action;
                connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
                        SLOT(AddNewObjectToDocument(Object *)));
+               connect(toolAction, SIGNAL(NeedRefresh()), this, SLOT(HandleActionUpdate()));
        }
 }
 
@@ -260,6 +255,12 @@ void DrawingView::AddNewObjectToDocument(Object * object)
 }
 
 
+void DrawingView::HandleActionUpdate(void)
+{
+       update();
+}
+
+
 void DrawingView::SetCurrentLayer(int layer)
 {
        Object::currentLayer = layer;
@@ -331,26 +332,11 @@ void DrawingView::mousePressEvent(QMouseEvent * event)
        if (event->button() == Qt::LeftButton)
        {
                Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
-
-// Problem with this: Can't select stuff very well with the snap grid on.
-// Completely screws things up, as sometimes things don't fall on the grid.
-/*
-So, how to fix this? Have the Object check itself?
-Maybe we can fix this by having the initial point not be snapped, but when there's
-a drag, we substitute the snapped point 'oldPoint' which the Object keeps track of
-internally to know how far it was dragged...
-
-Now we do... :-/
-*/
-#if 0
-               if (Object::snapToGrid)
-                       point = Object::SnapPointToGrid(point);
-#endif
-
                collided = document.Collided(point);
 
+               // Do an update if collided with at least *one* object in the document
                if (collided)
-                       update();       // Do an update if collided with at least *one* object in the document
+                       update();
 
                if (toolAction)
                {
@@ -388,7 +374,8 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
                point = Vector(event->x(), event->y());
                // Since we're using Qt coords for scrolling, we have to adjust them here to
                // conform to Cartesian coords, since the origin is using Cartesian. :-)
-               Vector delta(point, oldPoint);
+//             Vector delta(point, oldPoint);
+               Vector delta(oldPoint, point);
                delta /= Painter::zoom;
                delta.y = -delta.y;
                Painter::origin -= delta;
@@ -457,26 +444,45 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event)
 }
 
 
-void DrawingView::keyPressEvent(QKeyEvent * event)
+void DrawingView::wheelEvent(QWheelEvent * event)
 {
-       if (toolAction)
-       {
-               bool needUpdate = toolAction->KeyDown(event->key());
+       double zoomFactor = 1.25;
+       QSize sizeWin = size();
+       Vector center(sizeWin.width() / 2.0, sizeWin.height() / 2.0);
+       center = Painter::QtToCartesianCoords(center);
 
-               if (needUpdate)
-                       update();
+       // This is not centering for some reason. Need to figure out why. :-/
+       if (event->delta() > 0)
+       {
+               Vector newOrigin = center - ((center - Painter::origin) / zoomFactor);
+               Painter::origin = newOrigin;
+               Painter::zoom *= zoomFactor;
        }
+       else
+       {
+               Vector newOrigin = center + ((-center + Painter::origin) * zoomFactor);
+               Painter::origin = newOrigin;
+               Painter::zoom /= zoomFactor;
+       }
+
+//     Object::gridSpacing = gridPixels / Painter::zoom;
+//     UpdateGridBackground();
+       SetGridSize(Object::gridSpacing * Painter::zoom);
+       update();
+//     zoomIndicator->setText(QString("Grid: %1\", BU: Inch").arg(Object::gridSpacing));
 }
 
 
-void DrawingView::keyReleaseEvent(QKeyEvent * event)
+void DrawingView::keyPressEvent(QKeyEvent * event)
 {
        if (toolAction)
-       {
-               bool needUpdate = toolAction->KeyReleased(event->key());
+               toolAction->KeyDown(event->key());
+}
 
-               if (needUpdate)
-                       update();
-       }
+
+void DrawingView::keyReleaseEvent(QKeyEvent * event)
+{
+       if (toolAction)
+               toolAction->KeyReleased(event->key());
 }