summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_iconlistitem.cpp
blob: 7bf06b348fd1575167cd4a89a26fbdf07ea9089f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/***************************************************************************
                          sq_iconlistitem.cpp  -  description
                             -------------------
    begin                : ??? ??? 19 2004
    copyright            : (C) 2004 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tqlistbox.h>
#include <tqpainter.h>
#include <tqbitmap.h>
#include <tqpixmap.h>

#include "sq_iconlistitem.h"

SQ_IconListItem::SQ_IconListItem(TQListBox *listbox, const TQPixmap &pixmap, const TQString &text)
    : TQListBoxItem(listbox)
{
    mPixmap = pixmap;

    if(mPixmap.isNull())
    mPixmap = defaultPixmap();

    setText(text);
    mMinimumWidth = 0;
}

int SQ_IconListItem::expandMinimumWidth( int width )
{
    mMinimumWidth = TQMAX(mMinimumWidth, width);

    return mMinimumWidth;
}

const TQPixmap& SQ_IconListItem::defaultPixmap()
{
    static TQPixmap *pix=0;

    if(pix == 0)
    {
        pix = new TQPixmap( 32, 32 );
        TQPainter p(pix);
        p.eraseRect(0, 0, pix->width(), pix->height());
        p.setPen(TQt::red);
        p.drawRect(0, 0, pix->width(), pix->height());
        p.end();

        TQBitmap mask(pix->width(), pix->height(), true);
        mask.fill(TQt::black);
        p.begin(&mask);
        p.setPen(TQt::white);
        p.drawRect(0, 0, pix->width(), pix->height());
        p.end();

        pix->setMask(mask);
    }

    return *pix;
}

void SQ_IconListItem::paint(TQPainter *painter)
{
    TQFontMetrics fm = painter->fontMetrics();
    int ht = fm.boundingRect(0, 0, 0, 0, TQt::AlignCenter, text()).height();
    int wp = mPixmap.width();
    int hp = mPixmap.height();

    painter->drawPixmap((mMinimumWidth-wp)/2, 5, mPixmap);

    if(text().isEmpty() == false)
        painter->drawText(0, hp+7, mMinimumWidth, ht, TQt::AlignCenter, text());
}

int SQ_IconListItem::height( const TQListBox *lb ) const
{
    if(text().isEmpty() == true)
        return mPixmap.height();
    else
    {
        int ht = lb->fontMetrics().boundingRect(0, 0, 0, 0, TQt::AlignCenter, text()).height();
        return (mPixmap.height() + ht + 10);
    }
}

int SQ_IconListItem::width( const TQListBox *lb ) const
{
    int wt = lb->fontMetrics().boundingRect(0, 0, 0, 0, TQt::AlignCenter, text()).width() + 10;
    int wp = mPixmap.width() + 10;
    int w  = TQMAX(wt, wp);

    return TQMAX(w, mMinimumWidth);
}