Scribus
Open source desktop publishing at your fingertips
sclistboxpixmap.h
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>
11 
12 #include <QApplication>
13 #include <QDebug>
14 #include <QPainter>
15 #include <QPixmap>
16 #include <QVariant>
17 
18 #include "scguardedptr.h"
19 #include "scribusapi.h"
20 
21 class QVariant;
22 
23 template<unsigned int pixWidth, unsigned int pixHeight>
24 class ScListBoxPixmap : public QAbstractItemDelegate
25 {
26 public:
27  ScListBoxPixmap(void);
28 
29  virtual QString text(const QVariant&) const = 0;
30  virtual QSize sizeHint (const QStyleOptionViewItem & option, const QModelIndex & index ) const;
31  virtual void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
32 protected:
33  static std::auto_ptr<QPixmap> pmap;
34  // The drawPixmap function must not modify pixmap size
35  virtual void redraw(const QVariant&) const = 0;
36 };
37 
38 template<unsigned int pixWidth, unsigned int pixHeight>
39 std::auto_ptr<QPixmap> ScListBoxPixmap<pixWidth, pixHeight>::pmap;
40 
41 
42 template<unsigned int pixWidth, unsigned int pixHeight>
43 ScListBoxPixmap<pixWidth, pixHeight>::ScListBoxPixmap(void) : QAbstractItemDelegate()
44 {
45  if (!pmap.get())
46  {
47  pmap.reset( new QPixmap(pixWidth, pixHeight) );
48  }
49 };
50 
51 
52 template<unsigned int pixWidth, unsigned int pixHeight>
53 QSize ScListBoxPixmap<pixWidth, pixHeight>::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
54 {
55  int h,w;
56  QFontMetrics metrics(option.font);
57  const QVariant data(index.data(Qt::DisplayRole));
58  if ( text(data).isEmpty() )
59  {
60  h = pmap->height();
61  w = pmap->width() + 6;
62  }
63  else
64  {
65  h = qMax( pmap->height(), metrics.lineSpacing() + 2 );
66  w = pmap->width() + metrics.width(text(data)) + 6;
67  }
68  return QSize(qMax(w, QApplication::globalStrut().width()), qMax( h, QApplication::globalStrut().height()));
69 };
70 
71 
72 template<unsigned int pixWidth, unsigned int pixHeight>
73 void ScListBoxPixmap<pixWidth, pixHeight>::paint(QPainter * qpainter, const QStyleOptionViewItem & option, const QModelIndex & index) const
74 {
75  int yPos;
76 
77  const QVariant data(index.data(Qt::UserRole));
78  redraw(data);
79 
80  int itemHeight = sizeHint( option, index ).height();
81 
82  if (option.state & QStyle::State_Selected)
83  qpainter->fillRect(option.rect, option.palette.highlight());
84 
85  qpainter->save();
86  qpainter->setRenderHint(QPainter::Antialiasing, true);
87  qpainter->setPen(Qt::NoPen);
88 
89  if ( !pmap->isNull() ) {
90  yPos = ( itemHeight - pmap->height() ) / 2;
91  qpainter->drawPixmap( option.rect.x() + 3, option.rect.y() + yPos, *pmap);
92  }
93  if (option.state & QStyle::State_Selected)
94  qpainter->setBrush(option.palette.highlightedText());
95  else
96  qpainter->setBrush(QBrush(Qt::black));
97  qpainter->setPen(Qt::black);
98 
99  QString txt = index.data(Qt::DisplayRole).toString();
100 
101  if ( !txt.isEmpty() ) {
102  QFontMetrics fm = qpainter->fontMetrics();
103  yPos = ( ( itemHeight - fm.height() ) / 2 ) + fm.ascent();
104  qpainter->drawText( option.rect.x() + pmap->width() + 5, option.rect.y() + yPos, txt );
105  }
106  qpainter->restore();
107 };
108 
109 #endif
Definition: sclistboxpixmap.h:24