started feature to show bigger profile images
[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_WIDTH = 240; ///< Width for item
30 const int BACKGROUND_TOP_HEIGHT = 16; ///< Height for item top
31 const int BACKGROUND_BOTTOM_HEIGHT = 15; ///< Height for item bottom
32 const QColor COLOR_GRAY = QColor(152, 152, 152); ///< Gray color
33 const QFont NOKIA_FONT_NORMAL = QFont("Nokia Sans", 18, QFont::Normal); ///< Normal font
34 const QFont NOKIA_FONT_SMALL = QFont("Nokia Sans", 13, QFont::Normal);  ///< Small font
35 const int ICON_HEIGHT = 24; ///< Icon height
36 const int ICON_WIDTH = 24;  ///< Icon width
37 const int IMAGE_HEIGHT = 60;    ///< Profile image height
38 const int IMAGE_WIDTH = 60;     ///< Profile image width
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 = BACKGROUND_WIDTH - 3 * MARGIN - ICON_WIDTH + 130;
49
50 UserInfo::UserInfo(QWidget *parent)
51     : QWidget(parent),
52       m_expanded(false)
53 {
54     qDebug() << __PRETTY_FUNCTION__;
55
56     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
57     verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
58     verticalLayout->setSpacing(0);
59     setLayout(verticalLayout);
60
61     QFormLayout *infoLayout = new QFormLayout();
62     infoLayout->setMargin(0);
63     infoLayout->setSpacing(0);
64
65     QHBoxLayout *buttonLayout = new QHBoxLayout();
66     buttonLayout->setMargin(0);
67     buttonLayout->setSpacing(0);
68
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);
81
82     m_findButton = new ImageButton();
83
84     m_nameLabel = new QLabel();
85     m_nameLabel->setWordWrap(true);
86
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);
93
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");
98
99     buttonLayout->addWidget(updateFriendsButton);
100     buttonLayout->addWidget(updateStatusMessageButton);
101
102     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
103     infoLayout->addRow(compassLabel, m_locationLabel);
104     infoLayout->addRow(clockLabel, m_updatedLabel);
105
106     verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter);
107     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
108     verticalLayout->addLayout(infoLayout);
109     verticalLayout->addLayout(buttonLayout);
110
111     connect(updateStatusMessageButton,SIGNAL(clicked()),
112             this,SLOT(messageUpdate()));
113     connect(updateFriendsButton,SIGNAL(clicked()),
114             this, SIGNAL(refreshUserData()));
115     connect(m_findButton, SIGNAL(clicked()),
116         this, SLOT(findButtonClicked()));
117
118     setFixedWidth(BACKGROUND_WIDTH);
119     //setFixedWidth(500);
120
121     this->setFont(NOKIA_FONT_SMALL);
122     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
123     QPalette itemPalette = palette();
124     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
125     setPalette(itemPalette);
126     QPalette namePalette = m_nameLabel->palette();
127     namePalette.setColor(QPalette::Foreground, Qt::white);
128     m_nameLabel->setPalette(namePalette);
129
130     m_backgroundTopImage.load(":/res/images/user_info_item_top.png");
131     m_backgroundMiddleImage.load(":/res/images/user_info_item_middle.png");
132     m_backgroundBottomImage.load(":/res/images/user_info_item_bottom.png");
133 }
134
135 void UserInfo::setAddress(const QString &address)
136 {
137     qDebug() << __PRETTY_FUNCTION__;
138
139     m_locationLabel->setText(address);
140 }
141
142 void UserInfo::setCoordinates(const QPointF &coordinates)
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145
146     m_coordinates = coordinates;
147 }
148
149 void UserInfo::setMessageText(const QString &text)
150 {
151     qDebug() << __PRETTY_FUNCTION__;
152
153     m_messageText = text;
154     setText(false);
155 }
156
157 void UserInfo::setProfileImage(const QPixmap &image)
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160
161     m_findButton->setButtonIcon(image);
162 }
163
164 void UserInfo::setTime(const QString &time)
165 {
166     qDebug() << __PRETTY_FUNCTION__;
167
168     m_updatedLabel->setText(time);
169 }
170
171 void UserInfo::setUserName(const QString &name)
172 {
173     qDebug() << __PRETTY_FUNCTION__;
174
175     m_userName = name;
176     setText(false);
177 }
178
179 QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
180 {
181     qDebug() << __PRETTY_FUNCTION__;
182
183     QFontMetrics labelMetrics = label->fontMetrics();
184
185     QString textParam = text;
186     int index = textParam.indexOf('\n');
187
188     if (index > 0) {
189         textParam.truncate(index);
190         textParam.append("...");
191     }
192
193    return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
194 }
195
196 void UserInfo::setText(bool expanded)
197 {
198     qDebug() << __PRETTY_FUNCTION__;
199
200     if (expanded) {
201         m_nameLabel->setText(m_userName);
202         m_statusTextLabel->setText(m_messageText);
203     }
204     else {
205         m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
206         m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
207                                                LABEL_MAX_WIDTH));
208     }
209 }
210
211 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
212 {
213     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
214
215     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
216         (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
217         if (m_expanded) {
218             setText(false);
219             m_expanded = false;
220         }
221         else {
222             setText(true);
223             m_expanded = true;
224         }
225     }
226 }
227 void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
228 {
229     qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
230
231     QPainter painter(this);
232
233     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
234     QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
235                              height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
236     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
237                              BACKGROUND_BOTTOM_HEIGHT);
238
239     painter.drawPixmap(topRect, m_backgroundTopImage);
240     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
241     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
242 }
243
244 void UserInfo::mousePressEvent(QMouseEvent *event)
245 {
246     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
247
248     m_mousePosition = event->pos();
249 }
250
251 void UserInfo::findButtonClicked()
252 {
253     qDebug() << __PRETTY_FUNCTION__;
254
255     emit findUser(m_coordinates);
256 }
257
258 void UserInfo::messageUpdate()
259 {
260     qDebug() << __PRETTY_FUNCTION__;
261
262     UpdateLocationDialog updateLocationDialog(this);
263
264     emit requestReverseGeo();
265
266     connect(this, SIGNAL(reverseGeoReady(QString)),
267             &updateLocationDialog, SLOT(setAddress(QString)));
268     connect(&updateLocationDialog, SIGNAL(statusUpdate(QString, bool)),
269             this, SIGNAL(statusUpdate(QString, bool)));
270
271     updateLocationDialog.exec();
272 }
273