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,
27 #include "imagebutton.h"
28 #include "../user/user.h"
31 const int BACKGROUND_WIDTH = 240; ///< Width for item
32 const int BACKGROUND_TOP_HEIGHT = 16; ///< Height for item top
33 const int BACKGROUND_BOTTOM_HEIGHT = 15; ///< Height for item bottom
34 const int ICON_HEIGHT = 24; ///< Icon height
35 const int ICON_WIDTH = 24; ///< Icon width
36 const int MARGIN = 5; ///< Icon margin
37 const int MOUSE_PRESS_AREA_WIDTH = 20; ///< Area width for item height toggling
38 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
39 const QString USER_UNSEND_MESSAGE = "User_unsend_message_content";
40 const QString USER_UNSEND_MESSAGE_PUBLISH_POLICITY = "User_unsend_message_publish";
43 * @var LABEL_MAX_WIDTH
45 * @brief All label's maximum width
47 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - ICON_WIDTH + 130;
49 UserInfo::UserInfo(QWidget *parent)
52 m_messageUpdateVerified(false),
55 qDebug() << __PRETTY_FUNCTION__;
57 QVBoxLayout *verticalLayout = new QVBoxLayout(this);
58 verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
59 verticalLayout->setSpacing(0);
60 setLayout(verticalLayout);
62 QFormLayout *infoLayout = new QFormLayout();
63 infoLayout->setMargin(0);
64 infoLayout->setSpacing(0);
66 QHBoxLayout *buttonLayout = new QHBoxLayout();
67 buttonLayout->setMargin(0);
68 buttonLayout->setSpacing(0);
70 QLabel *envelopeLabel = new QLabel();
71 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
72 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
73 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
74 QLabel *compassLabel = new QLabel();
75 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
76 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
77 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
78 QLabel *clockLabel = new QLabel();
79 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
80 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
81 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
83 m_findButton = new ImageButton();
85 m_nameLabel = new QLabel();
86 m_nameLabel->setWordWrap(true);
88 m_statusTextLabel = new QLabel();
89 m_statusTextLabel->setWordWrap(true);
90 m_locationLabel = new QLabel();
91 m_locationLabel->setWordWrap(true);
92 m_updatedLabel = new QLabel();
93 m_updatedLabel->setWordWrap(true);
95 ImageButton *updateFriendsButton = new ImageButton(this, ":/res/images/refresh.png",
96 ":/res/images/refresh_s.png");
97 ImageButton *updateStatusMessageButton = new ImageButton(this, ":/res/images/send_position.png",
98 ":/res/images/send_position_s.png");
100 buttonLayout->addWidget(updateFriendsButton);
101 buttonLayout->addWidget(updateStatusMessageButton);
103 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
104 infoLayout->addRow(compassLabel, m_locationLabel);
105 infoLayout->addRow(clockLabel, m_updatedLabel);
107 verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
108 verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
109 verticalLayout->addLayout(infoLayout);
110 verticalLayout->addLayout(buttonLayout);
112 connect(updateStatusMessageButton,SIGNAL(clicked()),
113 this,SLOT(messageUpdate()));
115 connect(updateFriendsButton,SIGNAL(clicked()),
116 this, SIGNAL(refreshUserData()));
118 connect(m_findButton, SIGNAL(clicked()),
119 this, SLOT(findButtonClicked()));
121 setFixedWidth(BACKGROUND_WIDTH);
123 this->setFont(NOKIA_FONT_SMALL);
124 m_nameLabel->setFont(NOKIA_FONT_NORMAL);
125 QPalette itemPalette = palette();
126 itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
127 setPalette(itemPalette);
128 QPalette namePalette = m_nameLabel->palette();
129 namePalette.setColor(QPalette::Foreground, Qt::white);
130 m_nameLabel->setPalette(namePalette);
132 m_backgroundTopImage.load(":/res/images/user_info_item_top.png");
133 m_backgroundMiddleImage.load(":/res/images/user_info_item_middle.png");
134 m_backgroundBottomImage.load(":/res/images/user_info_item_bottom.png");
136 restoreUnsendMessage();
139 UserInfo::~UserInfo()
141 qDebug() << __PRETTY_FUNCTION__;
143 QSettings settings(DIRECTORY_NAME, FILE_NAME);
145 if (!m_backupMessage.isEmpty()) {
146 settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
147 settings.setValue(USER_UNSEND_MESSAGE_PUBLISH_POLICITY, m_backupFacebookPublishPolicity);
149 settings.remove(USER_UNSEND_MESSAGE);
150 settings.remove(USER_UNSEND_MESSAGE_PUBLISH_POLICITY);
154 void UserInfo::setAddress(const QString &address)
156 qDebug() << __PRETTY_FUNCTION__;
158 m_locationLabel->setText(address);
161 void UserInfo::setCoordinates(const QPointF &coordinates)
163 qDebug() << __PRETTY_FUNCTION__;
165 m_coordinates = coordinates;
168 void UserInfo::setMessageText(const QString &text)
170 qDebug() << __PRETTY_FUNCTION__;
172 m_messageText = text;
176 void UserInfo::setProfileImage(const QPixmap &image)
178 qDebug() << __PRETTY_FUNCTION__;
180 m_findButton->setButtonIcon(image);
183 void UserInfo::setTime(const QString &time)
185 qDebug() << __PRETTY_FUNCTION__;
187 m_updatedLabel->setText(time);
190 void UserInfo::setUserName(const QString &name)
192 qDebug() << __PRETTY_FUNCTION__;
198 void UserInfo::setText(bool expanded)
200 qDebug() << __PRETTY_FUNCTION__;
203 m_nameLabel->setText(m_userName);
204 m_statusTextLabel->setText(m_messageText);
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;
220 m_messageUpdateVerified = false;
223 void UserInfo::clearUpdateLocationDialogData()
225 qDebug() << __PRETTY_FUNCTION__;
227 m_backupMessage.clear();
228 m_backupFacebookPublishPolicity = false;
231 void UserInfo::findButtonClicked()
233 qDebug() << __PRETTY_FUNCTION__;
235 emit findUser(m_coordinates);
238 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
240 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
242 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
243 (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
255 void UserInfo::mousePressEvent(QMouseEvent *event)
257 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
259 m_mousePosition = event->pos();
262 void UserInfo::messageUpdate()
264 qDebug() << __PRETTY_FUNCTION__;
266 emit requestReverseGeo();
268 delete m_updateLocation;
269 m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
272 connect(this, SIGNAL(reverseGeoReady(QString)),
273 m_updateLocation, SLOT(setAddress(QString)));
275 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
276 this, SIGNAL(statusUpdate(QString,bool)));
278 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
279 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
281 connect(m_updateLocation, SIGNAL(finished(int)),
282 this, SLOT(updateLocationDialogFinished(int)));
284 m_updateLocation->show();
287 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
289 qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
291 QPainter painter(this);
293 QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
294 QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
295 height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
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, "").toString();
310 m_backupFacebookPublishPolicity =
311 settings.value(USER_UNSEND_MESSAGE_PUBLISH_POLICITY, false).toBool();
314 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
316 qDebug() << __PRETTY_FUNCTION__;
318 QFontMetrics labelMetrics = label->fontMetrics();
320 QString textParam = text;
321 int index = textParam.indexOf('\n');
324 textParam.truncate(index);
325 textParam.append("...");
328 return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
331 void UserInfo::verifyMessageUpdateFailure(const int error)
333 qDebug() << __PRETTY_FUNCTION__;
337 if (!m_messageUpdateVerified) {
339 if (m_messageText != m_backupMessage && !m_backupMessage.isEmpty())
340 emit notificateUpdateFailing(tr("Location update failed\n\nYour message is saved to "
341 "textbox until sending will succeed"), true);
344 m_messageUpdateVerified = true;
347 void UserInfo::updateLocationDialogFinished(int reason)
349 qDebug() << __PRETTY_FUNCTION__;
353 if (m_updateLocation) {
354 disconnect(this, SIGNAL(reverseGeoReady(QString)),
355 m_updateLocation, SLOT(setAddress(QString)));
357 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
358 this, SIGNAL(statusUpdate(QString,bool)));
360 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
361 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
363 disconnect(m_updateLocation, SIGNAL(finished(int)),
364 this, SLOT(updateLocationDialogFinished(int)));
366 m_updateLocation->deleteLater();
367 m_updateLocation = 0;