]> Shamusworld >> Repos - architektonas/commitdiff
Rendering for Line, Circle, and Arc work.
authorShamus Hammons <jlhamm@acm.org>
Fri, 17 Apr 2015 13:47:00 +0000 (08:47 -0500)
committerShamus Hammons <jlhamm@acm.org>
Fri, 17 Apr 2015 13:47:00 +0000 (08:47 -0500)
src/applicationwindow.cpp
src/drawingview.cpp
src/drawingview.h
src/global.cpp
src/global.h
src/painter.cpp
src/painter.h
src/structs.h

index 06ec68ce89a6ad4857843b53f53fc13e53320a8b..2049a097ca354b54e69297fcbc48d91753b7d528 100644 (file)
@@ -344,15 +344,15 @@ when zooming in, new origin will be (xCenter - origin.x) / 2, (yCenter - origin.
 //printf("Zoom in... Center=%.2f,%.2f; ", center.x, center.y);
        center = Painter::QtToCartesianCoords(center);
 //printf("(%.2f,%.2f); origin=%.2f,%.2f; ", center.x, center.y, Painter::origin.x, Painter::origin.y);
-       Vector newOrigin = center - ((center - Painter::origin) / zoomFactor);
+       Vector newOrigin = center - ((center - Global::origin) / zoomFactor);
 //printf("newOrigin=%.2f,%.2f;\n", newOrigin.x, newOrigin.y);
-       Painter::origin = newOrigin;
+       Global::origin = newOrigin;
 
 //printf("Zoom in... level going from %02f to ", Painter::zoom);
        // This just zooms leaving origin intact... should zoom in at the current
        // center! [DONE]
-       Painter::zoom *= zoomFactor;
-       Global::gridSpacing = drawing->gridPixels / Painter::zoom;
+       Global::zoom *= zoomFactor;
+       Global::gridSpacing = drawing->gridPixels / Global::zoom;
        drawing->UpdateGridBackground();
        drawing->update();
 
@@ -384,14 +384,14 @@ x 2 = (-426, -301)
 //printf("(%.2f,%.2f); origin=%.2f,%.2f; ", center.x, center.y, Painter::origin.x, Painter::origin.y);
 //     Vector newOrigin = (center - Painter::origin) * zoomFactor;
 //     Vector newOrigin = center - (Painter::origin * zoomFactor);
-       Vector newOrigin = center + ((Painter::origin - center) * zoomFactor);
+       Vector newOrigin = center + ((Global::origin - center) * zoomFactor);
 //printf("newOrigin=%.2f,%.2f;\n", newOrigin.x, newOrigin.y);
-       Painter::origin = newOrigin;
+       Global::origin = newOrigin;
 //printf("Zoom out...\n");
        // This just zooms leaving origin intact... should zoom out at the current
        // center! [DONE]
-       Painter::zoom /= zoomFactor;
-       Global::gridSpacing = drawing->gridPixels / Painter::zoom;
+       Global::zoom /= zoomFactor;
+       Global::gridSpacing = drawing->gridPixels / Global::zoom;
        drawing->UpdateGridBackground();
        drawing->update();
 
@@ -646,7 +646,7 @@ void ApplicationWindow::HandleGridSizeInBaseUnits(QString text)
 //     drawing->gridSpacing = value;
 //     Painter::zoom = drawing->gridPixels / drawing->gridSpacing;
        Global::gridSpacing = value;
-       Painter::zoom = drawing->gridPixels / Global::gridSpacing;
+       Global::zoom = drawing->gridPixels / Global::gridSpacing;
        drawing->UpdateGridBackground();
        drawing->update();
 }
index 5fe3841cab9a35c8cfdb5687ebdd8cd279962aa8..e93161a5a7235c544f93f0ae64b85d37e56c8c5f 100644 (file)
 #include "drawingview.h"
 
 #include <stdint.h>
