]> Shamusworld >> Repos - architektonas/commitdiff
Initial stab at proper grid display using background image.
authorShamus Hammons <jlhamm@acm.org>
Thu, 9 Feb 2012 23:14:23 +0000 (23:14 +0000)
committerShamus Hammons <jlhamm@acm.org>
Thu, 9 Feb 2012 23:14:23 +0000 (23:14 +0000)
src/applicationwindow.cpp
src/applicationwindow.h
src/circle.cpp
src/drawingview.cpp
src/drawingview.h
src/object.cpp
src/object.h
src/painter.cpp
src/painter.h

index 66f9edc3b9a2a1845063b770d588c378c5ed8b5f..92c0547f773281dcc7e516db5f4eba1a88f8f408 100644 (file)
@@ -80,6 +80,11 @@ void ApplicationWindow::closeEvent(QCloseEvent * event)
 //{
 //}
 
+void ApplicationWindow::SnapToGridTool(void)
+{
+       Object::SetSnapMode(snapToGridAct->isChecked());
+}
+
 void ApplicationWindow::FixAngle(void)
 {
        Object::SetFixedAngle(fixAngleAct->isChecked());
@@ -163,6 +168,7 @@ when zooming in, new origin will be (xCenter - origin.x) / 2, (yCenter - origin.
        // This just zooms leaving origin intact... should zoom in at the current center! [DONE]
        Painter::zoom *= zoomFactor;
        zoomIndicator->setText(QString("Zoom: %1%").arg(Painter::zoom * 100.0 * SCREEN_ZOOM));
+       drawing->UpdateGridBackground();
        drawing->update();
 }
 
@@ -196,6 +202,7 @@ x 2 = (-426, -301)
        // This just zooms leaving origin intact... should zoom out at the current center! [DONE]
        Painter::zoom /= zoomFactor;
        zoomIndicator->setText(QString("Zoom: %1%").arg(Painter::zoom * 100.0 * SCREEN_ZOOM));
+       drawing->UpdateGridBackground();
        drawing->update();
 }
 
@@ -256,6 +263,9 @@ void ApplicationWindow::CreateActions(void)
                QIcon(":/res/quit.png"), QKeySequence(tr("Ctrl+q")));
        connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
+       snapToGridAct = CreateAction(tr("&Snap To Grid"), tr("Snap To Grid"), tr("Snaps mouse cursor to the visible grid when moving/creating objects."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("S,G")), true);
+       connect(snapToGridAct, SIGNAL(triggered()), this, SLOT(SnapToGridTool()));
+
        fixAngleAct = CreateAction(tr("Fix &Angle"), tr("Fix Angle"), tr("Fixes the angle of an object."),
                QIcon(":/res/fix-angle.png"), QKeySequence(tr("F,A")), true);
        connect(fixAngleAct, SIGNAL(triggered()), this, SLOT(FixAngle()));
@@ -367,6 +377,7 @@ void ApplicationWindow::CreateMenus(void)
        menu->addAction(zoomOutAct);
 
        menu = menuBar()->addMenu(tr("&Edit"));
+       menu->addAction(snapToGridAct);
        menu->addAction(fixAngleAct);
        menu->addAction(fixLengthAct);
        menu->addAction(rotateAct);
@@ -395,10 +406,12 @@ void ApplicationWindow::CreateToolbars(void)
        toolbar->addAction(zoomOutAct);
 
        toolbar = addToolBar(tr("Edit"));
+       toolbar->addAction(snapToGridAct);
        toolbar->addAction(fixAngleAct);
        toolbar->addAction(fixLengthAct);
        toolbar->addAction(rotateAct);
        toolbar->addAction(deleteAct);
+       toolbar->addSeparator();
        toolbar->addAction(addLineAct);
        toolbar->addAction(addCircleAct);
        toolbar->addAction(addArcAct);
@@ -411,6 +424,7 @@ void ApplicationWindow::ReadSettings(void)
        QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
        QSize size = settings.value("size", QSize(400, 400)).toSize();
        drawing->useAntialiasing = settings.value("useAntialiasing", true).toBool();
