aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/formeditor/selectionindicator.cpp
blob: 80774414b66939352cc7ef9fa581509304b4d677 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "selectionindicator.h"

#include "annotation.h"
#include <designeractionmanager.h>
#include <formeditorannotationicon.h>

#include <QPen>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <memory>

#include <abstractview.h>

#include <designeractionmanager.h>

#include <utils/theme/theme.h>

namespace QmlDesigner {

SelectionIndicator::SelectionIndicator(LayerItem *layerItem)
    : m_layerItem(layerItem)
{
}

SelectionIndicator::~SelectionIndicator()
{
    clear();
}

void SelectionIndicator::show()
{
    for (QGraphicsPolygonItem *item : qAsConst(m_indicatorShapeHash))
        item->show();
    if (m_labelItem)
        m_labelItem->show();
}

void SelectionIndicator::hide()
{
    for (QGraphicsPolygonItem *item : qAsConst(m_indicatorShapeHash))
        item->hide();
    if (m_labelItem)
        m_labelItem->hide();
}

void SelectionIndicator::clear()
{
    if (m_layerItem) {
        for (QGraphicsPolygonItem *item : qAsConst(m_indicatorShapeHash)) {
            m_layerItem->scene()->removeItem(item);
            delete item;
        }
    }
    m_labelItem.reset(nullptr);
    m_annotationItem = nullptr;
    m_indicatorShapeHash.clear();
}

static QPolygonF boundingRectInLayerItemSpaceForItem(FormEditorItem *item, QGraphicsItem *layerItem)
{
     QPolygonF boundingRectInSceneSpace(item->mapToScene(item->qmlItemNode().instanceBoundingRect()));
     return layerItem->mapFromScene(boundingRectInSceneSpace);
}

static bool checkSingleSelection(const QList<FormEditorItem*> &itemList)
{
    return !itemList.isEmpty()
            && itemList.constFirst()
            && itemList.constFirst()->qmlItemNode().view()->singleSelectedModelNode().isValid();
}

const int labelHeight = 18;

void SelectionIndicator::setItems(const QList<FormEditorItem*> &itemList)
{
    clear();

    static QColor selectionColor = Utils::creatorTheme()->color(Utils::Theme::QmlDesigner_FormEditorSelectionColor);

    for (FormEditorItem *item : itemList) {
        if (!item->qmlItemNode().isValid())
            continue;

        auto newSelectionIndicatorGraphicsItem = new QGraphicsPolygonItem(m_layerItem.data());
        m_indicatorShapeHash.insert(item, newSelectionIndicatorGraphicsItem);
        newSelectionIndicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpaceForItem(item, m_layerItem.data()));
        newSelectionIndicatorGraphicsItem->setFlag(QGraphicsItem::ItemIsSelectable, false);

        QPen pen;
        pen.setCosmetic(true);
        pen.setWidth(2);
        pen.setJoinStyle(Qt::MiterJoin);
        pen.setColor(selectionColor);
        newSelectionIndicatorGraphicsItem->setPen(pen);
    }

