backup
[situare] / src / ui / userinfo.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
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
10    Situare is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License
12    version 2 as published by the Free Software Foundation.
13
14    Situare is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with Situare; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22    USA.
23 */
24
25 #include "common.h"
26 #include "imagebutton.h"
27 #include "../user/user.h"
28 #include "userinfo.h"
29
30 const int BACKGROUND_WIDTH = 240; ///< Width for item
31 const int BACKGROUND_TOP_HEIGHT = 16; ///< Height for item top
32 const int BACKGROUND_BOTTOM_HEIGHT = 15; ///< Height for item bottom
33 //const QColor COLOR_GRAY = QColor(152, 152, 152); ///< Gray color
34 //const QFont NOKIA_FONT_NORMAL = QFont("Nokia Sans", 18, QFont::Normal); ///< Normal font
35 //const QFont NOKIA_FONT_SMALL = QFont("Nokia Sans", 13, QFont::Normal);  ///< Small font
36 const int ICON_HEIGHT = 24; ///< Icon height
37 const int ICON_WIDTH = 24;  ///< Icon width
38 const int IMAGE_HEIGHT = 60;    ///< Profile image height
39 const int IMAGE_WIDTH = 60;     ///< Profile image width
40 const int MARGIN = 5; ///< Icon margin
41 const int MOUSE_PRESS_AREA_WIDTH = 20;  ///< Area width for item height toggling
42 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
43 const QString USER_UNSEND_MESSAGE = "User_unsend_message_content";
44 const QString USER_UNSEND_MESSAGE_PUBLISH_POLICITY = "User_unsend_message_publish";
45
46 /**
47 * @var LABEL_MAX_WIDTH
48 *
49 * @brief All label's maximum width
50 */
51 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - ICON_WIDTH + 130;
52
53 UserInfo::UserInfo(QWidget *parent)
54     : QWidget(parent),
55       m_expanded(false)
56 {
57     qDebug() << __PRETTY_FUNCTION__;
58
59     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
60     verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
61     verticalLayout->setSpacing(0);
62     setLayout(verticalLayout);
63
64     QFormLayout *infoLayout = new QFormLayout();
65     infoLayout->setMargin(0);
66     infoLayout->setSpacing(0);
67
68     QHBoxLayout *buttonLayout = new QHBoxLayout();
69     buttonLayout->setMargin(0);
70     buttonLayout->setSpacing(0);
71
72     QLabel *envelopeLabel = new QLabel();
73     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
74     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
75     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
76     QLabel *compassLabel = new QLabel();
77     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
78     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
79     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
80     QLabel *clockLabel = new QLabel();
81     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
82     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
83     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
84
85     m_findButton = new ImageButton();
86
87     m_nameLabel = new QLabel();
88     m_nameLabel->setWordWrap(true);
89
90     m_statusTextLabel = new QLabel();
91     m_statusTextLabel->setWordWrap(true);
92     m_locationLabel = new QLabel();
93     m_locationLabel->setWordWrap(true);
94     m_updatedLabel = new QLabel();
95     m_updatedLabel->setWordWrap(true);
96
97     m_messageDialog = new UpdateLocationDialog(this);
98     m_messageDialog->hide();
99
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");
104
105     buttonLayout->addWidget(updateFriendsButton);
106     buttonLayout->addWidget(updateStatusMessageButton);
107
108     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
109     infoLayout->addRow(compassLabel, m_locationLabel);
110     infoLayout->addRow(clockLabel, m_updatedLabel);
111
112     verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
113     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
114     verticalLayout->addLayout(infoLayout);
115     verticalLayout->addLayout(buttonLayout);
116
117     connect(updateStatusMessageButton,SIGNAL(clicked()),
118             this,SLOT(messageUpdate()));
119     connect(updateFriendsButton,SIGNAL(clicked()),
120             this, SIGNAL(refreshUserData()));
121     connect(m_findButton, SIGNAL(clicked()),
122         this, SLOT(findButtonClicked()));
123
124     setFixedWidth(BACKGROUND_WIDTH);
125
126     this->setFont(NOKIA_FONT_SMALL);
127     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
128     QPalette itemPalette = palette();
129     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
130     setPalette(itemPalette);
131     QPalette namePalette = m_nameLabel->palette();
132     namePalette.setColor(QPalette::Foreground, Qt::white);
133     m_nameLabel->setPalette(namePalette);
134
135     m_backgroundTopImage.load(":/res/images/user_info_item_top.png");
136     m_backgroundMiddleImage.load(":/res/images/user_info_item_middle.png");
137     m_backgroundBottomImage.load(":/res/images/user_info_item_bottom.png");
138
139     restoreUnsendMessage();
140 }
141
142 UserInfo::~UserInfo()
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145
146     if (!m_backupMessage.isEmpty()) {
147         QSettings settings(DIRECTORY_NAME, FILE_NAME);
148         settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
149         settings.setValue(USER_UNSEND_MESSAGE_PUBLISH_POLICITY, m_backupFacebookPublishPolicity);
150     }
151
152 }
153
154 void UserInfo::restoreUnsendMessage()
155 {
156     QSettings settings(DIRECTORY_NAME, FILE_NAME);
157     m_backupMessage = settings.value(USER_UNSEND_MESSAGE, "").toString();
158     m_backupFacebookPublishPolicity =
159             settings.value(USER_UNSEND_MESSAGE_PUBLISH_POLICITY, false).toBool();
160
161 }
162
163 void UserInfo::setAddress(const QString &address)
164 {
165     qDebug() << __PRETTY_FUNCTION__;
166
167     m_locationLabel->setText(address);
168 }
169
170 void UserInfo::setCoordinates(const QPointF &coordinates)
171 {
172     qDebug() << __PRETTY_FUNCTION__;
173
174     m_coordinates = coordinates;
175 }
176
177 void UserInfo::setMessageText(const QString &text)
178 {
179     qDebug() << __PRETTY_FUNCTION__ << "VIESTI LAITETAAN TALTEEN";
180
181     m_messageText = text;
182     setText(false);
183 }
184
185 void UserInfo::setProfileImage(const QPixmap &image)
186 {
187     qDebug() << __PRETTY_FUNCTION__;
188
189     m_findButton->setButtonIcon(image);
190 }
191
192 void UserInfo::setTime(const QString &time)
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196     m_updatedLabel->setText(time);
197 }
198
199 void UserInfo::setUserName(const QString &name)
200 {
201     qDebug() << __PRETTY_FUNCTION__;
202
203     m_userName = name;
204     setText(false);
205 }
206
207 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
208 {
209     qDebug() << __PRETTY_FUNCTION__;
210
211     QFontMetrics labelMetrics = label->fontMetrics();
212
213     QString textParam = text;
214     int index = textParam.indexOf('\n');
215
216     if (index > 0) {
217         textParam.truncate(index);
218         textParam.append("...");
219     }
220
221    return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
222 }
223
224 void UserInfo::setText(bool expanded)
225 {
226     qDebug() << __PRETTY_FUNCTION__;
227
228     if (expanded) {
229         m_nameLabel->setText(m_userName);
230         m_statusTextLabel->setText(m_messageText);
231     }
232     else {
233         m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
234         m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
235                                                LABEL_MAX_WIDTH));
236     }
237 }
238
239 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
240 {
241     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
242
243     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
244         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
245         if (m_expanded) {
246             setText(false);
247             m_expanded = false;
248         }
249         else {
250             setText(true);
251             m_expanded = true;
252         }
253     }
254 }
255
256 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
257 {
258     qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
259
260     QPainter painter(this);
261
262     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
263     QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
264                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
265     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
266                              BACKGROUND_BOTTOM_HEIGHT);
267
268     painter.drawPixmap(topRect, m_backgroundTopImage);
269     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
270     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
271 }
272
273 void UserInfo::mousePressEvent(QMouseEvent *event)
274 {
275     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
276
277     m_mousePosition = event->pos();
278 }
279
280 void UserInfo::findButtonClicked()
281 {
282     qDebug() << __PRETTY_FUNCTION__;
283
284     emit findUser(m_coordinates);
285 }
286
287 QString UserInfo::messageText() const
288 {
289     return m_messageText;
290 }
291
292 void UserInfo::messageUpdate()
293 {
294     qDebug() << __PRETTY_FUNCTION__;
295
296     m_messageDialog->show();
297     m_messageDialog->m_textEdit->setPlainText(m_backupMessage);
298     m_messageDialog->m_checkBox->setCheckState(Qt::Unchecked);
299
300     if (m_backupFacebookPublishPolicity == true)
301         m_messageDialog->m_checkBox->setCheckState(Qt::Checked);
302
303     emit requestReverseGeo();
304
305     connect(this, SIGNAL(reverseGeoReady(QString)),
306             m_messageDialog, SLOT(setAddress(QString)));
307
308     connect(m_messageDialog, SIGNAL(statusUpdate(QString,bool)),
309             this, SIGNAL(statusUpdate(QString,bool)));
310
311     connect(m_messageDialog, SIGNAL(statusUpdate(QString,bool)),
312             this, SLOT(backupUpdateLocationDialogData(QString,bool)));
313
314     connect(this, SIGNAL(messageUpdatedToSituare()),
315             this, SLOT(clearUpdateLocationDialogData()));
316
317     m_messageDialog->exec();
318 }
319
320 void UserInfo::backupUpdateLocationDialogData(const QString &status, const bool &publish)
321 {
322     qDebug() << __PRETTY_FUNCTION__;
323
324     m_backupMessage = status;
325     m_backupFacebookPublishPolicity = publish;
326 }
327
328 void UserInfo::clearUpdateLocationDialogData()
329 {
330     qDebug() << __PRETTY_FUNCTION__;
331
332     qDebug() << "TEKSTIN SISALTO ENNEN: " << m_backupMessage;
333     m_backupMessage.clear();
334     qDebug() << "TEKSTIN SISALTO JALKEEN: " << m_backupMessage;
335     m_backupFacebookPublishPolicity = false;
336 }