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"
37 const QString BACKGROUND_PATH = ":/res/images/list_item.png"; ///< Background image path
38 //const QString BACKGROUND_TOP_PATH = ":/res/images/list_item_top.png"; ///< Background image path
39 //const QString BACKGROUND_MIDDLE_PATH = ":/res/images/list_item_middle.png"; ///< Background image path
40 //const QString BACKGROUND_BOTTOM_PATH = ":/res/images/list_item_bottom.png"; ///< Background image path
41 const QString CLOCK_PATH = ":/res/images/clock.png"; ///< Clock image path
42 const QString COMPASS_PATH = ":/res/images/compass.png"; ///< Compass image path
43 const QString ENVELOPE_PATH = ":/res/images/envelope.png"; ///< Envelope image path
45 const int ICON_MARGIN = 5; ///< Icon margin
46 const int IMAGE_HEIGHT = 60; ///< Friend image height
47 const int IMAGE_WIDTH = 60; ///< Friend image width
49 const int ITEM_MAX_HEIGHT = 240; ///< Maximum height for item
50 const int ITEM_MAX_WIDTH = 368; ///< Maximum width for item
51 const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
52 const int ITEM_MIN_WIDTH = 368; ///< Minimum width for item
53 const int LABEL_MAX_WIDTH = 247; ///< Label maximum width
55 const int MOUSE_PRESS_AREA_WIDTH = 20; ///< Area width for item height toggling
56 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
58 const QFont NOKIA_FONT_NORMAL = QFont( "Nokia Sans", 18, QFont::Normal); ///< Normal font
59 const QFont NOKIA_FONT_SMALL = QFont( "Nokia Sans", 13, QFont::Normal); ///< Small font
63 * @brief Stylesheet for FriendListItem
65 const QString STYLESHEET = "QWidget#listItem { border-image: url(:/res/images/list_item.png) 20%; " \
66 "border-width: 20px 14px 16px 14px; } " \
67 "QLabel { font-size: 13pt; color: #989898; }" \
68 "#nameLabel { font-size: 18pt; color: #ffffff }";
70 FriendListItem::FriendListItem(QWidget *parent)
75 qDebug() << __PRETTY_FUNCTION__;
77 QVBoxLayout *layout = new QVBoxLayout(this);
78 this->setLayout(layout);
80 QHBoxLayout *topLayout = new QHBoxLayout();
81 topLayout->setMargin(0);
82 topLayout->setSpacing(0);
84 QHBoxLayout *bottomLayout = new QHBoxLayout();
85 bottomLayout->setMargin(0);
86 bottomLayout->setSpacing(0);
88 QFormLayout *infoLayout = new QFormLayout();
89 infoLayout->setMargin(0);
90 infoLayout->setSpacing(0);
92 QLabel *clockLabel = new QLabel();
93 clockLabel->setPixmap(QPixmap(CLOCK_PATH));
94 clockLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
95 QLabel *envelopeLabel = new QLabel();
96 envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
97 envelopeLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
98 QLabel *compassLabel = new QLabel();
99 compassLabel->setPixmap(QPixmap(COMPASS_PATH));
100 compassLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
102 m_imageLabel = new QLabel();
103 m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
105 m_nameLabel = new QLabel();
106 m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
108 m_updatedLabel = new QLabel();
109 m_updatedLabel->setWordWrap(true);
110 m_statusTextLabel = new QLabel();
111 m_statusTextLabel->setWordWrap(true);
112 m_locationLabel = new QLabel();
113 m_locationLabel->setWordWrap(true);
115 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
116 infoLayout->addRow(compassLabel, m_locationLabel);
117 infoLayout->addRow(clockLabel, m_updatedLabel);
119 topLayout->addWidget(m_imageLabel);
120 topLayout->addWidget(m_nameLabel);
122 bottomLayout->addSpacing(IMAGE_WIDTH);
123 bottomLayout->addLayout(infoLayout, 1);
125 layout->addLayout(topLayout, 0);
126 layout->addLayout(bottomLayout, 1);
128 setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
129 setMaximumSize(ITEM_MIN_WIDTH, ITEM_MAX_HEIGHT);
131 setFont(NOKIA_FONT_SMALL);
132 m_nameLabel->setFont(NOKIA_FONT_NORMAL);
133 QPalette itemPalette = palette();
134 itemPalette.setColor(QPalette::Foreground, QColor(152, 152, 152));
135 setPalette(itemPalette);
136 QPalette namePalette = m_nameLabel->palette();
137 namePalette.setColor(QPalette::Foreground, Qt::white);
138 m_nameLabel->setPalette(namePalette);
140 m_backgroundTopImage.load(":/res/images/list_item_top.png");
141 m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
142 m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
145 void FriendListItem::setData(User *user)
147 qDebug() << __PRETTY_FUNCTION__;
153 m_imageLabel->setPixmap(m_user->profileImage());
158 void FriendListItem::shortenTexts()
160 qDebug() << __PRETTY_FUNCTION__;
162 QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
163 QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
165 QString name = m_user->name();
166 QString updated = m_user->timestamp();
167 QString statusText = m_user->note();
168 QString location = m_user->address();
170 int nameIndex = name.indexOf('\n');
171 int updatedIndex = updated.indexOf('\n');
172 int statusTextIndex = statusText.indexOf('\n');
173 int locationIndex = location.indexOf('\n');
176 name.truncate(nameIndex);
179 if (updatedIndex > 0) {
180 updated.truncate(updatedIndex);
181 updated.append("...");
183 if (statusTextIndex > 0) {
184 statusText.truncate(statusTextIndex);
185 statusText.append("...");
187 if (locationIndex > 0) {
188 location.truncate(locationIndex);
189 location.append("...");
193 m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, LABEL_MAX_WIDTH + 30);
194 m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
195 m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
197 m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
200 void FriendListItem::setText(bool expanded)
202 qDebug() << __PRETTY_FUNCTION__;
205 m_nameLabel->setText(m_shortenedName);
206 m_updatedLabel->setText(m_user->timestamp());
207 m_statusTextLabel->setText(m_user->note());
208 m_locationLabel->setText(m_user->address());
211 m_nameLabel->setText(m_shortenedName);
212 m_updatedLabel->setText(m_shortenedUpdated);
213 m_statusTextLabel->setText(m_shortenedStatusText);
214 m_locationLabel->setText(m_shortenedLocation);
218 void FriendListItem::mousePressEvent(QMouseEvent *event)
220 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
222 m_mousePosition = event->pos();
225 void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
227 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
229 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
230 (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
242 void FriendListItem::paintEvent(QPaintEvent *event)
244 qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
246 QPainter painter(this);
248 int height = event->rect().height();
250 QRect topRect = QRect(0, 0, event->rect().width(), 20);
251 QRect middleRect = QRect(0, topRect.bottom() + 1, event->rect().width(),
252 height + event->rect().top() - 20 - 15);
253 QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, event->rect().width(),
256 painter.drawPixmap(topRect, m_backgroundTopImage);
257 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
258 painter.drawPixmap(bottomRect, m_backgroundBottomImage);