13b60bc6c0596b944653e8256c43d30cb9e5c447
[situare] / src / ui / extendedlistitemdelegate.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 <QPainter>
23 #include <QDebug>
24 #include <QRect>
25
26 #include "../common.h"
27 #include "listcommon.h"
28 #include "extendedlistitemstore.h"
29
30 #include "extendedlistitemdelegate.h"
31
32 ExtendedListItemDelegate::ExtendedListItemDelegate(QWidget *parent)
33     : ListItemDelegate(parent)
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36 }
37
38 void ExtendedListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
39                                      const QModelIndex &index) const
40 {
41     qDebug() << __PRETTY_FUNCTION__;
42
43     ListItemDelegate::paint(painter, option, index);
44
45     painter->setPen(COLOR_GRAY);
46     painter->setFont(NOKIA_FONT_SMALL);
47
48     QRect itemRect = option.rect;
49     QList<ExtendedListItemStore *> *subItems = (QList<ExtendedListItemStore *> *)
50                                                (index.data(SUBITEM_STORE_INDEX).value<void *>());
51     bool expanded = index.data(ITEM_EXPANDED_INDEX).toBool();
52     bool itemHasImage = index.data(ITEM_HAS_IMAGE_INDEX).toBool();
53
54     if (subItems) {
55
56         int previousSubItemTextRectBottom = itemRect.top() + IMAGE_HEIGHT - 2*MARGIN;
57
58         for (int i = 0; i < subItems->size(); ++i) {
59
60             ExtendedListItemStore *itemStore = subItems->at(i);
61             QRect subItemTextRect = itemStore->textRect();
62             QString text;
63
64             if (expanded) {
65                 text = itemStore->text();
66             }
67             else {
68                 subItemTextRect.setHeight(ICON_HEIGHT);
69                 text = itemStore->shortenedText();
70             }
71
72             QRect iconRect = QRect(itemRect.left() + MARGIN * 3,
73                                        previousSubItemTextRectBottom + ICON_MARGIN,
74                                        ICON_WIDTH, ICON_HEIGHT);
75
76             if (itemHasImage)
77                 iconRect.translate(IMAGE_WIDTH, 0);
78
79             subItemTextRect.translate(iconRect.right() + MARGIN, iconRect.top());
80
81             if (!itemStore->icon().isNull())
82                 painter->drawPixmap(iconRect, itemStore->icon());
83
84             painter->drawText(subItemTextRect, Qt::TextWrapAnywhere, text);
85             previousSubItemTextRectBottom = subItemTextRect.bottom();
86         }
87     }
88 }