summaryrefslogtreecommitdiffstats
blob: 1041e84bd51efafbff50f2650b6d69d97a8c8782 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * 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, int pos, GView::Type type) : graphicsView_(graphicsView), pos_(pos), type_(type) {}

		void visit(WhiteboardLineElement& element) {
			QGraphicsLineItem *item;
			if (type_ == GView::New) {
				item = new QGraphicsLineItem(element.x1(), element.y1(), element.x2(), element.y2());
				graphicsView_->addItem(item, P2QSTRING(element.getID()), pos_);
			} else {
				item = qgraphicsitem_cast<QGraphicsLineItem*>(graphicsView_->getItem(P2QSTRING(element.getID())));
				QLineF line(element.x1(), element.y1(), element.x2(), element.y2());
				item->setLine(line);
				item->setPos(0,0);
				graphicsView_->deselect(P2QSTRING(element.getID()));
			}
			if (item) {
				QPen pen;
				Color color = element.getColor();
				pen.setColor(QColor(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()));
				pen.setWidth(element.getPenWidth());
				item->setPen(pen);
				QString id = P2QSTRING(element.getID());
				item->setData(100, id);
			}
		}

		void visit(WhiteboardFreehandPathElement& element) {
			FreehandLineItem *item;
			if (type_ == GView::New) {
				item = new FreehandLineItem;
			} else {
				item = qgraphicsitem_cast<FreehandLineItem*>(graphicsView_->getItem(P2QSTRING(element.getID())));
				item->setPos(0,0);
				graphicsView_->deselect(P2QSTRING(element.getID()));
			}

			if (item) {
				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));
				}

				QString id = P2QSTRING(element.getID());
				item->setData(100, id);
			}
			if (type_ == GView::New) {
				graphicsView_->addItem(item, P2QSTRING(element.getID()), pos_);
			}
		}

		void visit(WhiteboardRectElement& element) {
			QGraphicsRectItem* item;
			if (type_ == GView::New) {
				item = new QGraphicsRectItem(element.getX(), element.getY(), element.getWidth(), element.getHeight());
				graphicsView_->addItem(item, P2QSTRING(element.getID()), pos_);
			} else {
				item = qgraphicsitem_cast<QGraphicsRectItem*>(graphicsView_->getItem(P2QSTRING(element.getID())));
				QRectF rect(element.getX(), element.getY(), element.getWidth(), element.getHeight());
				item->setRect(rect);
				item->setPos(0,0);
				graphicsView_->deselect(P2QSTRING(element.getID()));
			}

			if (item) {
				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);
				QString id = P2QSTRING(element.getID());
				item->setData(100, id);
			}
		}

		void visit(WhiteboardPolygonElement& element) {
			QGraphicsPolygonItem* item = qgraphicsitem_cast<QGraphicsPolygonItem*>(graphicsView_->getItem(P2QSTRING(element.getID())));
			if (item == 0 && type_ == GView::New) {
				item = new QGraphicsPolygonItem();
				QString id = P2QSTRING(element.getID());
				item->setData(100, id);
				graphicsView_->addItem(item, id, pos_);
			}
			graphicsView_->deselect(P2QSTRING(element.getID()));
			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);

			item->setPos(0,0);
			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;
			QString id = P2QSTRING(element.getID());
			if (type_ == GView::New) {
				item = new QGraphicsTextItem;
				graphicsView_->addItem(item, id, pos_);
			} else {
				item = qgraphicsitem_cast<QGraphicsTextItem*>(graphicsView_->getItem(id));
				graphicsView_->deselect(P2QSTRING(element.getID()));
			}
			if (item) {
				item->setPlainText(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()));
				item->setData(100, id);
			}
		}

		void visit(WhiteboardEllipseElement& element) {
			QRectF rect;
			QGraphicsEllipseItem* item;
			QString id = P2QSTRING(element.getID());
			rect.setTopLeft(QPointF(element.getCX()-element.getRX(), element.getCY()-element.getRY()));
			rect.setBottomRight(QPointF(element.getCX()+element.getRX(), element.getCY()+element.getRY()));
			if (type_ == GView::New) {
				item = new QGraphicsEllipseItem(rect);
				graphicsView_->addItem(item, id, pos_);
			} else {
				item = qgraphicsitem_cast<QGraphicsEllipseItem*>(graphicsView_->getItem(id));
				item->setRect(rect);
				item->setPos(0,0);
				graphicsView_->deselect(P2QSTRING(element.getID()));
			}
			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);
			item->setData(100, id);
		}

	private:
		GView* graphicsView_;
		int pos_;
		GView::Type type_;
	};
}