| 1 |
// Copyright (C) 2020 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtGui> |
| 5 |
#include <QtWidgets> |
| 6 |
|
| 7 |
class PixelGridViewWidget: public QWidget |
| 8 |
{ |
| 9 |
public: |
| 10 |
PixelGridViewWidget() { |
| 11 |
setMinimumSize(200, 200); |
| 12 |
} |
| 13 |
|
| 14 |
QImage sampleImage; |
| 15 |
qreal scale = 1; |
| 16 |
qreal deviceIndependentPixelSize = 40; |
| 17 |
bool drawDipGrid = true; |
| 18 |
bool drawDpGrid = false; |
| 19 |
QVector<QRectF> dpClipRects; |
| 20 |
QVector<QRectF> dipClipRects; |
| 21 |
|
| 22 |
void paintEvent(QPaintEvent *ev) override { |
| 23 |
QPainter p(this); |
| 24 |
|
| 25 |
const qreal devicePixelSize = deviceIndependentPixelSize / scale; |
| 26 |
QSize widgetSize = geometry().size(); |
| 27 |
|
| 28 |
p.setClipRect(ev->rect()); |
| 29 |
p.fillRect(ev->rect(), QColorConstants::Svg::gray); |
| 30 |
|
| 31 |
// draw device pixel grid and content |
| 32 |
for (qreal y = 0; y < widgetSize.height(); y += devicePixelSize) { |
| 33 |
for (qreal x = 0; x < widgetSize.width(); x += devicePixelSize) { |
| 34 |
QRectF pixelRect = QRect(x,y, qCeil(devicePixelSize), qCeil(devicePixelSize)); |
| 35 |
|
| 36 |
QPen pen; |
| 37 |
pen.setWidth(1); |
| 38 |
pen.setColor(QColor(100, 100, 100, 100)); |
| 39 |
|
| 40 |
// draw pixel outline |
| 41 |
if (drawDpGrid) |
| 42 |
p.drawRect(pixelRect); |
| 43 |
|
| 44 |
// draw content (if in QImage range) |
| 45 |
QPoint imagePos(qRound(x / devicePixelSize), qRound(y / devicePixelSize)); |
| 46 |
if (imagePos.x() < sampleImage.width() && imagePos.y() < sampleImage.height()) { |
| 47 |
QColor pixel = sampleImage.pixelColor(imagePos); |
| 48 |
p.fillRect(pixelRect, pixel); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// draw device-independent pixel grid |
| 54 |
if (drawDipGrid) |
| 55 |
for (qreal y = 0; y < widgetSize.height(); y += deviceIndependentPixelSize) { |
| 56 |
for (qreal x = 0; x < widgetSize.width(); x += deviceIndependentPixelSize) { |
| 57 |
|
| 58 |
QRectF pixelRect = QRect(x,y, deviceIndependentPixelSize, deviceIndependentPixelSize); |
| 59 |
QPen pen; |
| 60 |
pen.setWidth(1); |
| 61 |
pen.setColor(QColor(250, 100, 100, 255)); |
| 62 |
p.setPen(pen); |
| 63 |
p.drawRect(pixelRect); // pixel outline |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// draw clip rects |
| 68 |
for (auto it = dpClipRects.begin(); it != dpClipRects.end(); ++it) { |
| 69 |
QRect clipRectRect(it->x() * devicePixelSize, it->y() * devicePixelSize, |
| 70 |
it->width() * devicePixelSize, it->height() * devicePixelSize); |
| 71 |
QColor yellow(QColorConstants::Svg::yellow); |
| 72 |
p.fillRect(clipRectRect, yellow); |
| 73 |
} |
| 74 |
for (auto it = dipClipRects.begin(); it != dipClipRects.end(); ++it) { |
| 75 |
QRect clipRectRect(it->x() * deviceIndependentPixelSize, it->y() * deviceIndependentPixelSize, |
| 76 |
it->width() * deviceIndependentPixelSize, it->height() * deviceIndependentPixelSize); |
| 77 |
QColor yellow(QColorConstants::Svg::yellow); |
| 78 |
p.fillRect(clipRectRect, yellow); |
| 79 |
} |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
class PixelGadgetWidget : public QWidget |
| 84 |
{ |
| 85 |
public: |
| 86 |
std::function<void ()> updateSampleImage; |
| 87 |
QVector<QRectF> dpClipRects; |
| 88 |
QVector<QRectF> dipClipRects; |
| 89 |
|
| 90 |
PixelGadgetWidget() { |
| 91 |
|
| 92 |
QHBoxLayout *layout = new QHBoxLayout(); |
| 93 |
|
| 94 |
PixelGridViewWidget *pixelGridView = new PixelGridViewWidget(); |
| 95 |
layout->addWidget(pixelGridView, 10); |
| 96 |
|
| 97 |
QVBoxLayout *controlLayout = new QVBoxLayout(); |
| 98 |
layout->addLayout(controlLayout); |
| 99 |
|
| 100 |
controlLayout->addWidget(new QLabel("<b>Content</b>")); |
| 101 |
|
| 102 |
QComboBox *contentSelect = new QComboBox(); |
| 103 |
contentSelect->addItem("<empty>"); |
| 104 |
contentSelect->addItem("lines"); |
| 105 |
contentSelect->addItem("CE_ShapedFrame (fusion)"); |
| 106 |
contentSelect->addItem("CC_ScrollBar (fusion)"); |
| 107 |
connect(contentSelect, QOverload<int>::of(&QComboBox::currentIndexChanged), [this](int){ this->updateSampleImage(); }); |
| 108 |
controlLayout->addWidget(contentSelect); |
| 109 |
|
| 110 |
QCheckBox *clipping = new QCheckBox("Clipping"); |
| 111 |
connect(clipping, &QCheckBox::stateChanged, [this](int){ this->updateSampleImage(); }); |
| 112 |
controlLayout->addWidget(clipping); |
| 113 |
|
| 114 |
controlLayout->addWidget(new QLabel("Start Point")); |
| 115 |
QSpinBox *startX = new QSpinBox(); |
| 116 |
startX->setValue(1); |
| 117 |
startX->setMinimum(-1000); |
| 118 |
startX->setMaximum(1000); |
| 119 |
connect(startX, QOverload<int>::of(&QSpinBox::valueChanged), [this](int){ this->updateSampleImage(); }); |
| 120 |
controlLayout->addWidget(startX); |
| 121 |
|
| 122 |
QSpinBox *startY = new QSpinBox(); |
| 123 |
startY->setValue(1); |
| 124 |
startX->setMinimum(-1000); |
| 125 |
startX->setMaximum(1000); |
| 126 |
connect(startY, QOverload<int>::of(&QSpinBox::valueChanged), [this](int){ this->updateSampleImage(); }); |
| 127 |
controlLayout->addWidget(startY); |
| 128 |
|
| 129 |
controlLayout->addWidget(new QLabel("<b>Paint Settings</b>")); |
| 130 |
|
| 131 |
controlLayout->addWidget(new QLabel("Scale")); |
| 132 |
|
| 133 |
QCheckBox *quarterIncrement = new QCheckBox("25% Scale Increments"); |
| 134 |
quarterIncrement->setChecked(true); |
| 135 |
controlLayout->addWidget(quarterIncrement); |
| 136 |
|
| 137 |
QSpinBox *scale = new QSpinBox(); |
| 138 |
scale->setSuffix("%"); |
| 139 |
scale->setSingleStep(25); |
| 140 |
connect(quarterIncrement, &QCheckBox::stateChanged, [scale](int val){ |
| 141 |
scale->setSingleStep(val > 0 ? 25 : 1); |
| 142 |
}); |
| 143 |
scale->setMinimum(100); |
| 144 |
scale->setMaximum(200); |
| 145 |
scale->setValue(100); // 1x |
| 146 |
connect(scale, QOverload<int>::of(&QSpinBox::valueChanged), [this](int){ this->updateSampleImage(); }); |
| 147 |
controlLayout->addWidget(scale); |
| 148 |
|
| 149 |
QCheckBox *antialising = new QCheckBox("Antialiasing"); |
| 150 |
connect(antialising, &QCheckBox::stateChanged, [this](int){ this->updateSampleImage(); }); |
| 151 |
controlLayout->addWidget(antialising); |
| 152 |
|
| 153 |
QCheckBox *offset = new QCheckBox("+0.5 Offset"); |
| 154 |
connect(offset, &QCheckBox::stateChanged, [this](int){ this->updateSampleImage(); }); |
| 155 |
controlLayout->addWidget(offset); |
| 156 |
|
| 157 |
QCheckBox *dpAllign = new QCheckBox("Device Pixel Aligned Painting"); |
| 158 |
connect(dpAllign, &QCheckBox::stateChanged, [this](int){ this->updateSampleImage(); }); |
| 159 |
controlLayout->addWidget(dpAllign); |
| 160 |
|
| 161 |
controlLayout->addWidget(new QLabel("<b>Visualization Settings</b>")); |
| 162 |
|
| 163 |
QSpinBox *pixelSize = new QSpinBox(); |
| 164 |
pixelSize->setValue(40); |
| 165 |
pixelSize->setMinimum(10); |
| 166 |
connect(pixelSize, QOverload<int>::of(&QSpinBox::valueChanged), [this](int){ this->updateSampleImage(); }); |
| 167 |
controlLayout->addWidget(pixelSize); |
| 168 |
|
| 169 |
QCheckBox *dipGrid = new QCheckBox("Device Independent Pixel Grid"); |
| 170 |
dipGrid->setChecked(true); |
| 171 |
connect(dipGrid, &QCheckBox::stateChanged, [this](int){ this->updateSampleImage(); }); |
| 172 |
controlLayout->addWidget(dipGrid); |
| 173 |
|
| 174 |
QCheckBox *dpGrid = new QCheckBox("Device Pixel Grid"); |
| 175 |
dpGrid->setChecked(false); |
| 176 |
connect(dpGrid, &QCheckBox::stateChanged, [this](int){ this->updateSampleImage(); }); |
| 177 |
controlLayout->addWidget(dpGrid); |
| 178 |
|
| 179 |
QCheckBox *dipClipRects = new QCheckBox("Device Independent Clip Rects"); |
| 180 |
dipClipRects->setChecked(false); |
| 181 |
connect(dipClipRects, &QCheckBox::stateChanged, [this](int){ this->updateSampleImage(); }); |
| 182 |
controlLayout->addWidget(dipClipRects); |
| 183 |
|
| 184 |
QCheckBox *dpClipRects = new QCheckBox("Device Clip Rects"); |
| 185 |
dpClipRects->setChecked(false); |
| 186 |
connect(dpClipRects, &QCheckBox::stateChanged, [this](int){ this->updateSampleImage(); }); |
| 187 |
controlLayout->addWidget(dpClipRects); |
| 188 |
|
| 189 |
controlLayout->addStretch(10); |
| 190 |
|
| 191 |
setLayout(layout); |
| 192 |
|
| 193 |
auto setClip = [this](QPainter *p, QRect clipRect, bool enableClip, qreal currentScale, QPoint currentStart) { |
| 194 |
if (enableClip) { |
| 195 |
// Set the clip rect without the sub-pixel offset in order to |
| 196 |
// get the same device clip rect as the no-offset case. (This |
| 197 |
// simulates the case where painting code does not control clip |
| 198 |
// rects which have already been set.) |
| 199 |
p->setClipRect(clipRect); |
| 200 |
|
| 201 |
// Print/record clip debug info - device and device independent rects. |
| 202 |
p->save(); |
| 203 |
p->translate(-currentStart.x(), -currentStart.y()); |
| 204 |
QRegion dipClip = p->clipRegion(); |
| 205 |
p->scale(qreal(1) / currentScale, qreal(1) / currentScale); |
| 206 |
QRegion dpClip = p->clipRegion(); |
| 207 |
this->dipClipRects.append(dipClip.boundingRect()); |
| 208 |
this->dpClipRects.append(dpClip.boundingRect()); |
| 209 |
p->restore(); |
| 210 |
} else { |
| 211 |
// p->setClipEnabled(false); |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
auto initPainter_1 = [](QPainter *p, QPoint start, qreal scale) { |
| 216 |
p->scale(scale, scale); |
| 217 |
p->translate(start); |
| 218 |
}; |
| 219 |
|
| 220 |
auto initPainter_2 = [](QPainter *p, bool antialias, QPointF offset) { |
| 221 |
p->setRenderHint(QPainter::Antialiasing, antialias); |
| 222 |
p->translate(offset); // sub-(device-independent) pixel offset |
| 223 |
}; |
| 224 |
|
| 225 |
auto drawLines = [=](QPainter *p, QPoint start, qreal scale, bool antialias, bool clip, QPointF offset) { |
| 226 |
QPen pen; |
| 227 |
pen.setColor(QColor(50, 50, 250)); |
| 228 |
|
| 229 |
// 1-width line |
| 230 |
|
| 231 |
p->save(); { |
| 232 |
QPoint drawPoint = start + QPoint(0, 0); |
| 233 |
pen.setWidth(1); |
| 234 |
p->setPen(pen); |
| 235 |
initPainter_1(p, drawPoint, scale); |
| 236 |
setClip(p, QRect(0, 0, 1, 7), clip, scale, drawPoint); |
| 237 |
initPainter_2(p, antialias, offset); |
| 238 |
p->drawLine(0, 0, 0, 6); |
| 239 |
} p->restore(); |
| 240 |
|
| 241 |
|
| 242 |
// 2-width line |
| 243 |
p->save(); { |
| 244 |
QPoint drawPoint = start + QPoint(4, 0); |
| 245 |
pen.setWidth(2); |
| 246 |
p->setPen(pen); |
| 247 |
initPainter_1(p, drawPoint, scale); |
| 248 |
setClip(p, QRect(-1, -1, 2, 9), clip, scale, drawPoint); |
| 249 |
initPainter_2(p, antialias, offset); |
| 250 |
p->drawLine(0, 0, 0, 7); |
| 251 |
} p->restore(); |
| 252 |
|
| 253 |
// cosmetic line |
| 254 |
p->save(); { |
| 255 |
QPoint drawPoint = start + QPoint(8, 0); |
| 256 |
pen.setWidth(0); |
| 257 |
p->setPen(pen); |
| 258 |
initPainter_1(p, drawPoint, scale); |
| 259 |
setClip(p, QRect(0, 0, 1, 7), clip, scale, drawPoint); |
| 260 |
initPainter_2(p, antialias, offset); |
| 261 |
p->drawLine(0, 0, 0, 7); |
| 262 |
} p->restore(); |
| 263 |
}; |
| 264 |
|
| 265 |
auto drawCE_ShapedFrame = [=](QPainter *p, QPoint start, qreal scale, bool antialias, bool clip, QPointF offset) { |
| 266 |
|
| 267 |
QRect frameRect(0, 0, 8, 8); |
| 268 |
|
| 269 |
QStyleOptionFrame opt; |
| 270 |
opt.initFrom(this); |
| 271 |
opt.rect = frameRect; |
| 272 |
opt.frameShape = QFrame::StyledPanel; |
| 273 |
|
| 274 |
initPainter_1(p, start, scale); |
| 275 |
setClip(p, frameRect, clip, scale, start); |
| 276 |
initPainter_2(p, antialias, offset); |
| 277 |
|
| 278 |
QStyle *style = QStyleFactory::create("fusion"); |
| 279 |
style->drawControl(QStyle::CE_ShapedFrame, &opt, p, nullptr); |
| 280 |
}; |
| 281 |
|
| 282 |
auto drawCC_ScrollBar = [=](QPainter *p, QPoint start, qreal scale, bool antialias, bool clip, QPointF offset) { |
| 283 |
|
| 284 |
QRect scrollBarRect(0, 0, 100, 18); |
| 285 |
|
| 286 |
QStyleOptionSlider opt; |
| 287 |
opt.initFrom(this); |
| 288 |
// opt.palette = QPalette(QColor(200, 200, 200)); // force light mode |
| 289 |
opt.rect = scrollBarRect; |
| 290 |
opt.subControls = QStyle::SC_All; |
| 291 |
opt.orientation = Qt::Horizontal; |
| 292 |
opt.minimum = 0; |
| 293 |
opt.maximum = 10; |
| 294 |
opt.sliderPosition = 0; |
| 295 |
opt.sliderValue = 0; |
| 296 |
opt.singleStep = 1; |
| 297 |
opt.pageStep = 5; |
| 298 |
opt.upsideDown = false; |
| 299 |
opt.state |= QStyle::State_Horizontal; |
| 300 |
//opt.state |= QStyle::State_On; |
| 301 |
|
| 302 |
initPainter_1(p, start, scale); |
| 303 |
setClip(p, scrollBarRect.adjusted(0, 0, 0, 2), clip, scale, start); |
| 304 |
initPainter_2(p, antialias, offset); |
| 305 |
|
| 306 |
QStyle *style = QStyleFactory::create("fusion"); |
| 307 |
style->drawComplexControl(QStyle::CC_ScrollBar, &opt, p, nullptr); |
| 308 |
}; |
| 309 |
|
| 310 |
updateSampleImage = [=]() { |
| 311 |
|
| 312 |
bool clip = clipping->isChecked(); |
| 313 |
QPoint start(startX->value(), startY->value()); |
| 314 |
qreal _scale = qreal(scale->value()) / 100.0; |
| 315 |
bool antialias = antialising->isChecked(); |
| 316 |
|
| 317 |
// Set up sub-pixel offset |
| 318 |
QPointF _offset(0, 0); |
| 319 |
if (offset->isChecked()) { |
| 320 |
_offset += QPointF(0.5, 0.5); |
| 321 |
} |
| 322 |
if (dpAllign->isChecked()) { |
| 323 |
|
| 324 |
// Align to the closest device pixel, in accordance |
| 325 |
QPointF dpStart = QPointF(start) * _scale; |
| 326 |
QPointF dpStartRounded(qCeil(dpStart.x()), qCeil(dpStart.y())); // down/right |
| 327 |
// QPointF dpStartRounded(qRound(dpStart.x()), qRound(dpStart.y())); // nearest |
| 328 |
|
| 329 |
QPointF offset = dpStartRounded - dpStart; |
| 330 |
/* |
| 331 |
qDebug() << ""; |
| 332 |
qDebug() << "start" << start; |
| 333 |
qDebug() << "dpStart" << dpStart; |
| 334 |
qDebug() << "dpStartRounded" << dpStartRounded; |
| 335 |
qDebug() << "offset" << offset; |
| 336 |
*/ |
| 337 |
_offset += offset; |
| 338 |
} |
| 339 |
|
| 340 |
// qDebug() << "offset" << _offset; |
| 341 |
|
| 342 |
QImage img(200, 200, QImage::Format_ARGB32_Premultiplied); |
| 343 |
img.fill(QColorConstants::Svg::gray); |
| 344 |
QPainter painter(&img); |
| 345 |
|
| 346 |
// Prepare for recording clip rects during paint |
| 347 |
this->dipClipRects.clear(); |
| 348 |
this->dpClipRects.clear(); |
| 349 |
|
| 350 |
// paint currently selected content |
| 351 |
switch (contentSelect->currentIndex()) { |
| 352 |
case 0: |
| 353 |
break; |
| 354 |
case 1: |
| 355 |
drawLines(&painter, start, _scale, antialias, clip, _offset); |
| 356 |
break; |
| 357 |
case 2: |
| 358 |
drawCE_ShapedFrame(&painter, start, _scale, antialias, clip, _offset); |
| 359 |
break; |
| 360 |
case 3: |
| 361 |
drawCC_ScrollBar(&painter, start, _scale, antialias, clip, _offset); |
| 362 |
break; |
| 363 |
}; |
| 364 |
|
| 365 |
img.save("sampleimage.png"); |
| 366 |
|
| 367 |
// Update the pixel grid view |
| 368 |
pixelGridView->sampleImage = img; |
| 369 |
pixelGridView->scale = _scale; |
| 370 |
pixelGridView->deviceIndependentPixelSize = pixelSize->value(); |
| 371 |
pixelGridView->drawDipGrid = dipGrid->isChecked(); |
| 372 |
pixelGridView->drawDpGrid = dpGrid->isChecked(); |
| 373 |
pixelGridView->dipClipRects.clear(); |
| 374 |
if (dipClipRects->isChecked()) |
| 375 |
pixelGridView->dipClipRects = this->dipClipRects; |
| 376 |
pixelGridView->dpClipRects.clear(); |
| 377 |
if (dpClipRects->isChecked()) |
| 378 |
pixelGridView->dpClipRects = this->dpClipRects; |
| 379 |
pixelGridView->update(); |
| 380 |
}; |
| 381 |
|
| 382 |
updateSampleImage(); |
| 383 |
} |
| 384 |
}; |
| 385 |
|
| 386 |
int main(int argc, char **argv) { |
| 387 |
|
| 388 |
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); |
| 389 |
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); |
| 390 |
|
| 391 |
QApplication app(argc, argv); |
| 392 |
|
| 393 |
PixelGadgetWidget pixelGadget; |
| 394 |
pixelGadget.resize(400, 300); |
| 395 |
pixelGadget.show(); |
| 396 |
|
| 397 |
return app.exec(); |
| 398 |
} |
| 399 |
|