summaryrefslogtreecommitdiff
path: root/src/items/database/databaserelationship.cpp
blob: 02540c5d1954c9fe9edbdc654932e509e23241ea (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
// 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 <QGraphicsScene>
#include <QDebug>
#include "databasetable.h"
#include "databaserelationship.h"
#include "range.h"

DatabaseRelationship::DatabaseRelationship(DiagramItem *parent)
	: DiagramConnection(parent)
{
	setFlag(ItemIsSelectable);
	connect(this, SIGNAL(endPointChanged()), this, SLOT(updateLayout()));
}

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

QPainterPath
DatabaseRelationship::shape() const
{
	QPainterPath path;
	if (m_line.isEmpty())
		return path;
	path.moveTo(m_line[0]);
	for (int i = 1; i < m_line.size(); i++)
		path.lineTo(m_line[i]);
	path.addPolygon(m_arrowHead);
	QPen pen(QPen(QColor(0, 0, 0), 1));
	pen.setJoinStyle(Qt::MiterJoin);
	QPainterPathStroker ps;
	ps.setCapStyle(pen.capStyle());
	ps.setWidth(pen.widthF() + 0.3);
	ps.setJoinStyle(pen.joinStyle());
	ps.setMiterLimit(pen.miterLimit());
	return ps.createStroke(path);
}

void
DatabaseRelationship::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	Q_UNUSED(option);
	Q_UNUSED(widget);

	QPen pen(QPen(QColor(0, 0, 0), 1));
	pen.setJoinStyle(Qt::MiterJoin);
	painter->setPen(pen);
	painter->drawPolyline(m_line);
	painter->setBrush(pen.color());
	painter->drawPolygon(m_arrowHead);
}

QVariant
DatabaseRelationship::itemChange(GraphicsItemChange change, const QVariant &value)
{
	if (change == ItemSceneHasChanged) {
		updateLayout();
	}
	return QGraphicsItem::itemChange(change, value);
}

void
DatabaseRelationship::updatePositions()
{
	updateLayout();
	update();
}

void
DatabaseRelationship::updateLayout()
{
	if (!scene())
		return;

	DatabaseTable *source = sourceTable();
	DatabaseTable *target = targetTable();
	if (!source || !target)
		return;

	prepareGeometryChange();

	QRectF rect1 = source->boundingRect().translated(source->pos());
	QRectF rect2 = target->boundingRect().translated(target->pos());

	bool haveLine = false;

	Range<qreal> verticalRange1(rect1.top(), rect1.bottom());
	Range<qreal> verticalRange2(rect2.top(), rect2.bottom());
	Range<qreal> verticalIntersection = verticalRange1.intersected(verticalRange2);

	Range<qreal> horizontalRange1(rect1.left(), rect1.right());
	Range<qreal> horizontalRange2(rect2.left(), rect2.right());
	Range<qreal> horizontalIntersection = horizontalRange1.intersected(horizontalRange2);

	// Straight horizontal line
	if (0 && !haveLine) {
		if (!verticalIntersection.isNull() && horizontalIntersection.isNull()) {
			qreal y = verticalIntersection.min() + verticalIntersection.length() / 2;
			if (rect1.right() < rect2.left()) {
				m_line = QPolygonF() << QPointF(rect1.right(), y) << QPointF(rect2.left(), y);
			}
			else {
				m_line = QPolygonF() << QPointF(rect1.left(), y) << QPointF(rect2.right(), y);
			}
			haveLine = true;
		}
	}

	// Straight vertical line
	if (0 && !haveLine) {
		if (verticalIntersection.isNull() && !horizontalIntersection.isNull()) {
			qreal x = horizontalIntersection.min() + horizontalIntersection.length() / 2;
			if (rect1.bottom() < rect2.top()) {
				m_line = QPolygonF() << QPointF(x, rect1.bottom()) << QPointF(x, rect2.top());
			}
			else {
				m_line = QPolygonF() << QPointF(x, rect1.top()) << QPointF(x, rect2.bottom());
			}
			haveLine = true;
		}
	}

	// Two-segment line
	if (0 && !haveLine) {

		qreal x1, x2, x3, y1, y2, y3;

		if (rect1.right() < rect2.left()) {
			x1 = rect1.right();
			y1 = verticalRange1.center();

			x2 = horizontalRange2.center();
			y2 = verticalRange1.center();

			if (rect1.bottom() < rect2.top()) {
				x3 = horizontalRange2.center();
				y3 = rect2.top();
			}
			else {
				x3 = horizontalRange2.center();
				y3 = rect2.bottom();
			}
		}
		else {
			x1 = rect1.left();
			y1 = verticalRange1.center();

			x2 = horizontalRange2.center();
			y2 = verticalRange1.center();

			if (rect1.bottom() < rect2.top()) {
				x3 = horizontalRange2.center();
				y3 = rect2.top();
			}
			else {
				x3 = horizontalRange2.center();
				y3 = rect2.bottom();
			}
		}

		m_line = QPolygonF()
			<< QPointF(x1, y1)
			<< QPointF(x2, y2)
			<< QPointF(x3, y3);
		haveLine = true;
	}

	// Simple center<->center line
	if (!haveLine) {
		QPointF p1 = rect1.center();
		QPointF p2 = rect2.center();
		QLineF finalLine = QLineF(p1, p2);

		QPolygonF polygon;
	
		polygon = rect1;
		for (int i = 0; i < 4; i++) {
			QLineF line(polygon[i], polygon[(i+1)%4]);
			QPointF intersectionPoint;
			if (finalLine.intersect(line, &intersectionPoint) == QLineF::BoundedIntersection) {
				finalLine.setP1(intersectionPoint);
				break;
			}
		}
	
		polygon = rect2;
		for (int i = 0; i < 4; i++) {
			QLineF line(polygon[i], polygon[(i+1)%4]);
			QPointF intersectionPoint;
			if (finalLine.intersect(line, &intersectionPoint) == QLineF::BoundedIntersection) {
				finalLine.setP2(intersectionPoint);
				break;
			}
		}

		m_line = QPolygonF() << finalLine.p1() << finalLine.p2();
		haveLine = true;
	}

	Q_ASSERT(haveLine);

	int size = m_line.size();
	QLineF lastSegment(m_line[size-2], m_line[size-1]);

	m_arrowHead.resize(3);
	m_arrowHead[0] = QPointF(0.0, 0.0);
	m_arrowHead[1] = QPointF(-4.5, 10.0);
	m_arrowHead[2] = QPointF(4.5, 10.0);
	QMatrix matrix;
	matrix.rotate(-lastSegment.angle() + 90);
	m_arrowHead = matrix.map(m_arrowHead);
	m_arrowHead.translate(lastSegment.p2());
}

#include "domutils.h"

void
DatabaseRelationship::loadFromXml(QDomElement element, DiagramDocument *document)
{
	DiagramConnection::loadFromXml(element, document);
}

void
DatabaseRelationship::saveToXml(QDomDocument doc, QDomElement element)
{
	DiagramConnection::saveToXml(doc, element);
}