X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdrawingview.cpp;h=3020368913cfa3b4358df95669b1b65649523d15;hb=642cf72c11b49a9e00128ab6258a2438c785a5ab;hp=8f689a80bd0e33cd2ef3000ba7bff14fea6d5c59;hpb=11802354d1ddc5bc571d83d8fc9b600618cb4372;p=architektonas diff --git a/src/drawingview.cpp b/src/drawingview.cpp index 8f689a8..3020368 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -34,9 +34,6 @@ #include "arc.h" #include "circle.h" #include "dimension.h" -#include "drawcircleaction.h" -#include "drawdimensionaction.h" -#include "drawlineaction.h" #include "line.h" #include "painter.h" @@ -50,16 +47,17 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent), gridBackground(BACKGROUND_MAX_SIZE, BACKGROUND_MAX_SIZE), scale(1.0), offsetX(-10), offsetY(-10), document(Vector(0, 0)), - gridSpacing(12.0), gridPixels(0), collided(false), rotateTool(false), + /*gridSpacing(12.0),*/ gridPixels(0), collided(false), rotateTool(false), rx(150.0), ry(150.0), - scrollDrag(false), addLineTool(false), addCircleTool(false), - addDimensionTool(false), +// scrollDrag(false), addLineTool(false), addCircleTool(false), +// addDimensionTool(false), toolAction(NULL) { document.isTopLevelContainer = true; setBackgroundRole(QPalette::Base); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + Object::gridSpacing = 12.0; // toolPalette = new ToolWindow(); // CreateCursors(); // setCursor(cur[TOOLSelect]); @@ -124,25 +122,11 @@ 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. +need a thickness parameter similar to the "size" param for dimensions. (And now +we do! :-) */ -#if 0 - 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+=12) - { - 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 } @@ -153,43 +137,14 @@ 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*/) -{ - 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(); } @@ -214,7 +169,8 @@ void DrawingView::SetGridSize(uint32_t size) pmp.end(); // Set up new BG brush & zoom level (pixels per base unit) - Painter::zoom = gridPixels / gridSpacing; +// Painter::zoom = gridPixels / gridSpacing; + Painter::zoom = gridPixels / Object::gridSpacing; UpdateGridBackground(); } @@ -310,6 +266,13 @@ void DrawingView::AddNewObjectToDocument(Object * object) } +void DrawingView::SetCurrentLayer(int layer) +{ + Object::currentLayer = layer; +//printf("DrawingView::CurrentLayer = %i\n", layer); +} + + QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event) { // This is undoing the transform, e.g. going from client coords to local coords. @@ -328,6 +291,28 @@ QPoint DrawingView::GetAdjustedClientPosition(int x, int y) } +#if 0 +// +// 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; +} +#endif + + void DrawingView::paintEvent(QPaintEvent * /*event*/) { QPainter qtPainter(this); @@ -363,7 +348,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) { @@ -388,13 +378,34 @@ 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... + +Now we do... :-/ +*/ +#if 0 + if (Object::snapToGrid) + point = Object::SnapPointToGrid(point); +#endif + collided = document.Collided(point); if (collided) update(); // Do an update if collided with at least *one* object in the document if (toolAction) + { + if (Object::snapToGrid) + point = Object::SnapPointToGrid(point); + toolAction->MouseDown(point); + } // Didn't hit any object and not using a tool, so do a selection rectangle if (!(collided || toolAction)) @@ -436,27 +447,12 @@ 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) { - 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!!! - point *= gridSpacing; + point = Object::SnapPointToGrid(point); } -#endif + + oldPoint = point; //we should keep track of the last point here and only pass this down *if* the point //changed... document.PointerMoved(point); @@ -466,6 +462,12 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) if (toolAction) { + if (Object::snapToGrid) + { + point = Object::SnapPointToGrid(point); + oldPoint = point; + } + toolAction->MouseMoved(point); update(); } @@ -501,3 +503,27 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event) } } + +void DrawingView::keyPressEvent(QKeyEvent * event) +{ + if (toolAction) + { + bool needUpdate = toolAction->KeyDown(event->key()); + + if (needUpdate) + update(); + } +} + + +void DrawingView::keyReleaseEvent(QKeyEvent * event) +{ + if (toolAction) + { + bool needUpdate = toolAction->KeyReleased(event->key()); + + if (needUpdate) + update(); + } +} +