diagramitem.cpp 8.77 KB
Newer Older
1
/****************************************************************************
Jochen Becher's avatar
Jochen Becher committed
2
**
3
4
** Copyright (C) 2016 Jochen Becher
** Contact: https://www.qt.io/licensing/
Jochen Becher's avatar
Jochen Becher committed
5
6
7
8
9
10
11
**
** 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
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Jochen Becher's avatar
Jochen Becher committed
15
**
16
17
18
19
20
21
22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Jochen Becher's avatar
Jochen Becher committed
23
24
25
26
27
28
29
30
31
**
****************************************************************************/

#include "diagramitem.h"

#include "qmt/diagram/ddiagram.h"
#include "qmt/diagram_scene/diagramsceneconstants.h"
#include "qmt/diagram_scene/diagramscenemodel.h"
#include "qmt/diagram_scene/parts/customiconitem.h"
32
#include "qmt/diagram_scene/parts/editabletextitem.h"
Jochen Becher's avatar
Jochen Becher committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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 <QPointF>
#include <QFont>
#include <QBrush>
#include <QPen>

namespace qmt {

static const qreal MINIMUM_AUTO_WIDTH = 60.0;
static const qreal MINIMUM_AUTO_HEIGHT = 60.0;
static const qreal MINIMUM_WIDTH = 40.0;
static const qreal FOLD_WIDTH = 15.0;
static const qreal FOLD_HEIGHT = 15.0;
static const qreal BODY_HORIZ_BORDER = 4.0;
static const qreal BODY_VERT_BORDER = 4.0;

56
DiagramItem::DiagramItem(DDiagram *diagram, DiagramSceneModel *diagramSceneModel, QGraphicsItem *parent)
57
    : ObjectItem(diagram, diagramSceneModel, parent)
Jochen Becher's avatar
Jochen Becher committed
58
59
60
61
62
63
64
65
66
67
68
69
{
}

DiagramItem::~DiagramItem()
{
}

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

70
    const Style *style = adaptedStyle(stereotypeIconId());
Jochen Becher's avatar
Jochen Becher committed
71
72

    // custom icon
73
    if (stereotypeIconDisplay() == StereotypeIcon::DisplayIcon) {
74
        if (!m_customIcon)
75
76
77
78
79
            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());
80
81
82
83
84
        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
85
86
87
    }

    // shape
88
    if (!m_customIcon) {
89
        if (!m_body)
90
            m_body = new QGraphicsPolygonItem(this);
91
92
        m_body->setBrush(style->fillBrush());
        m_body->setPen(style->outerLinePen());
93
        m_body->setZValue(SHAPE_ZVALUE);
94
        if (!m_fold)
95
            m_fold = new QGraphicsPolygonItem(this);
96
97
        m_fold->setBrush(style->extraFillBrush());
        m_fold->setPen(style->outerLinePen());
98
        m_fold->setZValue(SHAPE_DETAILS_ZVALUE);
Jochen Becher's avatar
Jochen Becher committed
99
    } else {
100
101
102
103
        if (m_fold) {
            m_fold->scene()->removeItem(m_fold);
            delete m_fold;
            m_fold = 0;
Jochen Becher's avatar
Jochen Becher committed
104
        }
105
106
107
108
        if (m_body) {
            m_body->scene()->removeItem(m_body);
            delete m_body;
            m_body = 0;
Jochen Becher's avatar
Jochen Becher committed
109
110
111
112
        }
    }

    // stereotypes
113
    updateStereotypes(stereotypeIconId(), stereotypeIconDisplay(), style);
Jochen Becher's avatar
Jochen Becher committed
114
115

    // diagram name
116
    updateNameItem(style);
Jochen Becher's avatar
Jochen Becher committed
117

118
    updateSelectionMarker(m_customIcon);
Jochen Becher's avatar
Jochen Becher committed
119
120
121
122
    updateAlignmentButtons();
    updateGeometry();
}

123
bool DiagramItem::intersectShapeWithLine(const QLineF &line, QPointF *intersectionPoint, QLineF *intersectionLine) const
Jochen Becher's avatar
Jochen Becher committed
124
125
{
    QPolygonF polygon;
126
    if (m_customIcon) {
127
        // TODO use customIcon path as shape
128
129
        QRectF rect = object()->rect();
        rect.translate(object()->pos());
Jochen Becher's avatar
Jochen Becher committed
130
131
        polygon << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft() << rect.topLeft();
    } else {
132
133
        QRectF rect = object()->rect();
        rect.translate(object()->pos());
Jochen Becher's avatar
Jochen Becher committed
134
135
        polygon << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft() << rect.topLeft();
    }
136
    return GeometryUtilities::intersect(polygon, line, intersectionPoint, intersectionLine);
Jochen Becher's avatar
Jochen Becher committed
137
138
}

139
QSizeF DiagramItem::minimumSize() const
Jochen Becher's avatar
Jochen Becher committed
140
141
142
143
144
145
146
147
148
{
    return calcMinimumGeometry();
}