-#include "mathconstants.h"
-
-//#include "arc.h"
-//#include "circle.h"
-//#include "dimension.h"
-//#include "geometry.h"
-//#include "line.h"
 #include "global.h"
+#include "mathconstants.h"
 #include "painter.h"
+#include "structs.h"
 
 
 #define BACKGROUND_MAX_SIZE    512
@@ -78,6 +73,19 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
        // Alternate way to do the above...
        line->SetDimensionOnLine();
 #endif
+#else
+       Line * line = new Line;//(Vector(5, 5), Vector(50, 40), &document);
+       line->p1 = Vector(5, 5);
+       line->p2 = Vector(50, 40);
+       line->type = OTLine;
+       line->thickness = 1.0;
+       document.objects.push_back(line);
+       document.objects.push_back(new Line(Vector(50, 40), Vector(10, 83)));
+       document.objects.push_back(new Line(Vector(10, 83), Vector(17, 2)));
+       document.objects.push_back(new Circle(Vector(100, 100), 36));
+       document.objects.push_back(new Circle(Vector(50, 150), 49));
+       document.objects.push_back(new Arc(Vector(300, 300), 32, PI / 4.0, PI * 1.3)),
+       document.objects.push_back(new Arc(Vector(200, 200), 60, PI / 2.0, PI * 1.5));
 #endif
 
 /*
@@ -89,10 +97,10 @@ arbitrary 12 pixels) to anything we want (within reason, of course :-).
 
 The drawing enforces the grid spacing through the drawing->gridSpacing variable.
 
-       drawing->gridSpacing = 12.0 / Painter::zoom;
+       drawing->gridSpacing = 12.0 / Global::zoom;
 
-Painter::zoom is the zoom factor for the drawing, and all mouse clicks are
-translated to Cartesian coordinates through this. (Initially, Painter::zoom is
+Global::zoom is the zoom factor for the drawing, and all mouse clicks are
+translated to Cartesian coordinates through this. (Initially, Global::zoom is
 set to 1.0. SCREEN_ZOOM is set to 1.0/4.0.)
 
 Really, the 100% zoom level can be set at *any* zoom level, it's more of a
@@ -112,13 +120,13 @@ inch regardless of the zoom level a piece of text can be larger or smaller than
 this. Maybe this is the case for having a base unit and basing point sizes off
 of that.
 
-Here's what's been figured out. Painter::zoom is simply the ratio of pixels to
+Here's what's been figured out. Global::zoom is simply the ratio of pixels to
 base units. What that means is that if you have a 12px grid with a 6" grid size
-(& base unit of "inches"), Painter::zoom becomes 12px / 6" = 2.0 px/in.
+(& base unit of "inches"), Global::zoom becomes 12px / 6" = 2.0 px/in.
 
 Dimensions now have a "size" parameter to set their absolute size in relation
 to the base unit. ATM, the arrows are drawn in pixels, but also scaled by
-Painter::zoom *and* size. Same with the dimension text; it's drawn at 10pt and
+Global::zoom *and* size. Same with the dimension text; it's drawn at 10pt and
 scaled the same way as the arrowheads.
 
 Need a way to scale line widths as well. :-/ Shouldn't be too difficult, just
@@ -166,7 +174,7 @@ void DrawingView::SetGridSize(uint32_t size)
 
        // Set up new BG brush & zoom level (pixels per base unit)
 //     Painter::zoom = gridPixels / gridSpacing;
-       Painter::zoom = gridPixels / Global::gridSpacing;
+       Global::zoom = gridPixels / Global::gridSpacing;
        UpdateGridBackground();
 }
 
@@ -310,6 +318,8 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
 
        // The top level document takes care of rendering for us...
 //     document.Draw(&painter);
+       // Not any more it doesn't...
+       RenderObjects(&painter, &document);
 
 #if 0
        if (toolAction)
@@ -331,9 +341,45 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
 }
 
 
+void DrawingView::RenderObjects(Painter * p, Container * c)
+{
+       std::vector<void *>::iterator i;
+
+       for(i=c->objects.begin(); i!=c->objects.end(); i++)
+       {
+               Object * obj = (Object *)(*i);
+               p->SetPen(QPen(Qt::black, 1.0 * Global::zoom * obj->thickness, Qt::SolidLine));
+
+               switch (obj->type)
+               {
+               case OTLine:
+               {
+                       Line * l = (Line *)obj;
+                       p->DrawLine(l->p1, l->p2);
+                       break;
+               }
+               case OTCircle:
+               {
+                       Circle * ci = (Circle *)obj;
+                       p->DrawEllipse(ci->p1, ci->radius, ci->radius);
+                       break;
+               }
+               case OTArc:
+               {
+                       Arc * a = (Arc *)obj;
+                       p->DrawArc(a->p1, a->radius, a->angle1, a->angle2);
+                       break;
+               }
+               default:
+                       break;
+               }
+       }
+}
+
+
 void DrawingView::resizeEvent(QResizeEvent * /*event*/)
 {
-       Painter::screenSize = Vector(size().width(), size().height());
+       Global::screenSize = Vector(size().width(), size().height());
        UpdateGridBackground();
 }
 
