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 QHBoxLayout *buttonLayout = new QHBoxLayout();
64 buttonLayout->setMargin(0);
65 buttonLayout->setSpacing(0);
67 QLabel *envelopeLabel = new QLabel();
68 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
69 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
70 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
71 QLabel *compassLabel = new QLabel();
72 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
73 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
74 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
75 QLabel *clockLabel = new QLabel();
76 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
77 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
78 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
80 m_findButton = new ImageButton();
82 m_nameLabel = new QLabel();
83 m_nameLabel->setWordWrap(true);
85 m_statusTextLabel = new QLabel();
86 m_statusTextLabel->setWordWrap(true);
87 m_locationLabel = new QLabel();
88 m_locationLabel->setWordWrap(true);
89 m_updatedLabel = new QLabel();
90 m_updatedLabel->setWordWrap(true);
92 ImageButton *updateFriendsButton = new ImageButton(":/res/images/refresh.png",
93 ":/res/images/refresh_s.png",
95 ImageButton *updateStatusMessageButton = new ImageButton(":/res/images/send_position.png",
96 ":/res/images/send_position_s.png",
99 buttonLayout->addWidget(updateFriendsButton);
100 buttonLayout->addWidget(updateStatusMessageButton);
102 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
103 infoLayout->addRow(compassLabel, m_locationLabel);
104 infoLayout->addRow(clockLabel, m_updatedLabel);
106 verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
107 verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
108 verticalLayout->addLayout(infoLayout);
109 verticalLayout->addLayout(buttonLayout);
111 connect(updateStatusMessageButton,SIGNAL(clicked()),
112 this,SLOT(messageUpdate()));
114 connect(updateFriendsButton,SIGNAL(clicked()),
115 this, SIGNAL(refreshUserData()));
117 connect(m_findButton, SIGNAL(clicked()),
118 this, SLOT(findButtonClicked()));
120 setFixedWidth(BACKGROUND_WIDTH);
122 this->setFont(NOKIA_FONT_SMALL);
123 m_nameLabel->setFont(NOKIA_FONT_NORMAL);
124 QPalette itemPalette = palette();
125 itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
126 setPalette(itemPalette);
127 QPalette namePalette = m_nameLabel->palette();
128 namePalette.setColor(QPalette::Foreground, Qt::white);
129 m_nameLabel->setPalette(namePalette);
131 m_backgroundTopImage.load(":/res/images/list_item_top.png");
132 m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
133 m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
135 restoreUnsendMessage();
138 UserInfo::~UserInfo()
140 qDebug() << __PRETTY_FUNCTION__;
142 QSettings settings(DIRECTORY_NAME, FILE_NAME);
144 if (!m_backupMessage.isEmpty()) {
145 settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
146 settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
148 settings.remove(USER_UNSEND_MESSAGE);
149 settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
153 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
155 qDebug() << __PRETTY_FUNCTION__;
157 m_backupMessage = status;
158 m_backupFacebookPublishPolicity = publish;
161 void UserInfo::clearUpdateLocationDialogData()
163 qDebug() << __PRETTY_FUNCTION__;
165 m_backupMessage.clear();
166 m_backupFacebookPublishPolicity = false;
169 void UserInfo::collapse()
171 qDebug() << __PRETTY_FUNCTION__;
176 void UserInfo::findButtonClicked()
178 qDebug() << __PRETTY_FUNCTION__;
180 emit findUser(m_coordinates);
183 void UserInfo::messageUpdate()
185 qDebug() << __PRETTY_FUNCTION__;
187 delete m_updateLocation;
188 m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
191 connect(this, SIGNAL(reverseGeoReady(QString)),
192 m_updateLocation, SLOT(setAddress(QString)));
194 connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
195 this, SIGNAL(statusUpdate(QString, bool)));
197 connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
198 this, SLOT(backupUpdateLocationDialogData(QString, bool)));
200 connect(m_updateLocation, SIGNAL(finished(int)),
201 this, SLOT(updateLocationDialogFinished(int)));
203 m_updateLocation->show();
205 emit requestReverseGeo();
208 void UserInfo::mousePressEvent(QMouseEvent *event)
210 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
212 m_mousePosition = event->pos();
215 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
217 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
219 const int MOUSE_PRESS_AREA_HEIGHT = 20;
220 const int MOUSE_PRESS_AREA_WIDTH = 20;
222 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH)
223 && (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
235 void UserInfo::paintEvent(QPaintEvent *event)
237 qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
239 QPainter painter(this);
241 QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
243 QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH,
244 this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
246 QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH,
247 BACKGROUND_BOTTOM_HEIGHT);
249 painter.drawPixmap(topRect, m_backgroundTopImage);
250 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
251 painter.drawPixmap(bottomRect, m_backgroundBottomImage);
254 void UserInfo::restoreUnsendMessage()
256 qDebug() << __PRETTY_FUNCTION__;
258 QSettings settings(DIRECTORY_NAME, FILE_NAME);
259 m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
260 m_backupFacebookPublishPolicity = settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
263 void UserInfo::setAddress(const QString &address)
265 qDebug() << __PRETTY_FUNCTION__;
267 m_locationLabel->setText(address);
270 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
272 qDebug() << __PRETTY_FUNCTION__;
274 m_coordinates = coordinates;
277 void UserInfo::setMessageText(const QString &text)
279 qDebug() << __PRETTY_FUNCTION__;
282 list = text.split(' ');
284 for (int i = 0; i < list.count(); i++) {
285 if (fontMetrics().width(list.at(i)) > LABEL_MAX_WIDTH)
286 list.replace(i, splitWord(list.at(i)));
289 m_messageText = list.join(" ");
294 void UserInfo::setProfileImage(const QPixmap &image)
296 qDebug() << __PRETTY_FUNCTION__;
299 m_findButton->setButtonIcon(image);
302 void UserInfo::setExpanded(bool expanded)
304 qDebug() << __PRETTY_FUNCTION__;
307 m_statusTextLabel->setText(m_messageText);
309 m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText, LABEL_MAX_WIDTH));
312 void UserInfo::setTime(const QString &time)
314 qDebug() << __PRETTY_FUNCTION__;
316 m_updatedLabel->setText(time);
319 void UserInfo::setUserName(const QString &name)
321 qDebug() << __PRETTY_FUNCTION__;
325 m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
328 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
330 qDebug() << __PRETTY_FUNCTION__;
332 QFontMetrics labelMetrics = label->fontMetrics();
333 QString copiedText = text;
334 int index = copiedText.indexOf('\n');
337 copiedText.truncate(index);
338 copiedText.append("...");
341 return labelMetrics.elidedText(copiedText, Qt::ElideRight, textMaxWidth);
344 QString UserInfo::splitWord(const QString &word) const
346 qDebug() << __PRETTY_FUNCTION__;
351 for (int i = 0; i < word.length(); i++) {
352 if (fontMetrics().width(temp.append(word.at(i))) > LABEL_MAX_WIDTH) {
353 result.append(temp.left(temp.length() - 1));
355 temp.remove(0, temp.length() - 1);
364 void UserInfo::updateLocationDialogFinished(int reason)
366 qDebug() << __PRETTY_FUNCTION__;
370 if (m_updateLocation) {
371 disconnect(this, SIGNAL(reverseGeoReady(QString)),
372 m_updateLocation, SLOT(setAddress(QString)));
374 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
375 this, SIGNAL(statusUpdate(QString,bool)));
377 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
378 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
380 disconnect(m_updateLocation, SIGNAL(finished(int)),
381 this, SLOT(updateLocationDialogFinished(int)));
383 m_updateLocation->deleteLater();
384 m_updateLocation = 0;