Modified FriendGroupItem paint event. Changed friend_group.png image.
[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_TOP_HEIGHT = 20;
40 const int BACKGROUND_BOTTOM_HEIGHT = 15;
41 const int ICON_HEIGHT = 24;     ///< Icon height
42 const int ICON_WIDTH = 24;       ///< Icon width
43 const int IMAGE_HEIGHT = 60; ///< Friend image height
44 const int IMAGE_WIDTH = 60;  ///< Friend image width
45 const int ITEM_MAX_HEIGHT = 240; ///< Maximum height for item
46 const int ITEM_MAX_WIDTH = 368;  ///< Maximum width for item
47 const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
48 const int ITEM_MIN_WIDTH = 368;  ///< Minimum width for item
49 const int MARGIN = 5;   ///< Icon margin
50 const int MOUSE_PRESS_AREA_WIDTH = 20;  ///< Area width for item height toggling
51 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
52 /**
53 * @var NAME_LABEL_MAX_WIDTH
54 *
55 * @brief Name label's maximum width
56 */
57 const int NAME_LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH;
58 /**
59 * @var LABEL_MAX_WIDTH
60 *
61 * @brief All label's maximum width
62 */
63 const int LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
64
65 FriendListItem::FriendListItem(QWidget *parent)
66     : QWidget(parent)
67     , m_expanded(false)
68     , m_user(0)
69 {
70     qDebug() << __PRETTY_FUNCTION__;
71
72     QVBoxLayout *layout = new QVBoxLayout(this);
73     layout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
74     layout->setSpacing(0);
75     setLayout(layout);
76
77     QHBoxLayout *topLayout = new QHBoxLayout();
78     topLayout->setMargin(0);
79     topLayout->setSpacing(0);
80
81     QHBoxLayout *bottomLayout = new QHBoxLayout();
82     bottomLayout->setMargin(0);
83     bottomLayout->setSpacing(0);
84
85     QFormLayout *infoLayout = new QFormLayout();
86     infoLayout->setMargin(0);
87     infoLayout->setSpacing(0);
88     infoLayout->setLabelAlignment(Qt::AlignTop);
89
90     QLabel *clockLabel = new QLabel();
91     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
92     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
93     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
94     QLabel *envelopeLabel = new QLabel();
95     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
96     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
97     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
98     QLabel *compassLabel = new QLabel();
99     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
100     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
101     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
102
103     m_imageLabel = new QLabel();
104     m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
105
106     m_nameLabel = new QLabel();
107     m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
108
109     m_distanceLabel = new QLabel();
110     m_distanceLabel->setFixedHeight(IMAGE_HEIGHT);
111
112     m_findButton = new ImageButton(this, ":/res/images/show_position.png",
113                                    ":/res/images/show_position_s.png");
114
115     m_updatedLabel = new QLabel();
116     m_updatedLabel->setWordWrap(true);
117     m_statusTextLabel = new QLabel();
118     m_statusTextLabel->setWordWrap(true);
119     m_locationLabel = new QLabel();
120     m_locationLabel->setWordWrap(true);
121
122     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
123     infoLayout->addRow(compassLabel, m_locationLabel);
124     infoLayout->addRow(clockLabel, m_updatedLabel);
125
126     topLayout->addWidget(m_imageLabel);
127     topLayout->addWidget(m_nameLabel, 1);
128     topLayout->addWidget(m_distanceLabel);
129
130     bottomLayout->addWidget(m_findButton, 0, Qt::AlignTop);
131     bottomLayout->addLayout(infoLayout);
132
133     layout->addLayout(topLayout, 0);
134     layout->addLayout(bottomLayout, 1);
135
136     setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
137     setMaximumSize(ITEM_MIN_WIDTH, ITEM_MAX_HEIGHT);
138
139     setFont(NOKIA_FONT_SMALL);
140     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
141     QPalette itemPalette = palette();
142     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
143     setPalette(itemPalette);
144     QPalette namePalette = m_nameLabel->palette();
145     namePalette.setColor(QPalette::Foreground, Qt::white);
146     m_nameLabel->setPalette(namePalette);
147
148     m_backgroundTopImage.load(":/res/images/list_item_top.png");
149     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
150     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
151
152     connect(m_findButton, SIGNAL(clicked()),
153         this, SLOT(findButtonClicked()));
154 }
155
156 void FriendListItem::setData(User *user)
157 {
158     qDebug() << __PRETTY_FUNCTION__;
159
160     m_user = user;
161
162     m_imageLabel->setPixmap(m_user->profileImage());
163
164     QString unit;
165     double value;
166     user->distance(value, unit);
167     m_distanceLabel->setText(QString::number(value) + " " + unit);
168
169     shortenTexts();
170     setText(false);
171 }
172
173 void FriendListItem::shortenTexts()
174 {
175     qDebug() << __PRETTY_FUNCTION__;
176
177     QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
178     QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
179
180     QString name = m_user->name();
181     QString updated = m_user->timestamp();
182     QString statusText = m_user->note();
183     QString location = m_user->address();
184
185     int nameIndex = name.indexOf('\n');
186     int updatedIndex = updated.indexOf('\n');
187     int statusTextIndex = statusText.indexOf('\n');
188     int locationIndex = location.indexOf('\n');
189
190     if (nameIndex > 0) {
191         name.truncate(nameIndex);
192         name.append("...");
193     }
194     if (updatedIndex > 0) {
195         updated.truncate(updatedIndex);
196         updated.append("...");
197     }
198     if (statusTextIndex > 0) {
199         statusText.truncate(statusTextIndex);
200         statusText.append("...");
201     }
202     if (locationIndex > 0) {
203         location.truncate(locationIndex);
204         location.append("...");
205     }
206
207     int distanceLabelWidth = otherLabelsMetrics.width(m_distanceLabel->text());
208     m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
209                                                   - distanceLabelWidth);
210     m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
211     m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
212                                                           LABEL_MAX_WIDTH);
213     m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
214 }
215
216 void FriendListItem::setText(bool expanded)
217 {
218     qDebug() << __PRETTY_FUNCTION__;
219
220     if (expanded) {
221         setUpdatesEnabled(false);
222         m_nameLabel->setText(m_shortenedName);
223         m_updatedLabel->setText(m_user->timestamp());
224         m_statusTextLabel->setText(m_user->note());
225         m_locationLabel->setText(m_user->address());
226         setUpdatesEnabled(true);
227     }
228     else {
229         setUpdatesEnabled(false);
230         m_nameLabel->setText(m_shortenedName);
231         m_updatedLabel->setText(m_shortenedUpdated);
232         m_statusTextLabel->setText(m_shortenedStatusText);
233         m_locationLabel->setText(m_shortenedLocation);
234         setUpdatesEnabled(true);
235     }
236 }
237
238 void FriendListItem::mousePressEvent(QMouseEvent *event)
239 {
240     qDebug() << __PRETTY_FUNCTION__;
241
242     m_mousePosition = event->pos();
243 }
244
245 void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
246 {
247     qDebug() << __PRETTY_FUNCTION__;
248
249     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
250         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
251         if (m_expanded) {
252             setText(false);
253             m_expanded = false;
254         }
255         else {
256             setText(true);
257             m_expanded = true;
258         }
259     }
260 }
261
262 void FriendListItem::paintEvent(QPaintEvent *event)
263 {
264     qDebug() << __PRETTY_FUNCTION__;
265
266     Q_UNUSED(event);
267
268     QPainter painter(this);
269
270     QRect topRect = QRect(0, 0, ITEM_MIN_WIDTH, BACKGROUND_TOP_HEIGHT);
271     QRect middleRect = QRect(0, topRect.bottom(), ITEM_MIN_WIDTH,
272                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
273     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_MIN_WIDTH,
274                              BACKGROUND_BOTTOM_HEIGHT);
275
276     painter.drawPixmap(topRect, m_backgroundTopImage);
277     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
278     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
279 }
280
281 void FriendListItem::findButtonClicked()
282 {
283     qDebug() << __PRETTY_FUNCTION__;
284
285     emit findFriend(m_user->coordinates());
286 }