Added AppTableViewDelegate class for painting tableview (painting not yet done, just...
[meego-garage:garage-netbook-ui.git] / appiconviewdelegate.cpp
1 #include "appiconviewdelegate.h"
2 #include "application.h"
3 #include "model.h"
4 #include "apptablemodel.h"
5 #include "imagemanager.h"
6
7 using namespace MeeGoGarage;
8
9 Q_DECLARE_METATYPE(QWeakPointer<Application>);
10
11 AppIconViewDelegate::AppIconViewDelegate(QObject *parent)
12     : QStyledItemDelegate(parent)
13 {
14 }
15
16 void AppIconViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
17                                 const QModelIndex &index) const
18 {
19     //QStyledItemDelegate::paint(painter, option, index);
20
21     QVariant val = index.data(Qt::UserRole);
22     if (val.isValid() && val.canConvert< QWeakPointer<Application> >()) {
23         QWeakPointer<Application> application = val.value< QWeakPointer<Application> >();
24
25         painter->save();
26        // painter->setPen(Qt::NoPen);
27
28         if (option.state & QStyle::State_Selected) {
29             painter->fillRect(option.rect, option.palette.highlight());
30             painter->drawRect(option.rect);
31         } else {
32             painter->fillRect(option.rect, option.palette.window());
33         }
34
35         painter->setRenderHint(QPainter::Antialiasing, true);
36         const Image * image = application.data()->getIconImage(Application::LargeIcon);
37         QPixmap p = ImageManager::getPixmap(image, painter->device());
38         painter->drawPixmap(option.rect.x(), option.rect.y(), p);
39
40         painter->setFont(QFont ("Trebuchet MS", 13, 500));
41         QFontMetrics fm = painter->fontMetrics();
42         QRect textRect (option.rect);
43         textRect.adjust(0,fm.height(),0,0);
44         QTextOption to (Qt::AlignHCenter | Qt::AlignBottom);
45         painter->drawText(textRect, application.data()->getName(), to);
46
47         if (option.state & QStyle::State_Selected) {
48             painter->drawRect(option.rect);
49         }
50          painter->restore();
51     }
52 }
53
54 QSize AppIconViewDelegate::sizeHint(const QStyleOptionViewItem &option,
55                                     const QModelIndex &index ) const
56 {
57    // return QStyledItemDelegate::sizeHint(option, index);
58     Q_UNUSED (option)
59     Q_UNUSED (index)
60     return QSize(100,140);
61 }