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 QHBoxLayout *buttonLayout = new QHBoxLayout();
73 buttonLayout->setMargin(0);
74 buttonLayout->setSpacing(0);
76 QLabel *envelopeLabel = new QLabel();
77 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
78 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
79 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
80 QLabel *compassLabel = new QLabel();
81 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
82 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
83 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
84 QLabel *clockLabel = new QLabel();
85 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
86 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
87 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
89 m_findButton = new ImageButton();
91 m_nameLabel = new QLabel();
92 m_nameLabel->setWordWrap(true);
94 m_statusTextLabel = new QLabel();
95 m_statusTextLabel->setWordWrap(true);
96 m_locationLabel = new QLabel();
97 m_locationLabel->setWordWrap(true);
98 m_updatedLabel = new QLabel();
99 m_updatedLabel->setWordWrap(true);
101 ImageButton *updateFriendsButton = new ImageButton(":/res/images/refresh.png",
102 ":/res/images/refresh_s.png",
104 ImageButton *updateStatusMessageButton = new ImageButton(":/res/images/send_position.png",
105 ":/res/images/send_position_s.png",
108 buttonLayout->addWidget(updateFriendsButton);
109 buttonLayout->addWidget(updateStatusMessageButton);
111 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
112 infoLayout->addRow(compassLabel, m_locationLabel);
113 infoLayout->addRow(clockLabel, m_updatedLabel);
115 verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
116 verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
117 verticalLayout->addLayout(infoLayout);
118 verticalLayout->addLayout(buttonLayout);
120 connect(updateStatusMessageButton,SIGNAL(clicked()),
121 this,SLOT(messageUpdate()));
123 connect(updateFriendsButton,SIGNAL(clicked()),
124 this, SIGNAL(refreshUserData()));
126 connect(m_findButton, SIGNAL(clicked()),
127 this, SLOT(findButtonClicked()));
129 setFixedWidth(BACKGROUND_WIDTH);
131 this->setFont(NOKIA_FONT_SMALL);
132 m_nameLabel->setFont(NOKIA_FONT_NORMAL);
133 QPalette itemPalette = palette();
134 itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
135 setPalette(itemPalette);
136 QPalette namePalette = m_nameLabel->palette();
137 namePalette.setColor(QPalette::Foreground, Qt::white);
138 m_nameLabel->setPalette(namePalette);
140 m_backgroundTopImage.load(":/res/images/list_item_top.png");
141 m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
142 m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
144 restoreUnsendMessage();
147 UserInfo::~UserInfo()
149 qDebug() << __PRETTY_FUNCTION__;
151 QSettings settings(DIRECTORY_NAME, FILE_NAME);
153 if (!m_backupMessage.isEmpty()) {
154 settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
155 settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
157 settings.remove(USER_UNSEND_MESSAGE);
158 settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
162 void UserInfo::setAddress(const QString &address)
164 qDebug() << __PRETTY_FUNCTION__;
166 m_locationLabel->setText(address);
169 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
171 qDebug() << __PRETTY_FUNCTION__;
173 m_coordinates = coordinates;
176 void UserInfo::setMessageText(const QString &text)
178 qDebug() << __PRETTY_FUNCTION__;
180 m_messageText = text;
181 m_expandedMessageText.clear();
183 for(int i=0;i < text.length();i++) {
184 if(fontMetrics().width(temp.append(text.at(i))) > 170) {
186 if(temp.startsWith(QString(" ")))
189 m_expandedMessageText.append(temp);
193 m_expandedMessageText.append(temp);
197 void UserInfo::setProfileImage(const QPixmap &image)
199 qDebug() << __PRETTY_FUNCTION__;
202 m_findButton->setButtonIcon(image);
205 void UserInfo::setTime(const QString &time)
207 qDebug() << __PRETTY_FUNCTION__;
209 m_updatedLabel->setText(time);
212 void UserInfo::setUserName(const QString &name)
214 qDebug() << __PRETTY_FUNCTION__;
220 void UserInfo::setText(bool expanded)
222 qDebug() << __PRETTY_FUNCTION__;
225 m_statusTextLabel->setText(m_expandedMessageText);
228 m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
229 m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
234 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
236 qDebug() << __PRETTY_FUNCTION__;
238 m_backupMessage = status;
239 m_backupFacebookPublishPolicity = publish;
242 void UserInfo::clearUpdateLocationDialogData()
244 qDebug() << __PRETTY_FUNCTION__;
246 m_backupMessage.clear();
247 m_backupFacebookPublishPolicity = false;
250 void UserInfo::findButtonClicked()
252 qDebug() << __PRETTY_FUNCTION__;
254 emit findUser(m_coordinates);
257 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
259 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
261 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
262 (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
274 void UserInfo::mousePressEvent(QMouseEvent *event)
276 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
278 m_mousePosition = event->pos();
281 void UserInfo::messageUpdate()
283 qDebug() << __PRETTY_FUNCTION__;
285 delete m_updateLocation;
286 m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
289 connect(this, SIGNAL(reverseGeoReady(QString)),
290 m_updateLocation, SLOT(setAddress(QString)));
292 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
293 this, SIGNAL(statusUpdate(QString,bool)));
295 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
296 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
298 connect(m_updateLocation, SIGNAL(finished(int)),
299 this, SLOT(updateLocationDialogFinished(int)));
301 m_updateLocation->show();
303 emit requestReverseGeo();
306 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
308 qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
310 QPainter painter(this);
312 QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
313 ///< @todo Overlaps with topRect?
314 QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
315 this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
316 ///< @todo Overlaps with middleRect
317 QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
318 BACKGROUND_BOTTOM_HEIGHT);
320 painter.drawPixmap(topRect, m_backgroundTopImage);
321 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
322 painter.drawPixmap(bottomRect, m_backgroundBottomImage);
325 void UserInfo::restoreUnsendMessage()
327 qDebug() << __PRETTY_FUNCTION__;
329 QSettings settings(DIRECTORY_NAME, FILE_NAME);
330 m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
331 m_backupFacebookPublishPolicity =
332 settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
335 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
337 qDebug() << __PRETTY_FUNCTION__;
339 QFontMetrics labelMetrics = label->fontMetrics();
340 QString textParam = text;
341 int index = textParam.indexOf('\n');
342 int textWidth = fontMetrics().width(textParam);
345 textParam.truncate(index);
346 textParam.append("...");
348 if (textWidth > 250) ///< @todo magic number
349 textParam.insert(LINE_LENGTH, QString("\n"));
351 return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
354 void UserInfo::updateLocationDialogFinished(int reason)
356 qDebug() << __PRETTY_FUNCTION__;
360 if (m_updateLocation) {
361 disconnect(this, SIGNAL(reverseGeoReady(QString)),
362 m_updateLocation, SLOT(setAddress(QString)));
364 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
365 this, SIGNAL(statusUpdate(QString,bool)));
367 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
368 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
370 disconnect(m_updateLocation, SIGNAL(finished(int)),
371 this, SLOT(updateLocationDialogFinished(int)));
373 m_updateLocation->deleteLater();
374 m_updateLocation = 0;