| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
| 5 |
|
| 6 |
#include "widget.h" |
| 7 |
#include "ui_widget.h" |
| 8 |
#include <QGraphicsOpacityEffect> |
| 9 |
#include <QPropertyAnimation> |
| 10 |
|
| 11 |
Widget::Widget(QWidget *parent) : |
| 12 |
QWidget(parent), |
| 13 |
ui(new Ui::Widget), |
| 14 |
previousSelectionCount(0) |
| 15 |
{ |
| 16 |
ui->setupUi(this); |
| 17 |
|
| 18 |
effect = new QGraphicsOpacityEffect; |
| 19 |
effect->setOpacity(0.3); |
| 20 |
ui->groupBox->setGraphicsEffect(effect); |
| 21 |
|
| 22 |
fadeIn = new QPropertyAnimation(effect, "opacity"); |
| 23 |
fadeIn->setDuration(200); |
| 24 |
fadeIn->setStartValue(0.3); |
| 25 |
fadeIn->setEndValue(1.); |
| 26 |
|
| 27 |
fadeOut = new QPropertyAnimation(effect, "opacity"); |
| 28 |
fadeOut->setDuration(200); |
| 29 |
fadeOut->setStartValue(1.); |
| 30 |
fadeOut->setEndValue(0.3); |
| 31 |
|
| 32 |
scene = new CustomScene; |
| 33 |
scene->setSceneRect(-250,-250,500,500); |
| 34 |
ui->view->setScene(scene); |
| 35 |
scene->setBackgroundBrush(Qt::white); |
| 36 |
ui->view->setInteractive(true); |
| 37 |
ui->view->setDragMode(QGraphicsView::RubberBandDrag); |
| 38 |
ui->view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate); |
| 39 |
ui->view->setRenderHints( QPainter::SmoothPixmapTransform | |
| 40 |
QPainter::TextAntialiasing | |
| 41 |
QPainter::Antialiasing ); |
| 42 |
|
| 43 |
rectBlue = new CustomItem(-100, -100, 50, 50, QColor(80, 80, 200)); |
| 44 |
scene->addItem(rectBlue); |
| 45 |
rectGreen = new CustomItem(100, -100, 50, 50, QColor(80, 200, 80)); |
| 46 |
scene->addItem(rectGreen); |
| 47 |
rectRed = new CustomItem(-100, 100, 50, 50, QColor(200, 80, 80)); |
| 48 |
scene->addItem(rectRed); |
| 49 |
rectYellow = new CustomItem(100, 100, 50, 50, QColor(200, 200, 20)); |
| 50 |
scene->addItem(rectYellow); |
| 51 |
|
| 52 |
connect(scene, SIGNAL(selectionChanged()), this, SLOT(onSceneSelectionChanged())); |
| 53 |
} |
| 54 |
|
| 55 |
Widget::~Widget() |
| 56 |
{ |
| 57 |
delete ui; |
| 58 |
delete fadeIn; |
| 59 |
delete fadeOut; |
| 60 |
} |
| 61 |
|
| 62 |
void Widget::on_rotate_valueChanged(int value) |
| 63 |
{ |
| 64 |
if (scene->selectedItems().count() != 1) |
| 65 |
return; |
| 66 |
|
| 67 |
QGraphicsItem *item = scene->selectedItems().at(0); |
| 68 |
item->setRotation(value); |
| 69 |
ui->rotateItem->setValue(checkedItem()->rotation()); |
| 70 |
} |
| 71 |
|
| 72 |
void Widget::on_scale_valueChanged(int value) |
| 73 |
{ |
| 74 |
if (scene->selectedItems().count() != 1) |
| 75 |
return; |
| 76 |
|
| 77 |
QGraphicsItem *item = scene->selectedItems().at(0); |
| 78 |
item->setScale(value / 10.); |
| 79 |
ui->scaleItem->setValue(checkedItem()->scale() * 10); |
| 80 |
} |
| 81 |
|
| 82 |
void Widget::on_rotateItem_valueChanged(int value) |
| 83 |
{ |
| 84 |
if (!scene->selectedItems().empty() && scene->selectedItems().at(0) == checkedItem()) |
| 85 |
ui->rotate->setValue(value); |
| 86 |
else |
| 87 |
checkedItem()->setRotation(value); |
| 88 |
} |
| 89 |
|
| 90 |
void Widget::on_scaleItem_valueChanged(int value) |
| 91 |
{ |
| 92 |
if (!scene->selectedItems().empty() && scene->selectedItems().at(0) == checkedItem()) |
| 93 |
ui->scale->setValue(value); |
| 94 |
else |
| 95 |
checkedItem()->setScale(value / 10.); |
| 96 |
} |
| 97 |
|
| 98 |
void Widget::on_group_clicked() |
| 99 |
{ |
| 100 |
const QList<QGraphicsItem*> all = scene->selectedItems(); |
| 101 |
if (all.size() < 2) |
| 102 |
return; |
| 103 |
|
| 104 |
const QList<CustomItem*> items = scene->selectedCustomItems(); |
| 105 |
QList<CustomGroup*> groups = scene->selectedCustomGroups(); |
| 106 |
|
| 107 |
if (groups.size() == 1) { |
| 108 |
for (CustomItem *item : items) { |
| 109 |
item->setSelected(false); |
| 110 |
groups[0]->addToGroup(item); |
| 111 |
} |
| 112 |
|
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
CustomGroup* group = new CustomGroup; |
| 117 |
scene->addItem(group); |
| 118 |
for (QGraphicsItem *item : all) { |
| 119 |
item->setSelected(false); |
| 120 |
group->addToGroup(item); |
| 121 |
} |
| 122 |
group->setSelected(true); |
| 123 |
|
| 124 |
updateUngroupButton(); |
| 125 |
} |
| 126 |
|
| 127 |
void Widget::on_dismantle_clicked() |
| 128 |
{ |
| 129 |
const QList<CustomGroup*> groups = scene->selectedCustomGroups(); |
| 130 |
|
| 131 |
for (CustomGroup *group : groups) { |
| 132 |
foreach (QGraphicsItem *item, group->childItems()) |
| 133 |
group->removeFromGroup(item); |
| 134 |
|
| 135 |
delete group; |
| 136 |
} |
| 137 |
|
| 138 |
updateUngroupButton(); |
| 139 |
} |
| 140 |
|
| 141 |
void Widget::on_merge_clicked() |
| 142 |
{ |
| 143 |
QList<CustomGroup*> groups = scene->selectedCustomGroups(); |
| 144 |
if (groups.size() < 2) |
| 145 |
return; |
| 146 |
|
| 147 |
CustomGroup *newBigGroup = groups.takeFirst(); |
| 148 |
foreach (CustomGroup *group, groups) { |
| 149 |
foreach (QGraphicsItem *item, group->childItems()) |
| 150 |
item->setGroup(newBigGroup); |
| 151 |
|
| 152 |
delete group; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
void Widget::onSceneSelectionChanged() |
| 157 |
{ |
| 158 |
QList<QGraphicsItem*> all = scene->selectedItems(); |
| 159 |
QList<CustomGroup*> groups = scene->selectedCustomGroups(); |
| 160 |
|
| 161 |
if (all.empty() && all.count() != previousSelectionCount) { |
| 162 |
fadeOut->start(); |
| 163 |
} else if (previousSelectionCount == 0) { |
| 164 |
fadeIn->start(); |
| 165 |
} |
| 166 |
|
| 167 |
if (all.count() == 1) { |
| 168 |
QGraphicsItem *item = all.at(0); |
| 169 |
ui->rotate->setValue(item->rotation()); |
| 170 |
ui->scale->setValue(item->scale() * 10); |
| 171 |
} else { |
| 172 |
ui->rotate->setValue(ui->rotate->minimum()); |
| 173 |
ui->scale->setValue(ui->scale->minimum()); |
| 174 |
} |
| 175 |
|
| 176 |
ui->rotate->setEnabled(all.size() == 1); |
| 177 |
ui->scale->setEnabled(all.size() == 1); |
| 178 |
ui->group->setEnabled(all.size() > 1); |
| 179 |
ui->dismantle->setEnabled(!groups.empty()); |
| 180 |
ui->merge->setEnabled(groups.size() > 1); |
| 181 |
|
| 182 |
previousSelectionCount = all.size(); |
| 183 |
|
| 184 |
updateUngroupButton(); |
| 185 |
} |
| 186 |
|
| 187 |
void Widget::on_ungroup_clicked() |
| 188 |
{ |
| 189 |
QGraphicsItemGroup *oldGroup = checkedItem()->group(); |
| 190 |
checkedItem()->setGroup(0); |
| 191 |
if (oldGroup && oldGroup->childItems().empty()) |
| 192 |
delete oldGroup; |
| 193 |
|
| 194 |
updateUngroupButton(); |
| 195 |
} |
| 196 |
|
| 197 |
void Widget::updateUngroupButton() |
| 198 |
{ |
| 199 |
ui->ungroup->setEnabled(checkedItem()->group() != 0); |
| 200 |
} |
| 201 |
|
| 202 |
CustomItem * Widget::checkedItem() const |
| 203 |
{ |
| 204 |
CustomItem *item = nullptr; |
| 205 |
|
| 206 |
if (ui->blue->isChecked()) |
| 207 |
item = rectBlue; |
| 208 |
else if (ui->red->isChecked()) |
| 209 |
item = rectRed; |
| 210 |
else if (ui->green->isChecked()) |
| 211 |
item = rectGreen; |
| 212 |
else if (ui->yellow->isChecked()) |
| 213 |
item = rectYellow; |
| 214 |
|
| 215 |
return item; |
| 216 |
} |
| 217 |
|
| 218 |
void Widget::on_buttonGroup_buttonClicked() |
| 219 |
{ |
| 220 |
ui->rotateItem->setValue(checkedItem()->rotation()); |
| 221 |
ui->scaleItem->setValue(checkedItem()->scale() * 10); |
| 222 |
|
| 223 |
updateUngroupButton(); |
| 224 |
} |