QSizeF DiagramItem::calcMinimumGeometry() const
{
    double width = MINIMUM_WIDTH;
    double height = 0.0;

149
150
151
152
    if (m_customIcon) {
        return stereotypeIconMinimumSize(m_customIcon->stereotypeIcon(),
                                         CUSTOM_ICON_MINIMUM_AUTO_WIDTH, CUSTOM_ICON_MINIMUM_AUTO_HEIGHT);
    }
Jochen Becher's avatar
Jochen Becher committed
153
154

    height += BODY_VERT_BORDER;
155
    if (CustomIconItem *stereotypeIconItem = this->stereotypeIconItem()) {
156
157
        width = std::max(width, stereotypeIconItem->boundingRect().width() + 2 * BODY_HORIZ_BORDER);
        height += std::max(FOLD_HEIGHT, stereotypeIconItem->boundingRect().height());
Jochen Becher's avatar
Jochen Becher committed
158
159
160
    } else {
        height += FOLD_HEIGHT;
    }
161
    if (StereotypesItem *stereotypesItem = this->stereotypesItem()) {
162
163
        width = std::max(width, stereotypesItem->boundingRect().width() + 2 * BODY_HORIZ_BORDER);
        height += stereotypesItem->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
164
    }
165
166
167
    if (nameItem()) {
        width = std::max(width, nameItem()->boundingRect().width() + 2 * BODY_HORIZ_BORDER);
        height += nameItem()->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    }
    height += BODY_VERT_BORDER;

    return GeometryUtilities::ensureMinimumRasterSize(QSizeF(width, height), 2 * RASTER_WIDTH, 2 * RASTER_HEIGHT);
}

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

    // calc width and height
    double width = 0.0;
    double height = 0.0;

    QSizeF geometry = calcMinimumGeometry();
    width = geometry.width();
    height = geometry.height();

186
    if (object()->isAutoSized()) {
187
        if (!m_customIcon) {
188
            if (width < MINIMUM_AUTO_WIDTH)
Jochen Becher's avatar
Jochen Becher committed
189
                width = MINIMUM_AUTO_WIDTH;
190
            if (height < MINIMUM_AUTO_HEIGHT)
Jochen Becher's avatar
Jochen Becher committed
191
192
193
                height = MINIMUM_AUTO_HEIGHT;
        }
    } else {
194
        QRectF rect = object()->rect();
195
        if (rect.width() > width)
Jochen Becher's avatar
Jochen Becher committed
196
            width = rect.width();
197
        if (rect.height() > height)
Jochen Becher's avatar
Jochen Becher committed
198
199
200
201
202
203
204
205
            height = rect.height();
    }

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

206
    setPos(object()->pos());
Jochen Becher's avatar
Jochen Becher committed
207
208
209
210
211
212

    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.
213
    object()->setRect(rect);
Jochen Becher's avatar
Jochen Becher committed
214

215
216
217
    if (m_customIcon) {
        m_customIcon->setPos(left, top);
        m_customIcon->setActualSize(QSizeF(width, height));
Jochen Becher's avatar
Jochen Becher committed
218
219
220
        y += height;
    }

221
    if (m_body) {
222
223
        QPolygonF bodyPolygon;
        bodyPolygon
Jochen Becher's avatar
Jochen Becher committed
224
225
226
227
228
                << rect.topLeft()
                << rect.topRight() + QPointF(-FOLD_WIDTH, 0.0)
                << rect.topRight() + QPointF(0.0, FOLD_HEIGHT)
                << rect.bottomRight()
                << rect.bottomLeft();
229
        m_body->setPolygon(bodyPolygon);
Jochen Becher's avatar
Jochen Becher committed
230
    }
231
    if (m_fold) {
232
233
        QPolygonF foldPolygon;
        foldPolygon
Jochen Becher's avatar
Jochen Becher committed
234
235
236
                << rect.topRight() + QPointF(-FOLD_WIDTH, 0.0)
                << rect.topRight() + QPointF(0.0, FOLD_HEIGHT)
                << rect.topRight() + QPointF(-FOLD_WIDTH, FOLD_HEIGHT);
237
        m_fold->setPolygon(foldPolygon);
Jochen Becher's avatar
Jochen Becher committed
238
239
240
    }

    y += BODY_VERT_BORDER;
241
    if (!m_customIcon) {
242
        if (CustomIconItem *stereotypeIconItem = this->stereotypeIconItem()) {
243
244
            stereotypeIconItem->setPos(left + BODY_HORIZ_BORDER, y);
            y += std::max(FOLD_HEIGHT, stereotypeIconItem->boundingRect().height());
Jochen Becher's avatar
Jochen Becher committed
245
246
247
248
        } else {
            y += FOLD_HEIGHT;
        }
    }
249
    if (StereotypesItem *stereotypesItem = this->stereotypesItem()) {
250
251
        stereotypesItem->setPos(-stereotypesItem->boundingRect().width() / 2.0, y);
        y += stereotypesItem->boundingRect().height();
Jochen Becher's avatar
Jochen Becher committed
252
    }
253
254
    if (nameItem()) {
        nameItem()->setPos(-nameItem()->boundingRect().width() / 2.0, y);
Jochen Becher's avatar
Jochen Becher committed
255
256
257
258
259
260
261
    }

    updateSelectionMarkerGeometry(rect);
    updateAlignmentButtonsGeometry(rect);
    updateDepth();
}

262
} // namespace qmt