summaryrefslogtreecommitdiff
path: root/src/items/database/databaserelationship.h
blob: 044a89178fa7d8e8afad0f3ccbd6c67f36dd9b77 (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
// 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.

#ifndef DATABASERELATION_H
#define DATABASERELATION_H

#include <QPair>
#include <QGraphicsItem>
#include <QPainter>
#include "line.h"
#include "databasetable.h"
#include "connector.h"

class DatabaseRelationship : public Line
{
	Q_OBJECT
	Q_ENUMS(Cardinality Action)
	Q_PROPERTY(Cardinality cardinality READ cardinality WRITE setCardinality);
	Q_PROPERTY(bool childOptional READ isChildOptional WRITE setChildOptional);
	Q_PROPERTY(bool parentOptional READ isParentOptional WRITE setParentOptional);
	Q_PROPERTY(Column* childColumn READ childColumn WRITE setChildColumn);
	Q_PROPERTY(Column* parentColumn READ parentColumn WRITE setParentColumn);

public:
	DatabaseRelationship(DiagramItem *parent = 0);
	~DatabaseRelationship();
	
	QRectF boundingRect() const;
	QPainterPath shape() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

	DatabaseTable *childTable() const { return qobject_cast<DatabaseTable *>(connector(0)->connectedObject()); }
	DatabaseTable *parentTable() const { return qobject_cast<DatabaseTable *>(connector(1)->connectedObject()); }

	enum { Type = DiagramItem::Relation };
	virtual int type() const { return Type; }

	static const char *staticTypeName() { return "database-relationship"; }
	virtual const char *typeName() { return staticTypeName(); }

	void loadFromXml(QDomElement element, DiagramDocument *document = 0);
	void saveToXml(QDomDocument doc, QDomElement element);

	//! Returns true if the foreign key on the child table is also the primary key
	bool isIdentifying() const;

	bool isChildOptional() const;
	void setChildOptional(bool optional);

	bool isParentOptional() const;
	void setParentOptional(bool optional);

	enum Cardinality {
		OneToOne,
		OneToMany,
		ManyToMany,
	};

	//! Returns the cardinality of the relationship
	Cardinality cardinality() const;

	//! Sets the relationship cardinality
	void setCardinality(Cardinality cardinality);

	//! List of columns in the child (referencing) table
	Column *childColumn() const;

	void setChildColumn(Column *column);

	//! List of columns in the parent (referenced) table
	Column *parentColumn() const;

	void setParentColumn(Column *column);

	enum Action {
		NoAction,
		Restrict,
		Cascade,
		SetNull,
		SetDefault
	};

	Action onUpdateAction() const;

	Action onDeleteAction() const;

	static DiagramItemProperties *createPropertiesEditor(QWidget *parent = 0);

public slots:
	void updateLayout();
	void updateEnds();

protected:
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);

private:
	class PrivateData;
	PrivateData *const d;
};

#endif