summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMateusz Piekos <mateuszpiekos@gmail.com>2012-05-30 12:40:27 (GMT)
committerMateusz Piekos <mateuszpiekos@gmail.com>2012-05-30 12:40:27 (GMT)
commitf20c70620ff8a15079bef36a844800cf544662e0 (patch)
tree4f9293726d9a8f41a31f69d05622fd32791d5b73 /Swift/QtUI/Whiteboard/GView.cpp
parente7db09d201280cb64de649f5e33ca13d82045f38 (diff)
downloadswift-contrib-f20c70620ff8a15079bef36a844800cf544662e0.zip
swift-contrib-f20c70620ff8a15079bef36a844800cf544662e0.tar.bz2
Added whiteboard files to swift tree
Diffstat (limited to 'Swift/QtUI/Whiteboard/GView.cpp')
-rw-r--r--Swift/QtUI/Whiteboard/GView.cpp259
1 files changed, 259 insertions, 0 deletions
diff --git a/Swift/QtUI/Whiteboard/GView.cpp b/Swift/QtUI/Whiteboard/GView.cpp
new file mode 100644
index 0000000..c3782db
--- /dev/null
+++ b/Swift/QtUI/Whiteboard/GView.cpp
@@ -0,0 +1,259 @@
+/*
+ * Copyright (c) 2012 Mateusz Piękos
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#include "GView.h"
+#include "FreehandLineItem.h"
+
+namespace Swift {
+ GView::GView(QGraphicsScene* scene, QWidget* parent) : QGraphicsView(scene, parent), brush(QColor(Qt::white))
+ {
+ selectionRect = 0;
+ zValue = 0;
+ }
+
+ void GView::setLineWidth(int i)
+ {
+ pen.setWidth(i);
+ }
+
+ void GView::setLineColor(QColor color)
+ {
+ pen.setColor(color);
+ }
+
+ QColor GView::getLineColor()
+ {
+ return pen.color();
+ }
+
+ void GView::setBrushColor(QColor color)
+ {
+ brush.setColor(color);
+// brush.setStyle(Qt::SolidPattern);
+ }
+
+ QColor GView::getBrushColor()
+ {
+ return brush.color();
+ }
+
+ void GView::setMode(Mode mode)
+ {
+ this->mode = mode;
+ lastItem = 0;
+ }
+
+ void GView::mouseMoveEvent(QMouseEvent* event)
+ {
+ if (!mousePressed) {
+ return;
+ }
+
+ if (mode == Line) {
+ QGraphicsLineItem* item = qgraphicsitem_cast<QGraphicsLineItem*>(lastItem);
+ if(item != 0) {
+ QLineF line = item->line();
+ line.setP1(this->mapToScene(event->pos()));
+ item->setLine(line);
+ }
+ }
+ else if (mode == Rect) {
+ QGraphicsRectItem* item = qgraphicsitem_cast<QGraphicsRectItem*>(lastItem);
+ if (item != 0) {
+ QPointF beginPoint = item->data(0).toPointF();
+ QPointF newPoint = this->mapToScene(event->pos());
+ QRectF rect = item->rect();
+ if (beginPoint.x() <= newPoint.x() && beginPoint.y() <= newPoint.y()) {
+ rect.setTopLeft(beginPoint);
+ rect.setBottomRight(newPoint);
+ }
+ else if (beginPoint.x() > newPoint.x() && beginPoint.y() <= newPoint.y()) {
+ rect.setTopRight(beginPoint);
+ rect.setBottomLeft(newPoint);
+ }
+ else if (beginPoint.x() <= newPoint.x() && beginPoint.y() > newPoint.y()) {
+ rect.setBottomLeft(beginPoint);
+ rect.setTopRight(newPoint);
+ }
+ else if (beginPoint.x() > newPoint.x() && beginPoint.y() > newPoint.y()) {
+ rect.setBottomRight(beginPoint);
+ rect.setTopLeft(newPoint);
+ }
+ item->setRect(rect);
+ }
+ }
+ else if (mode == Circle) {
+ QGraphicsPathItem* item = qgraphicsitem_cast<QGraphicsPathItem*>(lastItem);
+ QPainterPath path;
+ QPointF beginPoint = item->data(0).toPointF();
+ QPointF newPoint = this->mapToScene(event->pos());
+ path.moveTo((newPoint.x()+beginPoint.x())/2, beginPoint.y());
+ path.cubicTo(beginPoint.x(), beginPoint.y(), beginPoint.x(), newPoint.y(), (newPoint.x()+beginPoint.x())/2, newPoint.y());
+ path.cubicTo(newPoint.x(), newPoint.y(), newPoint.x(), beginPoint.y(), (newPoint.x()+beginPoint.x())/2, beginPoint.y());
+ item->setPath(path);
+ }
+ else if (mode == HandLine) {
+ FreehandLineItem* item = qgraphicsitem_cast<FreehandLineItem*>(lastItem);
+ if (item != 0) {
+ QPointF newPoint = this->mapToScene(event->pos());
+ item->lineTo(newPoint);
+ }
+ }
+ else if (mode == FilledHandLine) {
+ QGraphicsPathItem* item = qgraphicsitem_cast<QGraphicsPathItem*>(lastItem);
+ QPainterPath path;
+ path = item->path();
+ if (path.isEmpty())
+ path.moveTo(item->data(0).toPointF());
+ QPointF newPoint = this->mapToScene(event->pos());
+ path.lineTo(newPoint);
+ item->setPath(path);
+ }
+ else if (mode == Polygon) {
+ QGraphicsPolygonItem* item = qgraphicsitem_cast<QGraphicsPolygonItem*>(lastItem);
+ QPointF newPoint = this->mapToScene(event->pos());
+ QPolygonF polygon = item->polygon();
+ polygon.erase(polygon.end()-1);
+ polygon.append(newPoint);
+ item->setPolygon(polygon);
+ }
+ else if (mode == Select) {
+ QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>();
+ if (item != 0) {
+ QPainterPath path;
+ QPointF beginPoint = selectionRect->data(0).toPointF();
+ QPointF newPoint = this->mapToScene(event->pos());
+ item->setPos(beginPoint + newPoint);
+ selectionRect->setPos(beginPoint + newPoint);
+ }
+ }
+ }
+
+ void GView::mousePressEvent(QMouseEvent *event)
+ {
+ mousePressed = true;
+ if (selectionRect != 0) {
+ scene()->removeItem(selectionRect);
+ delete selectionRect;
+ selectionRect = 0;
+ }
+ if (mode == Line) {
+ QPointF point = this->mapToScene(event->pos());
+ QGraphicsItem* item = scene()->addLine(point.x(), point.y(), point.x(), point.y(), pen);
+ item->setZValue(zValue++);
+ lastItem = item;
+
+ }
+ else if (mode == Rect) {
+ QPointF point = this->mapToScene(event->pos());
+ QGraphicsRectItem* item = scene()->addRect(point.x(), point.y(), 0, 0, pen, brush);
+ item->setZValue(zValue++);
+ item->setData(0, point);
+ lastItem = item;
+ }
+ else if (mode == Rubber) {
+ QPointF point = this->mapToScene(event->pos());
+ int w = pen.width();
+ QRectF rect(point.x()-w, point.y()-w, w*2, w*2);
+ QList<QGraphicsItem*> list = scene()->items(rect);
+ if (!list.isEmpty())
+ {
+ QGraphicsItem* item = scene()->items(rect).first();
+ scene()->removeItem(item);
+ delete item;
+ }
+ }
+ else if (mode == Circle) {
+ QPointF point = this->mapToScene(event->pos());
+ QGraphicsPathItem* item = scene()->addPath(QPainterPath(), pen, brush);
+ item->setZValue(zValue++);
+ item->setData(0, point);
+ lastItem = item;
+ }
+ else if (mode == HandLine) {
+ QPointF point = this->mapToScene(event->pos());
+ FreehandLineItem* item = new FreehandLineItem;
+ item->setPen(pen);
+ item->setStartPoint(point);
+ item->setZValue(zValue++);
+ scene()->addItem(item);
+ lastItem = item;
+ }
+ else if (mode == FilledHandLine) {
+ QPointF point = this->mapToScene(event->pos());
+ QPainterPath path;
+ QGraphicsPathItem* item = scene()->addPath(path, pen, brush);
+ item->setZValue(zValue++);
+ item->setData(0, point);
+ lastItem = item;
+ }
+ else if (mode == Text) {
+ QPointF point = this->mapToScene(event->pos());
+ QGraphicsTextItem* item = scene()->addText("");
+ item->setZValue(zValue++);
+ item->setDefaultTextColor(pen.color());
+ textDialog = new TextDialog(item, this);
+ textDialog->setAttribute(Qt::WA_DeleteOnClose);
+ textDialog->show();
+ item->setPos(point);
+ }
+ else if (mode == Polygon) {
+ QPointF point = this->mapToScene(event->pos());
+ QGraphicsPolygonItem* item = dynamic_cast<QGraphicsPolygonItem*>(lastItem);
+ if (item == 0) {
+ QPolygonF polygon;
+ polygon.append(point);
+ polygon.append(point);
+ item = scene()->addPolygon(polygon, pen, brush);
+ item->setZValue(zValue++);
+ lastItem = item;
+ }
+ else {
+ QPolygonF polygon;
+ polygon = item->polygon();
+ polygon.append(point);
+ item->setPolygon(polygon);
+ }
+ }
+ else if (mode == Select) {
+ QPointF point = this->mapToScene(event->pos());
+ int w = pen.width();
+ QRectF rect(point.x()-w, point.y()-w, w*2, w*2);
+ QList<QGraphicsItem*> list = scene()->items(rect);
+ if (!list.isEmpty()) {
+ QPen pen;
+ pen.setColor(QColor(Qt::gray));
+ pen.setStyle(Qt::DashLine);
+ QGraphicsItem *item = scene()->items(rect).first();
+ selectionRect = scene()->addRect(item->boundingRect(), pen);
+ selectionRect->setZValue(1000000);
+ selectionRect->setData(0, item->pos()-point);
+ selectionRect->setPos(item->pos());
+ QVariant var(QVariant::UserType);
+ var.setValue(item);
+ selectionRect->setData(1, var);
+ }
+ }
+ }
+
+ void GView::mouseReleaseEvent(QMouseEvent *event)
+ {
+ mousePressed = false;
+ }
+
+
+ void GView::moveUpSelectedItem()
+ {
+ QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>();
+ item->setZValue(item->zValue()+1);
+ }
+
+ void GView::moveDownSelectedItem()
+ {
+ QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>();
+ item->setZValue(item->zValue()-1);
+ }
+}