27 #include <QGraphicsView>
28 #include <QGraphicsSceneMouseEvent>
29 #include <QApplication>
32 #define RESIZE_RECT 30
33 #define ROUNDEDGESIZE 50
37 QGraphicsItem* parent)
38 : QGraphicsObject(parent),
43 scalingActive =
false;
46 setCacheMode(DeviceCoordinateCache);
48 setFlag(QGraphicsItem::ItemIsSelectable,
true);
49 setFlag(QGraphicsItem::ItemIsMovable,
true);
51 setFlag(QGraphicsItem::ItemSendsGeometryChanges,
true);
52 setAcceptHoverEvents(
true);
55 float curScale = scale();
58 QPointF curPos = pos();
66 fillGradient.setStart(
bounds.topLeft());
67 fillGradient.setFinalStop(
bounds.bottomRight());
68 fillGradient.setColorAt(0, newColor);
69 fillGradient.setColorAt(1, newColor.darker(100));
80 return bounds.adjusted(0, 0, 2, 2);
90 prepareGeometryChange();
98 if (newSize ==
bounds.size())
103 QRectF oldBounds =
bounds;
104 prepareGeometryChange();
115 painter->setPen(Qt::NoPen);
116 painter->setBrush(QColor(0, 0, 0, 64));
119 QLinearGradient tempGradient = fillGradient;
120 QPen tmpRimPen = rimPen;
123 tempGradient.setColorAt(1, color.lighter(130));
124 tmpRimPen.setStyle(Qt::DashLine);
128 painter->setBrush(tempGradient);
129 painter->setPen(tmpRimPen);
132 painter->setPen(QPen(Qt::black, 1));
141 if (event->modifiers().testFlag(Qt::ShiftModifier)
144 setFlag(QGraphicsItem::ItemIsMovable,
true);
148 setFlag(QGraphicsItem::ItemIsMovable,
false);
159 QPointF pos =
event->pos();
168 if (rect.contains(pos))
170 scalingActive =
true;
171 prepareGeometryChange();
180 resizingMode = eBottomResizing;
184 resizingMode = eTopResizing;
189 resizingMode = eLeftResizing;
194 resizingMode = eRightResizing;
199 QGraphicsObject::mousePressEvent(event);
208 QApplication::restoreOverrideCursor();
212 scalingActive =
false;
213 resizingMode = eNone;
214 QGraphicsItem::mouseReleaseEvent(event);
219 QPointF pos =
event->pos();
224 prepareGeometryChange();
225 QRectF newRect = oldRect;
226 newRect.setBottomRight(QPointF(pos.x() + 2, pos.y() + 2));
242 float xScale = newRect.width() / oldRect.width();
243 float yScale = newRect.height() / oldRect.height();
247 float resultScalefactor =
std::max(xScale, yScale);
248 resultScalefactor *= scale();
249 resultScalefactor =
std::max(resultScalefactor, 0.01f);
252 setScale(resultScalefactor);
254 else if (resizingMode != eNone)
257 prepareGeometryChange();
258 QRectF newBounds =
bounds;
260 switch (resizingMode)
266 case eBottomResizing:
267 newBounds.setBottom(pos.y() + 2);
274 newBounds.setRight(pos.x() + 2);
281 newBounds.setWidth(
std::max(50.0, newBounds.width()));
282 newBounds.setHeight(
std::max(50.0, newBounds.height()));
284 QRectF bbInP = mapRectToParent(newBounds);
285 QRectF obbInP = mapRectToParent(oldRect);
291 std::max(bbInP.width(), bbInP.height()));
297 QGraphicsItem::mouseMoveEvent(event);
303 if (!setCursor || (QApplication::overrideCursor() && QApplication::overrideCursor()->shape() != shape))
306 if (QApplication::overrideCursor() && QApplication::overrideCursor()->shape() != shape)
308 QApplication::changeOverrideCursor(QCursor(shape));
312 QApplication::setOverrideCursor(QCursor(shape));
321 QPointF pos =
event->pos();
331 if (rect.contains(pos))
351 else if (QApplication::overrideCursor())
353 QApplication::restoreOverrideCursor();
369 QApplication::restoreOverrideCursor();
378 result.setTop(result.bottom());
385 result.setBottom(result.top());
392 result.setRight(result.left());
399 result.setLeft(result.right());
411 QRectF rectP = (parentItem()->boundingRect());
413 float areaP = rectP.width() * rectP.height();
414 float area = rect.width() * rect.height() * resultScalefactor;
418 if (area / areaP > 0.4)
420 resultScalefactor =
std::max(rect.width(), rect.height()) /
std::max(rectP.width(), rectP.height());
432 QRectF rect = parentItem()->boundingRect();
435 newPos.setX(qMin(rect.right() -
boundingRect().width()*scale() - rect.width() * 0.03,
437 rect.left() + rect.width() * 0.03)));
438 newPos.setY(qMin(rect.bottom() -
boundingRect().height()*scale() - rect.height() * 0.03,
440 rect.top() + rect.height() * 0.20)));
449 QGraphicsView* view = qobject_cast<QGraphicsView*>(event->widget()->parent());
453 auto bbparent = view->mapFromScene(parentItem()->mapToScene(parentItem()->
boundingRect())).boundingRect();
485 this->editable = editable;
490 if (change == ItemPositionChange)
493 QPointF newPos =
value.toPointF();
494 RoundRectItem* itemParent = qgraphicsitem_cast<RoundRectItem*>(parentItem());
505 else if (change == ItemScaleChange)
510 float resultScalefactor =
value.toFloat();
513 return resultScalefactor;
517 return QGraphicsItem::itemChange(change,
value);