File indexing completed on 2024-05-12 16:02:29

0001 /*
0002  * SPDX-FileCopyrightText: 2022 Alvin Wong <alvin@alvinhc.com>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "KisRepaintDebugger.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include <QPaintDevice>
0012 #include <QPainter>
0013 #include <QPaintEvent>
0014 #include <QThread>
0015 #include <QApplication>
0016 #include <QMessageBox>
0017 #include <QDebug>
0018 
0019 bool KisRepaintDebugger::enabled()
0020 {
0021     static bool enabled = qEnvironmentVariableIntValue("KRITA_DEBUG_REPAINT") == 1;
0022     return enabled;
0023 }
0024 
0025 void KisRepaintDebugger::paint(QPaintDevice *paintDevice, const QRect &widgetRect)
0026 {
0027     paint(paintDevice, &widgetRect, 1);
0028 }
0029 
0030 void KisRepaintDebugger::paint(QPaintDevice *paintDevice, const QVector<QRect> &widgetRects)
0031 {
0032     paint(paintDevice, widgetRects.constData(), widgetRects.size());
0033 }
0034 
0035 void KisRepaintDebugger::paint(QPaintDevice *paintDevice, const QPaintEvent *event)
0036 {
0037     paint(paintDevice, event->rect());
0038 }
0039 
0040 void KisRepaintDebugger::paintFull(QPaintDevice *pd)
0041 {
0042     if (!enabled()) {
0043         return;
0044     }
0045     const QRect rect = QRectF(QPointF(), QSizeF(pd->width(), pd->height()) * pd->devicePixelRatioF())
0046             .toAlignedRect();
0047     paint(pd, &rect, 1);
0048 }
0049 
0050 void KisRepaintDebugger::paint(QPaintDevice *paintDevice, const QRect *widgetRects, size_t count)
0051 {
0052     if (!enabled()) {
0053         return;
0054     }
0055     constexpr int ALPHA = 63;
0056     static QVector<QColor> colors {
0057         QColor(255, 0, 0, ALPHA),
0058         QColor(0, 255, 0, ALPHA),
0059         QColor(0, 0, 255, ALPHA),
0060         QColor(255, 255, 0, ALPHA),
0061         QColor(255, 0, 255, ALPHA),
0062         QColor(0, 255, 255, ALPHA),
0063     };
0064     m_colorIndex = (m_colorIndex + 1) % colors.size();
0065     QPainter gc(paintDevice);
0066     for (size_t i = 0; i < count; i++) {
0067         gc.fillRect(widgetRects[i], colors[m_colorIndex]);
0068     }
0069 }