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_BOTTOM_HEIGHT = 15;
40 const int BACKGROUND_TOP_HEIGHT = 20;
41 const int BACKGROUND_WIDTH = 368;
42 const int ICON_HEIGHT = 24;
43 const int ICON_WIDTH = 24;
44 const int LABEL_MAX_WIDTH = 300;
47 UserInfo::UserInfo(QWidget *parent)
52 qDebug() << __PRETTY_FUNCTION__;
54 QVBoxLayout *verticalLayout = new QVBoxLayout(this);
55 verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
56 verticalLayout->setSpacing(0);
57 setLayout(verticalLayout);
59 QFormLayout *infoLayout = new QFormLayout();
60 infoLayout->setMargin(0);
61 infoLayout->setSpacing(0);
63 QLabel *envelopeLabel = new QLabel();
64 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
65 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
66 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
67 QLabel *compassLabel = new QLabel();
68 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
69 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
70 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
71 QLabel *clockLabel = new QLabel();
72 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
73 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
74 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
76 m_avatar = new ImageButton();
78 m_nameLabel = new QLabel();
80 m_statusTextLabel = new QLabel();
81 m_statusTextLabel->setWordWrap(true);
82 m_locationLabel = new QLabel();
83 m_locationLabel->setWordWrap(true);
84 m_updatedLabel = new QLabel();
85 m_updatedLabel->setWordWrap(true);
87 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
88 infoLayout->addRow(compassLabel, m_locationLabel);
89 infoLayout->addRow(clockLabel, m_updatedLabel);
91 verticalLayout->addWidget(m_avatar, 0, Qt::AlignHCenter);
92 verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
93 verticalLayout->addLayout(infoLayout);
95 connect(m_avatar, SIGNAL(clicked()),
96 this, SLOT(findButtonClicked()));
98 setFixedWidth(BACKGROUND_WIDTH);
100 this->setFont(NOKIA_FONT_SMALL);
101 m_nameLabel->setFont(NOKIA_FONT_NORMAL);
102 QPalette itemPalette = palette();
103 itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
104 setPalette(itemPalette);
105 QPalette namePalette = m_nameLabel->palette();
106 namePalette.setColor(QPalette::Foreground, Qt::white);
107 m_nameLabel->setPalette(namePalette);
109 m_backgroundTopImage.load(":/res/images/list_item_top.png");
110 m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
111 m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
113 restoreUnsendMessage();
116 UserInfo::~UserInfo()
118 qDebug() << __PRETTY_FUNCTION__;
120 QSettings settings(DIRECTORY_NAME, FILE_NAME);
122 if (!m_backupMessage.isEmpty()) {
123 settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
124 settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
126 settings.remove(USER_UNSEND_MESSAGE);
127 settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
131 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
133 qDebug() << __PRETTY_FUNCTION__;
135 m_backupMessage = status;
136 m_backupFacebookPublishPolicity = publish;
139 void UserInfo::clearUpdateLocationDialogData()
141 qDebug() << __PRETTY_FUNCTION__;
143 m_backupMessage.clear();
144 m_backupFacebookPublishPolicity = false;
147 void UserInfo::collapse()
149 qDebug() << __PRETTY_FUNCTION__;
154 void UserInfo::findButtonClicked()
156 qDebug() << __PRETTY_FUNCTION__;
158 emit findUser(m_coordinates);
161 void UserInfo::messageUpdate()
163 qDebug() << __PRETTY_FUNCTION__;
165 delete m_updateLocation;
166 m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
169 connect(this, SIGNAL(reverseGeoReady(QString)),
170 m_updateLocation, SLOT(setAddress(QString)));
172 connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
173 this, SIGNAL(statusUpdate(QString, bool)));
175 connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
176 this, SLOT(backupUpdateLocationDialogData(QString, bool)));
178 connect(m_updateLocation, SIGNAL(finished(int)),
179 this, SLOT(updateLocationDialogFinished(int)));
181 m_updateLocation->show();
183 emit requestReverseGeo();
186 void UserInfo::mousePressEvent(QMouseEvent *event)
188 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
190 m_mousePosition = event->pos();
193 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
195 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
197 const int MOUSE_PRESS_AREA_HEIGHT = 20;
198 const int MOUSE_PRESS_AREA_WIDTH = 20;
200 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH)
201 && (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
213 void UserInfo::paintEvent(QPaintEvent *event)
215 qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
217 QPainter painter(this);
219 QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
221 QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH,
222 this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
224 QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH,
225 BACKGROUND_BOTTOM_HEIGHT);
227 painter.drawPixmap(topRect, m_backgroundTopImage);
228 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
229 painter.drawPixmap(bottomRect, m_backgroundBottomImage);
232 void UserInfo::restoreUnsendMessage()
234 qDebug() << __PRETTY_FUNCTION__;
236 QSettings settings(DIRECTORY_NAME, FILE_NAME);
237 m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
238 m_backupFacebookPublishPolicity = settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
241 void UserInfo::setAddress(const QString &address)
243 qDebug() << __PRETTY_FUNCTION__;
245 m_locationLabel->setText(address);
248 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
250 qDebug() << __PRETTY_FUNCTION__;
252 m_coordinates = coordinates;
255 void UserInfo::setMessageText(const QString &text)
257 qDebug() << __PRETTY_FUNCTION__;
260 list = text.split(' ');
262 for (int i = 0; i < list.count(); i++) {
263 if (fontMetrics().width(list.at(i)) > LABEL_MAX_WIDTH)
264 list.replace(i, splitWord(list.at(i)));
267 m_messageText = list.join(" ");
272 void UserInfo::setProfileImage(const QPixmap &image)
274 qDebug() << __PRETTY_FUNCTION__;
277 m_avatar->setButtonIcon(image);
280 void UserInfo::setExpanded(bool expanded)
282 qDebug() << __PRETTY_FUNCTION__;
285 m_statusTextLabel->setText(m_messageText);
287 m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText, LABEL_MAX_WIDTH));
290 void UserInfo::setTime(const QString &time)
292 qDebug() << __PRETTY_FUNCTION__;
294 m_updatedLabel->setText(time);
297 void UserInfo::setUserName(const QString &name)
299 qDebug() << __PRETTY_FUNCTION__;
303 m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
306 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
308 qDebug() << __PRETTY_FUNCTION__;
310 QFontMetrics labelMetrics = label->fontMetrics();
311 QString copiedText = text;
312 int index = copiedText.indexOf('\n');
315 copiedText.truncate(index);
316 copiedText.append("...");
319 return labelMetrics.elidedText(copiedText, Qt::ElideRight, textMaxWidth);
322 QString UserInfo::splitWord(const QString &word) const
324 qDebug() << __PRETTY_FUNCTION__;
329 for (int i = 0; i < word.length(); i++) {
330 if (fontMetrics().width(temp.append(word.at(i))) > LABEL_MAX_WIDTH) {
331 result.append(temp.left(temp.length() - 1));
333 temp.remove(0, temp.length() - 1);
342 void UserInfo::updateLocationDialogFinished(int reason)
344 qDebug() << __PRETTY_FUNCTION__;
348 if (m_updateLocation) {
349 disconnect(this, SIGNAL(reverseGeoReady(QString)),
350 m_updateLocation, SLOT(setAddress(QString)));
352 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
353 this, SIGNAL(statusUpdate(QString,bool)));
355 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
356 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
358 disconnect(m_updateLocation, SIGNAL(finished(int)),
359 this, SLOT(updateLocationDialogFinished(int)));
361 m_updateLocation->deleteLater();
362 m_updateLocation = 0;