Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

sclistboxpixmap.h

Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #ifndef SCLISTBOXPIXMAP_H
00008 #define SCLISTBOXPIXMAP_H
00009 
00010 #include <memory>
00011 
00012 #include <QApplication>
00013 #include <QDebug>
00014 #include <QPainter>
00015 #include <QPixmap>
00016 #include <QVariant>
00017 
00018 #include "scguardedptr.h"
00019 #include "scribusapi.h"
00020 
00021 class QVariant;
00022 
00023 class ScListBoxDelegate
00024 {
00025 public:
00026     virtual QString text(const QVariant&) const = 0;
00027     virtual ~ScListBoxDelegate() {};
00028 };
00029 
00030 template<unsigned int pixWidth, unsigned int pixHeight>
00031 class ScListBoxPixmap : public QAbstractItemDelegate, public ScListBoxDelegate
00032 {
00033 public:
00034     ScListBoxPixmap(void);
00035     virtual QSize sizeHint (const QStyleOptionViewItem & option, const QModelIndex & index ) const;
00036     virtual void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
00037 protected:
00038     static std::auto_ptr<QPixmap> pmap;
00039     // The drawPixmap function must not modify pixmap size
00040     virtual void redraw(const QVariant&) const = 0;
00041 };
00042 
00043 template<unsigned int pixWidth, unsigned int pixHeight> 
00044 std::auto_ptr<QPixmap> ScListBoxPixmap<pixWidth, pixHeight>::pmap;
00045 
00046 
00047 template<unsigned int pixWidth, unsigned int pixHeight>
00048 ScListBoxPixmap<pixWidth, pixHeight>::ScListBoxPixmap(void) : QAbstractItemDelegate()
00049 {
00050     if (!pmap.get())
00051     {
00052         pmap.reset( new QPixmap(pixWidth, pixHeight) );
00053     }
00054 };
00055 
00056 
00057 template<unsigned int pixWidth, unsigned int pixHeight>
00058 QSize ScListBoxPixmap<pixWidth, pixHeight>::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
00059 {
00060     int h,w;
00061     QFontMetrics metrics(option.font);
00062     const QVariant data(index.data(Qt::DisplayRole));
00063     if ( text(data).isEmpty() )
00064     {
00065         h = pmap->height();
00066         w = pmap->width() + 6;
00067     }
00068     else
00069     {
00070         h = qMax( pmap->height(), metrics.lineSpacing() + 2 );
00071         w = pmap->width() + metrics.width(text(data)) + 6;
00072     }
00073     return QSize(qMax(w, QApplication::globalStrut().width()), qMax( h, QApplication::globalStrut().height()));
00074 };
00075 
00076 
00077 template<unsigned int pixWidth, unsigned int pixHeight>
00078 void ScListBoxPixmap<pixWidth, pixHeight>::paint(QPainter * qpainter, const QStyleOptionViewItem & option, const QModelIndex & index) const
00079 {
00080     int yPos;
00081     
00082     const QVariant data(index.data(Qt::UserRole));
00083     redraw(data);
00084     
00085     int itemHeight = sizeHint( option, index ).height();
00086 
00087     if (option.state & QStyle::State_Selected)
00088         qpainter->fillRect(option.rect, option.palette.highlight());
00089         
00090     qpainter->save();
00091     qpainter->setRenderHint(QPainter::Antialiasing, true);
00092     qpainter->setPen(Qt::NoPen);
00093 
00094     if ( !pmap->isNull() ) {
00095         yPos = ( itemHeight - pmap->height() ) / 2;
00096         qpainter->drawPixmap( option.rect.x() + 3, option.rect.y() + yPos, *pmap);
00097     }
00098     if (option.state & QStyle::State_Selected)
00099         qpainter->setBrush(option.palette.highlightedText());
00100     else
00101         qpainter->setBrush(QBrush(Qt::black));
00102     qpainter->setPen(Qt::black);
00103     
00104     QString txt = index.data(Qt::DisplayRole).toString();
00105 
00106     if ( !txt.isEmpty() ) {
00107         QFontMetrics fm = qpainter->fontMetrics();
00108         yPos = ( ( itemHeight - fm.height() ) / 2 ) + fm.ascent();
00109         qpainter->drawText( option.rect.x() + pmap->width() + 5, option.rect.y() + yPos, txt );
00110     }
00111     qpainter->restore();
00112 };
00113 
00114 #endif
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.