summaryrefslogtreecommitdiff
path: root/src/line.cpp
blob: 527fab3662d7d79eea664a2c332a5c0c19c7625d (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (C) 2008  Lukas Lalinsky
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include <QPainter>
#include "line.h"
#include "hub.h"
#include "diagramdocument.h"
#include "diagramobject.h"
#include "connector.h"

Line::Line(DiagramItem *parent)
	: DiagramItem(parent), m_fillStartArrow(false), m_fillEndArrow(false), m_dirty(false)
{
	m_connectors[0] = new Connector(this);
	m_connectors[1] = new Connector(this);
	setZValue(1.0);
}

Line::~Line()
{
	delete m_connectors[0];
	delete m_connectors[1];
}

Connector *
Line::connector(int index) const
{
	Q_ASSERT(index == 0 || index == 1);
	return m_connectors[index];
}

void
Line::updateLayout()
{
}

#include "domutils.h"

void
Line::loadFromXml(QDomElement element, DiagramDocument *document)
{
	DiagramItem::loadFromXml(element, document);
	QDomElement lineElement = element.firstChildElement("line");
	if (lineElement.isNull())
		return;
	QDomElement connectorListElement = lineElement.firstChildElement("connector-list");
	if (connectorListElement.isNull())
		return;

	QDomElement connectorElement = connectorListElement.firstChildElement("connector");
	int i = 0;
	while (!connectorElement.isNull()) {
		Connector *connector = m_connectors[i++];
		qreal angle = readFloatElement(connectorElement, "angle");
		connector->setAngle(angle);
		connector->setPos(readPointElement(connectorElement, "position"));
		QDomElement hubElement = connectorElement.firstChildElement("hub");
		QString ownerId = hubElement.attribute("owner");
		if (document && !ownerId.isEmpty()) {
			DiagramObject *owner = qobject_cast<DiagramObject *>(document->itemById(ownerId));
			connector->setHub(owner->hub());
		}
		connectorElement = connectorElement.nextSiblingElement("connector");
	}
}

void
Line::saveToXml(QDomDocument doc, QDomElement element)
{
	DiagramItem::saveToXml(doc, element);
	QDomElement lineElement = doc.createElement("line");
	element.appendChild(lineElement);
	QDomElement connectorListElement = doc.createElement("connector-list");
	lineElement.appendChild(connectorListElement);
	for (int i = 0; i < 2; i++) {
		Connector *connector = m_connectors[i];
		QDomElement connectorElement = doc.createElement("connector");
		connectorListElement.appendChild(connectorElement);
		appendPointElement(doc, connectorElement, "position", connector->pos());
		appendFloatElement(doc, connectorElement, "angle", connector->angle());
		if (connector->hub()) {
			QDomElement hubElement = doc.createElement("hub");
			connectorElement.appendChild(hubElement);
			hubElement.setAttribute("owner", connector->hub()->owner()->id());
		}
	}
}

QPolygonF
Line::linePoints() const
{
	return m_linePoints;
}

void
Line::setLinePoints(const QPolygonF& points)
{
	if (m_linePoints != points) {
		prepareGeometryChange();
		m_linePoints = points;
		m_dirty = true;
	}
}

Qt::PenStyle
Line::lineStyle() const
{
	return m_lineStyle;
}

void
Line::setLineStyle(Qt::PenStyle style)
{
	if (m_lineStyle != style) {
		// prepareGeometryChange();
		m_lineStyle = style;
		// m_dirty = true;
	}
}

QPainterPath
Line::startArrow() const
{
	return m_startArrow;
}

void
Line::setStartArrow(const QPainterPath& path)
{
	if (m_startArrow != path) {
		prepareGeometryChange();
		m_startArrow = path;
		m_dirty = true;
	}
}

bool
Line::fillStartArrow() const
{
	return m_fillStartArrow;
}

void
Line::setFillStartArrow(bool fill)
{
	if (m_fillStartArrow != fill) {
		m_fillStartArrow = fill;
		m_dirty = true;
	}
}

QPainterPath
Line::endArrow() const
{
	return m_endArrow;
}

void
Line::setEndArrow(const QPainterPath& path)
{
	if (m_endArrow != path) {
		prepareGeometryChange();
		m_endArrow = path;
		m_dirty = true;
	}
}

bool
Line::fillEndArrow() const
{
	return m_fillEndArrow;
}

void
Line::setFillEndArrow(bool fill)
{
	if (m_fillEndArrow != fill) {
		m_fillEndArrow = fill;
		m_dirty = true;
	}
}

void
Line::updateCache()
{
	m_dirty = false;

	QPainterPath path;
	if (m_linePoints.isEmpty()) {
		// Nothing to process
		m_shape = path;
		return;
	}

	if (!m_startArrow.isEmpty()) {
		QLineF firstSegment(m_linePoints.at(0), m_linePoints.at(1));
		QMatrix matrix;
		matrix.translate(firstSegment.p1().x(), firstSegment.p1().y());
		matrix.rotate(360 - firstSegment.angle() + 90 + 180);
		m_transformedStartArrow = matrix.map(m_startArrow);
	}
	else {
		m_transformedStartArrow = QPainterPath();
	}

	if (!m_endArrow.isEmpty()) {
		int size = m_linePoints.size();
		QLineF lastSegment(m_linePoints.at(size-2), m_linePoints.at(size-1));
		QMatrix matrix;
		matrix.translate(lastSegment.p2().x(), lastSegment.p2().y());
		matrix.rotate(-lastSegment.angle() + 90);
		m_transformedEndArrow = matrix.map(m_endArrow);
	}
	else {
		m_transformedEndArrow = QPainterPath();
	}

	// Add the main line
	path.moveTo(m_linePoints.at(0));
	for (int i = 1; i < m_linePoints.size(); i++)
		path.lineTo(m_linePoints.at(i));

	path.addPath(m_transformedStartArrow);
	path.addPath(m_transformedEndArrow);

	// Add an outline around the path
	QPainterPathStroker ps;
	ps.setWidth(4.33);
	ps.setJoinStyle(Qt::MiterJoin);
	m_shape = ps.createStroke(path);
}

QRectF
Line::boundingRect() const
{
	return shape().controlPointRect();
}

QPainterPath
Line::shape() const
{
	if (m_dirty)
		const_cast<Line *>(this)->updateCache();

	return m_shape;
}

void
Line::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
	if (m_dirty)
		updateCache();

	QPen pen(QPen(QColor(0, 0, 0), 1.33));
	pen.setJoinStyle(Qt::MiterJoin);
	pen.setStyle(m_lineStyle);
	if (!document()->isPrinting() && isSelected()) {
		pen.setColor(QColor(0, 96, 255));
	}

	painter->setPen(pen);
	painter->drawPolyline(m_linePoints);

	pen.setStyle(Qt::SolidLine);
	painter->setPen(pen);
	if (m_fillStartArrow) {
		painter->setBrush(pen.color());
	}
	painter->drawPath(m_transformedStartArrow);
	if (m_fillEndArrow) {
		if (!m_fillStartArrow) {
			painter->setBrush(pen.color());
		}
	}
	else {
		if (m_fillStartArrow) {
			painter->setBrush(QBrush());
		}
	}
	painter->drawPath(m_transformedEndArrow);
}