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 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
52 * @var NAME_LABEL_MAX_WIDTH
54 * @brief Name label's maximum width
56 const int NAME_LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3*MARGIN - IMAGE_WIDTH;
59 * @var LABEL_MAX_WIDTH
61 * @brief All label's maximum width
63 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
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
69 FriendListItem::FriendListItem(QWidget *parent)
74 qDebug() << __PRETTY_FUNCTION__;
76 // m_distanceTextLabel = new QLabel();
77 // m_distanceTextLabel->setFixedHeight(ICON_HEIGHT);
79 // m_distanceImageLabel = new QLabel();
80 // m_distanceImageLabel->setFixedSize(ICON_WIDTH, ICON_HEIGHT);
82 // m_findButton = new ImageButton(this);
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);
91 // distanceLayout->addWidget(m_distanceImageLabel, 0, Qt::AlignRight);
92 // distanceLayout->addWidget(m_distanceTextLabel, 0, Qt::AlignRight);
94 // infoLayout->addRow(envelopeLabel, m_statusTextLabel);
95 // infoLayout->addRow(compassLabel, m_locationLabel);
96 // infoLayout->addRow(clockLabel, m_updatedLabel);
98 // topLayout->addWidget(m_findButton);
99 // topLayout->addWidget(m_nameLabel, 1);
100 // topLayout->addLayout(distanceLayout);
102 // bottomLayout->addSpacing(IMAGE_WIDTH);
103 // bottomLayout->addLayout(infoLayout);
105 // layout->addLayout(topLayout, 0);
106 // layout->addLayout(bottomLayout, 1);
108 // setMinimumSize(BACKGROUND_WIDTH, ITEM_MIN_HEIGHT);
109 // setMaximumWidth(BACKGROUND_WIDTH);
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);
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");
124 // connect(m_findButton, SIGNAL(clicked()),
125 // this, SLOT(findButtonClicked()));
128 void FriendListItem::setUserData(User *user)
130 qDebug() << __PRETTY_FUNCTION__;
135 if (!m_user->profileImage().isNull())
136 setData(Qt::DisplayRole, m_user->profileImage());
140 // m_user->distance(value, unit);
141 // m_distanceTextLabel->setText(QString::number(value) + " " + unit);
142 // setDistanceIcon(value, unit);
149 void FriendListItem::setDistanceIcon(double value, const QString &unit)
151 QPixmap distanceImage;
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");
160 distanceImage.load(":/res/images/rocket_icon_gray.png");
162 m_distanceImageLabel->setPixmap(distanceImage);
165 void FriendListItem::shortenTexts()
167 qDebug() << __PRETTY_FUNCTION__;
169 QFontMetrics nameLabelMetrics = QFontMetrics(NOKIA_FONT_NORMAL);
170 QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
172 QString name = m_user->name();
173 QString updated = m_user->timestamp();
174 QString statusText = m_user->note();
175 QString location = m_user->address();
177 int nameIndex = name.indexOf('\n');
178 int updatedIndex = updated.indexOf('\n');
179 int statusTextIndex = statusText.indexOf('\n');
180 int locationIndex = location.indexOf('\n');
183 name.truncate(nameIndex);
186 if (updatedIndex > 0) {
187 updated.truncate(updatedIndex);
188 updated.append("...");
190 if (statusTextIndex > 0) {
191 statusText.truncate(statusTextIndex);
192 statusText.append("...");
194 if (locationIndex > 0) {
195 location.truncate(locationIndex);
196 location.append("...");
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,
205 m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
208 void FriendListItem::setText(bool expanded)
210 qDebug() << __PRETTY_FUNCTION__;
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);
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);
230 void FriendListItem::findButtonClicked()
232 qDebug() << __PRETTY_FUNCTION__;
234 emit findFriend(m_user->coordinates());