+       snapToGridAct->setChecked(settings.value("snapToGrid", true).toBool());
        resize(size);
        move(pos);
 //     pos = settings.value("charWndPos", QPoint(0, 0)).toPoint();
@@ -424,6 +438,7 @@ void ApplicationWindow::WriteSettings(void)
        settings.setValue("pos", pos());
        settings.setValue("size", size());
        settings.setValue("useAntialiasing", drawing->useAntialiasing);
+       settings.setValue("snapToGrid", snapToGridAct->isChecked());
 //     settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
 //     settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
 }
index c220548c7d8f98baed78394f2b98ff41ed5cd668..c9551d50b1c9382d62de42275662c1146a6b9044 100644 (file)
@@ -24,6 +24,7 @@ class ApplicationWindow: public QMainWindow
 
        private slots:
 //             void FileOpen();
+               void SnapToGridTool(void);
                void FixAngle(void);
                void FixLength(void);
                void DeleteTool(void);
@@ -76,6 +77,7 @@ class ApplicationWindow: public QMainWindow
                QAction * rotateAct;
                QAction * zoomInAct;
                QAction * zoomOutAct;
+               QAction * snapToGridAct;
 };
 
 #endif // __APPLICATIONWINDOW_H__
index 5750b1f0ecabb66ac2929f68cb4b6543ea035bbf..8e5f6cddc7fc3937e116d25340e8af1ffc4738e0 100644 (file)
@@ -35,6 +35,12 @@ Circle::~Circle()
        else
                painter->SetPen(QPen(Qt::black, 1.0, Qt::SolidLine));
 
+       // Hatch/Fill...
+//     QBrush brush(Qt::DiagCrossPattern);
+//     brush.setColor(QColor(255, 255, 0));
+//     painter->SetBrush(brush);
+       painter->SetBrush(QBrush(Qt::NoBrush));
+
        // Draw the object...
        painter->DrawEllipse(position, radius, radius);
 
index eccb0b170d2647c421d9269ae8701aad5aa199b5..0d62be16552d6eafc26e70268b8b085faa6b2ace 100644 (file)
 
 // FIXED:
 //
+// - Redo rendering code to *not* use Qt's transform functions, as they are tied
+//   to a left-handed system and we need a right-handed one. [DONE]
 //
 // STILL TO BE DONE:
 //
-// - Redo rendering code to *not* use Qt's transform functions, as they are tied
-//   to a left-handed system and we need a right-handed one.
 //
 
 // Uncomment this for debugging...
@@ -43,6 +43,7 @@
 DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
        // The value in the settings file will override this.
        useAntialiasing(true),
+       gridBackground(256, 256),
        scale(1.0), offsetX(-10), offsetY(-10),
        document(Vector(0, 0)),
        gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0),
@@ -74,6 +75,30 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
 #endif
 //     connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
 //             SLOT(AddNewObjectToDocument(Object *)));
+//This works, now how to scroll it???
+//     QPixmap pm(256, 256);
+       QPainter pmp(&gridBackground);
+#if 0
+       pmp.fillRect(0, 0, 256, 256, Qt::lightGray);
+
+       pmp.fillRect(0,   64,  64, 64, Qt::darkGray);
+       pmp.fillRect(0,   192, 64, 64, Qt::darkGray);
+       pmp.fillRect(64,  0,   64, 64, Qt::darkGray);
+       pmp.fillRect(64,  128, 64, 64, Qt::darkGray);
+       pmp.fillRect(128, 64,  64, 64, Qt::darkGray);
+       pmp.fillRect(128, 192, 64, 64, Qt::darkGray);
+       pmp.fillRect(192, 0,   64, 64, Qt::darkGray);
+       pmp.fillRect(192, 128, 64, 64, Qt::darkGray);
+#else
+       pmp.fillRect(0, 0, 256, 256, QColor(240, 240, 240));
+       pmp.setPen(QPen(QColor(190, 190, 255), 2.0, Qt::SolidLine));
+       for(int i=0; i<255; i+=12)
+               pmp.drawLine(i, 0, i, 255);
+       for(int i=0; i<255; i+=12)
+               pmp.drawLine(0, i, 255, i);
+#endif
+       pmp.end();
+       UpdateGridBackground();
 }
 
 void DrawingView::SetRotateToolActive(bool state/*= true*/)
