summaryrefslogtreecommitdiffstats
blob: 214ea10f414d0ebdea829989eaebead0e32e8224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2012 Mateusz Piękos
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#pragma once

#include <Swiften/Whiteboard/Elements/WhiteboardElementVisitor.h>
#include <Swiften/Whiteboard/Elements/WhiteboardLineElement.h>
#include <Swiften/Whiteboard/Elements/WhiteboardPolygonElement.h>
#include <Swiften/Whiteboard/Elements/WhiteboardTextElement.h>
#include <Swiften/Whiteboard/Elements/WhiteboardEllipseElement.h>
#include <Swiften/Whiteboard/Elements/WhiteboardFreehandPathElement.h>
#include <Swift/QtUI/Whiteboard/GView.h>
#include <QtSwiftUtil.h>

namespace Swift {
	class WhiteboardElementDrawingVisitor : public WhiteboardElementVisitor {
	public:
		WhiteboardElementDrawingVisitor(GView* graphicsView) : graphicsView_(graphicsView) {}

		void visit(WhiteboardLineElement& element) {
			QGraphicsLineItem *item = new QGraphicsLineItem(element.x1(), element.y1(), element.x2(), element.y2());
			QPen pen;
			Color color = element.getColor();
			pen.setColor(QColor(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()));
			pen.setWidth(element.getPenWidth());
			item->setPen(pen);
			graphicsView_->scene()->addItem(item);
		}

		void visit(WhiteboardFreehandPathElement& element) {
			FreehandLineItem *item = new FreehandLineItem;
			QPen pen;
			Color color = element.getColor();
			pen.setColor(QColor(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()));
			pen.setWidth(element.getPenWidth());
			item->setPen(pen);

			std::vector<std::pair<int, int> >::const_iterator it = element.getPoints().begin();
			item->setStartPoint(QPointF(it->first, it->second));
			for (++it; it != element.getPoints().end(); ++it) {
				item->lineTo(QPointF(it->first, it->second));
			}

			graphicsView_->scene()->addItem(item);
		}

		void visit(WhiteboardRectElement& element) {
			QGraphicsRectItem* item = new QGraphicsRectItem(element.getX(), element.getY(), element.getWidth(), element.getHeight());
			QPen pen;
			QBrush brush(Qt::SolidPattern);
			Color penColor = element.getPenColor();
			Color brushColor = element.getBrushColor();
			pen.setColor(QColor(penColor.getRed(), penColor.getGreen(), penColor.getBlue(), penColor.getAlpha()));
			pen.setWidth(element.getPenWidth());
			brush.setColor(QColor(brushColor.getRed(), brushColor.getGreen(), brushColor.getBlue(), brushColor.getAlpha()));
			item->setPen(pen);
			item->setBrush(brush);
			graphicsView_->scene()->addItem(item);
		}

		void visit(WhiteboardPolygonElement& element) {
			QGraphicsPolygonItem* item = qgraphicsitem_cast<QGraphicsPolygonItem*>(graphicsView_->getItem(P2QSTRING(element.getID())));
			if (item == 0) {
				item = new QGraphicsPolygonItem();
				QPen pen;
				QBrush brush(Qt::SolidPattern);
				Color penColor = element.getPenColor();
				Color brushColor = element.getBrushColor();
				pen.setColor(QColor(penColor.getRed(), penColor.getGreen(), penColor.getBlue(), penColor.getAlpha()));
				pen.setWidth(element.getPenWidth());
				brush.setColor(QColor(brushColor.getRed(), brushColor.getGreen(), brushColor.getBlue(), brushColor.getAlpha()));
				item->setPen(pen);
				item->setBrush(brush);
				graphicsView_->scene()->addItem(item);
			}
			QPolygonF polygon;
			std::vector<std::pair<int, int> >::const_iterator it = element.getPoints().begin();
			for (; it != element.getPoints().end(); ++it) {
				polygon.append(QPointF(it->first, it->second));
			}
			item->setPolygon(polygon);
		}

		void visit(WhiteboardTextElement& element) {
			QGraphicsTextItem* item = new QGraphicsTextItem(P2QSTRING(element.getText()));
			item->setPos(QPointF(element.getX(), element.getY()));
			QFont font = item->font();
			font.setPointSize(element.getSize());
			item->setFont(font);
			Color color = element.getColor();
			item->setDefaultTextColor(QColor(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()));
			graphicsView_->scene()->addItem(item);
		}

		void visit(WhiteboardEllipseElement& element) {
			QRectF rect;

			rect.setTopLeft(QPointF(element.getCX()-element.getRX(), element.getCY()-element.getRY()));
			rect.setBottomRight(QPointF(element.getCX()+element.getRX(), element.getCY()+element.getRY()));

			QGraphicsEllipseItem* item = new QGraphicsEllipseItem(rect);
			QPen pen;
			QBrush brush(Qt::SolidPattern);
			Color penColor = element.getPenColor();
			Color brushColor = element.getBrushColor();
			pen.setColor(QColor(penColor.getRed(), penColor.getGreen(), penColor.getBlue(), penColor.getAlpha()));
			pen.setWidth(element.getPenWidth());
			brush.setColor(QColor(brushColor.getRed(), brushColor.getGreen(), brushColor.getBlue(), brushColor.getAlpha()));
			item->setPen(pen);
			item->setBrush(brush);
			graphicsView_->scene()->addItem(item);
		}

	private:
		GView* graphicsView_;
	};
}