• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • krita API Reference
  • KDE Home
  • Contact Us
 

KritaWidgets

  • sources
  • kfour-appscomplete
  • krita
  • libs
  • widgets
KisVisualRectangleSelectorShape.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Wolthera van Hovell tot Westerflier <[email protected]>, (C) 2016
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 #include "KisVisualRectangleSelectorShape.h"
7 
8 #include <QColor>
9 #include <QPainter>
10 #include <QRect>
11 #include <QList>
12 #include <QLineF>
13 #include <QtMath>
14 
15 #include "kis_debug.h"
16 
17 KisVisualRectangleSelectorShape::KisVisualRectangleSelectorShape(QWidget *parent,
18  Dimensions dimension,
19  const KoColorSpace *cs,
20  int channel1, int channel2,
21  const KoColorDisplayRendererInterface *displayRenderer,
22  int width,
23  singelDTypes d)
24  : KisVisualColorSelectorShape(parent, dimension, cs, channel1, channel2, displayRenderer)
25 {
26  //qDebug() << "creating KisVisualRectangleSelectorShape" << this;
27  m_type = d;
28  m_barWidth = width;
29 }
30 
31 KisVisualRectangleSelectorShape::~KisVisualRectangleSelectorShape()
32 {
33  //qDebug() << "deleting KisVisualRectangleSelectorShape" << this;
34 }
35 
36 void KisVisualRectangleSelectorShape::setBorderWidth(int width)
37 {
38  m_barWidth = width;
39 }
40 
41 QRect KisVisualRectangleSelectorShape::getSpaceForSquare(QRect geom)
42 {
43  QPointF tl;
44  QPointF br;
45 
46  if (m_type==KisVisualRectangleSelectorShape::vertical) {
47  br = geom.bottomRight();
48  tl = QPoint(geom.topLeft().x()+m_barWidth, geom.topLeft().y());
49  } else if (m_type==KisVisualRectangleSelectorShape::horizontal) {
50  br = geom.bottomRight();
51  tl = QPoint(geom.topLeft().x(), geom.topLeft().y()+m_barWidth);
52  } else {
53  tl = QPointF (geom.topLeft().x()+m_barWidth, geom.topLeft().y()+m_barWidth);
54  br = QPointF (geom.bottomRight().x()-m_barWidth, geom.bottomRight().y()-m_barWidth);
55 
56  }
57  QRect a(tl.toPoint(), br.toPoint());
58  QRect r(a.left(), a.top(), qMin(a.height(), a.width()), qMin(a.height(), a.width()));
59  return r;
60 }
61 
62 QRect KisVisualRectangleSelectorShape::getSpaceForCircle(QRect geom)
63 {
64  return getSpaceForSquare(geom);
65 }
66 
67 QRect KisVisualRectangleSelectorShape::getSpaceForTriangle(QRect geom)
68 {
69  return getSpaceForSquare(geom);
70 }
71 
72 QPointF KisVisualRectangleSelectorShape::convertShapeCoordinateToWidgetCoordinate(QPointF coordinate) const
73 {
74  // Reminder: in Qt widget space, origin is top-left, but we want zero y to be at the bottom
75  qreal x = 0.5 * m_barWidth;
76  qreal y = 0.5 * m_barWidth;
77  qreal offset = 5.0;
78  KisVisualColorSelectorShape::Dimensions dimension = getDimensions();
79  if (dimension == KisVisualColorSelectorShape::onedimensional) {
80  if ( m_type == KisVisualRectangleSelectorShape::vertical) {
81  y = qMin((1.0 - coordinate.x())*(height()-offset*2)+offset, (qreal)height());
82  } else if (m_type == KisVisualRectangleSelectorShape::horizontal) {
83  x = qMin(coordinate.x()*(width()-offset*2)+offset, (qreal)width());
84  } else if (m_type == KisVisualRectangleSelectorShape::border) {
85 
86  QRectF innerRect(m_barWidth/2, m_barWidth/2, width()-m_barWidth, height()-m_barWidth);
87  QPointF left (innerRect.left(),innerRect.center().y());
88  QList <QLineF> polygonLines;
89  polygonLines.append(QLineF(left, innerRect.topLeft()));
90  polygonLines.append(QLineF(innerRect.topLeft(), innerRect.topRight()));
91  polygonLines.append(QLineF(innerRect.topRight(), innerRect.bottomRight()));
92  polygonLines.append(QLineF(innerRect.bottomRight(), innerRect.bottomLeft()));
93  polygonLines.append(QLineF(innerRect.bottomLeft(), left));
94 
95  qreal totalLength =0.0;
96  Q_FOREACH(QLineF line, polygonLines) {
97  totalLength += line.length();
98  }
99 
100  qreal length = coordinate.x()*totalLength;
101  QPointF intersect(x,y);
102  Q_FOREACH(QLineF line, polygonLines) {
103  if (line.length()>length && length>0){
104  intersect = line.pointAt(length/line.length());
105 
106  }
107  length-=line.length();
108  }
109  x = qRound(intersect.x());
110  y = qRound(intersect.y());
111 
112  }
113  else /*if (m_type == KisVisualRectangleSelectorShape::borderMirrored)*/ {
114 
115  QRectF innerRect(m_barWidth/2, m_barWidth/2, width()-m_barWidth, height()-m_barWidth);
116  QPointF bottom (innerRect.center().x(), innerRect.bottom());
117  QList <QLineF> polygonLines;
118  polygonLines.append(QLineF(bottom, innerRect.bottomLeft()));
119  polygonLines.append(QLineF(innerRect.bottomLeft(), innerRect.topLeft()));
120  polygonLines.append(QLineF(innerRect.topLeft(), innerRect.topRight()));
121  polygonLines.append(QLineF(innerRect.topRight(), innerRect.bottomRight()));
122  polygonLines.append(QLineF(innerRect.bottomRight(), bottom));
123 
124  qreal totalLength =0.0;
125  Q_FOREACH(QLineF line, polygonLines) {
126  totalLength += line.length();
127  }
128 
129  qreal length = coordinate.x()*(totalLength/2);
130  QPointF intersect(x,y);
131  if (coordinate.y()==1) {
132  for (int i = polygonLines.size()-1; i==0; i--) {
133  QLineF line = polygonLines.at(i);
134  if (line.length()>length && length>0){
135  intersect = line.pointAt(length/line.length());
136 
137  }
138  length-=line.length();
139  }
140  } else {
141  Q_FOREACH(QLineF line, polygonLines) {
142  if (line.length()>length && length>0){
143  intersect = line.pointAt(length/line.length());
144 
145  }
146  length-=line.length();
147  }
148  }
149  x = qRound(intersect.x());
150  y = qRound(intersect.y());
151 
152  }
153  } else {
154  x = qMin(coordinate.x()*(width()-offset*2)+offset, (qreal)width());
155  y = qMin((1.0 - coordinate.y())*(height()-offset*2)+offset, (qreal)height());
156  }
157  return QPointF(x,y);
158 }
159 
160 QPointF KisVisualRectangleSelectorShape::convertWidgetCoordinateToShapeCoordinate(QPointF coordinate) const
161 {
162  // Reminder: in Qt widget space, origin is top-left, but we want zero y to be at the bottom
163  //default values:
164  qreal x = 0.5;
165  qreal y = 0.5;
166  qreal offset = 5.0;
167  KisVisualColorSelectorShape::Dimensions dimension = getDimensions();
168  if (dimension == KisVisualColorSelectorShape::onedimensional ) {
169  if (m_type == KisVisualRectangleSelectorShape::vertical) {
170  x = 1.0 - (coordinate.y()-offset)/(height()-offset*2);
171  } else if (m_type == KisVisualRectangleSelectorShape::horizontal) {
172  x = (coordinate.x()-offset)/(width()-offset*2);
173  } else if (m_type == KisVisualRectangleSelectorShape::border) {
174  //border
175 
176  QRectF innerRect(m_barWidth, m_barWidth, width()-(m_barWidth*2), height()-(m_barWidth*2));
177  QPointF left (innerRect.left(),innerRect.center().y());
178  QList <QLineF> polygonLines;
179  polygonLines.append(QLineF(left, innerRect.topLeft()));
180  polygonLines.append(QLineF(innerRect.topLeft(), innerRect.topRight()));
181  polygonLines.append(QLineF(innerRect.topRight(), innerRect.bottomRight()));
182  polygonLines.append(QLineF(innerRect.bottomRight(), innerRect.bottomLeft()));
183  polygonLines.append(QLineF(innerRect.bottomLeft(), left));
184 
185  QLineF radius(coordinate, this->geometry().center());
186  QPointF intersect(0.5,0.5);
187  qreal length = 0.0;
188  qreal totalLength = 0.0;
189  bool foundIntersect = false;
190  Q_FOREACH(QLineF line, polygonLines) {
191  if (line.intersect(radius,&intersect)==QLineF::BoundedIntersection && foundIntersect==false)
192  {
193  foundIntersect = true;
194  length+=QLineF(line.p1(), intersect).length();
195 
196  }
197  if (foundIntersect==false) {
198  length+=line.length();
199  }
200  totalLength+=line.length();
201  }
202 
203  x = length/totalLength;
204 
205  } else /*if (m_type == KisVisualRectangleSelectorShape::borderMirrored)*/ {
206  //border
207 
208  QRectF innerRect(m_barWidth, m_barWidth, width()-(m_barWidth*2), height()-(m_barWidth*2));
209  QPointF bottom (innerRect.center().x(), innerRect.bottom());
210  QList <QLineF> polygonLines;
211  polygonLines.append(QLineF(bottom, innerRect.bottomLeft()));
212  polygonLines.append(QLineF(innerRect.bottomLeft(), innerRect.topLeft()));
213  polygonLines.append(QLineF(innerRect.topLeft(), innerRect.topRight()));
214  polygonLines.append(QLineF(innerRect.topRight(), innerRect.bottomRight()));
215  polygonLines.append(QLineF(innerRect.bottomRight(), bottom));
216 
217  QLineF radius(coordinate, this->geometry().center());
218  QPointF intersect(0.5,0.5);
219  qreal length = 0.0;
220  qreal totalLength = 0.0;
221  bool foundIntersect = false;
222  Q_FOREACH(QLineF line, polygonLines) {
223  if (line.intersect(radius,&intersect)==QLineF::BoundedIntersection && foundIntersect==false)
224  {
225  foundIntersect = true;
226  length+=QLineF(line.p1(), intersect).length();
227 
228  }
229  if (foundIntersect==false) {
230  length+=line.length();
231  }
232  totalLength+=line.length();
233  }
234  int halflength = totalLength/2;
235 
236  if (length>halflength) {
237  x = (halflength - (length-halflength))/halflength;
238  y = 1.0;
239  } else {
240  x = length/halflength;
241  y = 0.0;
242  }
243  }
244  }
245  else {
246  x = (coordinate.x()-offset)/(width()-offset*2);
247  y = 1.0 - (coordinate.y()-offset)/(height()-offset*2);
248  }
249  x = qBound((qreal)0.0, x, (qreal)1.0);
250  y = qBound((qreal)0.0, y, (qreal)1.0);
251  return QPointF(x, y);
252 }
253 
254 QRegion KisVisualRectangleSelectorShape::getMaskMap()
255 {
256  QRegion mask = QRegion(0,0,width(),height());
257  if (m_type==KisVisualRectangleSelectorShape::border || m_type==KisVisualRectangleSelectorShape::borderMirrored) {
258  mask = mask.subtracted(QRegion(m_barWidth, m_barWidth, width()-(m_barWidth*2), height()-(m_barWidth*2)));
259  }
260  return mask;
261 }
262 
263 QImage KisVisualRectangleSelectorShape::renderAlphaMask() const
264 {
265  // Hi-DPI aware rendering requires that we determine the device pixel dimension;
266  // actual widget size in device pixels is not accessible unfortunately, it might be 1px smaller...
267  const int deviceWidth = qCeil(width() * devicePixelRatioF());
268  const int deviceHeight = qCeil(height() * devicePixelRatioF());
269 
270  QImage alphaMask(deviceWidth, deviceHeight, QImage::Format_Alpha8);
271  alphaMask.fill(0);
272  alphaMask.setDevicePixelRatio(devicePixelRatioF());
273  QPainter painter(&alphaMask);
274  painter.setRenderHint(QPainter::Antialiasing);
275  painter.setBrush(Qt::white);
276  painter.setPen(Qt::NoPen);
277 
278  painter.drawRect(3, 3, width() - 6, height() - 6);
279  if (m_type == border || m_type == borderMirrored) {
280  painter.setBrush(Qt::black);
281  painter.drawRect(m_barWidth, m_barWidth, width() - 2 * m_barWidth, height() - 2 * m_barWidth);
282  }
283 
284  return alphaMask;
285 }
286 
287 void KisVisualRectangleSelectorShape::drawCursor()
288 {
289  //qDebug() << this << "KisVisualRectangleSelectorShape::drawCursor: image needs update" << imagesNeedUpdate();
290  QPointF cursorPoint = convertShapeCoordinateToWidgetCoordinate(getCursorPosition());
291  QImage fullSelector = getImageMap();
292  QColor col = getColorFromConverter(getCurrentColor());
293  QPainter painter;
294  painter.begin(&fullSelector);
295  painter.setRenderHint(QPainter::Antialiasing);
296  //QPainterPath path;
297  QBrush fill;
298  fill.setStyle(Qt::SolidPattern);
299 
300  int cursorwidth = 5;
301  QRect rect(cursorPoint.toPoint().x()-cursorwidth,cursorPoint.toPoint().y()-cursorwidth,
302  cursorwidth*2,cursorwidth*2);
303  if (m_type==KisVisualRectangleSelectorShape::vertical){
304  int x = ( cursorPoint.x()-(width()/2)+1 );
305  int y = ( cursorPoint.y()-cursorwidth );
306  rect.setCoords(x, y, x+width()-2, y+(cursorwidth*2));
307  } else {
308  int x = cursorPoint.x()-cursorwidth;
309  int y = cursorPoint.y()-(height()/2)+1;
310  rect.setCoords(x, y, x+(cursorwidth*2), y+cursorwidth-2);
311  }
312  QRectF innerRect(m_barWidth, m_barWidth, width()-(m_barWidth*2), height()-(m_barWidth*2));
313  if (getDimensions() == KisVisualColorSelectorShape::onedimensional && m_type!=KisVisualRectangleSelectorShape::border && m_type!=KisVisualRectangleSelectorShape::borderMirrored) {
314  painter.setPen(Qt::white);
315  fill.setColor(Qt::white);
316  painter.setBrush(fill);
317  painter.drawRect(rect);
318  //set filter conversion!
319  fill.setColor(col);
320  painter.setPen(Qt::black);
321  painter.setBrush(fill);
322  rect.setCoords(rect.topLeft().x()+1, rect.topLeft().y()+1,
323  rect.topLeft().x()+rect.width()-2, rect.topLeft().y()+rect.height()-2);
324  painter.drawRect(rect);
325 
326  }else if(m_type==KisVisualRectangleSelectorShape::borderMirrored){
327  painter.setPen(Qt::white);
328  fill.setColor(Qt::white);
329  painter.setBrush(fill);
330  painter.drawEllipse(cursorPoint, cursorwidth, cursorwidth);
331  QPoint mirror(innerRect.center().x()+(innerRect.center().x()-cursorPoint.x()),cursorPoint.y());
332  painter.drawEllipse(mirror, cursorwidth, cursorwidth);
333  fill.setColor(col);
334  painter.setPen(Qt::black);
335  painter.setBrush(fill);
336  painter.drawEllipse(cursorPoint, cursorwidth-1, cursorwidth-1);
337  painter.drawEllipse(mirror, cursorwidth-1, cursorwidth-1);
338 
339  } else {
340  painter.setPen(Qt::white);
341  fill.setColor(Qt::white);
342  painter.setBrush(fill);
343  painter.drawEllipse(cursorPoint, cursorwidth, cursorwidth);
344  fill.setColor(col);
345  painter.setPen(Qt::black);
346  painter.setBrush(fill);
347  painter.drawEllipse(cursorPoint, cursorwidth-1.0, cursorwidth-1.0);
348  }
349  painter.end();
350  setFullImage(fullSelector);
351 }
QList::append
void append(const T &value)
KisVisualColorSelectorShape::getDimensions
Dimensions getDimensions() const
getDimensions
Definition: KisVisualColorSelectorShape.cpp:345
QColor
QWidget::y
int y() const
KisVisualRectangleSelectorShape::singelDTypes
singelDTypes
Definition: KisVisualRectangleSelectorShape.h:15
QPainter::setPen
void setPen(const QColor &color)
QRegion
QRect::topLeft
QPoint topLeft() const
KisVisualRectangleSelectorShape::renderAlphaMask
QImage renderAlphaMask() const override
render the alpha mask for the widget background the returned image is expected to be QImage::Format_A...
Definition: KisVisualRectangleSelectorShape.cpp:263
QImage::fill
void fill(uint pixelValue)
KisVisualRectangleSelectorShape::getSpaceForCircle
QRect getSpaceForCircle(QRect geom) override
Definition: KisVisualRectangleSelectorShape.cpp:62
QPainter::drawEllipse
void drawEllipse(const QRectF &rectangle)
QRect
KisVisualColorSelectorShape::getCurrentColor
KoColor getCurrentColor()
getCurrentColor
Definition: KisVisualColorSelectorShape.cpp:354
QPainter::drawRect
void drawRect(const QRectF &rectangle)
QWidget::rect
QRect rect() const
KisVisualColorSelectorShape::onedimensional
Definition: KisVisualColorSelectorShape.h:45
KisVisualColorSelectorShape::Dimensions
Dimensions
The Dimensions enum Whether or not the shape is single or two dimensional.
Definition: KisVisualColorSelectorShape.h:45
QWidget
KisVisualRectangleSelectorShape::setBorderWidth
void setBorderWidth(int width) override
setBorderWidth set the border of the single dimensional selector.
Definition: KisVisualRectangleSelectorShape.cpp:36
QRect::width
int width() const
KisVisualColorSelectorShape::getImageMap
QImage getImageMap()
Definition: KisVisualColorSelectorShape.cpp:156
QWidget::x
int x() const
QPoint::x
int x() const
QPoint::y
int y() const
QRect::bottomRight
QPoint bottomRight() const
QList
KisVisualColorSelectorShape::setFullImage
void setFullImage(QImage full)
setFullImage Set the full widget image to be painted.
Definition: KisVisualColorSelectorShape.cpp:350
QPainter
QPointF
KisVisualRectangleSelectorShape.h
KisVisualRectangleSelectorShape::border
Definition: KisVisualRectangleSelectorShape.h:15
QRect::setCoords
void setCoords(int x1, int y1, int x2, int y2)
QPainter::begin
bool begin(QPaintDevice *device)
QLineF::intersect
IntersectType intersect(const QLineF &line, QPointF *intersectionPoint) const
KisVisualRectangleSelectorShape::borderMirrored
Definition: KisVisualRectangleSelectorShape.h:15
QPainter::end
bool end()
QBrush::setStyle
void setStyle(Qt::BrushStyle style)
KisVisualRectangleSelectorShape::vertical
Definition: KisVisualRectangleSelectorShape.h:15
KisVisualRectangleSelectorShape::KisVisualRectangleSelectorShape
KisVisualRectangleSelectorShape(QWidget *parent, Dimensions dimension, const KoColorSpace *cs, int channel1, int channel2, const KoColorDisplayRendererInterface *displayRenderer=KoDumbColorDisplayRenderer::instance(), int width=20, KisVisualRectangleSelectorShape::singelDTypes d=KisVisualRectangleSelectorShape::vertical)
Definition: KisVisualRectangleSelectorShape.cpp:17
KisVisualColorSelectorShape::getCursorPosition
QPointF getCursorPosition()
getCursorPosition
Definition: KisVisualColorSelectorShape.cpp:67
QLineF::length
qreal length() const
QLineF
QBrush
QPainter::setBrush
void setBrush(const QBrush &brush)
KisVisualColorSelectorShape
The KisVisualColorSelectorShape class A 2d widget can represent at maximum 2 coordinates.
Definition: KisVisualColorSelectorShape.h:37
QWidget::height
int height() const
QRect::height
int height() const
QPointF::x
qreal x() const
QPointF::y
qreal y() const
QWidget::mask
QRegion mask() const
QBrush::setColor
void setColor(const QColor &color)
QPointF::toPoint
QPoint toPoint() const
QRectF
KisVisualRectangleSelectorShape::~KisVisualRectangleSelectorShape
~KisVisualRectangleSelectorShape() override
Definition: KisVisualRectangleSelectorShape.cpp:31
QWidget::geometry
const QRect & geometry() const
KisVisualRectangleSelectorShape::getSpaceForTriangle
QRect getSpaceForTriangle(QRect geom) override
Definition: KisVisualRectangleSelectorShape.cpp:67
QLineF::pointAt
QPointF pointAt(qreal t) const
KisVisualRectangleSelectorShape::horizontal
Definition: KisVisualRectangleSelectorShape.h:15
QRegion::subtracted
QRegion subtracted(const QRegion &r) const
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
QWidget::width
int width() const
KisVisualRectangleSelectorShape::getSpaceForSquare
QRect getSpaceForSquare(QRect geom) override
getSpaceForSquare
Definition: KisVisualRectangleSelectorShape.cpp:41
QImage
QPoint
KisVisualColorSelectorShape::getColorFromConverter
QColor getColorFromConverter(KoColor c)
getColorFromConverter
Definition: KisVisualColorSelectorShape.cpp:129
QLineF::p1
QPointF p1() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 23 2021 11:48:22 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KritaWidgets

Skip menu "KritaWidgets"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

krita API Reference

Skip menu "krita API Reference"
  • libs
  •   KritaBasicFlakes
  •   brush
  •   KritaUndo2
  •   KritaFlake
  •   image
  •   KritaPlugin
  •   Krita
  •   KritaPigment
  •   KritaResources
  •   KritaStore
  •   ui
  •   KritaWidgets
  •   KritaWidgetUtils
  • plugins
  •   Assitants
  •   Extensions
  •   Filters
  •   Generators
  •   Formats
  •           src
  •   PaintOps
  •     libpaintop

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal