2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jukka Saastamoinen - jukka.saastamoinen@ixonos.com
6 Jussi Laitinen - jussi.laitinen@ixonos.com
7 Katri Kaikkonen - katri.kaikkonen@ixonos.com
8 Henri Lampela - henri.lampela@ixonos.com
9 Ville Tiensuu - ville.tiensuu@ixonos.com
11 Situare is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 version 2 as published by the Free Software Foundation.
15 Situare is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with Situare; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 #include <QFormLayout>
28 #include <QMouseEvent>
31 #include <QVBoxLayout>
34 #include "imagebutton.h"
35 #include "user/user.h"
39 const int BACKGROUND_WIDTH = 368; ///< Width for item
40 const int BACKGROUND_TOP_HEIGHT = 20; ///< Height for item top
41 const int BACKGROUND_BOTTOM_HEIGHT = 15; ///< Height for item bottom
42 const int ICON_HEIGHT = 24; ///< Icon height
43 const int ICON_WIDTH = 24; ///< Icon width
44 const int MARGIN = 5; ///< Icon margin
45 const int LINE_LENGTH = 27; ///< Line length
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 LABEL_MAX_WIDTH
52 * @brief All label's maximum width
54 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - ICON_WIDTH + 130;
56 UserInfo::UserInfo(QWidget *parent)
61 qDebug() << __PRETTY_FUNCTION__;
63 QVBoxLayout *verticalLayout = new QVBoxLayout(this);
64 verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
65 verticalLayout->setSpacing(0);
66 setLayout(verticalLayout);
68 QFormLayout *infoLayout = new QFormLayout();
69 infoLayout->setMargin(0);
70 infoLayout->setSpacing(0);
72 QLabel *envelopeLabel = new QLabel();
73 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
74 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
75 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
76 QLabel *compassLabel = new QLabel();
77 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
78 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
79 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
80 QLabel *clockLabel = new QLabel();
81 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
82 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
83 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
85 m_findButton = new ImageButton();
87 m_nameLabel = new QLabel();
88 m_nameLabel->setWordWrap(true);
90 m_statusTextLabel = new QLabel();
91 m_statusTextLabel->setWordWrap(true);
92 m_locationLabel = new QLabel();
93 m_locationLabel->setWordWrap(true);
94 m_updatedLabel = new QLabel();
95 m_updatedLabel->setWordWrap(true);
97 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
98 infoLayout->addRow(compassLabel, m_locationLabel);
99 infoLayout->addRow(clockLabel, m_updatedLabel);
101 verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
102 verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
103 verticalLayout->addLayout(infoLayout);
105 connect(m_findButton, SIGNAL(clicked()),
106 this, SLOT(findButtonClicked()));
108 setFixedWidth(BACKGROUND_WIDTH);
110 this->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 restoreUnsendMessage();
126 UserInfo::~UserInfo()
128 qDebug() << __PRETTY_FUNCTION__;
130 QSettings settings(DIRECTORY_NAME, FILE_NAME);
132 if (!m_backupMessage.isEmpty()) {
133 settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
134 settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
136 settings.remove(USER_UNSEND_MESSAGE);
137 settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
141 void UserInfo::setAddress(const QString &address)
143 qDebug() << __PRETTY_FUNCTION__;
145 m_locationLabel->setText(address);
148 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
150 qDebug() << __PRETTY_FUNCTION__;
152 m_coordinates = coordinates;
155 void UserInfo::setMessageText(const QString &text)
157 qDebug() << __PRETTY_FUNCTION__;
159 m_messageText = text;
160 m_expandedMessageText.clear();
162 for(int i=0;i < text.length();i++) {
163 if(fontMetrics().width(temp.append(text.at(i))) > 170) {
165 if(temp.startsWith(QString(" ")))
168 m_expandedMessageText.append(temp);
172 m_expandedMessageText.append(temp);
176 void UserInfo::setProfileImage(const QPixmap &image)
178 qDebug() << __PRETTY_FUNCTION__;
181 m_findButton->setButtonIcon(image);
184 void UserInfo::setTime(const QString &time)
186 qDebug() << __PRETTY_FUNCTION__;
188 m_updatedLabel->setText(time);
191 void UserInfo::setUserName(const QString &name)
193 qDebug() << __PRETTY_FUNCTION__;
199 void UserInfo::setText(bool expanded)
201 qDebug() << __PRETTY_FUNCTION__;
204 m_statusTextLabel->setText(m_expandedMessageText);
207 m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
208 m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
213 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
215 qDebug() << __PRETTY_FUNCTION__;
217 m_backupMessage = status;
218 m_backupFacebookPublishPolicity = publish;
221 void UserInfo::clearUpdateLocationDialogData()
223 qDebug() << __PRETTY_FUNCTION__;
225 m_backupMessage.clear();
226 m_backupFacebookPublishPolicity = false;
229 void UserInfo::findButtonClicked()
231 qDebug() << __PRETTY_FUNCTION__;
233 emit findUser(m_coordinates);
236 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
238 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
240 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
241 (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
253 void UserInfo::mousePressEvent(QMouseEvent *event)
255 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
257 m_mousePosition = event->pos();
260 void UserInfo::messageUpdate()
262 qDebug() << __PRETTY_FUNCTION__;
264 delete m_updateLocation;
265 m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
268 connect(this, SIGNAL(reverseGeoReady(QString)),
269 m_updateLocation, SLOT(setAddress(QString)));
271 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
272 this, SIGNAL(statusUpdate(QString,bool)));
274 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
275 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
277 connect(m_updateLocation, SIGNAL(finished(int)),
278 this, SLOT(updateLocationDialogFinished(int)));
280 m_updateLocation->show();
282 emit requestReverseGeo();
285 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
287 qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
289 QPainter painter(this);
291 QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
292 ///< @todo Overlaps with topRect?
293 QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
294 this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
295 ///< @todo Overlaps with middleRect
296 QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
297 BACKGROUND_BOTTOM_HEIGHT);
299 painter.drawPixmap(topRect, m_backgroundTopImage);
300 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
301 painter.drawPixmap(bottomRect, m_backgroundBottomImage);
304 void UserInfo::restoreUnsendMessage()
306 qDebug() << __PRETTY_FUNCTION__;
308 QSettings settings(DIRECTORY_NAME, FILE_NAME);
309 m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
310 m_backupFacebookPublishPolicity =
311 settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
314 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
316 qDebug() << __PRETTY_FUNCTION__;
318 QFontMetrics labelMetrics = label->fontMetrics();
319 QString textParam = text;
320 int index = textParam.indexOf('\n');
321 int textWidth = fontMetrics().width(textParam);
324 textParam.truncate(index);
325 textParam.append("...");
327 if (textWidth > 250) ///< @todo magic number
328 textParam.insert(LINE_LENGTH, QString("\n"));
330 return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
333 void UserInfo::updateLocationDialogFinished(int reason)
335 qDebug() << __PRETTY_FUNCTION__;
339 if (m_updateLocation) {
340 disconnect(this, SIGNAL(reverseGeoReady(QString)),
341 m_updateLocation, SLOT(setAddress(QString)));
343 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
344 this, SIGNAL(statusUpdate(QString,bool)));
346 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
347 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
349 disconnect(m_updateLocation, SIGNAL(finished(int)),
350 this, SLOT(updateLocationDialogFinished(int)));
352 m_updateLocation->deleteLater();
353 m_updateLocation = 0;