From: Shamus Hammons Date: Thu, 9 Feb 2012 23:14:23 +0000 (+0000) Subject: Initial stab at proper grid display using background image. X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b7c8ec7d7f5379e09a8b7392f465f41639b0c79;p=architektonas Initial stab at proper grid display using background image. --- diff --git a/src/applicationwindow.cpp b/src/applicationwindow.cpp index 66f9edc..92c0547 100644 --- a/src/applicationwindow.cpp +++ b/src/applicationwindow.cpp @@ -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()); } diff --git a/src/applicationwindow.h b/src/applicationwindow.h index c220548..c9551d5 100644 --- a/src/applicationwindow.h +++ b/src/applicationwindow.h @@ -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__ diff --git a/src/circle.cpp b/src/circle.cpp index 5750b1f..8e5f6cd 100644 --- a/src/circle.cpp +++ b/src/circle.cpp @@ -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); diff --git a/src/drawingview.cpp b/src/drawingview.cpp index eccb0b1..0d62be1 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -14,11 +14,11 @@ // 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; diff --git a/src/drawingview.h b/src/drawingview.h index f11bf15..98b34b5 100644 --- a/src/drawingview.h +++ b/src/drawingview.h @@ -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; diff --git a/src/object.cpp b/src/object.cpp index 03d1f70..b5bab70 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -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; +} diff --git a/src/object.h b/src/object.h index b6f9558..875e92c 100644 --- a/src/object.h +++ b/src/object.h @@ -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__ diff --git a/src/painter.cpp b/src/painter.cpp index ba5e3f8..73e5bf6 100644 --- a/src/painter.cpp +++ b/src/painter.cpp @@ -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); diff --git a/src/painter.h b/src/painter.h index d82c000..a4d9dc0 100644 --- a/src/painter.h +++ b/src/painter.h @@ -4,7 +4,7 @@ #include #include "vector.h" -#define SCREEN_ZOOM (1.0 / 8.0) +#define SCREEN_ZOOM (1.0 / 4.0) // Forward declarations