Commit ab206418 authored by Thomas Hartmann's avatar Thomas Hartmann

QmlDesigner: Improve selection indicator

The indicator for a single selection now has a label.
I also adjusted the color a bit.
The selection indicator now has a width of 2.

Change-Id: I541e8d3ab3f648af8b1fc6b0c005ddd538bb7de8
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 05e07316
......@@ -27,6 +27,9 @@
#include <QPen>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <abstractview.h>
namespace QmlDesigner {
......@@ -60,45 +63,100 @@ void SelectionIndicator::clear()
delete item;
}
}
m_labelItem.reset(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.first()->qmlItemNode().view()->singleSelectedModelNode().isValid();
}
const int labelHeight = 16;
void SelectionIndicator::setItems(const QList<FormEditorItem*> &itemList)
{
clear();
static QColor selectionColor = QColor(75, 162, 255);
foreach (FormEditorItem *item, itemList) {
if (!item->qmlItemNode().isValid())
continue;
QGraphicsPolygonItem *newSelectionIndicatorGraphicsItem = new QGraphicsPolygonItem(m_layerItem.data());
m_indicatorShapeHash.insert(item, newSelectionIndicatorGraphicsItem);
QPolygonF boundingRectInSceneSpace(item->mapToScene(item->qmlItemNode().instanceBoundingRect()));
QPolygonF boundingRectInLayerItemSpace = m_layerItem->mapFromScene(boundingRectInSceneSpace);
newSelectionIndicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);
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(QColor(108, 141, 221));
pen.setColor(selectionColor);
newSelectionIndicatorGraphicsItem->setPen(pen);
newSelectionIndicatorGraphicsItem->setCursor(m_cursor);
}
}
if (checkSingleSelection(itemList)) {
FormEditorItem *selectedItem = itemList.first();
m_labelItem.reset(new QGraphicsPolygonItem(m_layerItem.data()));
ModelNode modelNode = selectedItem->qmlItemNode().modelNode();
QGraphicsTextItem *textItem = new QGraphicsTextItem(modelNode.simplifiedTypeName(), m_labelItem.get());
if (modelNode.hasId())
textItem->setPlainText(modelNode.id());
textItem->setDefaultTextColor(Qt::white);
QPolygonF labelPolygon = boundingRectInLayerItemSpaceForItem(selectedItem, m_layerItem.data());
QRectF labelRect = labelPolygon.boundingRect();
labelRect.setHeight(labelHeight);
labelRect.setWidth(textItem->boundingRect().width());
QPointF pos = labelRect.topLeft();
labelRect.moveTo(0, 0);
m_labelItem->setPolygon(labelRect);
m_labelItem->setPos(pos + QPointF(0, -labelHeight));
int offset = labelHeight + 2 - textItem->boundingRect().height();
textItem->setPos(QPointF(0, offset));
m_labelItem->setFlag(QGraphicsItem::ItemIsSelectable, false);
QPen pen;
pen.setCosmetic(true);
pen.setWidth(2);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::BevelJoin);
pen.setColor(selectionColor);
m_labelItem->setPen(pen);
m_labelItem->setBrush(selectionColor);
}
}
void SelectionIndicator::updateItems(const QList<FormEditorItem*> &itemList)
{
foreach (FormEditorItem *item, itemList) {
if (m_indicatorShapeHash.contains(item)) {
QGraphicsPolygonItem *indicatorGraphicsItem = m_indicatorShapeHash.value(item);
QPolygonF boundingRectInSceneSpace(item->mapToScene(item->qmlItemNode().instanceBoundingRect()));
QPolygonF boundingRectInLayerItemSpace = m_layerItem->mapFromScene(boundingRectInSceneSpace);
indicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpace);
indicatorGraphicsItem->setPolygon(boundingRectInLayerItemSpaceForItem(item, m_layerItem.data()));
}
}
if (!itemList.isEmpty())
itemList.first()->qmlItemNode().view()->singleSelectedModelNode().isValid();
if (checkSingleSelection(itemList)) {
FormEditorItem *selectedItem = itemList.first();
QPolygonF labelPolygon = boundingRectInLayerItemSpaceForItem(selectedItem, m_layerItem.data());
QRectF labelRect = labelPolygon.boundingRect();
QPointF pos = labelRect.topLeft();
m_labelItem->setPos(pos + QPointF(0, -labelHeight));
m_layerItem->update();
}
}
void SelectionIndicator::setCursor(const QCursor &cursor)
......
......@@ -31,6 +31,8 @@
#include <QPointer>
#include <QGraphicsPolygonItem>
#include <memory>
namespace QmlDesigner {
class SelectionIndicator
......@@ -53,7 +55,7 @@ private:
QHash<FormEditorItem*, QGraphicsPolygonItem *> m_indicatorShapeHash;
QPointer<LayerItem> m_layerItem;
QCursor m_cursor;
std::unique_ptr<QGraphicsPolygonItem> m_labelItem;
};
} // namespace QmlDesigner
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment