Moved location update logic to new class called UpdateLocation.
[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        Ville Tiensuu - ville.tiensuu@ixonos.com
10
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.
14
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.
19
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,
23    USA.
24 */
25
26 #include <QFormLayout>
27 #include <QLabel>
28 #include <QMouseEvent>
29 #include <QPainter>
30 #include <QSettings>
31 #include <QVBoxLayout>
32
33 #include "common.h"
34 #include "imagebutton.h"
35 #include "textmodifier.h"
36 #include "user/user.h"
37
38 #include "userinfo.h"
39
40 const int BACKGROUND_BOTTOM_HEIGHT = 15;
41 const int BACKGROUND_TOP_HEIGHT = 20;
42 const int BACKGROUND_WIDTH = 368;
43 const int ICON_HEIGHT = 24;
44 const int ICON_WIDTH = 24;
45 const int MARGIN = 5;
46 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - ICON_WIDTH - 5 * MARGIN;
47
48 UserInfo::UserInfo(QWidget *parent)
49     : QWidget(parent),
50       m_expanded(false)
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
55     verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
56     verticalLayout->setSpacing(0);
57     setLayout(verticalLayout);
58
59     QFormLayout *infoLayout = new QFormLayout();
60     infoLayout->setMargin(0);
61     infoLayout->setSpacing(0);
62
63     QLabel *envelopeLabel = new QLabel();
64     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
65     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
66     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
67     QLabel *compassLabel = new QLabel();
68     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
69     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
70     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
71     QLabel *clockLabel = new QLabel();
72     clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
73     clockLabel->setContentsMargins(0, 0, MARGIN, 0);
74     clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
75
76     m_avatar = new ImageButton();
77
78     m_nameLabel = new QLabel();
79
80     m_statusTextLabel = new QLabel();
81     m_statusTextLabel->setWordWrap(true);
82     m_statusTextLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
83     m_locationLabel = new QLabel();
84     m_locationLabel->setWordWrap(true);
85     m_locationLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
86     m_updatedLabel = new QLabel();
87     m_updatedLabel->setWordWrap(true);
88     m_updatedLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
89
90     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
91     infoLayout->addRow(compassLabel, m_locationLabel);
92     infoLayout->addRow(clockLabel, m_updatedLabel);
93
94     verticalLayout->addWidget(m_avatar, 0, Qt::AlignHCenter);
95     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
96     verticalLayout->addLayout(infoLayout);
97
98     connect(m_avatar, SIGNAL(clicked()),
99             this, SLOT(findButtonClicked()));
100
101     setFixedWidth(BACKGROUND_WIDTH);
102
103     this->setFont(NOKIA_FONT_SMALL);
104     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
105     QPalette itemPalette = palette();
106     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
107     setPalette(itemPalette);
108     QPalette namePalette = m_nameLabel->palette();
109     namePalette.setColor(QPalette::Foreground, Qt::white);
110     m_nameLabel->setPalette(namePalette);
111
112     m_backgroundTopImage.load(":/res/images/list_item_top.png");
113     m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
114     m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
115 }
116
117 UserInfo::~UserInfo()
118 {
119     qDebug() << __PRETTY_FUNCTION__;
120 }
121
122 void UserInfo::collapse()
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125
126     setExpanded(false);
127 }
128
129 void UserInfo::findButtonClicked()
130 {
131     qDebug() << __PRETTY_FUNCTION__;
132
133     emit findUser(m_coordinates);
134 }
135
136 void UserInfo::mousePressEvent(QMouseEvent *event)
137 {
138     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
139
140     m_mousePosition = event->pos();
141 }
142
143 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
144 {
145     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
146
147     const int MOUSE_PRESS_AREA_HEIGHT = 20;
148     const int MOUSE_PRESS_AREA_WIDTH = 20;
149
150     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH)
151         && (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
152         if (m_expanded) {
153             setExpanded(false);
154             m_expanded = false;
155         }
156         else {
157             setExpanded(true);
158             m_expanded = true;
159         }
160     }
161 }
162
163 void UserInfo::paintEvent(QPaintEvent *event)
164 {
165     qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
166
167     QPainter painter(this);
168
169     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
170
171     QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH,
172                              this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
173
174     QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH,
175                              BACKGROUND_BOTTOM_HEIGHT);
176
177     painter.drawPixmap(topRect, m_backgroundTopImage);
178     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
179     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
180 }
181
182 void UserInfo::setAddress(const QString &address)
183 {
184     qDebug() << __PRETTY_FUNCTION__;
185
186     m_locationLabel->setText(address);
187 }
188
189 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
190 {
191     qDebug() << __PRETTY_FUNCTION__;
192
193     m_coordinates = coordinates;
194 }
195
196 void UserInfo::setMessageText(const QString &text)
197 {
198     qDebug() << __PRETTY_FUNCTION__;
199
200     m_messageText = TextModifier::splitLongWords(m_statusTextLabel->fontMetrics(), text,
201                                                  LABEL_MAX_WIDTH);
202
203     setExpanded(false);
204 }
205
206 void UserInfo::setProfileImage(const QPixmap &image)
207 {
208     qDebug() << __PRETTY_FUNCTION__;
209
210     if(!image.isNull())
211         m_avatar->setButtonIcon(image);
212 }
213
214 void UserInfo::setExpanded(bool expanded)
215 {
216     qDebug() << __PRETTY_FUNCTION__;
217
218     if (expanded) {
219         m_statusTextLabel->setText(m_messageText);
220     } else {
221         m_statusTextLabel->setText(TextModifier::shortenText(m_statusTextLabel->fontMetrics(),
222                                                              m_messageText, LABEL_MAX_WIDTH));
223     }
224 }
225
226 void UserInfo::setTime(const QString &time)
227 {
228     qDebug() << __PRETTY_FUNCTION__;
229
230     m_updatedLabel->setText(time);
231 }
232
233 void UserInfo::setUserName(const QString &name)
234 {
235     qDebug() << __PRETTY_FUNCTION__;
236
237     m_userName = name;
238
239     m_nameLabel->setText(TextModifier::shortenText(m_nameLabel->fontMetrics(), m_userName,
240                                                    LABEL_MAX_WIDTH));
241 }