Back to index

scribus-ng  1.3.4.dfsg+svn20071115
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 "scribusapi.h"
00011 #include <memory>
00012 #include <qapplication.h>
00013 #include <qlistbox.h>
00014 #include <qpainter.h>
00015 #include <qpixmap.h>
00016 using namespace std;
00017 
00018 template<unsigned int pixWidth, unsigned int pixHeight>
00019 class ScListBoxPixmap : public QListBoxItem
00020 {
00021 public:
00022        ScListBoxPixmap(void);
00023        virtual int   width(const QListBox *)  const;
00024        virtual int   height(const QListBox *) const;
00025        virtual void paint(QPainter *);
00026 protected:
00027        static  auto_ptr<QPixmap> pmap;
00028        // The drawPixmap function must not modify pixmap size
00029        virtual void redraw(void) = 0;
00030 };
00031 
00032 template<unsigned int pixWidth, unsigned int pixHeight> 
00033 auto_ptr<QPixmap> ScListBoxPixmap<pixWidth, pixHeight>::pmap;
00034 
00035 template<unsigned int pixWidth, unsigned int pixHeight>
00036 ScListBoxPixmap<pixWidth, pixHeight>::ScListBoxPixmap(void) : QListBoxItem()
00037 {
00038        if (!pmap.get())
00039        {
00040               pmap.reset( new QPixmap(pixWidth, pixHeight) );
00041        }
00042 };
00043 
00044 template<unsigned int pixWidth, unsigned int pixHeight>
00045 int    ScListBoxPixmap<pixWidth, pixHeight>::width(const QListBox* lb)  const
00046 {
00047        if ( text().isEmpty() )
00048               return QMAX( pmap->width() + 6, QApplication::globalStrut().width() );
00049     return QMAX( pmap->width() + lb->fontMetrics().width(text()) + 6,QApplication::globalStrut().width() );
00050 };
00051 
00052 template<unsigned int pixWidth, unsigned int pixHeight>
00053 int    ScListBoxPixmap<pixWidth, pixHeight>::height(const QListBox* lb) const
00054 {
00055        int h;
00056        if ( text().isEmpty() )
00057               h = pmap->height();
00058     else
00059               h = QMAX( pmap->height(), lb->fontMetrics().lineSpacing() + 2 );
00060     return QMAX( h, QApplication::globalStrut().height() );
00061 };
00062 
00063 template<unsigned int pixWidth, unsigned int pixHeight>
00064 void ScListBoxPixmap<pixWidth, pixHeight>::paint(QPainter * qpainter)
00065 {
00066        int yPos;
00067        redraw();
00068        int itemHeight = height( listBox() );
00069        if ( !pmap->isNull() ) {
00070               yPos = ( itemHeight - pmap->height() ) / 2;
00071               qpainter->drawPixmap( 3, yPos, *pmap);
00072     }
00073     if ( !text().isEmpty() ) {
00074               QFontMetrics fm = qpainter->fontMetrics();
00075               yPos = ( ( itemHeight - fm.height() ) / 2 ) + fm.ascent();
00076               qpainter->drawText( pmap->width() + 5, yPos, text() );
00077     }
00078 };
00079 
00080 #endif