Added ListItem, ListItemDelegate, ListView classes.
[situare] / src / ui / friendlistitem.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 <QVBoxLayout>
23 #include <QPushButton>
24 #include <QPainter>
25 #include <QDebug>
26 #include <QPaintEvent>
27 #include <QLabel>
28 #include <QPixmap>
29 #include <QFormLayout>
30 #include <QSpacerItem>
31 #include <QStylePainter>
32 #include <math.h>
33
34 #include "friendlistitem.h"
35 #include "../user/user.h"
36 #include "imagebutton.h"
37 #include "../common.h"
38
39 const int BACKGROUND_BOTTOM_HEIGHT = 15;
40 const int BACKGROUND_TOP_HEIGHT = 20;
41 const int BACKGROUND_WIDTH = 368;
42 const int ICON_HEIGHT = 24;     ///< Icon height
43 const int ICON_WIDTH = 24;       ///< Icon width
44 const int IMAGE_HEIGHT = 64; ///< Friend image height
45 const int IMAGE_WIDTH = 64;  ///< Friend image width
46 const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
47 const int MARGIN = 5;   ///< Icon margin
48 const int MOUSE_PRESS_AREA_WIDTH = 20;  ///< Area width for item height toggling
49 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
50
51 /**
52 * @var NAME_LABEL_MAX_WIDTH
53 *
54 * @brief Name label's maximum width
55 */
56 const int NAME_LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3*MARGIN - IMAGE_WIDTH;
57
58 /**
59 * @var LABEL_MAX_WIDTH
60 *
61 * @brief All label's maximum width
62 */
63 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
64
65 const int WALK_DISTANCE = 5;        ///< Walk distance limit for distance icon
66 const int CAR_DISTANCE = 500;       ///< Car distance limit for distance icon
67 const int AEROPLANE_DISTANCE = 5000;///< Aeroplane distance limit for distance icon
68
69 FriendListItem::FriendListItem(QWidget *parent)
70     : ListItem(parent)
71     , m_expanded(false)
72     , m_user(0)
73 {
74     qDebug() << __PRETTY_FUNCTION__;
75
76 //    m_distanceTextLabel = new QLabel();
77 //    m_distanceTextLabel->setFixedHeight(ICON_HEIGHT);
78
79 //    m_distanceImageLabel = new QLabel();
80 //    m_distanceImageLabel->setFixedSize(ICON_WIDTH, ICON_HEIGHT);
81
82 //    m_findButton = new ImageButton(this);
83
84 //    m_updatedLabel = new QLabel();
85 //    m_updatedLabel->setWordWrap(true);
86 //    m_statusTextLabel = new QLabel();
87 //    m_statusTextLabel->setWordWrap(true);
88 //    m_locationLabel = new QLabel();
89 //    m_locationLabel->setWordWrap(true);
90
91 //    distanceLayout->addWidget(m_distanceImageLabel, 0, Qt::AlignRight);
92 //    distanceLayout->addWidget(m_distanceTextLabel, 0, Qt::AlignRight);
93
94 //    infoLayout->addRow(envelopeLabel, m_statusTextLabel);
95 //    infoLayout->addRow(compassLabel, m_locationLabel);
96 //    infoLayout->addRow(clockLabel, m_updatedLabel);
97
98 //    topLayout->addWidget(m_findButton);
99 //    topLayout->addWidget(m_nameLabel, 1);
100 //    topLayout->addLayout(distanceLayout);
101
102 //    bottomLayout->addSpacing(IMAGE_WIDTH);
103 //    bottomLayout->addLayout(infoLayout);
104
105 //    layout->addLayout(topLayout, 0);
106 //    layout->addLayout(bottomLayout, 1);
107
108 //    setMinimumSize(BACKGROUND_WIDTH, ITEM_MIN_HEIGHT);
109 //    setMaximumWidth(BACKGROUND_WIDTH);
110
111 //    setFont(NOKIA_FONT_SMALL);
112 //    m_nameLabel->setFont(NOKIA_FONT_NORMAL);
113 //    QPalette itemPalette = palette();
114 //    itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
115 //    setPalette(itemPalette);
116 //    QPalette namePalette = m_nameLabel->palette();
117 //    namePalette.setColor(QPalette::Foreground, Qt::white);
118 //    m_nameLabel->setPalette(namePalette);
119
120 //    m_backgroundTopImage.load(":/res/images/list_item_top.png");
121 //    m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
122 //    m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
123
124 //    connect(m_findButton, SIGNAL(clicked()),
125 //        this, SLOT(findButtonClicked()));
126 }
127
128 void FriendListItem::setUserData(User *user)
129 {
130     qDebug() << __PRETTY_FUNCTION__;
131
132     if(user) {
133         m_user = user;
134
135         if (!m_user->profileImage().isNull())
136             setData(Qt::DisplayRole, m_user->profileImage());
137
138 //        QString unit;
139 //        double value;
140 //        m_user->distance(value, unit);
141 //        m_distanceTextLabel->setText(QString::number(value) + " " + unit);
142 //        setDistanceIcon(value, unit);
143
144         shortenTexts();
145         setText(false);
146     }
147 }
148
149 void FriendListItem::setDistanceIcon(double value, const QString &unit)
150 {
151     QPixmap distanceImage;
152
153     if ((unit == "m") || (value < WALK_DISTANCE))
154         distanceImage.load(":/res/images/walk_icon_gray.png");
155     else if (value < CAR_DISTANCE)
156         distanceImage.load(":/res/images/car_icon_gray.png");
157     else if (value < AEROPLANE_DISTANCE)
158         distanceImage.load(":/res/images/aeroplane_icon_gray.png");
159     else
160         distanceImage.load(":/res/images/rocket_icon_gray.png");
161
162     m_distanceImageLabel->setPixmap(distanceImage);
163 }
164
165 void FriendListItem::shortenTexts()
166 {
167     qDebug() << __PRETTY_FUNCTION__;
168
169     QFontMetrics nameLabelMetrics = QFontMetrics(NOKIA_FONT_NORMAL);
170     QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
171
172     QString name = m_user->name();
173     QString updated = m_user->timestamp();
174     QString statusText = m_user->note();
175     QString location = m_user->address();
176
177     int nameIndex = name.indexOf('\n');
178     int updatedIndex = updated.indexOf('\n');
179     int statusTextIndex = statusText.indexOf('\n');
180     int locationIndex = location.indexOf('\n');
181
182     if (nameIndex > 0) {
183         name.truncate(nameIndex);
184         name.append("...");
185     }
186     if (updatedIndex > 0) {
187         updated.truncate(updatedIndex);
188         updated.append("...");
189     }
190     if (statusTextIndex > 0) {
191         statusText.truncate(statusTextIndex);
192         statusText.append("...");
193     }
194     if (locationIndex > 0) {
195         location.truncate(locationIndex);
196         location.append("...");
197     }
198
199     int distanceLabelWidth = otherLabelsMetrics.width(m_distanceTextLabel->text());
200     m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
201                                                   - distanceLabelWidth);
202     m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
203     m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
204                                                           LABEL_MAX_WIDTH);
205     m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
206 }
207
208 void FriendListItem::setText(bool expanded)
209 {
210     qDebug() << __PRETTY_FUNCTION__;
211
212     if (expanded) {
213         //setUpdatesEnabled(false);
214         setData(Qt::UserRole, m_shortenedName);
215         setData(Qt::UserRole + 1, m_user->timestamp());
216         setData(Qt::UserRole + 2, m_user->note());
217         setData(Qt::UserRole + 3, m_user->address());
218         //setUpdatesEnabled(true);
219     }
220     else {
221         //setUpdatesEnabled(false);
222         setData(Qt::UserRole, m_shortenedName);
223         setData(Qt::UserRole + 1, m_shortenedUpdated);
224         setData(Qt::UserRole + 2, m_shortenedStatusText);
225         setData(Qt::UserRole + 3, m_shortenedLocation);
226         //setUpdatesEnabled(true);
227     }
228 }
229
230 void FriendListItem::findButtonClicked()
231 {
232     qDebug() << __PRETTY_FUNCTION__;
233
234     emit findFriend(m_user->coordinates());
235 }