Emit error signal when login fails
[situare] / src / ui / listitemdelegate.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QDebug>
23 #include <QPainter>
24
25 #include "../common.h"
26 #include "avatarimage.h"
27 #include "listcommon.h"
28 #include "listitem.h"
29
30 #include "listitemdelegate.h"
31
32 const int BACKGROUND_BOTTOM_HEIGHT = 15;    ///< Background image bottom height
33 const int BACKGROUND_TOP_HEIGHT = 20;       ///< Background image top height
34 const int NAME_TOP_MARGIN = 40;             ///< Name text top margin
35
36 ListItemDelegate::ListItemDelegate(QWidget *parent) :
37     QStyledItemDelegate(parent)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     m_backgroundTopImage.load(":/res/images/list_item_top.png");
42     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
43     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
44     m_backgroundTopSelectedImage.load(":/res/images/list_item_top_selected.png");
45     m_backgroundMiddleSelectedImage.load(":/res/images/list_item_middle_selected.png");
46     m_backgroundBottomSelectedImage.load(":/res/images/list_item_bottom_selected.png");
47 }
48
49 void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
50                         const QModelIndex &index) const
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     QPixmap image = QPixmap(qvariant_cast<QPixmap>(index.data(AVATAR_IMAGE_INDEX)));
55     QString name = index.data(TITLE_DISPLAY_INDEX).toString();
56     QSize size = index.data(ITEM_SIZE_HINT_INDEX).toSize();
57     bool selected = index.data(ITEM_EXPANDED_INDEX).toBool();
58
59     QRect itemRect = option.rect;
60
61     QRect topRect = QRect(itemRect.left(), itemRect.top(), ITEM_WIDTH,
62                           BACKGROUND_TOP_HEIGHT);
63     QRect middleRect = QRect(itemRect.left(), topRect.bottom(), ITEM_WIDTH,
64                              size.height() - BACKGROUND_TOP_HEIGHT
65                              - BACKGROUND_BOTTOM_HEIGHT);
66     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_WIDTH,
67                              BACKGROUND_BOTTOM_HEIGHT);
68
69     if (selected) {
70         painter->drawPixmap(topRect, m_backgroundTopSelectedImage);
71         painter->drawPixmap(middleRect, m_backgroundMiddleSelectedImage);
72         painter->drawPixmap(bottomRect, m_backgroundBottomSelectedImage);
73     } else {
74         painter->drawPixmap(topRect, m_backgroundTopImage);
75         painter->drawPixmap(middleRect, m_backgroundMiddleImage);
76         painter->drawPixmap(bottomRect, m_backgroundBottomImage);
77     }
78
79     painter->setPen(Qt::white);
80     painter->setFont(NOKIA_FONT_NORMAL);
81
82     //Image is set, draw image and text
83     if (!image.isNull()) {
84         painter->drawPixmap(itemRect.left() + MARGIN * 2, itemRect.top(), image);
85         painter->drawText(itemRect.topLeft() + QPoint(MARGIN * 2 + IMAGE_WIDTH + MARGIN,
86                                                       NAME_TOP_MARGIN), name);
87     }
88     //Draw only text
89     else {
90         painter->drawText(itemRect.topLeft() + QPoint(MARGIN * 3, NAME_TOP_MARGIN), name);
91     }
92 }
93
94 QSize ListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     Q_UNUSED(option);
99
100     return index.data(Qt::SizeHintRole).toSize();
101 }