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