7#include "selectionitem.h"
8#include <QGraphicsView>
9#include <QGraphicsScene>
16static const qreal selMargin = 4.0;
17static const QPointF boundMargin(selMargin, selMargin);
18static const qreal addRemMargin = 8.0;
19static const QPointF addRemMarginPoint(addRemMargin, addRemMargin);
21struct SelectionItem::Private {
36 qreal devicePixelRatio = 1.0;
38 bool addButtonEnabled =
true;
50 d->penDark.setWidth(0);
53 d->penLight.setWidth(0);
58 d->penAddRemFg.setWidth(3);
61 d->showAddRem =
false;
63 d->selMargin = selMargin;
65 d->addRemRect =
QRectF(0, 0, 0, 0);
67 d->devicePixelRatio = 1.0;
70SelectionItem::~SelectionItem()
75void SelectionItem::saveZoom(qreal zoom)
80 d->invZoom = 1 /
zoom;
82 d->selMargin = selMargin * d->invZoom;
84 qreal margin = addRemMargin * d->invZoom;
85 QPointF pMargin = addRemMarginPoint * d->invZoom;
86 d->addRemRect =
QRectF(d->rect.center() / d->devicePixelRatio - pMargin,
QSizeF(margin * 2.0, margin * 2.0));
87 d->penAddRemFg.setWidthF(3.0 * d->invZoom);
90void SelectionItem::setSaved(
bool isSaved)
105void SelectionItem::setMaxRight(qreal maxX)
114void SelectionItem::setMaxBottom(qreal maxY)
123SelectionItem::Intersects SelectionItem::intersects(
const QPointF &point)
125 bool oldState = d->showAddRem;
126 d->showAddRem =
false;
128 if ((point.
x() < (d->rect.left() - d->selMargin)) ||
129 (point.
x() > (d->rect.right() + d->selMargin)) ||
130 (point.
y() < (d->rect.top() - d->selMargin)) ||
131 (point.
y() > (d->rect.bottom() + d->selMargin))) {
132 if (oldState != d->showAddRem) {
138 if (point.
x() < (d->rect.left() + d->selMargin)) {
139 if (oldState != d->showAddRem) {
142 if (point.
y() < (d->rect.top() + d->selMargin)) {
145 if (point.
y() > (d->rect.bottom() - d->selMargin)) {
151 if (point.
x() > (d->rect.right() - d->selMargin)) {
152 if (oldState != d->showAddRem) {
155 if (point.
y() < (d->rect.top() + d->selMargin)) {
158 if (point.
y() > (d->rect.bottom() - d->selMargin)) {
164 if (point.
y() < (d->rect.top() + d->selMargin)) {
165 if (oldState != d->showAddRem) {
170 if (point.
y() > (d->rect.bottom() - d->selMargin)) {
171 if (oldState != d->showAddRem) {
177 d->showAddRem = d->addButtonEnabled;
178 if (oldState != d->showAddRem) {
182 if (d->addButtonEnabled && d->addRemRect.contains(point / d->devicePixelRatio)) {
188void SelectionItem::setRect(
const QRectF &rect)
190 prepareGeometryChange();
194 if (d->rect.top() < 0) {
197 if (d->rect.left() < 0) {
200 if (d->rect.right() > d->maxX) {
201 d->rect.setRight(d->maxX);
203 if (d->rect.bottom() > d->maxY) {
204 d->rect.setBottom(d->maxY);
209 qreal margin = addRemMargin * d->invZoom;
210 QPointF pMargin = addRemMarginPoint * d->invZoom;
211 d->addRemRect =
QRectF(d->rect.center() / d->devicePixelRatio - pMargin,
QSizeF(margin * 2, margin * 2));
216 if ((d->rect.left() + dp.
x()) < 0) {
217 dp.
setX(-d->rect.left());
219 if ((d->rect.top() + dp.
y()) < 0) {
220 dp.
setY(-d->rect.top());
222 if ((d->rect.right() + dp.
x()) > d->maxX) {
223 dp.
setX(d->maxX - d->rect.right());
225 if ((d->rect.bottom() + dp.
y()) > d->maxY) {
226 dp.
setY(d->maxY - d->rect.bottom());
231QRectF SelectionItem::rect()
236qreal SelectionItem::devicePixelRatio()
const
238 return d->devicePixelRatio;
241void SelectionItem::setDevicePixelRatio(qreal dpr)
243 d->devicePixelRatio = dpr;
246QRectF SelectionItem::boundingRect()
const
248 const auto dpr = d->devicePixelRatio;
249 QRectF tmp(d->rect.topLeft() / dpr - boundMargin, d->rect.bottomRight() / dpr + boundMargin);
250 return tmp.united(d->addRemRect);
255 const auto dpr = d->devicePixelRatio;
256 QRectF rect(d->rect.topLeft() / dpr, d->rect.size() / dpr);
258 painter->
setPen(d->penDark);
261 painter->
setPen(d->penLight);
266 QLineF minus(d->addRemRect.left() + 3 * d->invZoom, d->addRemRect.center().y(),
267 d->addRemRect.right() - 3 * d->invZoom, d->addRemRect.center().y());
268 painter->
setPen(d->penAddRemFg);
273 QLineF plus(d->addRemRect.center().x(), d->addRemRect.top() + 3 * d->invZoom,
274 d->addRemRect.center().x(), d->addRemRect.bottom() - 3 * d->invZoom);
280void SelectionItem::setAddButtonEnabled(
bool enabled)
282 d->addButtonEnabled = enabled;
void update(Part *part, const QByteArray &data, qint64 dataSize)
QAction * zoom(const QObject *recvr, const char *slot, QObject *parent)
void drawLine(const QLine &line)
void drawRect(const QRect &rectangle)
void fillRect(const QRect &rectangle, QGradient::Preset preset)
void setPen(Qt::PenStyle style)
QRectF normalized() const const