visibility-checkbox.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <QPaintEvent>
  2. #include <QPixmap>
  3. #include <QPainter>
  4. #include "visibility-checkbox.hpp"
  5. #include <util/c99defs.h>
  6. VisibilityCheckBox::VisibilityCheckBox() : QCheckBox()
  7. {
  8. QString checkedFile;
  9. QString uncheckedFile;
  10. if (devicePixelRatio() >= 2) {
  11. checkedFile = ":/res/images/visible_mask_2x.png";
  12. uncheckedFile = ":/res/images/invisible_mask_2x.png";
  13. } else {
  14. checkedFile = ":/res/images/visible_mask.png";
  15. uncheckedFile = ":/res/images/invisible_mask.png";
  16. }
  17. checkedImage = QPixmap::fromImage(QImage(checkedFile));
  18. uncheckedImage = QPixmap::fromImage(QImage(uncheckedFile));
  19. setMinimumSize(16, 16);
  20. setStyleSheet("outline: none;");
  21. }
  22. void VisibilityCheckBox::paintEvent(QPaintEvent *event)
  23. {
  24. UNUSED_PARAMETER(event);
  25. QPixmap &pixmap = isChecked() ? checkedImage : uncheckedImage;
  26. QImage image(pixmap.size(), QImage::Format_ARGB32);
  27. QPainter draw(&image);
  28. draw.setCompositionMode(QPainter::CompositionMode_Source);
  29. draw.drawPixmap(0, 0, pixmap.width(), pixmap.height(), pixmap);
  30. draw.setCompositionMode(QPainter::CompositionMode_SourceIn);
  31. draw.fillRect(QRectF(QPointF(0.0f, 0.0f), pixmap.size()),
  32. palette().color(foregroundRole()));
  33. QPainter p(this);
  34. p.drawPixmap(0, 0, 16, 16, QPixmap::fromImage(image));
  35. }