BiblioteQ

biblioteq_graphicsitempixmap.h
1 #ifndef _BIBLIOTEQ_GRAPHICSITEMPIXMAP_H_
2 #define _BIBLIOTEQ_GRAPHICSITEMPIXMAP_H_
3
4 static void qt_graphicsItem_highlightSelected
5 (QGraphicsItem *item,
6 QPainter *painter,
7 const QStyleOptionGraphicsItem *option)
8 {
9 if(!item || !option || !painter)
10 return;
11
12 const auto &murect = painter->transform().mapRect(QRectF(0, 0, 1, 1));
13
14 if(qFuzzyIsNull(qMax(murect.width(), murect.height())))
15 return;
16
17 const auto &mbrect = painter->transform().mapRect(item->boundingRect());
18
19 if(qMin(mbrect.width(), mbrect.height()) < qreal(1.0))
20 return;
21
22 const QColor bgcolor(70, 130, 180);
23 const qreal pad = 0.0;
24 const qreal penWidth = 2.5;
25
26 painter->setBrush(Qt::NoBrush);
27 painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine));
28 painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad));
29 }
30
31 class biblioteq_graphicsitempixmap: public QGraphicsPixmapItem
32 {
33 public:
34 biblioteq_graphicsitempixmap(const QPixmap &pixmap, QGraphicsItem *parent):
35 QGraphicsPixmapItem(pixmap, parent)
36 {
37 }
38
39 ~biblioteq_graphicsitempixmap()
40 {
41 }
42
43 void paint(QPainter *painter,
44 const QStyleOptionGraphicsItem *option,
45 QWidget *widget = nullptr)
46 {
47 Q_UNUSED(widget);
48
49 if(!option || !painter)
50 return;
51
52 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
53
54 auto exposed_rect(option->exposedRect.adjusted(-1, -1, 1, 1));
55
56 exposed_rect &= QRectF(offset().x(),
57 offset().y(),
58 pixmap().width(),
59 pixmap().height());
60 painter->drawPixmap
61 (exposed_rect, pixmap(), exposed_rect.translated(-offset()));
62
63 if(option->state & (QStyle::State_Selected | QStyle::State_HasFocus))
64 qt_graphicsItem_highlightSelected(this, painter, option);
65 }
66 };
67
68 #endif
biblioteq_graphicsitempixmap
Definition: biblioteq_graphicsitempixmap.h:31