]> Shamusworld >> Repos - architektonas/blobdiff - src/drawingview.cpp
Readded click to add dimension to object (for Line).
[architektonas] / src / drawingview.cpp
index ab9f565fc2423bcb4f5a0c2b076eeaf63e64f500..acfad409c2f22e685e49c646de30c355039684df 100644 (file)
@@ -19,6 +19,7 @@
 //
 // STILL TO BE DONE:
 //
+// - Lots of stuff
 //
 
 // Uncomment this for debugging...
 
 #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);
 
-//     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);
@@ -125,14 +121,7 @@ need a thickness parameter similar to the "size" param for dimensions. (And now
 we do! :-)
 
 */
-       SetGridSize(12);
-}
-
-
-void DrawingView::SetRotateToolActive(bool state/*= true*/)
-{
-       rotateTool = state;
-       update();
+       SetGridSize(12);        // This is in pixels
 }
 
 
@@ -143,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()));
        }
 }
 
@@ -168,7 +158,8 @@ void DrawingView::SetGridSize(uint32_t size)
        pmp.end();
 
        // Set up new BG brush & zoom level (pixels per base unit)
-       Painter::zoom = gridPixels / gridSpacing;
+//     Painter::zoom = gridPixels / gridSpacing;
+       Painter::zoom = gridPixels / Object::gridSpacing;
        UpdateGridBackground();
 }
 
@@ -264,6 +255,19 @@ void DrawingView::AddNewObjectToDocument(Object * object)
 }
 
 
+void DrawingView::HandleActionUpdate(void)
+{
+       update();
+}
+
+
+void DrawingView::SetCurrentLayer(int layer)
+{
+       Object::currentLayer = layer;
+//printf("DrawingView::CurrentLayer = %i\n", layer);
+}
+
+
 QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event)
 {
        // This is undoing the transform, e.g. going from client coords to local coords.
@@ -282,26 +286,6 @@ QPoint DrawingView::GetAdjustedClientPosition(int x, int y)
 }
 
 
-//
-// This looks strange, but it's really quite simple: We want a point that's
-// more than half-way to the next grid point to snap there while conversely we
-// want a point that's less than half-way to to the next grid point then snap
-// to the one before it. So we add half of the grid spacing to the point, then
-// divide by it so that we can remove the fractional part, then multiply it
-// back to get back to the correct answer.
-//
-Vector DrawingView::SnapPointToGrid(Vector point)
-{
-       point += gridSpacing / 2.0;             // *This* adds to Z!!!
-       point /= gridSpacing;
-       point.x = floor(point.x);//need to fix this for negative numbers...
-       point.y = floor(point.y);
-       point.z = 0;                                    // Make *sure* Z doesn't go anywhere!!!
-       point *= gridSpacing;
-       return point;
-}
-
-
 void DrawingView::paintEvent(QPaintEvent * /*event*/)
 {
        QPainter qtPainter(this);
@@ -310,35 +294,18 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
        if (useAntialiasing)
                qtPainter.setRenderHint(QPainter::Antialiasing);
 
-//     Painter::screenSize = Vector(size().width(), size().height());
        Object::SetViewportHeight(size().height());
 
        // Draw coordinate axes
-
        painter.SetPen(QPen(Qt::blue, 1.0, Qt::DotLine));
        painter.DrawLine(0, -16384, 0, 16384);
        painter.DrawLine(-16384, 0, 16384, 0);
 
-       // Draw supplemental (tool related) points
-// NOTE that this can be done as an action!
-// In that case, we would need access to the document...
-// [We can do that by making the document a class object...]
-       if (rotateTool)
-       {
-               painter.SetPen(QPen(QColor(0, 200, 0), 2.0, Qt::SolidLine));
-               painter.DrawLine(rx - 10, ry, rx + 10, ry);
-               painter.DrawLine(rx, ry - 10, rx, ry + 10);
-       }
-
-// Maybe we can make the grid into a background brush instead, and let Qt deal
-// with it??? YES!!
-
        // The top level document takes care of rendering for us...
        document.Draw(&painter);
 
        if (toolAction)
        {
-//             painter.SetPen(QPen(Qt::green, 1.0, Qt::DashLine));
                painter.SetPen(QPen(QColor(200, 100, 0, 255), 1.0, Qt::DashLine));
                painter.DrawCrosshair(oldPoint);
                toolAction->Draw(&painter);
@@ -346,9 +313,7 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
 
        if (Object::selectionInProgress)
        {
-//             painter.SetPen(QPen(Qt::green, 1.0, Qt::SolidLine));
                painter.SetPen(QPen(QColor(255, 127, 0, 255)));
-//             painter.SetBrush(QBrush(Qt::NoBrush));
                painter.SetBrush(QBrush(QColor(255, 127, 0, 100)));
                painter.DrawRect(Object::selection);
        }
@@ -367,19 +332,19 @@ 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.
-               if (Object::snapToGrid)
-                       point = SnapPointToGrid(point);
-
                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)
+               {
+                       if (Object::snapToGrid)
+                               point = Object::SnapPointToGrid(point);
+
                        toolAction->MouseDown(point);
+               }
 
                // Didn't hit any object and not using a tool, so do a selection rectangle
                if (!(collided || toolAction))
@@ -409,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;
@@ -421,25 +387,11 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
        }
 
        // Grid processing...
-#if 1
        if ((event->buttons() & Qt::LeftButton) && Object::snapToGrid)
        {
-#if 0
-               point += gridSpacing / 2.0;                                     // *This* adds to Z!!!
-               point /= gridSpacing;
-//200% is ok, gridSpacing = 6 in this case...
-//won't run into problems until gridSpacing = 1.5 (zoom = 800%)
-//run into problems with this approach: when zoom level is 200% this truncates to
-//integers, which is *not* what's wanted here...
-               point.x = floor(point.x);//need to fix this for negative numbers...
-               point.y = floor(point.y);
-               point.z = 0;                                                            // Make *sure* Z doesn't go anywhere!!!
-               point *= gridSpacing;
-#else
-               point = SnapPointToGrid(point);
-#endif
+               point = Object::SnapPointToGrid(point);
        }
-#endif
+
        oldPoint = point;
 //we should keep track of the last point here and only pass this down *if* the point
 //changed...
@@ -452,7 +404,7 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
        {
                if (Object::snapToGrid)
                {
-                       point = SnapPointToGrid(point);
+                       point = Object::SnapPointToGrid(point);
                        oldPoint = point;
                }
 
@@ -491,3 +443,46 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event)
        }
 }
 
+
+void DrawingView::wheelEvent(QWheelEvent * event)
+{
+       double zoomFactor = 1.25;
+       QSize sizeWin = size();
+       Vector center(sizeWin.width() / 2.0, sizeWin.height() / 2.0);
+       center = Painter::QtToCartesianCoords(center);
+
+       // 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::keyPressEvent(QKeyEvent * event)
+{
+       if (toolAction)
+               toolAction->KeyDown(event->key());
+}
+
+
+void DrawingView::keyReleaseEvent(QKeyEvent * event)
+{
+       if (toolAction)
+               toolAction->KeyReleased(event->key());
+}
+