From e23b78998b04bc91d022dd82880179118203efb9 Mon Sep 17 00:00:00 2001 From: Lukáš Lalinský Date: Tue, 9 Dec 2008 13:41:26 +0100 Subject: Move grid drawing to DiagramView --- src/diagramview.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/diagramview.cpp') diff --git a/src/diagramview.cpp b/src/diagramview.cpp index 0c5a45b..e2a5779 100644 --- a/src/diagramview.cpp +++ b/src/diagramview.cpp @@ -14,10 +14,14 @@ // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +#include #include #include #include #include "diagramview.h" +#include "diagramdocument.h" + +using namespace std; DiagramView::DiagramView(QWidget *parent) : QGraphicsView(parent), m_handScrolling(false) @@ -25,6 +29,13 @@ DiagramView::DiagramView(QWidget *parent) setAlignment(Qt::AlignLeft | Qt::AlignTop); setRenderHint(QPainter::Antialiasing); setDragMode(QGraphicsView::RubberBandDrag); + setRubberBandSelectionMode(Qt::ContainsItemBoundingRect); +} + +DiagramDocument * +DiagramView::document() const +{ + return static_cast(scene()); } void @@ -82,3 +93,48 @@ DiagramView::mouseReleaseEvent(QMouseEvent *event) } QGraphicsView::mouseReleaseEvent(event); } + +void +DiagramView::drawBackground(QPainter *painter, const QRectF &rect) +{ + DiagramDocument *doc = document(); + if (doc && doc->isGridVisible()) { + drawGrid(painter, rect); + } +} + +void +DiagramView::drawGrid(QPainter *painter, const QRectF &rect) +{ + const int pointBufferSize = 5000; + static QPoint pointBuffer[pointBufferSize]; + DiagramDocument *doc = document(); + int gridSize = doc->gridSize(); + int x0 = gridSize * floor(rect.left() / gridSize); + int y0 = gridSize * floor(rect.top() / gridSize); + int x1 = gridSize * ceil(rect.right() / gridSize); + int y1 = gridSize * ceil(rect.bottom() / gridSize); + painter->save(); + QPen pen; + pen.setColor(doc->gridColor()); + pen.setWidth(0); + painter->setPen(pen); + painter->setRenderHint(QPainter::Antialiasing, false); + int pointsUsed = 0; + for (int x = x0; x < x1; x += gridSize) { + for (int y = y0; y < y1; y += gridSize) { + pointBuffer[pointsUsed].setX(x); + pointBuffer[pointsUsed].setY(y); + pointsUsed++; + if (pointsUsed == pointBufferSize) { + painter->drawPoints(pointBuffer, pointsUsed); + pointsUsed = 0; + } + } + } + if (pointsUsed > 0) { + painter->drawPoints(pointBuffer, pointsUsed); + pointsUsed = 0; + } + painter->restore(); +} -- cgit v1.2.3-54-g00ecf