X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdrawingview.cpp;h=5bbcddf62ffeb0396b5f5edc5924e46ceaea9a5e;hb=eb711912d64f17cf9c18c74c4d78d9867bd066ad;hp=1a6d120852d8d98c52930c7fcc47a708b9dac27f;hpb=631fbe38f52222a65f1c3bcefb11a616d6806dd6;p=architektonas diff --git a/src/drawingview.cpp b/src/drawingview.cpp index 1a6d120..5bbcddf 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -34,24 +34,23 @@ #include "arc.h" #include "circle.h" #include "dimension.h" -#include "drawcircleaction.h" -#include "drawdimensionaction.h" -#include "drawlineaction.h" #include "line.h" #include "painter.h" +#define BACKGROUND_MAX_SIZE 512 + + DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent), // The value in the settings file will override this. useAntialiasing(true), - gridBackground(512, 512), + gridBackground(BACKGROUND_MAX_SIZE, BACKGROUND_MAX_SIZE), scale(1.0), offsetX(-10), offsetY(-10), document(Vector(0, 0)), -// 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), + gridSpacing(12.0), gridPixels(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; @@ -112,20 +111,21 @@ 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 +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. -*/ - QPainter pmp(&gridBackground); - pmp.fillRect(0, 0, 512, 512, QColor(240, 240, 240)); - pmp.setPen(QPen(QColor(210, 210, 255), 2.0, Qt::SolidLine)); +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 +scaled the same way as the arrowheads. - for(int i=0; i<511; i+=12) - { - pmp.drawLine(i, 0, i, 511); - pmp.drawLine(0, i, 511, i); - } +Need a way to scale line widths as well. :-/ Shouldn't be too difficult, just +need a thickness parameter similar to the "size" param for dimensions. (And now +we do! :-) - pmp.end(); - UpdateGridBackground(); +*/ + SetGridSize(12); } @@ -136,62 +136,115 @@ void DrawingView::SetRotateToolActive(bool state/*= true*/) } -void DrawingView::SetAddLineToolActive(bool state/*= true*/) +void DrawingView::SetToolActive(Action * action) { - if (state) + if (action != NULL) { - toolAction = new DrawLineAction(); + toolAction = action; connect(toolAction, SIGNAL(ObjectReady(Object *)), this, SLOT(AddNewObjectToDocument(Object *))); } - - update(); -//printf("DrawingView::SetAddLineToolActive(). toolAction=%08X\n", toolAction); } -void DrawingView::SetAddCircleToolActive(bool state/*= true*/) +void DrawingView::SetGridSize(uint32_t size) { - if (state) - { - toolAction = new DrawCircleAction(); - connect(toolAction, SIGNAL(ObjectReady(Object *)), this, - SLOT(AddNewObjectToDocument(Object *))); - } - - update(); -} + // Sanity check + if (size == gridPixels) + return; + // Recreate the background bitmap + gridPixels = size; + QPainter pmp(&gridBackground); + pmp.fillRect(0, 0, BACKGROUND_MAX_SIZE, BACKGROUND_MAX_SIZE, QColor(240, 240, 240)); + pmp.setPen(QPen(QColor(210, 210, 255), 2.0, Qt::SolidLine)); -void DrawingView::SetAddDimensionToolActive(bool state/*= true*/) -{ - if (state) + for(int i=0; i<(BACKGROUND_MAX_SIZE-1); i+=gridPixels) { - toolAction = new DrawDimensionAction(); - connect(toolAction, SIGNAL(ObjectReady(Object *)), this, - SLOT(AddNewObjectToDocument(Object *))); + pmp.drawLine(i, 0, i, BACKGROUND_MAX_SIZE - 1); + pmp.drawLine(0, i, BACKGROUND_MAX_SIZE - 1, i); } - update(); + pmp.end(); + + // Set up new BG brush & zoom level (pixels per base unit) + Painter::zoom = gridPixels / gridSpacing; + UpdateGridBackground(); } void DrawingView::UpdateGridBackground(void) { -//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); +/* +Negative numbers still screw it up... Need to think about what we're +trying to do here. The fact that it worked with 72 seems to have been pure luck. +It seems the problem is negative numbers: We can't let that happen. +When taking away the zero, it pops over 1 px at zero, then goes about 1/2 a +grid at x<0. + +The bitmap looks like this: + ++---+---+---+---+--- +| | | | | +| | | | | ++---+---+---+---+--- +| | | | | +| | | | | +| | | | | + +@ x = 1, we want it to look like: + +-+---+---+---+---+--- + | | | | | + | | | | | +-+---+---+---+---+--- + | | | | | + | | | | | + | | | | | + +Which means we need to grab the sample from x = 3. @ x = -1: + +---+---+---+---+--- + | | | | + | | | | +---+---+---+---+--- + | | | | + | | | | + | | | | + +Which means we need to grab the sample from x = 1. Which means we have to take +the mirror of the modulus of gridPixels. + +Doing a mod of a negative number is problematic: 1st, the compiler converts the +negative number to an unsigned int, then it does the mod. Gets you wrong answers +most of the time, unless you use a power of 2. :-P So what we do here is just +take the modulus of the negation, which means we don't have to worry about +mirroring it later. + +The positive case looks gruesome (and it is) but it boils down to this: We take +the modulus of the X coordinate, then mirror it by subtraction from the +maximum (in this case, gridPixels). This gives us a number in the range of 1 to +gridPixels. But we need the case where the result equalling gridPixels to be +zero; so we do another modulus operation on the result to achieve this. +*/ + if (x < 0) + x = -x % gridPixels; + else + x = (gridPixels - (x % gridPixels)) % gridPixels; + + if (y < 0) + y = -y % gridPixels; + else + y = (gridPixels - (y % gridPixels)) % gridPixels; // 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); + QPixmap pm = gridBackground.copy(x, y, gridPixels, gridPixels); QPalette pal = palette(); pal.setBrush(backgroundRole(), QBrush(pm)); setAutoFillBackground(true); @@ -229,6 +282,26 @@ 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); @@ -237,7 +310,7 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/) if (useAntialiasing) qtPainter.setRenderHint(QPainter::Antialiasing); - Painter::screenSize = Vector(size().width(), size().height()); +// Painter::screenSize = Vector(size().width(), size().height()); Object::SetViewportHeight(size().height()); // Draw coordinate axes @@ -264,7 +337,12 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/) 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); + } if (Object::selectionInProgress) { @@ -277,11 +355,30 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/) } +void DrawingView::resizeEvent(QResizeEvent * /*event*/) +{ + Painter::screenSize = Vector(size().width(), size().height()); + UpdateGridBackground(); +} + + 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, as sometimes things don't fall on the grid. +/* +So, how to fix this? Have the Object check itself? +Maybe we can fix this by having the initial point not be snapped, but when there's +a drag, we substitute the snapped point 'oldPoint' which the Object keeps track of +internally to know how far it was dragged... +*/ + if (Object::snapToGrid) + point = SnapPointToGrid(point); + collided = document.Collided(point); if (collided) @@ -331,14 +428,9 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) // Grid processing... #if 1 - // 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. - if (event->buttons() & Qt::LeftButton) + 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... @@ -349,8 +441,12 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) point.y = floor(point.y); point.z = 0; // Make *sure* Z doesn't go anywhere!!! point *= gridSpacing; +#else + point = SnapPointToGrid(point); +#endif } #endif + oldPoint = point; //we should keep track of the last point here and only pass this down *if* the point //changed... document.PointerMoved(point); @@ -360,6 +456,12 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) if (toolAction) { + if (Object::snapToGrid) + { + point = SnapPointToGrid(point); + oldPoint = point; + } + toolAction->MouseMoved(point); update(); }