Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
selectionpainter.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#include "selectionpainter.h"
18
19#include "object.h"
20#include "qpainter.h"
21
22SelectionPainter::SelectionPainter()
23{
24}
25
26void SelectionPainter::paint(QPainter& painter,
27 const Object* object,
28 int layerIndex,
29 const TransformToolProperties& toolProperties,
30 TransformParameters& tParams)
31{
32 Layer* layer = object->getLayer(layerIndex);
33
34 if (layer == nullptr) { return; }
35
36 QTransform transform = tParams.selectionTransform * tParams.viewTransform;
37 QPolygonF projectedSelectionPolygon = transform.map(tParams.originalSelectionRectF);
38
39 if (layer->type() == Layer::BITMAP)
40 {
41 painter.setBrush(Qt::NoBrush);
42 painter.setPen(QPen(Qt::DashLine));
43
44 // Draw current selection
45 painter.drawPolygon(projectedSelectionPolygon.toPolygon());
46 }
47 if (layer->type() == Layer::VECTOR)
48 {
49 painter.setBrush(QColor(0, 0, 0, 20));
50 painter.setPen(Qt::gray);
51 painter.drawPolygon(projectedSelectionPolygon);
52 }
53
54 painter.setPen(Qt::SolidLine);
55 painter.setBrush(QBrush(Qt::gray));
56 int radius = HANDLE_WIDTH / 2;
57
58 const QRectF topLeftCorner = QRectF(projectedSelectionPolygon[0].x() - radius,
59 projectedSelectionPolygon[0].y() - radius,
60 HANDLE_WIDTH, HANDLE_WIDTH);
61 painter.drawRect(topLeftCorner);
62
63 const QRectF topRightCorner = QRectF(projectedSelectionPolygon[1].x() - radius,
64 projectedSelectionPolygon[1].y() - radius,
65 HANDLE_WIDTH, HANDLE_WIDTH);
66 painter.drawRect(topRightCorner);
67
68 const QRectF bottomRightCorner = QRectF(projectedSelectionPolygon[2].x() - radius,
69 projectedSelectionPolygon[2].y() - radius,
70 HANDLE_WIDTH, HANDLE_WIDTH);
71 painter.drawRect(bottomRightCorner);
72
73 const QRectF bottomLeftCorner = QRectF(projectedSelectionPolygon[3].x() - radius,
74 projectedSelectionPolygon[3].y() - radius,
75 HANDLE_WIDTH, HANDLE_WIDTH);
76 painter.drawRect(bottomLeftCorner);
77
78 if (toolProperties.showSelectionInfoEnabled()) {
79 paintSelectionInfo(painter, transform, tParams.viewTransform, tParams.originalSelectionRectF, projectedSelectionPolygon);
80 }
81}
82
83void SelectionPainter::paintSelectionInfo(QPainter& painter, const QTransform& mergedTransform, const QTransform& viewTransform, const QRectF& selectionRect, const QPolygonF& projectedPolygonF)
84{
85 QRect projectedSelectionRect = mergedTransform.mapRect(selectionRect).toAlignedRect();
86 QRect originalSelectionRect = viewTransform.mapRect(selectionRect).toAlignedRect();
87 QPolygon projectedPolygon = projectedPolygonF.toPolygon();
88
89 QPoint projectedCenter = projectedSelectionRect.center();
90 QPoint originalCenter = originalSelectionRect.center();
91 int diffX = static_cast<int>(projectedCenter.x() - originalCenter.x());
92 int diffY = static_cast<int>(originalCenter.y() - projectedCenter.y());
93 painter.drawText(projectedPolygon[0] - QPoint(HANDLE_WIDTH, HANDLE_WIDTH),
94 QString("Size: %1x%2. Diff: %3, %4.").arg(QString::number(selectionRect.width()),
95 QString::number(selectionRect.height()),
96 QString::number(diffX),
97 QString::number(diffY)));
98}
Layer
Definition: layer.h:33
Object
Definition: object.h:42
QBrush
QColor
QPainter
QPainter::drawPolygon
void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule)
QPainter::drawRect
void drawRect(const QRectF &rectangle)
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
QPainter::setBrush
void setBrush(const QBrush &brush)
QPainter::setPen
void setPen(const QColor &color)
QPen
QPoint
QPoint::x
int x() const const
QPoint::y
int y() const const
QPolygon
QPolygonF
QPolygonF::toPolygon
QPolygon toPolygon() const const
QRect
QRect::center
QPoint center() const const
QRectF
QRectF::height
qreal height() const const
QRectF::width
qreal width() const const
QString
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString::number
QString number(int n, int base)
Qt::NoBrush
NoBrush
Qt::gray
gray
Qt::DashLine
DashLine
QTransform
QTransform::map
QPoint map(const QPoint &point) const const
QTransform::mapRect
QRect mapRect(const QRect &rectangle) const const
TransformParameters
Definition: selectionpainter.h:30
TransformToolProperties
Definition: toolproperties.h:573
Generated on Thu Jan 29 2026 05:10:58 for Pencil2D by doxygen 1.9.6 based on revision 2c971b937d0608b05aa496b0f7e9aebcddf8e7fc