2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jussi Laitinen - jussi.laitinen@ixonos.com
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.
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.
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,
22 #include <QVBoxLayout>
23 #include <QPushButton>
26 #include <QPaintEvent>
29 #include <QFormLayout>
30 #include <QSpacerItem>
31 #include <QStylePainter>
34 #include "friendlistitem.h"
35 #include "../user/user.h"
36 #include "imagebutton.h"
37 #include "../common.h"
39 const int ITEM_WIDTH = 368;
40 const int ICON_HEIGHT = 24; ///< Icon height
41 const int ICON_WIDTH = 24; ///< Icon width
42 const int IMAGE_HEIGHT = 64; ///< Friend image height
43 const int IMAGE_WIDTH = 64; ///< Friend image width
44 const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
45 const int MARGIN = 5; ///< Icon margin
46 const int MOUSE_PRESS_AREA_WIDTH = 20; ///< Area width for item height toggling
47 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
50 * @var NAME_LABEL_MAX_WIDTH
52 * @brief Name label's maximum width
54 const int NAME_LABEL_MAX_WIDTH = ITEM_WIDTH - 3*MARGIN - IMAGE_WIDTH;
57 * @var LABEL_MAX_WIDTH
59 * @brief All label's maximum width
61 const int LABEL_MAX_WIDTH = ITEM_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH - MARGIN;
63 const int WALK_DISTANCE = 5; ///< Walk distance limit for distance icon
64 const int CAR_DISTANCE = 500; ///< Car distance limit for distance icon
65 const int AEROPLANE_DISTANCE = 5000;///< Aeroplane distance limit for distance icon
67 FriendListItem::FriendListItem()
71 qDebug() << __PRETTY_FUNCTION__;
73 setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
75 // m_distanceTextLabel = new QLabel();
76 // m_distanceTextLabel->setFixedHeight(ICON_HEIGHT);
78 // m_distanceImageLabel = new QLabel();
79 // m_distanceImageLabel->setFixedSize(ICON_WIDTH, ICON_HEIGHT);
81 // m_findButton = new ImageButton(this);
83 // m_updatedLabel = new QLabel();
84 // m_updatedLabel->setWordWrap(true);
85 // m_statusTextLabel = new QLabel();
86 // m_statusTextLabel->setWordWrap(true);
87 // m_locationLabel = new QLabel();
88 // m_locationLabel->setWordWrap(true);
90 // distanceLayout->addWidget(m_distanceImageLabel, 0, Qt::AlignRight);
91 // distanceLayout->addWidget(m_distanceTextLabel, 0, Qt::AlignRight);
93 // infoLayout->addRow(envelopeLabel, m_statusTextLabel);
94 // infoLayout->addRow(compassLabel, m_locationLabel);
95 // infoLayout->addRow(clockLabel, m_updatedLabel);
97 // topLayout->addWidget(m_findButton);
98 // topLayout->addWidget(m_nameLabel, 1);
99 // topLayout->addLayout(distanceLayout);
101 // bottomLayout->addSpacing(IMAGE_WIDTH);
102 // bottomLayout->addLayout(infoLayout);
104 // layout->addLayout(topLayout, 0);
105 // layout->addLayout(bottomLayout, 1);
107 // setMinimumSize(ITEM_WIDTH, ITEM_MIN_HEIGHT);
108 // setMaximumWidth(ITEM_WIDTH);
110 // setFont(NOKIA_FONT_SMALL);
111 // m_nameLabel->setFont(NOKIA_FONT_NORMAL);
112 // QPalette itemPalette = palette();
113 // itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
114 // setPalette(itemPalette);
115 // QPalette namePalette = m_nameLabel->palette();
116 // namePalette.setColor(QPalette::Foreground, Qt::white);
117 // m_nameLabel->setPalette(namePalette);
119 // m_backgroundTopImage.load(":/res/images/list_item_top.png");
120 // m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
121 // m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
123 // connect(m_findButton, SIGNAL(clicked()),
124 // this, SLOT(findButtonClicked()));
127 void FriendListItem::calculateTextRects()
129 QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
131 QRect statusTextRect = otherLabelsMetrics.boundingRect(QRect(0, 0, LABEL_MAX_WIDTH,
132 1000), Qt::AlignLeft,
134 QRect updatedRect = otherLabelsMetrics.boundingRect(QRect(0, 0, LABEL_MAX_WIDTH,
135 1000), Qt::AlignLeft,
136 m_user->timestamp());
137 QRect locationRect = otherLabelsMetrics.boundingRect(QRect(0, 0, LABEL_MAX_WIDTH,
138 1000), Qt::AlignLeft,
142 qDebug() << m_shortenedStatusText;
144 int statusTextFactor = statusTextRect.width() / LABEL_MAX_WIDTH;
146 m_statusTextRect = QRect(0, 0, LABEL_MAX_WIDTH, 24 * statusTextFactor);
147 int updatedFactor = updatedRect.width() / LABEL_MAX_WIDTH;
149 m_updatedRect = QRect(0, 0, LABEL_MAX_WIDTH, 24 * updatedFactor);
150 int locationFactor = locationRect.width() / LABEL_MAX_WIDTH;
152 m_locationRect = QRect(0, 0, LABEL_MAX_WIDTH, 24 * locationFactor);
154 qDebug() << statusTextRect;
155 qDebug() << updatedRect;
156 qDebug() << locationRect;
158 qDebug() << m_statusTextRect;
159 qDebug() << m_updatedRect;
160 qDebug() << m_locationRect;
162 m_expandedHeight = ITEM_MIN_HEIGHT + ((statusTextFactor + updatedFactor + locationFactor - 3)*ICON_HEIGHT);
165 void FriendListItem::setId(const QString &id)
167 qDebug() << __PRETTY_FUNCTION__;
170 QString FriendListItem::id() const
172 return m_user->userId();
175 void FriendListItem::setUserData(User *user)
177 qDebug() << __PRETTY_FUNCTION__;
182 if (!m_user->profileImage().isNull())
183 setData(Qt::DecorationRole, m_user->profileImage());
187 m_user->distance(value, unit);
188 m_distanceText = QString::number(value) + " " + unit;
189 setData(Qt::UserRole + 3, m_distanceText);
190 setDistanceIcon(value, unit);
193 calculateTextRects();
198 void FriendListItem::setDistanceIcon(double value, const QString &unit)
200 QPixmap distanceImage;
202 if ((unit == "m") || (value < WALK_DISTANCE))
203 distanceImage.load(":/res/images/walk_icon_gray.png");
204 else if (value < CAR_DISTANCE)
205 distanceImage.load(":/res/images/car_icon_gray.png");
206 else if (value < AEROPLANE_DISTANCE)
207 distanceImage.load(":/res/images/aeroplane_icon_gray.png");
209 distanceImage.load(":/res/images/rocket_icon_gray.png");
211 qDebug() << __PRETTY_FUNCTION__ << distanceImage.isNull();
213 setData(Qt::UserRole + 4, distanceImage);
216 void FriendListItem::shortenTexts()
218 qDebug() << __PRETTY_FUNCTION__;
220 QFontMetrics nameLabelMetrics = QFontMetrics(NOKIA_FONT_NORMAL);
221 QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
223 QString name = m_user->name();
224 QString updated = m_user->timestamp();
225 QString statusText = m_user->note();
226 QString location = m_user->address();
228 int nameIndex = name.indexOf('\n');
229 int updatedIndex = updated.indexOf('\n');
230 int statusTextIndex = statusText.indexOf('\n');
231 int locationIndex = location.indexOf('\n');
234 name.truncate(nameIndex);
237 if (updatedIndex > 0) {
238 updated.truncate(updatedIndex);
239 updated.append("...");
241 if (statusTextIndex > 0) {
242 statusText.truncate(statusTextIndex);
243 statusText.append("...");
245 if (locationIndex > 0) {
246 location.truncate(locationIndex);
247 location.append("...");
250 int distanceLabelWidth = otherLabelsMetrics.width(m_distanceText) + 24;
251 m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
252 - distanceLabelWidth);
253 m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
254 LABEL_MAX_WIDTH - 70);
255 m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH - 70);
256 m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH - 70);
258 setData(Qt::SizeHintRole + 4, QRect(0, 0, distanceLabelWidth, 24));
261 QString FriendListItem::elideText(int width, const QString &text)
263 QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
267 for (int i = 0; i < text.size(); ++i) {
268 QChar c = text.at(i);
270 widthSum += otherLabelsMetrics.width(c);
272 if (widthSum > width) {
282 void FriendListItem::setText(bool expanded)
284 qDebug() << __PRETTY_FUNCTION__;
286 setData(Qt::DisplayRole, m_shortenedName);
289 setData(Qt::UserRole, m_user->note());
290 setData(Qt::UserRole + 1, m_user->timestamp());
291 setData(Qt::UserRole + 2, m_user->address());
293 setData(Qt::SizeHintRole + 1, m_statusTextRect);
294 setData(Qt::SizeHintRole + 2, m_updatedRect);
295 setData(Qt::SizeHintRole + 3, m_locationRect);
298 setData(Qt::UserRole, m_shortenedStatusText);
299 setData(Qt::UserRole + 1, m_shortenedUpdated);
300 setData(Qt::UserRole + 2, m_shortenedLocation);
302 setData(Qt::SizeHintRole + 1, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
303 setData(Qt::SizeHintRole + 2, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
304 setData(Qt::SizeHintRole + 3, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
308 void FriendListItem::toggleHeight()
313 setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
318 setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, m_expandedHeight));
322 void FriendListItem::setExpanded(bool expanded)
324 m_expanded = expanded;
328 setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, m_expandedHeight));
332 setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));