| 267 | } |
| 268 | |
| 269 | QIcon createColorIcon(const QList<QColor> &colors, const QIcon &background, ColorIconFlags flags) |
| 270 | { |
| 271 | QIcon colorIcon; |
| 272 | |
| 273 | // Create a pixmap for each common icon size. |
| 274 | for (int size : {16, 22, 24, 32, 48}) { |
| 275 | QPixmap pixmap(QSize(size, size) * qApp->devicePixelRatio()); |
| 276 | pixmap.setDevicePixelRatio(qApp->devicePixelRatio()); |
| 277 | pixmap.fill(Qt::transparent); |
| 278 | QPainter painter(&pixmap); |
| 279 | // Configure hairlines for visualization of outline or transparency (visualizeTransparent): |
| 280 | painter.setPen(QPen(qApp->palette().color(QPalette::Active, QPalette::WindowText), 0)); |
| 281 | painter.setBrush(Qt::NoBrush); |
| 282 | |
| 283 | if (background.isNull()) { |
| 284 | // Full-size color rectangles. |
| 285 | // Draw rectangles left to right: |
| 286 | for (int i = 0; i < colors.count(); ++i) { |
| 287 | if (!colors.at(i).isValid()) { |
| 288 | continue; |
| 289 | } |
| 290 | QRect rect(QPoint(size * i / colors.count(), 0), QPoint(size * (i + 1) / colors.count(), size)); |
| 291 | if ((flags & VisualizeTransparent) && (colors.at(i) == Qt::transparent)) { |
| 292 | painter.drawLine(rect.topLeft(), rect.bottomRight()); |
| 293 | painter.drawLine(rect.bottomLeft(), rect.topRight()); |
| 294 | } else { |
| 295 | painter.fillRect(rect, colors.at(i)); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Draw hairline outline: |
| 300 | // To get the hairline on the outermost pixels, we shrink the rectangle by a half pixel on each edge. |
| 301 | const qreal halfPixelWidth = 0.5 / pixmap.devicePixelRatio(); |
| 302 | painter.drawRect(QRectF(QPointF(halfPixelWidth, halfPixelWidth), QPointF(qreal(size) - halfPixelWidth, qreal(size) - halfPixelWidth))); |
| 303 | } else { |
| 304 | // Lower 25% color rectangles. |
| 305 | // Draw background icon: |
| 306 | background.paint(&painter, QRect(QPoint(0, 0), QSize(size, size))); |
| 307 | |
| 308 | // Draw rectangles left to right: |
| 309 | for (int i = 0; i < colors.count(); ++i) { |
| 310 | if (!colors.at(i).isValid()) { |
| 311 | continue; |
| 312 | } |
| 313 | QRect rect(QPoint(size * i / colors.count(), size * 3 / 4), QPoint(size * (i + 1) / colors.count(), size)); |
| 314 | if ((flags & VisualizeTransparent) && (colors.at(i) == Qt::transparent)) { |
| 315 | painter.drawLine(rect.topLeft(), rect.bottomRight()); |
| 316 | painter.drawLine(rect.bottomLeft(), rect.topRight()); |
| 317 | } else { |
| 318 | painter.fillRect(rect, colors.at(i)); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | painter.end(); |
| 324 | colorIcon.addPixmap(pixmap); |
| 325 | } |
| 326 |
no test coverage detected