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#include "basetool.h"
22
23SelectionPainter::SelectionPainter()
24{
25}
26
27void SelectionPainter::paint(QPainter& painter,
28 const Object* object,
29 int layerIndex,
30 BaseTool* tool,
31 TransformParameters& tParams)
32{
33 Layer* layer = object->getLayer(layerIndex);
34
35 if (layer == nullptr) { return; }
36
37 QTransform transform = tParams.selectionTransform * tParams.viewTransform;
38 QPolygonF projectedSelectionPolygon = transform.map(tParams.originalSelectionRectF);
39
40 if (layer->type() == Layer::BITMAP)
41 {
42 painter.setBrush(Qt::NoBrush);
43 painter.setPen(QPen(Qt::DashLine));
44
45 // Draw current selection
46 painter.drawPolygon(projectedSelectionPolygon.toPolygon());
47
48 }
49 if (layer->type() == Layer::VECTOR)
50 {
51 painter.setBrush(QColor(0, 0, 0, 20));
52 painter.setPen(Qt::gray);
53 painter.drawPolygon(projectedSelectionPolygon);
54 }
55
56 if (tool->type() == SELECT || tool->type() == MOVE)
57 {
58 painter.setPen(Qt::SolidLine);
59 painter.setBrush(QBrush(Qt::gray));
60 int radius = HANDLE_WIDTH / 2;
61
62 const QRectF topLeftCorner = QRectF(projectedSelectionPolygon[0].x() - radius,
63 projectedSelectionPolygon[0].y() - radius,
64 HANDLE_WIDTH, HANDLE_WIDTH);
65 painter.drawRect(topLeftCorner);
66
67 const QRectF topRightCorner = QRectF(projectedSelectionPolygon[1].x() - radius,
68 projectedSelectionPolygon[1].y() - radius,
69 HANDLE_WIDTH, HANDLE_WIDTH);
70 painter.drawRect(topRightCorner);
71
72 const QRectF bottomRightCorner = QRectF(projectedSelectionPolygon[2].x() - radius,
73 projectedSelectionPolygon[2].y() - radius,
74 HANDLE_WIDTH, HANDLE_WIDTH);
75 painter.drawRect(bottomRightCorner);
76
77 const QRectF bottomLeftCorner = QRectF(projectedSelectionPolygon[3].x() - radius,
78 projectedSelectionPolygon[3].y() - radius,
79 HANDLE_WIDTH, HANDLE_WIDTH);
80 painter.drawRect(bottomLeftCorner);
81 }
82
83 if (tool->properties.showSelectionInfo) {
84 paintSelectionInfo(painter, transform, tParams.viewTransform, tParams.originalSelectionRectF, projectedSelectionPolygon);
85 }
86}
87
88void SelectionPainter::paintSelectionInfo(QPainter& painter, const QTransform& mergedTransform, const QTransform& viewTransform, const QRectF& selectionRect, const QPolygonF& projectedPolygonF)
89{
90 QRect projectedSelectionRect = mergedTransform.mapRect(selectionRect).toAlignedRect();
91 QRect originalSelectionRect = viewTransform.mapRect(selectionRect).toAlignedRect();
92 QPolygon projectedPolygon = projectedPolygonF.toPolygon();
93
94 QPoint projectedCenter = projectedSelectionRect.center();
95 QPoint originalCenter = originalSelectionRect.center();
96 int diffX = static_cast<int>(projectedCenter.x() - originalCenter.x());
97 int diffY = static_cast<int>(originalCenter.y() - projectedCenter.y());
98 painter.drawText(projectedPolygon[0] - QPoint(HANDLE_WIDTH, HANDLE_WIDTH),
99 QString("Size: %1x%2. Diff: %3, %4.").arg(QString::number(selectionRect.width()),
100 QString::number(selectionRect.height()),
101 QString::number(diffX),
102 QString::number(diffY)));
103}
BaseTool
Definition: basetool.h:70
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:29
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39