@@ -399,9 +445,9 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
                // 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(oldPoint, point);
-               delta /= Painter::zoom;
+               delta /= Global::zoom;
                delta.y = -delta.y;
-               Painter::origin -= delta;
+               Global::origin -= delta;
 
                UpdateGridBackground();
                update();
@@ -560,21 +606,21 @@ void DrawingView::wheelEvent(QWheelEvent * event)
        // 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;
+               Vector newOrigin = center - ((center - Global::origin) / zoomFactor);
+               Global::origin = newOrigin;
+               Global::zoom *= zoomFactor;
        }
        else
        {
-               Vector newOrigin = center + ((-center + Painter::origin) * zoomFactor);
-               Painter::origin = newOrigin;
-               Painter::zoom /= zoomFactor;
+               Vector newOrigin = center + ((-center + Global::origin) * zoomFactor);
+               Global::origin = newOrigin;
+               Global::zoom /= zoomFactor;
        }
 
 #if 1
 //     Global::gridSpacing = gridPixels / Painter::zoom;
 //     UpdateGridBackground();
-       SetGridSize(Global::gridSpacing * Painter::zoom);
+       SetGridSize(Global::gridSpacing * Global::zoom);
        update();
 //     zoomIndicator->setText(QString("Grid: %1\", BU: Inch").arg(Global::gridSpacing));
 #endif
index 5cbd53b8952411808ec7ebb9622d72251613df17..3531291ac5bb4b1f51d84b65068032ebd6394171 100644 (file)
@@ -7,6 +7,8 @@
 //#include "container.h"
 #include "structs.h"
 
