Rev 21430 | Rev 23330 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6490 | jghali | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
7 | #ifndef SCLISTBOXPIXMAP_H |
||
8 | #define SCLISTBOXPIXMAP_H |
||
9 | |||
10 | #include <memory> |
||
10223 | cbradney | 11 | |
22440 | jghali | 12 | #include <QAbstractItemDelegate> |
10223 | cbradney | 13 | #include <QApplication> |
14 | #include <QDebug> |
||
15 | #include <QPainter> |
||
16 | #include <QPixmap> |
||
21430 | jghali | 17 | #include <QScopedPointer> |
22440 | jghali | 18 | #include <QStyleOptionViewItem> |
9874 | avox | 19 | #include <QVariant> |
10223 | cbradney | 20 | |
9874 | avox | 21 | #include "scguardedptr.h" |
10223 | cbradney | 22 | #include "scribusapi.h" |
9874 | avox | 23 | |
24 | class QVariant; |
||
25 | |||
6490 | jghali | 26 | template<unsigned int pixWidth, unsigned int pixHeight> |
20427 | jghali | 27 | class ScListBoxPixmap : public QAbstractItemDelegate |
6490 | jghali | 28 | { |
29 | public: |
||
30 | ScListBoxPixmap(void); |
||
20427 | jghali | 31 | |
32 | virtual QString text(const QVariant&) const = 0; |
||
9874 | avox | 33 | virtual QSize sizeHint (const QStyleOptionViewItem & option, const QModelIndex & index ) const; |
34 | virtual void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const; |
||
6490 | jghali | 35 | protected: |
21430 | jghali | 36 | static QScopedPointer<QPixmap> pmap; |
6490 | jghali | 37 | // The drawPixmap function must not modify pixmap size |
9874 | avox | 38 | virtual void redraw(const QVariant&) const = 0; |
6490 | jghali | 39 | }; |
40 | |||
41 | template<unsigned int pixWidth, unsigned int pixHeight> |
||
21430 | jghali | 42 | QScopedPointer<QPixmap> ScListBoxPixmap<pixWidth, pixHeight>::pmap; |
6490 | jghali | 43 | |
9874 | avox | 44 | |
6490 | jghali | 45 | template<unsigned int pixWidth, unsigned int pixHeight> |
9874 | avox | 46 | ScListBoxPixmap<pixWidth, pixHeight>::ScListBoxPixmap(void) : QAbstractItemDelegate() |
6490 | jghali | 47 | { |
21430 | jghali | 48 | if (pmap.isNull()) |
6490 | jghali | 49 | { |
50 | pmap.reset( new QPixmap(pixWidth, pixHeight) ); |
||
51 | } |
||
52 | }; |
||
53 | |||
54 | |||
55 | template<unsigned int pixWidth, unsigned int pixHeight> |
||
9874 | avox | 56 | QSize ScListBoxPixmap<pixWidth, pixHeight>::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const |
6490 | jghali | 57 | { |
9874 | avox | 58 | int h,w; |
59 | QFontMetrics metrics(option.font); |
||
60 | const QVariant data(index.data(Qt::DisplayRole)); |
||
61 | if ( text(data).isEmpty() ) |
||
62 | { |
||
63 | h = pmap->height(); |
||
64 | w = pmap->width() + 6; |
||
65 | } |
||
66 | else |
||
67 | { |
||
68 | h = qMax( pmap->height(), metrics.lineSpacing() + 2 ); |
||
69 | w = pmap->width() + metrics.width(text(data)) + 6; |
||
70 | } |
||
20427 | jghali | 71 | return QSize(qMax(w, QApplication::globalStrut().width()), qMax( h, QApplication::globalStrut().height())); |
9874 | avox | 72 | }; |
73 | |||
74 | |||
75 | template<unsigned int pixWidth, unsigned int pixHeight> |
||
76 | void ScListBoxPixmap<pixWidth, pixHeight>::paint(QPainter * qpainter, const QStyleOptionViewItem & option, const QModelIndex & index) const |
||
77 | { |
||
6490 | jghali | 78 | int yPos; |
9874 | avox | 79 | |
80 | const QVariant data(index.data(Qt::UserRole)); |
||
81 | redraw(data); |
||
82 | |||
83 | int itemHeight = sizeHint( option, index ).height(); |
||
84 | |||
85 | if (option.state & QStyle::State_Selected) |
||
86 | qpainter->fillRect(option.rect, option.palette.highlight()); |
||
87 | |||
20427 | jghali | 88 | qpainter->save(); |
9874 | avox | 89 | qpainter->setRenderHint(QPainter::Antialiasing, true); |
90 | qpainter->setPen(Qt::NoPen); |
||
91 | |||
6490 | jghali | 92 | if ( !pmap->isNull() ) { |
93 | yPos = ( itemHeight - pmap->height() ) / 2; |
||
9874 | avox | 94 | qpainter->drawPixmap( option.rect.x() + 3, option.rect.y() + yPos, *pmap); |
20427 | jghali | 95 | } |
9874 | avox | 96 | if (option.state & QStyle::State_Selected) |
97 | qpainter->setBrush(option.palette.highlightedText()); |
||
98 | else |
||
99 | qpainter->setBrush(QBrush(Qt::black)); |
||
100 | qpainter->setPen(Qt::black); |
||
101 | |||
102 | QString txt = index.data(Qt::DisplayRole).toString(); |
||
103 | |||
20427 | jghali | 104 | if ( !txt.isEmpty() ) { |
6490 | jghali | 105 | QFontMetrics fm = qpainter->fontMetrics(); |
106 | yPos = ( ( itemHeight - fm.height() ) / 2 ) + fm.ascent(); |
||
9874 | avox | 107 | qpainter->drawText( option.rect.x() + pmap->width() + 5, option.rect.y() + yPos, txt ); |
20427 | jghali | 108 | } |
109 | qpainter->restore(); |
||
6490 | jghali | 110 | }; |
111 | |||
112 | #endif |