Added one comment line
[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 "imagebutton.h"
26 #include "../user/user.h"
27 #include "userinfo.h"
28
29 const int BACKGROUND_TOP_HEIGHT = 20;
30 const int BACKGROUND_BOTTOM_HEIGHT = 15;
31 const QColor COLOR_GRAY = QColor(152, 152, 152);    ///< Gray color
32 const QFont NOKIA_FONT_NORMAL = QFont("Nokia Sans", 18, QFont::Normal);    ///< Normal font
33 const QFont NOKIA_FONT_SMALL = QFont("Nokia Sans", 13, QFont::Normal);     ///< Small font
34 const int ICON_HEIGHT = 24;     ///< Icon height
35 const int ICON_WIDTH = 24;       ///< Icon width
36 const int IMAGE_HEIGHT = 60; ///< Friend image height
37 const int IMAGE_WIDTH = 60;  ///< Friend image width
38 const int ITEM_MIN_WIDTH = 250;  ///< Minimum width for item
39 const int MARGIN = 5;   ///< Icon margin
40 const int MOUSE_PRESS_AREA_WIDTH = 20;  ///< Area width for item height toggling
41 const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
42
43 /**
44 * @var LABEL_MAX_WIDTH
45 *
46 * @brief All label's maximum width
47 */
48 const int LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - ICON_WIDTH;
49
50 /**
51 * @brief Name label's maximum width
52 *
53 * @var NAME_LABEL_MAX_WIDTH
54 */
55 const int NAME_LABEL_MAX_WIDTH = LABEL_MAX_WIDTH + 30;
56
57 UserInfo::UserInfo(QWidget *parent)
58     : QWidget(parent)
59         , m_expanded(false)
60 {
61     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
62     verticalLayout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
63     verticalLayout->setSpacing(0);
64     setLayout(verticalLayout);
65
66     QFormLayout *infoLayout = new QFormLayout(this);
67     infoLayout->setMargin(0);
68     infoLayout->setSpacing(0);
69
70     QHBoxLayout *buttonLayout = new QHBoxLayout(this);
71     buttonLayout->setMargin(0);
72     buttonLayout->setSpacing(0);
73
74     QLabel *clockLabel = new QLabel();
75     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
76     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
77     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
78     QLabel *envelopeLabel = new QLabel();
79     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
80     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
81     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
82     QLabel *compassLabel = new QLabel();
83     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
84     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
85     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
86
87     m_imageLabel = new QLabel();
88     m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
89     m_imageLabel->setAlignment(Qt::AlignHCenter);
90
91     m_nameLabel = new QLabel();
92     m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
93     m_nameLabel->setAlignment(Qt::AlignHCenter);
94
95     m_updatedLabel = new QLabel();
96     m_updatedLabel->setWordWrap(true);
97     m_statusTextLabel = new QLabel();
98     m_statusTextLabel->setWordWrap(true);
99     m_locationLabel = new QLabel();
100     m_locationLabel->setWordWrap(true);
101
102     ImageButton *updateFriendsButton = new ImageButton(this, ":/res/images/refresh.png",
103                                                              ":/res/images/refresh_s.png");
104     ImageButton *updateStatusMessageButton = new ImageButton(this, ":/res/images/send_position.png",
105                                                                    ":/res/images/send_position_s.png");
106
107     buttonLayout->addWidget(updateFriendsButton);
108     buttonLayout->addWidget(updateStatusMessageButton);
109
110     infoLayout->addRow(compassLabel, m_locationLabel);
111     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
112     infoLayout->addRow(clockLabel, m_updatedLabel);
113
114     verticalLayout->addWidget(m_imageLabel, 0, Qt::AlignHCenter);
115
116     verticalLayout->addWidget(m_nameLabel, 1);
117     verticalLayout->addLayout(infoLayout);
118     verticalLayout->addLayout(buttonLayout, 2);
119
120     connect(updateStatusMessageButton,SIGNAL(clicked()),
121             this,SLOT(messageUpdate()));
122     connect(updateFriendsButton,SIGNAL(clicked()),
123             this, SIGNAL(refreshUserData()));
124
125     this->setFont(NOKIA_FONT_SMALL);
126     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
127     QPalette itemPalette = palette();
128     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
129     setPalette(itemPalette);
130     QPalette namePalette = m_nameLabel->palette();
131     namePalette.setColor(QPalette::Foreground, Qt::white);
132     m_nameLabel->setPalette(namePalette);
133
134     m_backgroundTopImage.load(":/res/images/list_item_top.png");
135     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
136     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
137
138     this->setMinimumWidth(ITEM_MIN_WIDTH);
139 }
140
141 void UserInfo::setUserName(const QString &name)
142 {
143     qDebug() << __PRETTY_FUNCTION__;
144
145     m_userName = name;
146     setText(false);
147 }
148
149 void UserInfo::setAvatar(const QPixmap &image)
150 {
151     qDebug() << __PRETTY_FUNCTION__;
152
153     m_imageLabel->setPixmap(image);
154 }
155
156 void UserInfo::setMessageText(const QString &text)
157 {
158     qDebug() << __PRETTY_FUNCTION__;
159
160     m_messageText = text;
161     setText(false);
162 }
163
164 void UserInfo::setAddress(const QString &addr)
165 {
166     qDebug() << __PRETTY_FUNCTION__;
167
168     m_locationLabel->setText(addr);
169 }
170
171 void UserInfo::setTime(const QString &tim)
172 {
173     qDebug() << __PRETTY_FUNCTION__;
174
175     m_updatedLabel->setText(tim);
176 }
177
178 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
179 {
180     qDebug() << __PRETTY_FUNCTION__;
181
182     QFontMetrics labelMetrics = label->fontMetrics();
183
184     QString textParam = text;
185     int index = textParam.indexOf('\n');
186
187     if (index > 0) {
188         textParam.truncate(index);
189         textParam.append("...");
190     }
191
192    return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
193 }
194
195 void UserInfo::setText(bool expanded)
196 {
197     qDebug() << __PRETTY_FUNCTION__;
198
199     if (expanded) {
200         m_nameLabel->setText(m_userName);
201         m_statusTextLabel->setText(m_messageText);
202     }
203     else {
204         m_nameLabel->setText(shortenText(m_nameLabel, m_userName, NAME_LABEL_MAX_WIDTH));
205         m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
206                                                LABEL_MAX_WIDTH));
207     }
208 }
209
210 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
211 {
212     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
213
214     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
215         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
216         if (m_expanded) {
217             setText(false);
218             m_expanded = false;
219         }
220         else {
221             setText(true);
222             m_expanded = true;
223         }
224     }
225 }
226 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
227 {
228     qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
229
230     QPainter painter(this);
231
232     QRect topRect = QRect(0, 0, ITEM_MIN_WIDTH, BACKGROUND_TOP_HEIGHT);
233     QRect middleRect = QRect(0, topRect.bottom(), ITEM_MIN_WIDTH,
234                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
235     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_MIN_WIDTH,
236                              BACKGROUND_BOTTOM_HEIGHT);
237
238     painter.drawPixmap(topRect, m_backgroundTopImage);
239     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
240     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
241 }
242
243 void UserInfo::mousePressEvent(QMouseEvent *event)
244 {
245     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
246
247     m_mousePosition = event->pos();
248 }
249
250 void UserInfo::messageUpdate()
251 {
252     qDebug() << __PRETTY_FUNCTION__;
253
254     UpdateLocationDialog updateLocationDialog(this);
255
256     emit requestReverseGeo();
257
258     connect(this, SIGNAL(reverseGeoReady(QString)),
259             &updateLocationDialog, SLOT(setAddress(QString)));
260     connect(&updateLocationDialog, SIGNAL(statusUpdate(QString, bool)),
261             this, SIGNAL(statusUpdate(QString, bool)));
262
263     updateLocationDialog.exec();
264 }
265