All Classes Namespaces Functions Variables Enumerations Properties Pages
selectionpainter.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU 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 
23 
24 SelectionPainter::SelectionPainter()
25 {
26 }
27 
28 void SelectionPainter::paint(QPainter& painter,
29  const Object* object,
30  int layerIndex,
31  BaseTool* tool,
32  TransformParameters& tParams,
33  QPolygonF original,
34  QPolygonF currentNotMapped)
35 {
36  Layer* layer = object->getLayer(layerIndex);
37 
38  if (layer == nullptr) { return; }
39 
40  if (layer->type() == Layer::BITMAP)
41  {
42  painter.setBrush(Qt::NoBrush);
43  painter.setPen(QPen(Qt::DashLine));
44 
45  // Draw previous selection
46  painter.drawPolygon(tParams.lastSelectionPolygonF.toPolygon());
47 
48  // Draw current selection
49  painter.drawPolygon(tParams.currentSelectionPolygonF.toPolygon());
50 
51  }
52  if (layer->type() == Layer::VECTOR)
53  {
54  painter.setBrush(QColor(0, 0, 0, 20));
55  painter.setPen(Qt::gray);
56  painter.drawPolygon(tParams.currentSelectionPolygonF);
57  }
58 
59  if (layer->type() != Layer::VECTOR || tool->type() != SELECT)
60  {
61  painter.setPen(Qt::SolidLine);
62  painter.setBrush(QBrush(Qt::gray));
63  int width = 6;
64  int radius = width / 2;
65 
66  const QRectF topLeftCorner = QRectF(tParams.currentSelectionPolygonF[0].x() - radius,
67  tParams.currentSelectionPolygonF[0].y() - radius,
68  width, width);
69  painter.drawRect(topLeftCorner);
70 
71  const QRectF topRightCorner = QRectF(tParams.currentSelectionPolygonF[1].x() - radius,
72  tParams.currentSelectionPolygonF[1].y() - radius,
73  width, width);
74  painter.drawRect(topRightCorner);
75 
76  const QRectF bottomRightCorner = QRectF(tParams.currentSelectionPolygonF[2].x() - radius,
77  tParams.currentSelectionPolygonF[2].y() - radius,
78  width, width);
79  painter.drawRect(bottomRightCorner);
80 
81  const QRectF bottomLeftCorner = QRectF(tParams.currentSelectionPolygonF[3].x() - radius,
82  tParams.currentSelectionPolygonF[3].y() - radius,
83  width, width);
84  painter.drawRect(bottomLeftCorner);
85 
86  if (tool->properties.showSelectionInfo)
87  {
88  int diffX = static_cast<int>(currentNotMapped.boundingRect().x() - original.boundingRect().x());
89  int diffY = static_cast<int>(currentNotMapped.boundingRect().y() - original.boundingRect().y());
90  painter.drawText(static_cast<int>(tParams.currentSelectionPolygonF[0].x()),
91  static_cast<int>(tParams.currentSelectionPolygonF[0].y() - width),
92  QString("Size: %1x%2. Diff: %3, %4.").arg(QString::number(currentNotMapped.boundingRect().width()),
93  QString::number(currentNotMapped.boundingRect().height()),
94  QString::number(diffX),
95  QString::number(diffY)));
96  }
97 
98  painter.setBrush(QColor(0, 255, 0, 50));
99  painter.setPen(Qt::green);
100  }
101 }
QPolygon toPolygon() const const
DashLine
qreal x() const const
qreal y() const const
void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule)
void drawRect(const QRectF &rectangle)
QString number(int n, int base)
void setPen(const QColor &color)
Definition: layer.h:38
void setBrush(const QBrush &brush)
void drawText(const QPointF &position, const QString &text)
QRectF boundingRect() const const
qreal width() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
qreal height() const const
Definition: object.h:41