ad5dce3d69f74922a0df8db8a5efe6a9b73ab4c0
[situare] / src / ui / friendlistitemdelegate.cpp
1 #include <QDebug>
2 #include <QPainter>
3
4 #include "friendlistitemdelegate.h"
5 #include "../common.h"
6
7 const int BACKGROUND_BOTTOM_HEIGHT = 15;
8 const int BACKGROUND_TOP_HEIGHT = 20;
9 const int BACKGROUND_WIDTH = 368;
10 const int ICON_HEIGHT = 24;     ///< Icon height
11 const int ICON_WIDTH = 24;       ///< Icon width
12 const int IMAGE_HEIGHT = 64; ///< Friend image height
13 const int IMAGE_WIDTH = 64;  ///< Friend image width
14 const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
15 const int MARGIN = 5;   ///< Icon margin
16 const int MOUSE_PRESS_AREA_WIDTH = 20;  ///< Area width for item height toggling
17 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
18
19 /**
20 * @var NAME_LABEL_MAX_WIDTH
21 *
22 * @brief Name label's maximum width
23 */
24 const int NAME_LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3*MARGIN - IMAGE_WIDTH;
25
26 /**
27 * @var LABEL_MAX_WIDTH
28 *
29 * @brief All label's maximum width
30 */
31 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
32
33 FriendListItemDelegate::FriendListItemDelegate()
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36
37     m_clockImage = QPixmap(":/res/images/clock.png");
38     m_envelopeImage = QPixmap(":/res/images/envelope.png");
39     m_compassImage = QPixmap(":/res/images/compass.png");
40 }
41
42 void FriendListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
43                                    const QModelIndex &index) const
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46
47     ListItemDelegate::paint(painter, option, index);
48
49     painter->setPen(COLOR_GRAY);
50     painter->setFont(NOKIA_FONT_SMALL);
51
52     QString statusText = index.data(Qt::UserRole).toString();
53     QString address = index.data(Qt::UserRole + 1).toString();
54     QString timestamp = index.data(Qt::UserRole + 2).toString();
55     QString distance = index.data(Qt::UserRole + 3).toString();
56     QPixmap distanceIcon = QPixmap(qvariant_cast<QPixmap>(index.data(Qt::UserRole + 4)));
57
58     QRect itemRect = option.rect;
59
60     QPoint distanceIconPoint = QPoint(BACKGROUND_WIDTH - ICON_WIDTH - 15, itemRect.top() + 5);
61     QRect distanceRect = index.data(Qt::SizeHintRole + 4).toRect();
62     distanceRect.translate(BACKGROUND_WIDTH - distanceRect.width(), itemRect.top() + ICON_HEIGHT);
63     qDebug() << __PRETTY_FUNCTION__ << distanceRect << distance;
64     painter->drawPixmap(distanceIconPoint, distanceIcon);
65     painter->drawText(distanceRect, Qt::TextWordWrap, distance);
66
67     QRect envelopeRect = QRect(itemRect.left() + IMAGE_WIDTH + 15, itemRect.top() + IMAGE_HEIGHT - 10,
68                                ICON_WIDTH, ICON_HEIGHT);
69     QRect statusTextRect = index.data(Qt::SizeHintRole + 1).toRect();
70     statusTextRect.translate(envelopeRect.right() + 5, envelopeRect.top());
71
72     painter->drawPixmap(envelopeRect, m_envelopeImage);
73     painter->drawText(statusTextRect, Qt::TextWordWrap, statusText);
74
75
76     QRect compassRect = QRect(envelopeRect.left(), statusTextRect.bottom() + 2, ICON_WIDTH,
77                               ICON_HEIGHT);
78     QRect locationRect = index.data(Qt::SizeHintRole + 2).toRect();
79     locationRect.translate(compassRect.right() + 5, compassRect.top());
80
81     painter->drawPixmap(compassRect, m_compassImage);
82     painter->drawText(locationRect, Qt::TextWordWrap, address);
83
84
85     QRect clockRect = QRect(envelopeRect.left(), locationRect.bottom() + 2, ICON_WIDTH, ICON_HEIGHT);
86     QRect timestampRect = index.data(Qt::SizeHintRole + 3).toRect();
87     timestampRect.translate(clockRect.right() + 5, clockRect.top());
88
89     painter->drawPixmap(clockRect, m_clockImage);
90     painter->drawText(timestampRect, Qt::TextWordWrap, timestamp);
91 }
92
93 QSize FriendListItemDelegate::sizeHint(const QStyleOptionViewItem &option,
94                                       const QModelIndex &index) const
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     return ListItemDelegate::sizeHint(option, index);
99 }