packageitem.cpp 13.1 KB
Newer Older
Jochen Becher's avatar
Jochen Becher committed
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
/***************************************************************************
**
** Copyright (C) 2015 Jochen Becher
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "packageitem.h"

#include "qmt/diagram_controller/diagramcontroller.h"
#include "qmt/diagram/dpackage.h"
#include "qmt/diagram_scene/diagramsceneconstants.h"
#include "qmt/diagram_scene/diagramscenemodel.h"
#include "qmt/diagram_scene/parts/contextlabelitem.h"
#include "qmt/diagram_scene/parts/customiconitem.h"
#include "qmt/diagram_scene/parts/relationstarter.h"
#include "qmt/diagram_scene/parts/stereotypesitem.h"
#include "qmt/infrastructure/geometryutilities.h"
#include "qmt/stereotype/stereotypecontroller.h"
#include "qmt/stereotype/stereotypeicon.h"
#include "qmt/style/style.h"
#include "qmt/tasks/diagramscenecontroller.h"

#include <QGraphicsScene>
#include <QBrush>
#include <QPen>
#include <QFont>

#include <algorithm>

namespace qmt {

static const qreal TAB_HORIZ_BORDER = 4.0;
static const qreal TAB_VERT_BORDER = 4.0;
static const qreal TAB_MIN_RIGHT_SPACE = 16.0;
static const qreal BODY_VERT_BORDER = 4.0;
static const qreal BODY_HORIZ_BORDER = 4.0;
static const qreal BODY_MIN_HEIGHT = 24.0;
static const qreal MINIMUM_AUTO_WIDTH = 100.0;
static const qreal MINIMUM_AUTO_HEIGHT = 70.0;

65 66 67
class PackageItem::ShapeGeometry
{
public:
68 69 70
    ShapeGeometry(const QSizeF &minimumSize, const QSizeF &minimumTabSize)
        : m_minimumSize(minimumSize),
          m_minimumTabSize(minimumTabSize)
Jochen Becher's avatar
Jochen Becher committed
71 72 73
    {
    }

74 75
    QSizeF m_minimumSize;
    QSizeF m_minimumTabSize;
Jochen Becher's avatar
Jochen Becher committed
76 77
};

78
PackageItem::PackageItem(DPackage *package, DiagramSceneModel *diagramSceneModel, QGraphicsItem *parent)
79
    : ObjectItem(package, diagramSceneModel, parent)
Jochen Becher's avatar
Jochen Becher committed
80 81 82 83 84 85 86 87 88 89 90 91
{
}

PackageItem::~PackageItem()
{
}

void PackageItem::update()
{
    prepareGeometryChange();
    updateStereotypeIconDisplay();

92
    const Style *style = adaptedStyle(stereotypeIconId());
Jochen Becher's avatar
Jochen Becher committed
93 94

    // custom icon
95
    if (stereotypeIconDisplay() == StereotypeIcon::DisplayIcon) {
96
        if (!m_customIcon)
97 98 99 100 101
            m_customIcon = new CustomIconItem(diagramSceneModel(), this);
        m_customIcon->setStereotypeIconId(stereotypeIconId());
        m_customIcon->setBaseSize(stereotypeIconMinimumSize(m_customIcon->stereotypeIcon(), CUSTOM_ICON_MINIMUM_AUTO_WIDTH, CUSTOM_ICON_MINIMUM_AUTO_HEIGHT));
        m_customIcon->setBrush(style->fillBrush());
        m_customIcon->setPen(style->outerLinePen());
102 103 104 105 106
        m_customIcon->setZValue(SHAPE_ZVALUE);
    } else if (m_customIcon) {
        m_customIcon->scene()->removeItem(m_customIcon);
        delete m_customIcon;
        m_customIcon = 0;
Jochen Becher's avatar
Jochen Becher committed
107 108 109
    }

    // shape
110
    if (!m_customIcon) {
111
        if (!m_shape)
112
            m_shape = new QGraphicsPolygonItem(this);
113 114
        m_shape->setBrush(style->fillBrush());
        m_shape->setPen(style->outerLinePen());
115 116 117 118 119
        m_shape->setZValue(SHAPE_ZVALUE);
    } else if (m_shape) {
        m_shape->scene()->removeItem(m_shape);
        delete m_shape;
        m_shape = 0;
Jochen Becher's avatar
Jochen Becher committed
120 121 122
    }

    // stereotypes
123
    updateStereotypes(stereotypeIconId(), stereotypeIconDisplay(), style);
Jochen Becher's avatar
Jochen Becher committed
124 125

    // package name
126
    if (!m_packageName)
127
        m_packageName = new QGraphicsSimpleTextItem(this);
128 129 130
    m_packageName->setBrush(style->textBrush());
    m_packageName->setFont(style->headerFont());
    m_packageName->setText(object()->name());
Jochen Becher's avatar
Jochen Becher committed
131 132 133

    // context
    if (showContext()) {
134
        if (!m_contextLabel)
135
            m_contextLabel = new ContextLabelItem(this);
136 137 138
        m_contextLabel->setFont(style->smallFont());
        m_contextLabel->setBrush(style->textBrush());
        m_contextLabel->setContext(object()->context());
139 140 141 142
    } else if (m_contextLabel) {
        m_contextLabel->scene()->removeItem(m_contextLabel);
        delete m_contextLabel;
        m_contextLabel = 0;
Jochen Becher's avatar
Jochen Becher committed
143 144
    }

145
    updateSelectionMarker(m_customIcon);
Jochen Becher's avatar
Jochen Becher committed
146 147 148

    // relation starters
    if (isFocusSelected()) {
149
        if (!m_relationStarter) {
150
            m_relationStarter = new RelationStarter(this, diagramSceneModel(), 0);
151 152
            scene()->addItem(m_relationStarter);
            m_relationStarter->setZValue(RELATION_STARTER_ZVALUE);
153
            m_relationStarter->addArrow(QStringLiteral("dependency"), ArrowItem::ShaftDashed, ArrowItem::HeadOpen);
Jochen Becher's avatar
Jochen Becher committed
154
        }
155 156 157 158
    } else if (m_relationStarter) {
        scene()->removeItem(m_relationStarter);
        delete m_relationStarter;
        m_relationStarter = 0;
Jochen Becher's avatar
Jochen Becher committed
159 160 161 162 163 164
    }

    updateAlignmentButtons();
    updateGeometry();
}

165
bool PackageItem::intersectShapeWithLine(const QLineF &line, QPointF *intersectionPoint, QLineF *intersectionLine) const
Jochen Becher's avatar
Jochen Becher committed
166 167
{
    QPolygonF polygon;
168
    if (m_customIcon) {
169
        // TODO use customIcon path as shape
170 171
        QRectF rect = object()->rect();
        rect.translate(object()->pos());
Jochen Becher's avatar
Jochen Becher committed
172 173
        polygon << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft() << rect.topLeft();
    } else {
174 175
        QRectF rect = object()->rect();
        rect.translate(object()->pos());
Jochen Becher's avatar
Jochen Becher committed
176
        ShapeGeometry shape = calcMinimumGeometry();
177 178 179
        polygon << rect.topLeft() << (rect.topLeft() + QPointF(shape.m_minimumTabSize.width(), 0.0))
                << (rect.topLeft() + QPointF(shape.m_minimumTabSize.width(), shape.m_minimumTabSize.height()))
                << rect.topRight() + QPointF(0.0, shape.m_minimumTabSize.height())
Jochen Becher's avatar
Jochen Becher committed
180 181
                << rect.bottomRight() << rect.bottomLeft() << rect.topLeft();
    }
182
    return GeometryUtilities::intersect(polygon, line, intersectionPoint, intersectionLine);
Jochen Becher's avatar
Jochen Becher committed
183 184
}

185
QSizeF PackageItem::minimumSize() const
Jochen Becher's avatar
Jochen Becher committed
186 187
{
    ShapeGeometry geometry = calcMinimumGeometry();
188
    return geometry.m_minimumSize;
Jochen Becher's avatar
Jochen Becher committed
189 190
}

191
QList<ILatchable::Latch> PackageItem::horizontalLatches(ILatchable::Action action, bool grabbedItem) const
Jochen Becher's avatar
Jochen Becher committed
192
{
193
    return ObjectItem::horizontalLatches(action, grabbedItem);
Jochen Becher's avatar
Jochen Becher committed
194 195
}

196
QList<ILatchable::Latch> PackageItem::verticalLatches(ILatchable::Action action, bool grabbedItem) const
Jochen Becher's avatar
Jochen Becher committed
197
{
198
    return ObjectItem::verticalLatches(action, grabbedItem);
Jochen Becher's avatar
Jochen Becher committed
199 200
}

201
QPointF PackageItem::relationStartPos() const
Jochen Becher's avatar
Jochen Becher committed
202 203 204 205
{
    return pos();
}

206
void PackageItem::relationDrawn(const QString &id, const QPointF &toScenePos, const QList<QPointF> &intermediatePoints)
Jochen Becher's avatar
Jochen Becher committed
207
{
208
    DElement *targetElement = diagramSceneModel()->findTopmostElement(toScenePos);
209
    if (targetElement) {
Jochen Becher's avatar
Jochen Becher committed
210
        if (id == QStringLiteral("dependency")) {
Jochen Becher's avatar
Jochen Becher committed
211
            auto dependantObject = dynamic_cast<DObject *>(targetElement);
212
            if (dependantObject)
213
                diagramSceneModel()->diagramSceneController()->createDependency(object(), dependantObject, intermediatePoints, diagramSceneModel()->diagram());
Jochen Becher's avatar
Jochen Becher committed
214 215 216 217 218 219 220 221
        }
    }
}

PackageItem::ShapeGeometry PackageItem::calcMinimumGeometry() const
{
    double width = 0.0;
    double height = 0.0;
222 223
    double tabHeight = 0.0;
    double tabWidth = 0.0;
Jochen Becher's avatar
Jochen Becher committed
224

225 226 227 228
    if (m_customIcon) {
        return ShapeGeometry(stereotypeIconMinimumSize(m_customIcon->stereotypeIcon(), CUSTOM_ICON_MINIMUM_AUTO_WIDTH,
                                                       CUSTOM_ICON_MINIMUM_AUTO_HEIGHT), QSizeF(tabWidth, tabHeight));
    }
229 230
    double bodyHeight = 0.0;
    double bodyWidth = 0.0;
Jochen Becher's avatar
Jochen Becher committed
231

232
    tabHeight += TAB_VERT_BORDER;
233
    if (StereotypesItem *stereotypesItem = this->stereotypesItem()) {
234 235
        tabWidth = std::max(tabWidth, stereotypesItem->boundingRect().width() + 2 * TAB_HORIZ_BORDER);
        tabHeight += stereotypesItem->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
236
    }
237
    if (m_packageName) {
238 239
        tabWidth = std::max(tabWidth, m_packageName->boundingRect().width() + 2 * TAB_HORIZ_BORDER);
        tabHeight += m_packageName->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
240
    }
241 242 243 244 245
    tabHeight += TAB_VERT_BORDER;
    width = std::max(width, tabWidth + TAB_MIN_RIGHT_SPACE);
    height += tabHeight;

    bodyHeight = BODY_VERT_BORDER;
246
    if (CustomIconItem *stereotypeIconItem = this->stereotypeIconItem()) {
247 248
        bodyWidth = std::max(bodyWidth, stereotypeIconItem->boundingRect().width() + 2 * BODY_HORIZ_BORDER);
        bodyHeight += stereotypeIconItem->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
249
    }
250
    if (m_contextLabel)
251
        bodyHeight += m_contextLabel->height();
252 253 254 255
    bodyHeight += BODY_VERT_BORDER;
    bodyHeight = std::max(bodyHeight, BODY_MIN_HEIGHT);
    width = std::max(width, bodyWidth);
    height += bodyHeight;
Jochen Becher's avatar
Jochen Becher committed
256

257
    return ShapeGeometry(GeometryUtilities::ensureMinimumRasterSize(QSizeF(width, height), 2 * RASTER_WIDTH, 2 * RASTER_HEIGHT), QSizeF(tabWidth, tabHeight));
Jochen Becher's avatar
Jochen Becher committed
258 259 260 261 262 263 264 265
}

void PackageItem::updateGeometry()
{
    prepareGeometryChange();

    ShapeGeometry geometry = calcMinimumGeometry();

266 267
    double width = geometry.m_minimumSize.width();
    double height = geometry.m_minimumSize.height();
268 269
    double tabWidth = geometry.m_minimumTabSize.width();
    double tabHeight = geometry.m_minimumTabSize.height();
Jochen Becher's avatar
Jochen Becher committed
270 271

    // calc width and height
272
    if (object()->isAutoSized()) {
273
        if (!m_customIcon) {
274
            if (width < MINIMUM_AUTO_WIDTH)
Jochen Becher's avatar
Jochen Becher committed
275
                width = MINIMUM_AUTO_WIDTH;
276
            if (height < MINIMUM_AUTO_HEIGHT)
Jochen Becher's avatar
Jochen Becher committed
277 278 279
                height = MINIMUM_AUTO_HEIGHT;
        }
    } else {
280
        QRectF rect = object()->rect();
281
        if (rect.width() > width)
Jochen Becher's avatar
Jochen Becher committed
282
            width = rect.width();
283
        if (rect.height() > height)
Jochen Becher's avatar
Jochen Becher committed
284 285 286 287 288 289 290 291 292
            height = rect.height();
    }

    // update sizes and positions
    double left = -width / 2.0;
    double right = width / 2.0;
    double top = -height / 2.0;
    double y = top;

293
    setPos(object()->pos());
Jochen Becher's avatar
Jochen Becher committed
294 295 296 297 298 299

    QRectF rect(left, top, width, height);

    // the object is updated without calling DiagramController intentionally.
    // attribute rect is not a real attribute stored on DObject but
    // a backup for the graphics item used for manual resized and persistency.
300
    object()->setRect(rect);
Jochen Becher's avatar
Jochen Becher committed
301

302 303 304
    if (m_customIcon) {
        m_customIcon->setPos(left, top);
        m_customIcon->setActualSize(QSizeF(width, height));
Jochen Becher's avatar
Jochen Becher committed
305 306 307
        y += height;

        y += BODY_VERT_BORDER;
308
        if (StereotypesItem *stereotypesItem = this->stereotypesItem()) {
309 310
            stereotypesItem->setPos(-stereotypesItem->boundingRect().width() / 2.0, y);
            y += stereotypesItem->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
311
        }
312 313 314
        if (m_packageName) {
            m_packageName->setPos(-m_packageName->boundingRect().width() / 2.0, y);
            y += m_packageName->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
315
        }
316 317 318 319
        if (m_contextLabel) {
            m_contextLabel->resetMaxWidth();
            m_contextLabel->setPos(-m_contextLabel->boundingRect().width() / 2.0, y);
            y += m_contextLabel->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
320
        }
321
    } else if (m_shape) {
Jochen Becher's avatar
Jochen Becher committed
322 323
        QPolygonF polygon;
        polygon << rect.topLeft()
324 325 326
                << QPointF(left + tabWidth, top)
                << QPointF(left + tabWidth, top + tabHeight)
                << QPointF(right, top + tabHeight)
Jochen Becher's avatar
Jochen Becher committed
327 328
                << rect.bottomRight()
                << rect.bottomLeft();
329
        m_shape->setPolygon(polygon);
Jochen Becher's avatar
Jochen Becher committed
330 331

        y += TAB_VERT_BORDER;
332
        if (StereotypesItem *stereotypesItem = this->stereotypesItem()) {
333 334
            stereotypesItem->setPos(left + TAB_HORIZ_BORDER, y);
            y += stereotypesItem->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
335
        }
336 337 338
        if (m_packageName) {
            m_packageName->setPos(left + TAB_HORIZ_BORDER, y);
            y += m_packageName->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
339 340 341
        }
        y += TAB_VERT_BORDER;
        y += BODY_VERT_BORDER;
342
        if (CustomIconItem *stereotypeIconItem = this->stereotypeIconItem()) {
343 344
            stereotypeIconItem->setPos(right - stereotypeIconItem->boundingRect().width() - BODY_HORIZ_BORDER, y);
            y += stereotypeIconItem->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
345
        }
346 347 348 349
        if (m_contextLabel) {
            m_contextLabel->setMaxWidth(width - 2 * BODY_HORIZ_BORDER);
            m_contextLabel->setPos(-m_contextLabel->boundingRect().width() / 2.0, y);
            y += m_contextLabel->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
350 351 352 353 354
        }
    }

    updateSelectionMarkerGeometry(rect);

355
    if (m_relationStarter)
356
        m_relationStarter->setPos(mapToScene(QPointF(right + 8.0, top)));
Jochen Becher's avatar
Jochen Becher committed
357 358 359 360 361

    updateAlignmentButtonsGeometry(rect);
    updateDepth();
}

362
} // namespace qmt