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 LINE_LENGTH = 17; ///< Line length
38 const int MOUSE_PRESS_AREA_WIDTH = 20; ///< Area width for item height toggling
39 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
42 * @var LABEL_MAX_WIDTH
44 * @brief All label's maximum width
46 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - ICON_WIDTH + 130;
48 UserInfo::UserInfo(QWidget *parent)
51 m_messageUpdateVerified(false),
54 qDebug() << __PRETTY_FUNCTION__;
56 QVBoxLayout *verticalLayout = new QVBoxLayout(this);
57 verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
58 verticalLayout->setSpacing(0);
59 setLayout(verticalLayout);
61 QFormLayout *infoLayout = new QFormLayout();
62 infoLayout->setMargin(0);
63 infoLayout->setSpacing(0);
65 QHBoxLayout *buttonLayout = new QHBoxLayout();
66 buttonLayout->setMargin(0);
67 buttonLayout->setSpacing(0);
69 QLabel *envelopeLabel = new QLabel();
70 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
71 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
72 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
73 QLabel *compassLabel = new QLabel();
74 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
75 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
76 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
77 QLabel *clockLabel = new QLabel();
78 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
79 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
80 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
82 m_findButton = new ImageButton();
84 m_nameLabel = new QLabel();
85 m_nameLabel->setWordWrap(true);
87 m_statusTextLabel = new QLabel();
88 m_statusTextLabel->setWordWrap(true);
89 m_locationLabel = new QLabel();
90 m_locationLabel->setWordWrap(true);
91 m_updatedLabel = new QLabel();
92 m_updatedLabel->setWordWrap(true);
94 ImageButton *updateFriendsButton = new ImageButton(this, ":/res/images/refresh.png",
95 ":/res/images/refresh_s.png");
96 ImageButton *updateStatusMessageButton = new ImageButton(this, ":/res/images/send_position.png",
97 ":/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/user_info_item_top.png");
132 m_backgroundMiddleImage.load(":/res/images/user_info_item_middle.png");
133 m_backgroundBottomImage.load(":/res/images/user_info_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::setAddress(const QString &address)
155 qDebug() << __PRETTY_FUNCTION__;
157 m_locationLabel->setText(address);
160 void UserInfo::setCoordinates(const QPointF &coordinates)
162 qDebug() << __PRETTY_FUNCTION__;
164 m_coordinates = coordinates;
167 void UserInfo::setMessageText(const QString &text)
169 qDebug() << __PRETTY_FUNCTION__;
171 m_messageText = text;
172 m_expandedMessageText = text;
173 int textWidth = fontMetrics().width(m_expandedMessageText);
175 if (textWidth > 150){
176 QStringList words = m_expandedMessageText.split(" ");
178 for (int i = 0; i < words.count();i++){
179 QString temp = words.at(i);
181 if(temp.length() >= LINE_LENGTH) {
182 temp.insert(LINE_LENGTH,QString("\n"));
183 words.replace(i,temp);
186 m_expandedMessageText = words.join(" ");
192 void UserInfo::setProfileImage(const QPixmap &image)
194 qDebug() << __PRETTY_FUNCTION__;
196 m_findButton->setButtonIcon(image);
199 void UserInfo::setTime(const QString &time)
201 qDebug() << __PRETTY_FUNCTION__;
203 m_updatedLabel->setText(time);
206 void UserInfo::setUserName(const QString &name)
208 qDebug() << __PRETTY_FUNCTION__;
214 void UserInfo::setText(bool expanded)
216 qDebug() << __PRETTY_FUNCTION__;
219 m_statusTextLabel->setText(m_expandedMessageText);
222 m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
223 m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
228 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
230 qDebug() << __PRETTY_FUNCTION__;
232 m_backupMessage = status;
233 m_backupFacebookPublishPolicity = publish;
235 m_messageUpdateVerified = false;
238 void UserInfo::clearUpdateLocationDialogData()
240 qDebug() << __PRETTY_FUNCTION__;
242 m_backupMessage.clear();
243 m_backupFacebookPublishPolicity = false;
246 void UserInfo::findButtonClicked()
248 qDebug() << __PRETTY_FUNCTION__;
250 emit findUser(m_coordinates);
253 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
255 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
257 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
258 (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
270 void UserInfo::mousePressEvent(QMouseEvent *event)
272 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
274 m_mousePosition = event->pos();
277 void UserInfo::messageUpdate()
279 qDebug() << __PRETTY_FUNCTION__;
281 delete m_updateLocation;
282 m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
285 connect(this, SIGNAL(reverseGeoReady(QString)),
286 m_updateLocation, SLOT(setAddress(QString)));
288 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
289 this, SIGNAL(statusUpdate(QString,bool)));
291 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
292 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
294 connect(m_updateLocation, SIGNAL(finished(int)),
295 this, SLOT(updateLocationDialogFinished(int)));
297 m_updateLocation->show();
299 emit requestReverseGeo();
302 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
304 qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
306 QPainter painter(this);
308 QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
309 QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
310 height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
311 QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
312 BACKGROUND_BOTTOM_HEIGHT);
314 painter.drawPixmap(topRect, m_backgroundTopImage);
315 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
316 painter.drawPixmap(bottomRect, m_backgroundBottomImage);
319 void UserInfo::restoreUnsendMessage()
321 qDebug() << __PRETTY_FUNCTION__;
323 QSettings settings(DIRECTORY_NAME, FILE_NAME);
324 m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
325 m_backupFacebookPublishPolicity =
326 settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
329 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
331 qDebug() << __PRETTY_FUNCTION__;
333 QFontMetrics labelMetrics = label->fontMetrics();
334 QString textParam = text;
335 int index = textParam.indexOf('\n');
336 int textWidth = fontMetrics().width(textParam);
339 textParam.truncate(index);
340 textParam.append("...");
343 textParam.insert(LINE_LENGTH, QString("\n"));
345 return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
348 void UserInfo::verifyMessageUpdateFailure(const int error)
350 qDebug() << __PRETTY_FUNCTION__;
354 if (!m_messageUpdateVerified) {
356 if (m_messageText != m_backupMessage && !m_backupMessage.isEmpty())
357 emit notificateUpdateFailing(tr("Location update failed, please try again"), false);
360 m_messageUpdateVerified = true;
363 void UserInfo::updateLocationDialogFinished(int reason)
365 qDebug() << __PRETTY_FUNCTION__;
369 if (m_updateLocation) {
370 disconnect(this, SIGNAL(reverseGeoReady(QString)),
371 m_updateLocation, SLOT(setAddress(QString)));
373 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
374 this, SIGNAL(statusUpdate(QString,bool)));
376 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
377 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
379 disconnect(m_updateLocation, SIGNAL(finished(int)),
380 this, SLOT(updateLocationDialogFinished(int)));
382 m_updateLocation->deleteLater();
383 m_updateLocation = 0;