From 4022a373822cf0e17151dc2a599ff7310a822599 Mon Sep 17 00:00:00 2001 From: Lukáš Lalinský Date: Sun, 4 Jan 2009 20:59:00 +0100 Subject: Show "FK" next to foreign key columns --- src/hub.cpp | 15 ++++++++++++++ src/hub.h | 3 +++ src/items/database/column.cpp | 14 +++++++++++++ src/items/database/column.h | 2 ++ src/items/database/databaserelationship.cpp | 6 ++++-- src/items/database/databasetable.cpp | 32 ++++++++++++++++++++--------- src/items/database/databasetable.h | 4 +++- 7 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/hub.cpp b/src/hub.cpp index 52ca0e9..bdb1026 100644 --- a/src/hub.cpp +++ b/src/hub.cpp @@ -17,6 +17,8 @@ #include "hub.h" #include "diagramobject.h" #include "diagramdocument.h" +#include "connector.h" +#include "line.h" Hub::Hub(DiagramObject *owner) : m_owner(owner) @@ -57,3 +59,16 @@ void Hub::update() { } + +QList +Hub::outgoingLines() const +{ + QList result; + foreach (Connector *connector, m_connectors) { + Line *line = connector->owner(); + if (connector == line->connector(0)) { + result << line; + } + } + return result; +} diff --git a/src/hub.h b/src/hub.h index 83ff2fd..1e593e5 100644 --- a/src/hub.h +++ b/src/hub.h @@ -19,6 +19,7 @@ #include class Connector; +class Line; class DiagramObject; class Hub @@ -34,6 +35,8 @@ public: QSet connectors() const; + QList outgoingLines() const; + private: QSet m_connectors; DiagramObject *m_owner; diff --git a/src/items/database/column.cpp b/src/items/database/column.cpp index e8a355b..1af3b9f 100644 --- a/src/items/database/column.cpp +++ b/src/items/database/column.cpp @@ -16,6 +16,8 @@ #include "column.h" #include "columnlist.h" +#include "hub.h" +#include "databaserelationship.h" Column::Column(ColumnList *columnList) : QObject(columnList), m_primaryKey(false), m_required(false) @@ -66,3 +68,15 @@ Column::setRequired(bool required) emit propertyChanged("required", required); } } + +bool +Column::isForeignKey() const +{ + foreach (Line *line, columnList()->table()->hub()->outgoingLines()) { + DatabaseRelationship *rel = (DatabaseRelationship *)line; + if (rel->childColumn() == this) { + return true; + } + } + return false; +} diff --git a/src/items/database/column.h b/src/items/database/column.h index bbf4fff..79ca86c 100644 --- a/src/items/database/column.h +++ b/src/items/database/column.h @@ -49,6 +49,8 @@ public: bool isPrimaryKey() const { return m_primaryKey; } void setPrimaryKey(bool primaryKey); + bool isForeignKey() const; + bool isRequired() const { return m_required; } void setRequired(bool required); diff --git a/src/items/database/databaserelationship.cpp b/src/items/database/databaserelationship.cpp index 35e6658..e407ce2 100644 --- a/src/items/database/databaserelationship.cpp +++ b/src/items/database/databaserelationship.cpp @@ -19,9 +19,12 @@ #include "diagramdocument.h" #include "column.h" #include "databasetable.h" +#include "line.h" #include "databaserelationship.h" #include "databaserelationshipproperties.h" #include "utils/range.h" +#include "domutils.h" +#include "hub.h" class DatabaseRelationship::PrivateData { @@ -180,6 +183,7 @@ DatabaseRelationship::setChildColumn(Column *column) if (d->childColumn != column) { d->childColumn = column; emit propertyChanged("childColumn", column); + static_cast(connector(0)->hub()->owner())->updateLayout(); updateLayout(); update(); } @@ -368,8 +372,6 @@ DatabaseRelationship::updateLayout() updateEnds(); } -#include "domutils.h" - void DatabaseRelationship::loadFromXml(QDomElement element, DiagramDocument *document) { diff --git a/src/items/database/databasetable.cpp b/src/items/database/databasetable.cpp index a1f83c1..a987984 100644 --- a/src/items/database/databasetable.cpp +++ b/src/items/database/databasetable.cpp @@ -15,6 +15,7 @@ // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include +#include #include #include #include "databasetable.h" @@ -89,17 +90,20 @@ DatabaseTable::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, // Draw the table name QPointF colPos = m_firstColPos; QPointF leftSizePos = colPos - QPointF(m_leftSideWidth, 0); + int i = 0; foreach (Column *column, m_columnList->columns()) { - painter->setFont(column->isRequired() + bool isBold = column->isRequired(); + painter->setFont(isBold ? (column->isPrimaryKey() ? boldFontWithUnderline : boldFont) : font); painter->drawText(colPos, column->name()); - if (column->isPrimaryKey()) { - painter->setFont(column->isRequired() ? boldFont : font); - painter->drawText(leftSizePos, "PK"); + if (!m_columnLabels[i].isEmpty()) { + painter->setFont(isBold ? boldFont : font); + painter->drawText(leftSizePos, m_columnLabels[i]); } colPos += m_colPosIncrement; leftSizePos += m_colPosIncrement; + ++i; } // Draw the outside border @@ -166,17 +170,25 @@ DatabaseTable::updateLayout() qreal width = nameWidth + spaceWidth * 2; qreal maxColumnWidth = 0; + m_columnLabels.clear(); foreach (Column *column, m_columnList->columns()) { qreal columnWidth = column->isRequired() ? boldFontMetrics.width(column->name()) : fontMetrics.width(column->name()); maxColumnWidth = qMax(maxColumnWidth, columnWidth); qreal columnLeftSideWidth = 0; + QStringList label; if (column->isPrimaryKey()) { - if (column->isRequired()) { - columnLeftSideWidth = boldFontMetrics.width("PK"); - } - else { - columnLeftSideWidth = fontMetrics.width("PK"); - } + label << "PK"; + } + if (column->isForeignKey()) { + label << "FK"; + } + QString labelStr = label.join(","); + m_columnLabels << labelStr; + if (column->isRequired()) { + columnLeftSideWidth = boldFontMetrics.width(labelStr); + } + else { + columnLeftSideWidth = fontMetrics.width(labelStr); } m_leftSideWidth = qMax(m_leftSideWidth, columnLeftSideWidth); } diff --git a/src/items/database/databasetable.h b/src/items/database/databasetable.h index c2306e2..df3d630 100644 --- a/src/items/database/databasetable.h +++ b/src/items/database/databasetable.h @@ -19,6 +19,7 @@ #include #include +#include #include "diagramobject.h" class Column; @@ -62,7 +63,7 @@ public: static DiagramItemProperties *createPropertiesEditor(QWidget *parent = 0); -protected slots: +public slots: void updateLayout(); protected: @@ -71,6 +72,7 @@ protected: private: QString m_name; ColumnList *m_columnList; + QList m_columnLabels; QRectF m_outerRect, m_nameBgRect; QPointF m_namePos, m_firstColPos, m_colPosIncrement; -- cgit v1.2.3-54-g00ecf