    if (checkSingleSelection(itemList)) {
        FormEditorItem *selectedItem = itemList.constFirst();
        m_labelItem = std::make_unique<QGraphicsPolygonItem>(m_layerItem.data());
        const qreal scaleFactor = m_layerItem->viewportTransform().m11();

        QGraphicsWidget *toolbar = DesignerActionManager::instance().createFormEditorToolBar(m_labelItem.get());
        toolbar->setPos(1, -1);

        ModelNode modelNode = selectedItem->qmlItemNode().modelNode();
        QGraphicsTextItem *textItem = new QGraphicsTextItem(modelNode.simplifiedTypeName(), m_labelItem.get());

        if (modelNode.hasId())
            textItem->setPlainText(modelNode.id());

        if (modelNode.hasAnnotation() || modelNode.hasCustomId()) {
            m_annotationItem = new FormEditorAnnotationIcon(modelNode, m_labelItem.get());
            m_annotationItem->update();
        }
        else {
            m_annotationItem = nullptr;
        }

        static QColor textColor = Utils::creatorTheme()->color(Utils::Theme::QmlDesigner_FormEditorForegroundColor);

        textItem->setDefaultTextColor(textColor);
        QPolygonF labelPolygon = boundingRectInLayerItemSpaceForItem(selectedItem, m_layerItem.data());
        QRectF labelRect = labelPolygon.boundingRect();
        labelRect.setHeight(labelHeight);
        labelRect.setWidth(textItem->boundingRect().width() + toolbar->size().width());
        QPointF pos = labelRect.topLeft();
        labelRect.moveTo(0, 0);
        m_labelItem->setPolygon(labelRect);
        const int scaledHeight = labelHeight / scaleFactor;
        m_labelItem->setPos(pos + QPointF(0, -scaledHeight));
        const int offset = (labelHeight - textItem->boundingRect().height()) / 2;
        textItem->setPos(QPointF(toolbar->size().width(), offset));
        m_labelItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
        m_labelItem->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);

        QPen pen;
        pen.setCosmetic(true);
        pen.setWidth(2);
        pen.setCapStyle(Qt::RoundCap);
        pen.setJoinStyle(Qt::BevelJoin);
        pen.setColor(selectionColor);

        if (m_annotationItem) {
            m_annotationItem->setFlags(QGraphicsItem::ItemIgnoresTransformations);
            adjustAnnotationPosition(labelPolygon.boundingRect(), m_labelItem->boundingRect(), scaleFactor);
        }

        m_labelItem->setPen(pen);
        m_labelItem->setBrush(selectionColor);
        m_labelItem->update();
    }
}

void SelectionIndicator::updateItems(const QList<FormEditorItem*> &itemList)
{
    for (FormEditorItem *item : itemList) {
        if (m_indicatorShapeHash.contains(item)) {
            QGraphicsPolygonItem *indicatorGraphicsItem =  m_indicatorShapeHash.value(item);
            indicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpaceForItem(item, m_layerItem.data()));
        }
    }

    if (checkSingleSelection(itemList)
            && m_labelItem) {
        FormEditorItem *selectedItem = itemList.constFirst();
        QPolygonF labelPolygon = boundingRectInLayerItemSpaceForItem(selectedItem, m_layerItem.data());
        QRectF labelRect = labelPolygon.boundingRect();
        QPointF pos = labelRect.topLeft();
        const qreal scaleFactor = m_layerItem->viewportTransform().m11();
        const int scaledHeight = labelHeight / scaleFactor;
        m_labelItem->setPos(pos + QPointF(0, -scaledHeight));

        if (m_annotationItem) {
            adjustAnnotationPosition(labelPolygon.boundingRect(), m_labelItem->boundingRect(), scaleFactor);
        }

        m_layerItem->update();
    }
}

void SelectionIndicator::setCursor(const QCursor &cursor)
{
    m_cursor = cursor;

    for (QGraphicsItem  *item : qAsConst(m_indicatorShapeHash))
        item->setCursor(cursor);
}

void SelectionIndicator::adjustAnnotationPosition(const QRectF &itemRect, const QRectF &labelRect, qreal scaleFactor)
{
    if (!m_annotationItem) return;

    const qreal iconWShift = m_annotationItem->iconWidth() * 0.5;
    const qreal iconHShift = (m_annotationItem->iconHeight() * 0.45)/scaleFactor;
    qreal iconX = 0.0;
    qreal iconY = -(iconHShift);

    if (((labelRect.width() + iconWShift)/scaleFactor) > itemRect.width())
        iconY -= labelRect.height()/scaleFactor;

    if ((iconWShift/scaleFactor) > itemRect.width())
        iconX = 0.0;
    else
        iconX = (itemRect.width()) - (iconWShift/scaleFactor);

    m_annotationItem->setPos(iconX*scaleFactor, iconY*scaleFactor);
}

}