X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdrawingview.cpp;h=23c1d49100ad95f26a55aeb7e1a441a1a8875d0b;hb=8ab4e08bd78cb5b07f069e3e6c5bf76944cb14fa;hp=4a71fa6469a7160c644dddce86bd0a394927497e;hpb=b5981e2c452fa0ad5d922025780f9200450698dc;p=architektonas diff --git a/src/drawingview.cpp b/src/drawingview.cpp index 4a71fa6..23c1d49 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -4,7 +4,7 @@ // (C) 2011 Underground Software // See the README and GPLv3 files for licensing and warranty information // -// JLH = James L. Hammons +// JLH = James Hammons // // Who When What // --- ---------- ------------------------------------------------------------- @@ -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... @@ -34,6 +34,9 @@ #include "arc.h" #include "circle.h" #include "dimension.h" +#include "drawcircleaction.h" +#include "drawdimensionaction.h" +#include "drawlineaction.h" #include "line.h" #include "painter.h" @@ -41,11 +44,17 @@ 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), - scrollDrag(false) +// gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0), + gridSpacing(12.0), collided(false), rotateTool(false), rx(150.0), ry(150.0), + scrollDrag(false), addLineTool(false), addCircleTool(false), + addDimensionTool(false), +// selectionInProgress(false), + toolAction(NULL) { + document.isTopLevelContainer = true; setBackgroundRole(QPalette::Base); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); @@ -63,21 +72,142 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent), document.Add(new Arc(Vector(300, 300), 32, PI / 4.0, PI * 1.3, &document)), document.Add(new Arc(Vector(200, 200), 60, PI / 2.0, PI * 1.5, &document)); #if 1 - Dimension * dimension = new Dimension(Vector(0, 0), Vector(0, 0), &document); + Dimension * dimension = new Dimension(Vector(0, 0), Vector(0, 0), DTLinear, &document); line->SetDimensionOnLine(dimension); document.Add(dimension); #else // Alternate way to do the above... line->SetDimensionOnLine(); #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(210, 210, 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*/) { rotateTool = state; update(); } + +void DrawingView::SetAddLineToolActive(bool state/*= true*/) +{ + if (state) + { + toolAction = new DrawLineAction(); + connect(toolAction, SIGNAL(ObjectReady(Object *)), this, + SLOT(AddNewObjectToDocument(Object *))); + } + + update(); +//printf("DrawingView::SetAddLineToolActive(). toolAction=%08X\n", toolAction); +} + + +void DrawingView::SetAddCircleToolActive(bool state/*= true*/) +{ + if (state) + { + toolAction = new DrawCircleAction(); + connect(toolAction, SIGNAL(ObjectReady(Object *)), this, + SLOT(AddNewObjectToDocument(Object *))); + } + + update(); +} + + +void DrawingView::SetAddDimensionToolActive(bool state/*= true*/) +{ + if (state) + { + toolAction = new DrawDimensionAction(); + connect(toolAction, SIGNAL(ObjectReady(Object *)), this, + SLOT(AddNewObjectToDocument(Object *))); + } + + update(); +} + + +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; + // Use mod arithmetic to grab the correct swatch of background + // 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) + { + object->Reparent(&document); + document.Add(object); + update(); + } +//printf("DrawingView::AddNewObjectToDocument(). object=%08X\n", object); +} + + QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event) { // This is undoing the transform, e.g. going from client coords to local coords. @@ -86,6 +216,7 @@ QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event) return QPoint(offsetX + event->x(), offsetY + (size().height() - event->y())); } + QPoint DrawingView::GetAdjustedClientPosition(int x, int y) { // VOODOO ALERT (ON Y COMPONENT!!!!) (eh?) @@ -94,11 +225,15 @@ QPoint DrawingView::GetAdjustedClientPosition(int x, int y) return QPoint(-offsetX + x, (size().height() - (-offsetY + y)) * +1.0); } + 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); @@ -112,7 +247,8 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/) 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... if (rotateTool) { painter.SetPen(QPen(QColor(0, 200, 0), 2.0, Qt::SolidLine)); @@ -136,16 +272,29 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/) painter.drawLine(-16384, (int)y, 16384, (int)y); #endif - painter.SetPen(QPen(Qt::black, 1.0, Qt::SolidLine)); - - for(double x=0; xDraw(&painter); + + 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); + } } + void DrawingView::mousePressEvent(QMouseEvent * event) { if (event->button() == Qt::LeftButton) @@ -155,6 +304,17 @@ void DrawingView::mousePressEvent(QMouseEvent * event) if (collided) update(); // Do an update if collided with at least *one* object in the document + + if (toolAction) + toolAction->MouseDown(point); + + // Didn't hit any object and not using a tool, so do a selection rectangle + if (!(collided || toolAction)) + { + Object::selectionInProgress = true; + Object::selection.setTopLeft(QPointF(point.x, point.y)); + Object::selection.setBottomRight(QPointF(point.x, point.y)); + } } else if (event->button() == Qt::MiddleButton) { @@ -165,19 +325,23 @@ void DrawingView::mousePressEvent(QMouseEvent * event) } } + void DrawingView::mouseMoveEvent(QMouseEvent * event) { Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y())); + Object::selection.setBottomRight(QPointF(point.x, point.y)); if (event->buttons() & Qt::MiddleButton) { 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 them. :-) + // conform to Cartesian coords, since the origin is using Cartesian. :-) Vector delta(point, oldPoint); delta /= Painter::zoom; delta.y = -delta.y; Painter::origin -= delta; + + UpdateGridBackground(); update(); oldPoint = point; return; @@ -195,6 +359,10 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) { 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!!! @@ -205,10 +373,17 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) //changed... document.PointerMoved(point); - if (document.NeedsUpdate()) + if (document.NeedsUpdate() || Object::selectionInProgress) update(); + + if (toolAction) + { + toolAction->MouseMoved(point); + update(); + } } + void DrawingView::mouseReleaseEvent(QMouseEvent * event) { if (event->button() == Qt::LeftButton) @@ -221,6 +396,15 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event) // if (document.NeedsUpdate()) // if (collided) update(); // Do an update if collided with at least *one* object in the document + + if (toolAction) + toolAction->MouseReleased(); + + if (Object::selectionInProgress) + { + // Select all the stuff inside of selection + Object::selectionInProgress = false; + } } else if (event->button() == Qt::MiddleButton) { @@ -228,3 +412,4 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event) setCursor(Qt::ArrowCursor); } } +