From: Shamus Hammons Date: Wed, 31 Jul 2013 11:43:59 +0000 (-0500) Subject: Fixes for grid background drawing of arbitrary size. X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=architektonas;a=commitdiff_plain;h=2af76acd67a2703859c11446be3be4d3ff8ff4b4 Fixes for grid background drawing of arbitrary size. Also, preliminary input widget for resizing the grid. Since the unit involved is in pixels, will probably replace this with a slider. --- diff --git a/src/applicationwindow.cpp b/src/applicationwindow.cpp index 246e0bc..03f8cb5 100644 --- a/src/applicationwindow.cpp +++ b/src/applicationwindow.cpp @@ -290,6 +290,7 @@ when zooming in, new origin will be (xCenter - origin.x) / 2, (yCenter - origin. //printf("Zoom in... level going from %02f to ", Painter::zoom); // This just zooms leaving origin intact... should zoom in at the current center! [DONE] + // This should actually be calculated by drawing->gridPixels / grid size. Painter::zoom *= zoomFactor; drawing->gridSpacing = 12.0 / Painter::zoom; // zoomIndicator->setText(QString("Grid: %2\" Zoom: %1%").arg(Painter::zoom * 100.0 * SCREEN_ZOOM).arg(drawing->gridSpacing)); @@ -461,6 +462,13 @@ else } +void ApplicationWindow::HandleGridSizeInPixels(int size) +{ + drawing->SetGridSize(size); + drawing->update(); +} + + void ApplicationWindow::CreateActions(void) { exitAct = CreateAction(tr("&Quit"), tr("Quit"), tr("Exits the application."), @@ -622,6 +630,8 @@ void ApplicationWindow::CreateToolbars(void) toolbar->setObjectName("View"); toolbar->addAction(zoomInAct); toolbar->addAction(zoomOutAct); + QSpinBox * spinbox = new QSpinBox; + toolbar->addWidget(spinbox); toolbar = addToolBar(tr("Edit")); toolbar->setObjectName("Edit"); @@ -637,6 +647,10 @@ void ApplicationWindow::CreateToolbars(void) toolbar->addAction(addArcAct); toolbar->addAction(addPolygonAct); toolbar->addAction(addDimensionAct); + + spinbox->setRange(4, 256); + spinbox->setValue(12); + connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(HandleGridSizeInPixels(int))); } @@ -649,10 +663,6 @@ void ApplicationWindow::ReadSettings(void) resize(size); move(pos); restoreState(settings.value("windowState").toByteArray()); -// pos = settings.value("charWndPos", QPoint(0, 0)).toPoint(); -// size = settings.value("charWndSize", QSize(200, 200)).toSize(); -// ((TTEdit *)qApp)->charWnd->resize(size); -// ((TTEdit *)qApp)->charWnd->move(pos); } @@ -663,7 +673,5 @@ void ApplicationWindow::WriteSettings(void) settings.setValue("windowState", saveState()); 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 5905f70..89fb9aa 100644 --- a/src/applicationwindow.h +++ b/src/applicationwindow.h @@ -42,6 +42,7 @@ class ApplicationWindow: public QMainWindow void HelpAbout(void); void Settings(void); void HandleGrouping(void); + void HandleGridSizeInPixels(int); private: void ClearUIToolStatesExcept(QAction *); diff --git a/src/dimension.cpp b/src/dimension.cpp index 021f300..8b930f0 100644 --- a/src/dimension.cpp +++ b/src/dimension.cpp @@ -22,8 +22,7 @@ Dimension::Dimension(Vector p1, Vector p2, DimensionType dt/*= DTLinear*/ ,Object * p/*= NULL*/): Object(p1, p), endpoint(p2), dragging(false), draggingHandle1(false), draggingHandle2(false), - length(p2.Magnitude()), dimensionType(dt), point1(NULL), point2(NULL), - size(0.25) + length(p2.Magnitude()), dimensionType(dt), size(0.25), point1(NULL), point2(NULL) { // We set the size to 1/4 base unit. Could be anything. type = OTDimension; @@ -33,8 +32,7 @@ Dimension::Dimension(Vector p1, Vector p2, DimensionType dt/*= DTLinear*/ ,Objec // This is bad, p1 & p2 could be NULL, causing much consternation... Dimension::Dimension(Connection p1, Connection p2, DimensionType dt/*= DTLinear*/ , Object * p/*= NULL*/): dragging(false), draggingHandle1(false), draggingHandle2(false), - length(0), dimensionType(dt), point1(p1), point2(p2), - size(0.25) + length(0), dimensionType(dt), size(0.25), point1(p1), point2(p2) { type = OTDimension; } diff --git a/src/drawingview.cpp b/src/drawingview.cpp index 1a6d120..06f4496 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -41,17 +41,19 @@ #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 +114,35 @@ 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. + +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. + +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. */ +#if 0 QPainter pmp(&gridBackground); - pmp.fillRect(0, 0, 512, 512, QColor(240, 240, 240)); + 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)); - for(int i=0; i<511; i+=12) + for(int i=0; i<(BACKGROUND_MAX_SIZE-1); i+=12) { - pmp.drawLine(i, 0, i, 511); - pmp.drawLine(0, i, 511, i); + pmp.drawLine(i, 0, i, BACKGROUND_MAX_SIZE - 1); + pmp.drawLine(0, i, BACKGROUND_MAX_SIZE - 1, i); } pmp.end(); UpdateGridBackground(); +#else + SetGridSize(12); +#endif } @@ -176,18 +193,122 @@ void DrawingView::SetAddDimensionToolActive(bool state/*= true*/) } +void DrawingView::SetGridSize(uint32_t size) +{ + // 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)); + + for(int i=0; i<(BACKGROUND_MAX_SIZE-1); i+=gridPixels) + { + pmp.drawLine(i, 0, i, BACKGROUND_MAX_SIZE - 1); + pmp.drawLine(0, i, BACKGROUND_MAX_SIZE - 1, i); + } + + 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 +//#define BG_BRUSH_SPAN 72 +#define BG_BRUSH_SPAN (gridPixels) // 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 inverse 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 + +This has been solved below: +UGB: pmo=(2.000000,687.000000) X=2, X%48=2, new x=45 +UGB: pmo=(1.000000,687.000000) X=1, X%48=1, new x=46 +UGB: pmo=(0.000000,687.000000) X=0, X%48=0, new x=47 +UGB: pmo=(-1.000000,687.000000) X=-1, X%48=15, new x=1 +UGB: pmo=(-2.000000,686.000000) X=-2, X%48=14, new x=2 +UGB: pmo=(-3.000000,686.000000) X=-3, X%48=13, new x=3 + +Problem with changing grid size causes x/y origin to be shown incorrectly: +UGB: pmo=(10.000000,812.000000) X=10, X%12=10, new x=2 +UGB: pmo=(10.000000,812.000000) X=10, X%4=2, new x=2 +UGB: pmo=(3.333333,818.666667) X=3, X%48=3, new x=45 +UGB: pmo=(39.000000,782.000000) X=39, X%48=39, new x=9 <-- MMB move is here +UGB: pmo=(39.000000,781.000000) X=39, X%48=39, new x=9 + + + +*/ +//printf("UGB: pmo=(%f,%f) X=%i, X%%%i=%i, ", pixmapOrigin.x, pixmapOrigin.y, x, BG_BRUSH_SPAN, x % BG_BRUSH_SPAN); +// x = (x < 0 ? 0 : BG_BRUSH_SPAN - 1) - (x % BG_BRUSH_SPAN); +// y = (y < 0 ? 0 : BG_BRUSH_SPAN - 1) - (y % BG_BRUSH_SPAN); +// x = (BG_BRUSH_SPAN - 1) - (x % BG_BRUSH_SPAN); +// y = (BG_BRUSH_SPAN - 1) - (y % BG_BRUSH_SPAN); +// x = (x < 0 ? 0 : BG_BRUSH_SPAN - 1) - ((x < 0 ? -x : x) % BG_BRUSH_SPAN); + if (x < 0) + x = -x % BG_BRUSH_SPAN; + else + x = (BG_BRUSH_SPAN - (x % BG_BRUSH_SPAN)) % BG_BRUSH_SPAN; + +// y = (y < 0 ? 0 : BG_BRUSH_SPAN - 1) - ((y < 0 ? -y : y) % BG_BRUSH_SPAN); + if (y < 0) + y = -y % BG_BRUSH_SPAN; + else + y = (BG_BRUSH_SPAN - (y % BG_BRUSH_SPAN)) % BG_BRUSH_SPAN; +//printf("new x=%i\n", x); // Here we grab a section of the bigger pixmap, so that the background // *looks* like it's scrolling... @@ -237,7 +358,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 @@ -277,6 +398,15 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/) } +void DrawingView::resizeEvent(QResizeEvent * event) +{ + Painter::screenSize = Vector(size().width(), size().height()); +// Painter::screenSize = Vector(event->size().width(), event->size().height()); +//printf("DrawingView::resizeEvent: new size: + UpdateGridBackground(); +} + + void DrawingView::mousePressEvent(QMouseEvent * event) { if (event->button() == Qt::LeftButton) diff --git a/src/drawingview.h b/src/drawingview.h index cd76d1e..888737e 100644 --- a/src/drawingview.h +++ b/src/drawingview.h @@ -18,6 +18,7 @@ class DrawingView: public QWidget void SetAddLineToolActive(bool state = true); void SetAddCircleToolActive(bool state = true); void SetAddDimensionToolActive(bool state = true); + void SetGridSize(uint32_t); void UpdateGridBackground(void); public slots: @@ -25,6 +26,7 @@ class DrawingView: public QWidget protected: void paintEvent(QPaintEvent * event); + void resizeEvent(QResizeEvent * event); void mousePressEvent(QMouseEvent * event); void mouseMoveEvent(QMouseEvent * event); void mouseReleaseEvent(QMouseEvent * event); @@ -43,7 +45,9 @@ class DrawingView: public QWidget int32_t offsetX, offsetY; // Window offsets public: Container document; - double gridSpacing; + double gridSpacing; // Grid spacing in base units + uint32_t gridPixels; // Grid size in pixels +// double gridBaseUnits; // Grid size in base units private: bool collided; //Should this go into Object's class variables???