8#include <QtGui/QPainter>
12QSGSoftwareInternalRectangleNode::QSGSoftwareInternalRectangleNode()
17 , m_bottomLeftRadius(0)
18 , m_bottomRightRadius(0)
19 , m_devicePixelRatio(1)
21 , m_cornerPixmapIsDirty(
true)
22 , m_isTopLeftRadiusSet(
false)
23 , m_isTopRightRadiusSet(
false)
24 , m_isBottomLeftRadiusSet(
false)
25 , m_isBottomRightRadiusSet(
false)
27 m_pen.setJoinStyle(Qt::MiterJoin);
28 m_pen.setMiterLimit(0);
29 setMaterial((QSGMaterial*)1);
30 setGeometry((QSGGeometry*)1);
35 QRect alignedRect = rect.toAlignedRect();
36 if (m_rect != alignedRect) {
38 markDirty(DirtyMaterial);
44 if (m_color != color) {
46 m_cornerPixmapIsDirty =
true;
47 markDirty(DirtyMaterial);
53 if (m_penColor != color) {
55 m_cornerPixmapIsDirty =
true;
56 markDirty(DirtyMaterial);
62 if (m_penWidth != width) {
64 m_cornerPixmapIsDirty =
true;
65 markDirty(DirtyMaterial);
72 double distance = secondStop.first - firstStop.first;
73 double distanceDelta = newPos - firstStop.first;
74 double modifierValue = distanceDelta / distance;
75 const auto firstStopRgbColor = firstStop.second.toRgb();
76 const auto secondStopRgbColor = secondStop.second.toRgb();
77 int redDelta = (secondStopRgbColor.red() - firstStopRgbColor.red()) * modifierValue;
78 int greenDelta = (secondStopRgbColor.green() - firstStopRgbColor.green()) * modifierValue;
79 int blueDelta = (secondStopRgbColor.blue() - firstStopRgbColor.blue()) * modifierValue;
80 int alphaDelta = (secondStopRgbColor.alpha() - firstStopRgbColor.alpha()) * modifierValue;
82 QGradientStop newStop;
83 newStop.first = newPos;
84 newStop.second = QColor(firstStopRgbColor.red() + redDelta,
85 firstStopRgbColor.green() + greenDelta,
86 firstStopRgbColor.blue() + blueDelta,
87 firstStopRgbColor.alpha() + alphaDelta);
95 bool needsNormalization =
false;
96 for (
const QGradientStop &stop : std::as_const(stops)) {
97 if (stop.first < 0.0 || stop.first > 1.0) {
98 needsNormalization =
true;
103 if (needsNormalization) {
104 QGradientStops normalizedStops;
105 if (stops.size() == 1) {
108 QGradientStop stop = stops.at(0);
110 normalizedStops.append(stop);
116 for (
int i = 0; i < stops.size(); ++i) {
117 if (stops.at(i).first < 0.0) {
119 }
else if (stops.at(i).first > 1.0) {
130 if (below + 1 < stops.size()) {
131 normalizedStops.append(interpolateStop(stops.at(below), stops.at(below + 1), 0.0));
133 QGradientStop singleStop;
134 singleStop.first = 0.0;
135 singleStop.second = stops.at(below).second;
136 normalizedStops.append(singleStop);
140 for (
int i = 0; i < between.size(); ++i)
141 normalizedStops.append(stops.at(between.at(i)));
146 normalizedStops.append(interpolateStop(stops.at(above), stops.at(above - 1), 1.0));
148 QGradientStop singleStop;
149 singleStop.first = 1.0;
150 singleStop.second = stops.at(above).second;
151 normalizedStops.append(singleStop);
156 m_stops = normalizedStops;
161 m_cornerPixmapIsDirty =
true;
162 markDirty(DirtyMaterial);
167 if (m_vertical != vertical) {
168 m_vertical = vertical;
169 m_cornerPixmapIsDirty =
true;
170 markDirty(DirtyMaterial);
176 if (m_radius == radius)
179 m_cornerPixmapIsDirty =
true;
180 markDirty(DirtyMaterial);
185 if (m_isTopLeftRadiusSet && m_topLeftRadius == radius)
187 m_isTopLeftRadiusSet =
true;
188 m_topLeftRadius = radius;
189 m_cornerPixmapIsDirty =
true;
190 markDirty(DirtyMaterial);
195 if (m_isTopRightRadiusSet && m_topRightRadius == radius)
197 m_isTopRightRadiusSet =
true;
198 m_topRightRadius = radius;
199 m_cornerPixmapIsDirty =
true;
200 markDirty(DirtyMaterial);
205 if (m_isBottomLeftRadiusSet && m_bottomLeftRadius == radius)
207 m_isBottomLeftRadiusSet =
true;
208 m_bottomLeftRadius = radius;
209 m_cornerPixmapIsDirty =
true;
210 markDirty(DirtyMaterial);
215 if (m_isBottomRightRadiusSet && m_bottomRightRadius == radius)
217 m_isBottomRightRadiusSet =
true;
218 m_bottomRightRadius = radius;
219 m_cornerPixmapIsDirty =
true;
220 markDirty(DirtyMaterial);
225 if (!m_isTopLeftRadiusSet)
227 m_isTopLeftRadiusSet =
false;
228 m_cornerPixmapIsDirty =
true;
229 markDirty(DirtyMaterial);
234 if (!m_isTopRightRadiusSet)
236 m_isTopRightRadiusSet =
false;
237 m_cornerPixmapIsDirty =
true;
238 markDirty(DirtyMaterial);
243 if (!m_isBottomLeftRadiusSet)
245 m_isBottomLeftRadiusSet =
false;
246 m_cornerPixmapIsDirty =
true;
247 markDirty(DirtyMaterial);
252 if (!m_isBottomRightRadiusSet)
254 m_isBottomRightRadiusSet =
false;
255 m_cornerPixmapIsDirty =
true;
256 markDirty(DirtyMaterial);
265 if (!m_penWidth || m_penColor == Qt::transparent) {
268 m_pen = QPen(m_penColor);
269 m_pen.setWidthF(m_penWidth);
272 if (!m_stops.isEmpty()) {
273 QLinearGradient gradient(QPoint(0,0), QPoint(m_vertical ? 0 : m_rect.width(), m_vertical ? m_rect.height() : 0));
274 gradient.setStops(m_stops);
275 m_brush = QBrush(gradient);
277 m_brush = QBrush(m_color);
280 if (m_cornerPixmapIsDirty) {
281 generateCornerPixmap();
282 m_cornerPixmapIsDirty =
false;
290 if (!qFuzzyCompare(painter->device()->devicePixelRatio(), m_devicePixelRatio)) {
291 m_devicePixelRatio = painter->device()->devicePixelRatio();
292 generateCornerPixmap();
295 if (painter->transform().isRotating()) {
301 && !m_isTopLeftRadiusSet
302 && !m_isTopRightRadiusSet
303 && !m_isBottomLeftRadiusSet
304 && !m_isBottomRightRadiusSet) {
307 painter->setPen(Qt::NoPen);
308 painter->setBrush(m_brush);
309 painter->drawRect(m_rect);
310 }
else if (!m_isTopLeftRadiusSet
311 && !m_isTopRightRadiusSet
312 && !m_isBottomLeftRadiusSet
313 && !m_isBottomRightRadiusSet) {
316 QPixmap pixmap = QPixmap(qRound(m_rect.width() * m_devicePixelRatio), qRound(m_rect.height() * m_devicePixelRatio));
317 pixmap.fill(Qt::transparent);
318 pixmap.setDevicePixelRatio(m_devicePixelRatio);
319 QPainter pixmapPainter(&pixmap);
320 paintRectangle(&pixmapPainter, QRect(0, 0, m_rect.width(), m_rect.height()));
322 QPainter::RenderHints previousRenderHints = painter->renderHints();
323 painter->setRenderHint(QPainter::SmoothPixmapTransform,
true);
324 painter->drawPixmap(m_rect, pixmap);
325 painter->setRenderHints(previousRenderHints);
329 QPixmap pixmap = QPixmap(qRound(m_rect.width() * m_devicePixelRatio), qRound(m_rect.height() * m_devicePixelRatio));
330 pixmap.fill(Qt::transparent);
331 pixmap.setDevicePixelRatio(m_devicePixelRatio);
332 QPainter pixmapPainter(&pixmap);
334 paintRectangleIndividualCorners(&pixmapPainter, QRect(0, 0, m_rect.width(), m_rect.height()));
336 QPainter::RenderHints previousRenderHints = painter->renderHints();
337 painter->setRenderHint(QPainter::SmoothPixmapTransform,
true);
338 painter->drawPixmap(m_rect, pixmap);
339 painter->setRenderHints(previousRenderHints);
346 if (!m_isTopLeftRadiusSet
347 && !m_isTopRightRadiusSet
348 && !m_isBottomLeftRadiusSet
349 && !m_isBottomRightRadiusSet) {
350 paintRectangle(painter, m_rect);
352 paintRectangleIndividualCorners(painter, m_rect);
360 || m_topLeftRadius > 0.0f
361 || m_topRightRadius > 0.0f
362 || m_bottomRightRadius > 0.0f
363 || m_bottomLeftRadius > 0.0f)
365 if (m_color.alpha() < 255)
367 if (m_penWidth > 0.0f && m_penColor.alpha() < 255)
369 if (m_stops.size() > 0) {
370 for (
const QGradientStop &stop : std::as_const(m_stops)) {
371 if (stop.second.alpha() < 255)
388 int radius = qFloor(qMin(qMin(rect.width(), rect.height()) * 0.5, m_radius));
389 QPainter::RenderHints previousRenderHints = painter->renderHints();
390 painter->setRenderHint(QPainter::Antialiasing,
false);
392 if (m_penWidth > 0) {
396 double borderWidth = qMin(m_penWidth, rect.width() * 0.5);
397 double borderHeight = qMin(m_penWidth, rect.height() * 0.5);
401 if (borderWidth > radius) {
403 QRectF borderTopOutside(QPointF(rect.x() + radius, rect.y()),
404 QPointF(rect.x() + rect.width() - radius, rect.y() + radius));
405 QRectF borderTopInside(QPointF(rect.x() + borderWidth, rect.y() + radius),
406 QPointF(rect.x() + rect.width() - borderWidth, rect.y() + borderHeight));
407 QRectF borderBottomOutside(QPointF(rect.x() + radius, rect.y() + rect.height() - radius),
408 QPointF(rect.x() + rect.width() - radius, rect.y() + rect.height()));
409 QRectF borderBottomInside(QPointF(rect.x() + borderWidth, rect.y() + rect.height() - borderHeight),
410 QPointF(rect.x() + rect.width() - borderWidth, rect.y() + rect.height() - radius));
412 if (borderTopOutside.isValid())
413 painter->fillRect(borderTopOutside, m_penColor);
414 if (borderTopInside.isValid())
415 painter->fillRect(borderTopInside, m_penColor);
416 if (borderBottomOutside.isValid())
417 painter->fillRect(borderBottomOutside, m_penColor);
418 if (borderBottomInside.isValid())
419 painter->fillRect(borderBottomInside, m_penColor);
423 QRectF borderTop(QPointF(rect.x() + radius, rect.y()),
424 QPointF(rect.x() + rect.width() - radius, rect.y() + borderHeight));
425 QRectF borderBottom(QPointF(rect.x() + radius, rect.y() + rect.height() - borderHeight),
426 QPointF(rect.x() + rect.width() - radius, rect.y() + rect.height()));
427 if (borderTop.isValid())
428 painter->fillRect(borderTop, m_penColor);
429 if (borderBottom.isValid())
430 painter->fillRect(borderBottom, m_penColor);
432 QRectF borderLeft(QPointF(rect.x(), rect.y() + radius),
433 QPointF(rect.x() + borderWidth, rect.y() + rect.height() - radius));
434 QRectF borderRight(QPointF(rect.x() + rect.width() - borderWidth, rect.y() + radius),
435 QPointF(rect.x() + rect.width(), rect.y() + rect.height() - radius));
436 if (borderLeft.isValid())
437 painter->fillRect(borderLeft, m_penColor);
438 if (borderRight.isValid())
439 painter->fillRect(borderRight, m_penColor);
445 if (radius * 2 >= rect.width() && radius * 2 >= rect.height()) {
447 painter->drawPixmap(rect, m_cornerPixmap, m_cornerPixmap.rect());
451 int scaledRadius = qRound(radius * m_devicePixelRatio);
452 QRectF topLeftCorner(QPointF(rect.x(), rect.y()),
453 QPointF(rect.x() + radius, rect.y() + radius));
454 painter->drawPixmap(topLeftCorner, m_cornerPixmap, QRectF(0, 0, scaledRadius, scaledRadius));
455 QRectF topRightCorner(QPointF(rect.x() + rect.width() - radius, rect.y()),
456 QPointF(rect.x() + rect.width(), rect.y() + radius));
457 painter->drawPixmap(topRightCorner, m_cornerPixmap, QRectF(scaledRadius, 0, scaledRadius, scaledRadius));
458 QRectF bottomLeftCorner(QPointF(rect.x(), rect.y() + rect.height() - radius),
459 QPointF(rect.x() + radius, rect.y() + rect.height()));
460 painter->drawPixmap(bottomLeftCorner, m_cornerPixmap, QRectF(0, scaledRadius, scaledRadius, scaledRadius));
461 QRectF bottomRightCorner(QPointF(rect.x() + rect.width() - radius, rect.y() + rect.height() - radius),
462 QPointF(rect.x() + rect.width(), rect.y() + rect.height()));
463 painter->drawPixmap(bottomRightCorner, m_cornerPixmap, QRectF(scaledRadius, scaledRadius, scaledRadius, scaledRadius));
469 QRectF brushRect = QRectF(rect).marginsRemoved(QMarginsF(m_penWidth, m_penWidth, m_penWidth, m_penWidth));
470 if (brushRect.width() < 0)
471 brushRect.setWidth(0);
472 if (brushRect.height() < 0)
473 brushRect.setHeight(0);
474 double innerRectRadius = qMax(0.0, radius - m_penWidth);
477 if (m_color.alpha() > 0 || !m_stops.empty()) {
478 if (innerRectRadius > 0) {
480 if (m_stops.empty()) {
482 QRectF centerRect(QPointF(brushRect.x() + innerRectRadius, brushRect.y()),
483 QPointF(brushRect.x() + brushRect.width() - innerRectRadius, brushRect.y() + brushRect.height()));
484 painter->fillRect(centerRect, m_color);
485 QRectF leftRect(QPointF(brushRect.x(), brushRect.y() + innerRectRadius),
486 QPointF(brushRect.x() + innerRectRadius, brushRect.y() + brushRect.height() - innerRectRadius));
487 painter->fillRect(leftRect, m_color);
488 QRectF rightRect(QPointF(brushRect.x() + brushRect.width() - innerRectRadius, brushRect.y() + innerRectRadius),
489 QPointF(brushRect.x() + brushRect.width(), brushRect.y() + brushRect.height() - innerRectRadius));
490 painter->fillRect(rightRect, m_color);
493 painter->setPen(Qt::NoPen);
494 painter->setBrush(m_brush);
495 painter->drawRoundedRect(brushRect, innerRectRadius, innerRectRadius);
499 painter->fillRect(brushRect, m_brush);
503 painter->setRenderHints(previousRenderHints);
510 const float w = m_penWidth;
513 const float radiusTL = qMin(qMin(rect.width(), rect.height()) * 0.5f,
514 m_isTopLeftRadiusSet ? m_topLeftRadius : m_radius);
515 const float radiusTR = qMin(qMin(rect.width(), rect.height()) * 0.5f,
516 m_isTopRightRadiusSet ? m_topRightRadius : m_radius);
517 const float radiusBL = qMin(qMin(rect.width(), rect.height()) * 0.5f,
518 m_isBottomLeftRadiusSet ? m_bottomLeftRadius : m_radius);
519 const float radiusBR = qMin(qMin(rect.width(), rect.height()) * 0.5f,
520 m_isBottomRightRadiusSet ? m_bottomRightRadius : m_radius);
522 const float innerRadiusTL = qMin(qMin(rect.width(), rect.height()) * 0.5f, radiusTL - w);
523 const float innerRadiusTR = qMin(qMin(rect.width(), rect.height()) * 0.5f, radiusTR - w);
524 const float innerRadiusBL = qMin(qMin(rect.width(), rect.height()) * 0.5f, radiusBL - w);
525 const float innerRadiusBR = qMin(qMin(rect.width(), rect.height()) * 0.5f, radiusBR - w);
527 QRect rect2 = rect.adjusted(0, 0, 1, 1);
529 path.moveTo(rect2.topRight() - QPointF(radiusTR, -w));
530 if (innerRadiusTR > 0.)
531 path.arcTo(QRectF(rect2.topRight() - QPointF(radiusTR + innerRadiusTR, -w), 2. * QSizeF(innerRadiusTR, innerRadiusTR)), 90, -90);
533 path.lineTo(rect2.topRight() - QPointF(w, -w));
535 if (innerRadiusBR > 0.)
536 path.arcTo(QRectF(rect2.bottomRight() - QPointF(radiusBR + innerRadiusBR, radiusBR + innerRadiusBR), 2. * QSizeF(innerRadiusBR, innerRadiusBR)), 0, -90);
538 path.lineTo(rect2.bottomRight() - QPointF(w, w));
540 if (innerRadiusBL > 0.)
541 path.arcTo(QRectF(rect2.bottomLeft() - QPointF(-w, radiusBL + innerRadiusBL), 2. * QSizeF(innerRadiusBL, innerRadiusBL)), -90, -90);
543 path.lineTo(rect2.bottomLeft() - QPointF(-w, w));
544 if (innerRadiusTL > 0.)
545 path.arcTo(QRectF(rect2.topLeft() + QPointF(w, w), 2. * QSizeF(innerRadiusTL, innerRadiusTL)), -180, -90);
547 path.lineTo(rect2.topLeft() + QPointF(w, w));
550 painter->setPen(Qt::NoPen);
551 painter->setBrush(m_brush);
552 painter->drawPath(path);
555 path.moveTo(rect2.topRight() - QPointF(radiusTR, 0.));
557 path.arcTo(QRectF(rect2.topRight() - 2. * QPointF(radiusTR, 0.), 2. * QSizeF(radiusTR, radiusTR)), 90, -90);
559 path.lineTo(rect2.topRight());
561 path.arcTo(QRectF(rect2.bottomRight() - 2. * QPointF(radiusBR, radiusBR), 2. * QSizeF(radiusBR, radiusBR)), 0, -90);
563 path.lineTo(rect2.bottomRight());
565 path.arcTo(QRectF(rect2.bottomLeft() - 2. * QPointF(0., radiusBL), 2. * QSizeF(radiusBL, radiusBL)), -90, -90);
567 path.lineTo(rect2.bottomLeft());
569 path.arcTo(QRectF(rect2.topLeft() - 2. * QPointF(0., 0.), 2. * QSizeF(radiusTL, radiusTL)), -180, -90);
571 path.lineTo(rect2.topLeft());
574 painter->setBrush(m_penColor);
575 painter->drawPath(path);
582 int radius = qFloor(qMin(qMin(m_rect.width(), m_rect.height()) * 0.5, m_radius));
583 const auto width = qRound(radius * 2 * m_devicePixelRatio);
585 if (m_cornerPixmap.width() != width)
586 m_cornerPixmap = QPixmap(width, width);
588 m_cornerPixmap.setDevicePixelRatio(m_devicePixelRatio);
589 m_cornerPixmap.fill(Qt::transparent);
592 QPainter cornerPainter(&m_cornerPixmap);
593 cornerPainter.setRenderHint(QPainter::Antialiasing);
594 cornerPainter.setCompositionMode(QPainter::CompositionMode_Source);
597 if (m_penWidth > 0) {
598 cornerPainter.setPen(Qt::NoPen);
599 cornerPainter.setBrush(m_penColor);
600 cornerPainter.drawRoundedRect(QRectF(0, 0, radius * 2, radius *2), radius, radius);
604 if (radius > m_penWidth) {
605 cornerPainter.setPen(Qt::NoPen);
606 if (m_stops.isEmpty())
607 cornerPainter.setBrush(m_brush);
609 cornerPainter.setBrush(Qt::transparent);
611 QMarginsF adjustmentMargins(m_penWidth, m_penWidth, m_penWidth, m_penWidth);
612 QRectF cornerCircleRect = QRectF(0, 0, radius * 2, radius * 2).marginsRemoved(adjustmentMargins);
613 cornerPainter.drawRoundedRect(cornerCircleRect, radius, radius);
void setBottomRightRadius(qreal radius) override
void setRect(const QRectF &rect) override
void setRadius(qreal radius) override
void setGradientVertical(bool vertical) override
void resetBottomRightRadius() override
void setTopLeftRadius(qreal radius) override
void setTopRightRadius(qreal radius) override
void setBottomLeftRadius(qreal radius) override
void setColor(const QColor &color) override
void setGradientStops(const QGradientStops &stops) override
void setPenWidth(qreal width) override
void resetBottomLeftRadius() override
void setPenColor(const QColor &color) override
void setAligned(bool aligned) override
void resetTopLeftRadius() override
void resetTopRightRadius() override
Combined button and popup list for selecting options.
static QGradientStop interpolateStop(const QGradientStop &firstStop, const QGradientStop &secondStop, double newPos)