@@ -128,6 +153,45 @@ void DrawingView::SetAddCircleToolActive(bool state/*= true*/)
 //printf("DrawingView::SetAddCircleToolActive(). toolAction=%08X\n", toolAction);
 }
 
+void DrawingView::UpdateGridBackground(void)
+{
+#if 0
+// Shift the background to match our scrolling...
+QBrush newBrush = *backgroundBrush;
+//QMatrix brushMatrix = backgroundBrush->matrix();
+QTransform brushMatrix = backgroundBrush->transform();
+brushMatrix.translate(Painter::origin.x, Painter::origin.y);
+//brushMatrix.translate(15.0, 15.0);
+//backgroundBrush->setMatrix(brushMatrix);
+//backgroundBrush->setTransform(brushMatrix);
+newBrush.setTransform(brushMatrix);
+QPalette pal = palette();
+//pal.setBrush(backgroundRole(), *backgroundBrush);
+pal.setBrush(backgroundRole(), newBrush);
+setPalette(pal);
+//Background painting does not honor the transformation matrix (either one)...
+// So...
+#else
+//was: 128
+#define BG_BRUSH_SPAN 72
+       // Transform the origin to Qt coordinates
+       Vector pixmapOrigin = Painter::CartesianToQtCoords(Vector());
+       int x = (int)pixmapOrigin.x;
+       int y = (int)pixmapOrigin.y;
+       // Problem with mod 128: Negative numbers screw it up... [FIXED]
+       x = (x < 0 ? 0 : BG_BRUSH_SPAN - 1) - (x % BG_BRUSH_SPAN);
+       y = (y < 0 ? 0 : BG_BRUSH_SPAN - 1) - (y % BG_BRUSH_SPAN);
+
+       // Here we grab a section of the bigger pixmap, so that the background
+       // *looks* like it's scrolling...
+       QPixmap pm = gridBackground.copy(x, y, BG_BRUSH_SPAN, BG_BRUSH_SPAN);
+       QPalette pal = palette();
+       pal.setBrush(backgroundRole(), QBrush(pm));
+       setAutoFillBackground(true);
+       setPalette(pal);
+#endif
+}
+
 void DrawingView::AddNewObjectToDocument(Object * object)
 {
        if (object)
@@ -160,6 +224,9 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
        QPainter qtPainter(this);
        Painter painter(&qtPainter);
 
+//     qtPainter.setBackground(QBrush(Qt::DiagCrossPattern));
+//     qtPainter.setBackgroundMode(Qt::OpaqueMode);
+
        if (useAntialiasing)
                qtPainter.setRenderHint(QPainter::Antialiasing);
 
@@ -245,6 +312,8 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
                delta /= Painter::zoom;
                delta.y = -delta.y;
                Painter::origin -= delta;
+
+               UpdateGridBackground();
                update();
                oldPoint = point;
                return;
index f11bf152260d2d86a842dfaabdb926700fe6686f..98b34b5681fab05569975ca3754ffd86777cb421 100644 (file)
@@ -17,6 +17,7 @@ class DrawingView: public QWidget
                void SetRotateToolActive(bool state = true);
                void SetAddLineToolActive(bool state = true);
                void SetAddCircleToolActive(bool state = true);
+               void UpdateGridBackground(void);
 
        public slots:
                void AddNewObjectToDocument(Object *);
@@ -35,6 +36,8 @@ class DrawingView: public QWidget
                bool useAntialiasing;
 
        private:
+//             QBrush * backgroundBrush;
+               QPixmap gridBackground;
                double scale;                                                   // Window scaling factor
                int32_t offsetX, offsetY;                               // Window offsets
                Container document;
index 03d1f70eb47565271e7ac94070e29ea0834c2eed..b5bab70d954f7c2f80e93cc29de2d3f790484c72 100644 (file)
@@ -24,6 +24,7 @@ QFont * Object::font = 0;
 int Object::viewportHeight = 0;
 bool Object::deleteActive = false;
 bool Object::dimensionActive = false;
+bool Object::snapToGrid = true;
 
 
 Object::Object(): position(Vector(0, 0)), parent(0), state(OSInactive), oldState(OSInactive),
@@ -122,3 +123,8 @@ void Object::SetDimensionActive(bool state/*= true*/)
 {
        dimensionActive = state;
 }
+
+void Object::SetSnapMode(bool state/*= true*/)
+{
+       snapToGrid = state;
+}
index b6f9558e452b9386a80cab88f841f49651dfa88c..875e92ca6c358f7eec29ef60c3a1d24cf7d8895c 100644 (file)
@@ -37,6 +37,7 @@ class Object
                static void SetViewportHeight(int);
                static void SetDeleteActive(bool state = true);
                static void SetDimensionActive(bool state = true);
+               static void SetSnapMode(bool state = true);
 
        protected:
                Vector position;                                        // All objects have a position (doubles as reference point)
@@ -56,6 +57,7 @@ class Object
                static int viewportHeight;
                static bool deleteActive;
                static bool dimensionActive;
+               static bool snapToGrid;
 };
 
 #endif // __OBJECT_H__
index ba5e3f8aa2866ede59b6cc675757721f83cee050..73e5bf6e5fc539cca3984500d1822f9383fde030 100644 (file)
@@ -108,24 +108,24 @@ void Painter::DrawAngledText(Vector center, double angle, QString text)
        // then rotate the frame to the desired angle.
        center = CartesianToQtCoords(center);
 
-// NOTE: 1/32 is a kludge to make things look right at screen resolution...
        // 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 * zoom * SCREEN_ZOOM, -100 * zoom * SCREEN_ZOOM, 200 * zoom * SCREEN_ZOOM, 200 * zoom * SCREEN_ZOOM);       // x, y, w, h; x/y = upper left corner
+// NOTE: SCREEN_ZOOM is a kludge to make things look right at screen resolution...
+       QRectF textBox(-100.0 * zoom * SCREEN_ZOOM, -100.0 * zoom * SCREEN_ZOOM, 200.0 * zoom * SCREEN_ZOOM, 200.0 * zoom * SCREEN_ZOOM);       // 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.
        // If zoomable, this is incorrect:
        // (Added zoom, so this is correct now :-)
-       int yOffset = -12 * zoom * SCREEN_ZOOM;
+       float yOffset = -12.0 * zoom * SCREEN_ZOOM;
 
        // Fix text so it isn't upside down...
        if ((angle > PI * 0.5) && (angle < PI * 1.5))
        {
                angle += PI;
-               yOffset = 12 * zoom * SCREEN_ZOOM;
+               yOffset = 12.0 * zoom * SCREEN_ZOOM;
        }
 
 #if 0
@@ -167,6 +167,7 @@ void Painter::DrawEllipse(Vector center, double axis1, double axis2)
 void Painter::DrawHandle(Vector center)
 {
        center = CartesianToQtCoords(center);
+       painter->setBrush(Qt::NoBrush);
        painter->drawEllipse(QPointF(center.x, center.y), 4.0, 4.0);
 }
 
@@ -225,7 +226,7 @@ void Painter::DrawArrowhead(Vector head, Vector tail)
        Vector orthogonal = Vector(cos(orthoAngle), sin(orthoAngle));
        Vector unit = Vector(head - tail).Unit();
 
-// NOTE: 1/32 is a kludge to make things look right at scale...
+// NOTE: SCREEN_ZOOM is a kludge to make things look right at scale...
        Point p1 = head - (unit * 9.0 * SCREEN_ZOOM);
        Point p2 = p1 + (orthogonal * 3.0 * SCREEN_ZOOM);
        Point p3 = p1 - (orthogonal * 3.0 * SCREEN_ZOOM);
index d82c00078fba42e294b877ffb5791f843343ae18..a4d9dc020919df2b2f5c8da6415adbdd32c65733 100644 (file)
@@ -4,7 +4,7 @@
 #include <QtGui>
 #include "vector.h"
 
-#define SCREEN_ZOOM  (1.0 / 8.0)
+#define SCREEN_ZOOM  (1.0 / 4.0)
 
 // Forward declarations