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>
30 #include <QVBoxLayout>
33 #include "imagebutton.h"
34 #include "user/user.h"
38 const int BACKGROUND_WIDTH = 368; ///< Width for item
39 const int BACKGROUND_TOP_HEIGHT = 20; ///< Height for item top
40 const int BACKGROUND_BOTTOM_HEIGHT = 15; ///< Height for item bottom
41 const int ICON_HEIGHT = 24; ///< Icon height
42 const int ICON_WIDTH = 24; ///< Icon width
43 const int MARGIN = 5; ///< Icon margin
44 const int LINE_LENGTH = 27; ///< Line length
45 const int MOUSE_PRESS_AREA_WIDTH = 20; ///< Area width for item height toggling
46 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
49 * @var LABEL_MAX_WIDTH
51 * @brief All label's maximum width
53 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - ICON_WIDTH + 130;
55 UserInfo::UserInfo(QWidget *parent)
60 qDebug() << __PRETTY_FUNCTION__;
62 QVBoxLayout *verticalLayout = new QVBoxLayout(this);
63 verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
64 verticalLayout->setSpacing(0);
65 setLayout(verticalLayout);
67 QFormLayout *infoLayout = new QFormLayout();
68 infoLayout->setMargin(0);
69 infoLayout->setSpacing(0);
71 QHBoxLayout *buttonLayout = new QHBoxLayout();
72 buttonLayout->setMargin(0);
73 buttonLayout->setSpacing(0);
75 QLabel *envelopeLabel = new QLabel();
76 envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
77 envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
78 envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
79 QLabel *compassLabel = new QLabel();
80 compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
81 compassLabel->setContentsMargins(0, 0, MARGIN, 0);
82 compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
83 QLabel *clockLabel = new QLabel();
84 clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
85 clockLabel->setContentsMargins(0, 0, MARGIN, 0);
86 clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
88 m_findButton = new ImageButton();
90 m_nameLabel = new QLabel();
91 m_nameLabel->setWordWrap(true);
93 m_statusTextLabel = new QLabel();
94 m_statusTextLabel->setWordWrap(true);
95 m_locationLabel = new QLabel();
96 m_locationLabel->setWordWrap(true);
97 m_updatedLabel = new QLabel();
98 m_updatedLabel->setWordWrap(true);
100 ImageButton *updateFriendsButton = new ImageButton(this, ":/res/images/refresh.png",
101 ":/res/images/refresh_s.png");
102 ImageButton *updateStatusMessageButton = new ImageButton(this, ":/res/images/send_position.png",
103 ":/res/images/send_position_s.png");
105 buttonLayout->addWidget(updateFriendsButton);
106 buttonLayout->addWidget(updateStatusMessageButton);
108 infoLayout->addRow(envelopeLabel, m_statusTextLabel);
109 infoLayout->addRow(compassLabel, m_locationLabel);
110 infoLayout->addRow(clockLabel, m_updatedLabel);
112 verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
113 verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
114 verticalLayout->addLayout(infoLayout);
115 verticalLayout->addLayout(buttonLayout);
117 connect(updateStatusMessageButton,SIGNAL(clicked()),
118 this,SLOT(messageUpdate()));
120 connect(updateFriendsButton,SIGNAL(clicked()),
121 this, SIGNAL(refreshUserData()));
123 connect(m_findButton, SIGNAL(clicked()),
124 this, SLOT(findButtonClicked()));
126 setFixedWidth(BACKGROUND_WIDTH);
128 this->setFont(NOKIA_FONT_SMALL);
129 m_nameLabel->setFont(NOKIA_FONT_NORMAL);
130 QPalette itemPalette = palette();
131 itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
132 setPalette(itemPalette);
133 QPalette namePalette = m_nameLabel->palette();
134 namePalette.setColor(QPalette::Foreground, Qt::white);
135 m_nameLabel->setPalette(namePalette);
137 m_backgroundTopImage.load(":/res/images/list_item_top.png");
138 m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
139 m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
141 restoreUnsendMessage();
144 UserInfo::~UserInfo()
146 qDebug() << __PRETTY_FUNCTION__;
148 QSettings settings(DIRECTORY_NAME, FILE_NAME);
150 if (!m_backupMessage.isEmpty()) {
151 settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
152 settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
154 settings.remove(USER_UNSEND_MESSAGE);
155 settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
159 void UserInfo::setAddress(const QString &address)
161 qDebug() << __PRETTY_FUNCTION__;
163 m_locationLabel->setText(address);
166 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
168 qDebug() << __PRETTY_FUNCTION__;
170 m_coordinates = coordinates;
173 void UserInfo::setMessageText(const QString &text)
175 qDebug() << __PRETTY_FUNCTION__;
177 m_messageText = text;
178 m_expandedMessageText.clear();
180 for(int i=0;i < text.length();i++) {
181 if(fontMetrics().width(temp.append(text.at(i))) > 170) {
183 if(temp.startsWith(QString(" ")))
186 m_expandedMessageText.append(temp);
190 m_expandedMessageText.append(temp);
194 void UserInfo::setProfileImage(const QPixmap &image)
196 qDebug() << __PRETTY_FUNCTION__;
199 m_findButton->setButtonIcon(image);
202 void UserInfo::setTime(const QString &time)
204 qDebug() << __PRETTY_FUNCTION__;
206 m_updatedLabel->setText(time);
209 void UserInfo::setUserName(const QString &name)
211 qDebug() << __PRETTY_FUNCTION__;
217 void UserInfo::setText(bool expanded)
219 qDebug() << __PRETTY_FUNCTION__;
222 m_statusTextLabel->setText(m_expandedMessageText);
225 m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
226 m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
231 void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
233 qDebug() << __PRETTY_FUNCTION__;
235 m_backupMessage = status;
236 m_backupFacebookPublishPolicity = publish;
239 void UserInfo::clearUpdateLocationDialogData()
241 qDebug() << __PRETTY_FUNCTION__;
243 m_backupMessage.clear();
244 m_backupFacebookPublishPolicity = false;
247 void UserInfo::findButtonClicked()
249 qDebug() << __PRETTY_FUNCTION__;
251 emit findUser(m_coordinates);
254 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
256 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
258 if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
259 (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
271 void UserInfo::mousePressEvent(QMouseEvent *event)
273 qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
275 m_mousePosition = event->pos();
278 void UserInfo::messageUpdate()
280 qDebug() << __PRETTY_FUNCTION__;
282 delete m_updateLocation;
283 m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
286 connect(this, SIGNAL(reverseGeoReady(QString)),
287 m_updateLocation, SLOT(setAddress(QString)));
289 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
290 this, SIGNAL(statusUpdate(QString,bool)));
292 connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
293 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
295 connect(m_updateLocation, SIGNAL(finished(int)),
296 this, SLOT(updateLocationDialogFinished(int)));
298 m_updateLocation->show();
300 emit requestReverseGeo();
303 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
305 qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
307 QPainter painter(this);
309 QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
310 ///< @todo Overlaps with topRect?
311 QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
312 this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
313 ///< @todo Overlaps with middleRect
314 QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
315 BACKGROUND_BOTTOM_HEIGHT);
317 painter.drawPixmap(topRect, m_backgroundTopImage);
318 painter.drawPixmap(middleRect, m_backgroundMiddleImage);
319 painter.drawPixmap(bottomRect, m_backgroundBottomImage);
322 void UserInfo::restoreUnsendMessage()
324 qDebug() << __PRETTY_FUNCTION__;
326 QSettings settings(DIRECTORY_NAME, FILE_NAME);
327 m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
328 m_backupFacebookPublishPolicity =
329 settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
332 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
334 qDebug() << __PRETTY_FUNCTION__;
336 QFontMetrics labelMetrics = label->fontMetrics();
337 QString textParam = text;
338 int index = textParam.indexOf('\n');
339 int textWidth = fontMetrics().width(textParam);
342 textParam.truncate(index);
343 textParam.append("...");
345 if (textWidth > 250) ///< @todo magic number
346 textParam.insert(LINE_LENGTH, QString("\n"));
348 return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
351 void UserInfo::updateLocationDialogFinished(int reason)
353 qDebug() << __PRETTY_FUNCTION__;
357 if (m_updateLocation) {
358 disconnect(this, SIGNAL(reverseGeoReady(QString)),
359 m_updateLocation, SLOT(setAddress(QString)));
361 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
362 this, SIGNAL(statusUpdate(QString,bool)));
364 disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
365 this, SLOT(backupUpdateLocationDialogData(QString,bool)));
367 disconnect(m_updateLocation, SIGNAL(finished(int)),
368 this, SLOT(updateLocationDialogFinished(int)));
370 m_updateLocation->deleteLater();
371 m_updateLocation = 0;