+class Painter;
+
 class DrawingView: public QWidget
 {
        Q_OBJECT
@@ -19,6 +21,7 @@ class DrawingView: public QWidget
                void SetGridSize(uint32_t);
                void UpdateGridBackground(void);
                Point SnapPointToGrid(Point);
+               void RenderObjects(Painter *, Container *);
 
        public slots:
                void AddNewObjectToDocument(Object *);
index 52569532521599d9ed4250039431e05619286b62..296c6a3867689879608a873691b14d87e727bed8 100644 (file)
@@ -23,3 +23,9 @@ double Global::gridSpacing;
 int Global::currentLayer = 0;
 Point Global::snapPoint;
 bool Global::snapPointIsValid = false;
+uint32_t Global::objectID = 1;
+
+Vector Global::origin(-10.0, -10.0);
+double Global::zoom = 1.0;
+Vector Global::screenSize(200.0, 200.0);
+
index 33fcd7a44e87f99ea27fbbabb851d4a7af622c91..a98b9ac815436b270ed668fad87d3b4d7b5f581e 100644 (file)
@@ -4,6 +4,7 @@
 // Global variable class. Note that all vars are class vars, so we don't have
 // to do any instantiation shite.
 
+#include <stdint.h>
 #include <QRectF>
 #include "vector.h"
 
@@ -29,6 +30,7 @@ class Global
                static bool snapToGrid;
                static bool ignoreClicks;
                static bool dontMove;
+               static uint32_t objectID;
 
                // Coming soon...
                static Point origin;
index bdac361f59b64d7f68b2aca874ac169c0d92837d..35528faf9887ce5930306cb6f6d2c26465392c9a 100644 (file)
 //
 
 #include "painter.h"
-
-#include "mathconstants.h"
-//#include "object.h"
 #include "global.h"
+#include "mathconstants.h"
 
 
 // Set class variable defaults
-Vector Painter::origin(-10.0, -10.0);
-double Painter::zoom = 1.0;
-Vector Painter::screenSize(200.0, 200.0);
+//Vector Painter::origin(-10.0, -10.0);
+//double Painter::zoom = 1.0;
+//Vector Painter::screenSize(200.0, 200.0);
 
 
 Painter::Painter(QPainter * p/*= NULL*/): painter(p)
@@ -39,7 +37,7 @@ Vector Painter::CartesianToQtCoords(Vector v)
 {
        // Convert regular Cartesian coordinates to the inverted Y-axis Qt coordinates
        // at the current origin and zoom level.
-       return Vector((v.x - origin.x) * zoom, screenSize.y - ((v.y - origin.y) * zoom));
+       return Vector((v.x - Global::origin.x) * Global::zoom, Global::screenSize.y - ((v.y - Global::origin.y) * Global::zoom));
 }
 
 
@@ -47,7 +45,7 @@ Vector Painter::QtToCartesianCoords(Vector v)
 {
        // Convert screen location, with inverted Y-axis coordinates, to regular
        // Cartesian coordinates at the current zoom level.
-       return Vector((v.x / zoom) + origin.x, ((screenSize.y - v.y) / zoom) + origin.y);
+       return Vector((v.x / Global::zoom) + Global::origin.x, ((Global::screenSize.y - v.y) / Global::zoom) + Global::origin.y);
 /*
 How to do it:
 
@@ -121,19 +119,19 @@ void Painter::DrawAngledText(Vector center, double angle, QString text, double s
        // We may need this stuff... If dimension text is large enough.
 //     int textWidth = QFontMetrics(painter->font()).width(text);
 //     int textHeight = QFontMetrics(painter->font()).height();
-       QRectF textBox(-100.0 * zoom * size, -100.0 * zoom * size, 200.0 * zoom * size, 200.0 * zoom * size);   // x, y, w, h; x/y = upper left corner
+       QRectF textBox(-100.0 * Global::zoom * size, -100.0 * Global::zoom * size, 200.0 * Global::zoom * size, 200.0 * Global::zoom * size);   // x, y, w, h; x/y = upper left corner
 
        // This is in pixels. Might not render correctly at all zoom levels.
        // Need to figure out if dimensions are always rendered at one size
        // regardless of zoom, or if they have a definite size, and are thus
        // zoomable.
-       float yOffset = -12.0 * zoom * size;
+       float yOffset = -12.0 * Global::zoom * size;
 
        // Fix text so it isn't upside down...
        if ((angle > PI * 0.5) && (angle < PI * 1.5))
        {
                angle += PI;
-               yOffset = 12.0 * zoom * size;
+               yOffset = 12.0 * Global::zoom * size;
        }
 
        textBox.translate(0, yOffset);
@@ -151,7 +149,7 @@ void Painter::DrawArc(Vector center, double radius, double startAngle, double sp
 {
        center = CartesianToQtCoords(center);
        // Need to multiply scalar quantities by the zoom factor as well...
-       radius *= zoom;
+       radius *= Global::zoom;
        QRectF rectangle(QPointF(center.x - radius, center.y - radius),
                QPointF(center.x + radius, center.y + radius));
        int angle1 = (int)(startAngle * RADIANS_TO_DEGREES * 16.0);
@@ -164,7 +162,7 @@ void Painter::DrawEllipse(Vector center, double axis1, double axis2)
 {
        // Need to multiply scalar quantities by the zoom factor as well...
        center = CartesianToQtCoords(center);
-       painter->drawEllipse(QPointF(center.x, center.y), axis1 * zoom, axis2 * zoom);
+       painter->drawEllipse(QPointF(center.x, center.y), axis1 * Global::zoom, axis2 * Global::zoom);
 }
 
 
@@ -337,8 +335,8 @@ void Painter::DrawCrosshair(Vector point)
                return;
 
        Vector screenPoint = CartesianToQtCoords(point);
-       painter->drawLine(0, screenPoint.y, screenSize.x, screenPoint.y);
-       painter->drawLine(screenPoint.x, 0, screenPoint.x, screenSize.y);
+       painter->drawLine(0, screenPoint.y, Global::screenSize.x, screenPoint.y);
+       painter->drawLine(screenPoint.x, 0, screenPoint.x, Global::screenSize.y);
 }
 
 
index 178b7dbca605ad772f7b9d9c2f2ffe792c09e2e1..af591b8e0d59740513151dfe1c232b112ef20644 100644 (file)
@@ -41,9 +41,9 @@ class Painter
 
        public:
                // Class variables
-               static Vector origin;           // The window origin, not location of the origin
-               static double zoom;                     // Window zoom factor
-               static Vector screenSize;       // Width & height of the window we're drawing on
+//             static Vector origin;           // The window origin, not location of the origin
+//             static double zoom;                     // Window zoom factor
+//             static Vector screenSize;       // Width & height of the window we're drawing on
 
        private:
                QPainter * painter;
index b9f6e9b9bcf78a32c10ddbb269f854c759b14d75..a6b52ccb69a5fa3f4b70d3b58e65bba10e10eaf7 100644 (file)
@@ -4,25 +4,39 @@
 #include <stdint.h>
 #include <vector>
 #include <string>
+#include "global.h"
 #include "vector.h"
 
+enum ObjectType { OTNone, OTObject, OTLine, OTCircle, OTArc, OTDimension, OTEllipse, OTContainer, OTSpline };
+
 #define OBJECT_COMMON \
-       int type;       \
-       uint32_t id;    \
-       int layer;      \
-       uint32_t color; \
-       float thickness;
+       int type;         \
+       uint32_t id;      \
+       int layer;        \
+       uint32_t color;   \
+       float thickness;  \
+       int style;        \
+       bool selected;
 
 struct Line {
        OBJECT_COMMON;
        Point p1;
        Point p2;
+
+       Line(): type(OTLine), id(Global::objectID++) {}
+       Line(Vector pt1, Vector pt2, float th = 1.0, uint32_t c = 0, int l = 0):
+               type(OTLine), id(Global::objectID++), layer(l), color(c), thickness(th),
+               style(0), selected(false), p1(pt1), p2(pt2) {}
 };
 
 struct Circle {
        OBJECT_COMMON;
        Point p1;
        double radius;
+
+       Circle(Vector pt1, double r, float th = 1.0, uint32_t c = 0, int l = 0):
+               type(OTCircle), id(Global::objectID++), layer(l), color(c), thickness(th),
+               style(0), selected(false), p1(pt1), radius(r) {}
 };
 
 struct Arc {
@@ -31,6 +45,10 @@ struct Arc {
        double radius;
        double angle1;
        double angle2;
+
+       Arc(Vector pt1, double r, double a1, double a2, float th = 1.0, uint32_t c = 0, int l = 0):
+               type(OTArc), id(Global::objectID++), layer(l), color(c), thickness(th),
+               style(0), selected(false), p1(pt1), radius(r), angle1(a1), angle2(a2) {}
 };
 
 struct Text {