// 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 #include #include "databasetable.h" #include "databasemodel.h" #include "column.h" DatabaseTable::DatabaseTable(QGraphicsItem *parent) : DatabaseModelItem(parent) { setFlag(ItemIsMovable); setFlag(ItemIsSelectable); m_name = "Table"; } DatabaseModel * DatabaseTable::model() const { return qobject_cast(scene()); } 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(); emit propertyChanged("name", name); } 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(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(); }