summaryrefslogtreecommitdiff
path: root/databasetable.cpp
blob: 494dcdfbc8ce7ec61a46c54719ea2aed03f48ab1 (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
// 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 "databasemodel.h"
#include "column.h"

DatabaseTable::DatabaseTable(QGraphicsItem *parent)
	: DatabaseModelItem(parent)
{
	setFlag(ItemIsMovable);
	setFlag(ItemIsSelectable);
	m_name = "Table";
}

QRectF
DatabaseTable::boundingRect() const
{
	return m_outerRect;
}

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

	QFont font = scene()->font();
	QFont boldFont = font;
	boldFont.setBold(true);
	QFont boldFontWithUnderline = boldFont;
	boldFontWithUnderline.setUnderline(true);

	QPen pen(QPen(QColor(0, 0, 0)));
	pen.setJoinStyle(Qt::MiterJoin);
	QPen borderPen(pen);
	if (isSelected()) {
		borderPen.setColor(QColor(0, 96, 255));
		borderPen.setWidth(2);
	}

	painter->setPen(pen);
	painter->setFont(font);

	painter->fillRect(m_outerRect, QColor(255, 255, 255));

	// Draw the table name
	painter->fillRect(m_nameBgRect, QColor(205, 205, 205));
	painter->drawLine(m_nameBgRect.bottomLeft(), m_nameBgRect.bottomRight());
	painter->drawText(m_namePos, m_name);
	
	painter->drawLine(
		m_leftSideWidth, m_nameBgRect.bottom(),
		m_leftSideWidth, m_outerRect.bottom());
	
	// Draw the table name
	QPointF colPos = m_firstColPos;
	QPointF leftSizePos = colPos - QPointF(m_leftSideWidth, 0);
	foreach (Column *column, m_columns) {
		painter->setFont(column->isRequired()
			? (column->isPrimaryKey() ? boldFontWithUnderline : boldFont)
			: font);
		painter->drawText(colPos, column->name());
		if (column->isPrimaryKey()) {
			painter->setFont(column->isRequired() ? boldFont : font);
			painter->drawText(leftSizePos, "PK");
		}
		colPos += m_colPosIncrement;
		leftSizePos += m_colPosIncrement;
	}
	
	// Draw the outside border
	painter->setPen(borderPen);
	painter->drawRect(m_outerRect);
}

void
DatabaseTable::setName(const QString &name)
{
	m_name = name;
	updateLayout();
}

Column *
DatabaseTable::addColumn(const QString &name)
{
	Column *column = new Column(this);
	column->setName(name);
	m_columns.append(column);
	updateLayout();
	return column;
}

void
DatabaseTable::setColumn(int index, const QString &name)
{
	m_columns[index]->setName(name);
	updateLayout();
}

void
DatabaseTable::removeColumn(int index)
{
	delete m_columns.takeAt(index);
	updateLayout();
}

void
DatabaseTable::swapColumns(int index1, int index2)
{
	qSwap(m_columns[index1], m_columns[index2]);
	updateLayout();
}

QVariant
DatabaseTable::itemChange(GraphicsItemChange change, const QVariant &value)
{
	if (change == ItemSceneHasChanged) {
		updateLayout();
	}
	if (change == ItemPositionChange) {
		DatabaseModel *model = qobject_cast<DatabaseModel *>(scene());
		if (model) {
			emit model->tableMoved(this);
		}
	}
	return QGraphicsItem::itemChange(change, value);
}

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

	prepareGeometryChange();

	QFont font = scene()->font();
	QFontMetricsF fontMetrics(font);

	QFont boldFont = font;
	boldFont.setBold(true);
	QFontMetricsF boldFontMetrics(boldFont);

	qreal spaceWidth = fontMetrics.width(' ');
	qreal nameWidth = fontMetrics.width(m_name);
	qreal height = fontMetrics.height();
	
	m_leftSideWidth = fontMetrics.width("PK");

	qreal width = nameWidth + spaceWidth * 2;

	qreal maxColumnWidth = 0;
	foreach (Column *column, m_columns) {
		qreal columnWidth = column->isRequired() ? boldFontMetrics.width(column->name()) : fontMetrics.width(column->name());
		maxColumnWidth = qMax(maxColumnWidth, columnWidth);
		qreal columnLeftSideWidth = 0;
		if (column->isPrimaryKey()) {
			if (column->isRequired()) {
				columnLeftSideWidth = boldFontMetrics.width("PK");
			}
			else {
				columnLeftSideWidth = fontMetrics.width("PK");
			}
		}
		m_leftSideWidth = qMax(m_leftSideWidth, columnLeftSideWidth);
	}
	m_leftSideWidth += spaceWidth * 2;
	width = qMax(nameWidth + spaceWidth * 4, m_leftSideWidth + maxColumnWidth + spaceWidth * 2);
	
	qreal nameHeight = height + spaceWidth;
	m_outerRect = QRectF(0, 0, width, nameHeight + qMax(0.66, qreal(m_columns.size())) * fontMetrics.lineSpacing()
		- fontMetrics.leading() + fontMetrics.descent());

	m_nameBgRect = QRectF(0, 0, width, nameHeight);
	m_namePos = QPointF((width - nameWidth) / 2, fontMetrics.ascent() + spaceWidth / 2);

	m_firstColPos = QPointF(m_leftSideWidth + spaceWidth * 1 - 1, nameHeight + fontMetrics.ascent() + 2);
	m_colPosIncrement = QPointF(0, fontMetrics.lineSpacing());

	update();
}