summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukáš Lalinský <lalinsky@gmail.com>2008-12-09 13:41:26 +0100
committerLukáš Lalinský <lalinsky@gmail.com>2008-12-09 13:41:26 +0100
commite23b78998b04bc91d022dd82880179118203efb9 (patch)
tree8c3f9090d6465ff473781d54b26f950c6ed3c913
parent3bad0741c095f7aa5556b3d898cf995b238c3ac3 (diff)
downloaddbmodel-e23b78998b04bc91d022dd82880179118203efb9.tar.gz
dbmodel-e23b78998b04bc91d022dd82880179118203efb9.tar.bz2
Move grid drawing to DiagramView
-rw-r--r--src/diagramdocument.cpp34
-rw-r--r--src/diagramdocument.h2
-rw-r--r--src/diagramview.cpp56
-rw-r--r--src/diagramview.h6
4 files changed, 62 insertions, 36 deletions
diff --git a/src/diagramdocument.cpp b/src/diagramdocument.cpp
index d41ecdc..1f34d9f 100644
--- a/src/diagramdocument.cpp
+++ b/src/diagramdocument.cpp
@@ -90,40 +90,6 @@ DiagramDocument::setGridColor(const QColor &color)
update();
}
-void
-DiagramDocument::drawBackground(QPainter *painter, const QRectF &rect)
-{
- const int pointBufferSize = 5000;
- static QPoint pointBuffer[pointBufferSize];
- if (!d->printing && d->gridVisible) {
- int gridSize = d->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();
- painter->setPen(d->gridPen);
- 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();
- }
-}
-
bool
DiagramDocument::isPrinting() const
{
diff --git a/src/diagramdocument.h b/src/diagramdocument.h
index eb7aeba..350879c 100644
--- a/src/diagramdocument.h
+++ b/src/diagramdocument.h
@@ -94,8 +94,6 @@ protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
- void drawBackground(QPainter *painter, const QRectF &rect);
-
private:
class DiagramDocumentPrivate;
DiagramDocumentPrivate *const d;
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 <cmath>
#include <QMouseEvent>
#include <QScrollBar>
#include <QDebug>
#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<DiagramDocument *>(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();
+}
diff --git a/src/diagramview.h b/src/diagramview.h
index d5f1c56..9e57c36 100644
--- a/src/diagramview.h
+++ b/src/diagramview.h
@@ -18,6 +18,7 @@
#define DATABASEMODELVIEW_H
#include <QGraphicsView>
+class DiagramDocument;
class DiagramView : public QGraphicsView
{
@@ -26,6 +27,8 @@ class DiagramView : public QGraphicsView
public:
DiagramView(QWidget *parent = 0);
+ DiagramDocument *document() const;
+
void setScene(QGraphicsScene *scene);
protected:
@@ -33,6 +36,9 @@ protected:
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
+ void drawBackground(QPainter *painter, const QRectF &rect);
+ void drawGrid(QPainter *painter, const QRectF &rect);
+
protected slots:
void updateSceneRect2(const QRectF &rect);