| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "customitem.h" |
| 5 |
|
| 6 |
#include <QPainter> |
| 7 |
#include <QStyle> |
| 8 |
#include <QStyleOption> |
| 9 |
|
| 10 |
QList<CustomGroup*> CustomScene::selectedCustomGroups() const |
| 11 |
{ |
| 12 |
const QList<QGraphicsItem*> all = selectedItems(); |
| 13 |
QList<CustomGroup*> groups; |
| 14 |
|
| 15 |
for (QGraphicsItem *item : all) { |
| 16 |
CustomGroup* group = qgraphicsitem_cast<CustomGroup*>(item); |
| 17 |
if (group) |
| 18 |
groups.append(group); |
| 19 |
} |
| 20 |
|
| 21 |
return groups; |
| 22 |
} |
| 23 |
|
| 24 |
QList<CustomItem*> CustomScene::selectedCustomItems() const |
| 25 |
{ |
| 26 |
const QList<QGraphicsItem*> all = selectedItems(); |
| 27 |
QList<CustomItem*> items; |
| 28 |
|
| 29 |
for (QGraphicsItem *item : all) { |
| 30 |
CustomItem* citem = qgraphicsitem_cast<CustomItem*>(item); |
| 31 |
if (citem) |
| 32 |
items.append(citem); |
| 33 |
} |
| 34 |
|
| 35 |
return items; |
| 36 |
} |
| 37 |
|
| 38 |
CustomGroup::CustomGroup() : |
| 39 |
QGraphicsItemGroup() |
| 40 |
{ |
| 41 |
setFlag(QGraphicsItem::ItemIsMovable); |
| 42 |
setFlag(QGraphicsItem::ItemIsSelectable); |
| 43 |
} |
| 44 |
|
| 45 |
void CustomGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) |
| 46 |
{ |
| 47 |
if (option->state & QStyle::State_Selected) |
| 48 |
painter->setOpacity(1.); |
| 49 |
else |
| 50 |
painter->setOpacity(0.2); |
| 51 |
|
| 52 |
painter->setPen(QPen(QColor(100, 100, 100), 2, Qt::DashLine)); |
| 53 |
painter->drawRect(boundingRect().adjusted(-2, -2, 2, 2)); |
| 54 |
} |
| 55 |
|
| 56 |
QRectF CustomGroup::boundingRect() const |
| 57 |
{ |
| 58 |
return QGraphicsItemGroup::boundingRect().adjusted(-4, -4, 4 ,4); |
| 59 |
} |
| 60 |
|
| 61 |
CustomItem::CustomItem(qreal x, qreal y, qreal width, qreal height, const QBrush &brush) : |
| 62 |
QGraphicsRectItem(x, y, width, height) |
| 63 |
{ |
| 64 |
setFlag(QGraphicsItem::ItemIsMovable); |
| 65 |
setFlag(QGraphicsItem::ItemIsSelectable); |
| 66 |
setBrush(brush); |
| 67 |
setPen(Qt::NoPen); |
| 68 |
setTransformOriginPoint(boundingRect().center()); |
| 69 |
} |