Moved UpdateLocationDialog to be managed by MainWindow.
[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 //    restoreUnsendMessage();
117 }
118
119 UserInfo::~UserInfo()
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122 }
123
124 void UserInfo::collapse()
125 {
126     qDebug() << __PRETTY_FUNCTION__;
127
128     setExpanded(false);
129 }
130
131 void UserInfo::findButtonClicked()
132 {
133     qDebug() << __PRETTY_FUNCTION__;
134
135     emit findUser(m_coordinates);
136 }
137
138 void UserInfo::mousePressEvent(QMouseEvent *event)
139 {
140     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
141
142     m_mousePosition = event->pos();
143 }
144
145 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
146 {
147     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
148
149     const int MOUSE_PRESS_AREA_HEIGHT = 20;
150     const int MOUSE_PRESS_AREA_WIDTH = 20;
151
152     if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH)
153         && (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
154         if (m_expanded) {
155             setExpanded(false);
156             m_expanded = false;
157         }
158         else {
159             setExpanded(true);
160             m_expanded = true;
161         }
162     }
163 }
164
165 void UserInfo::paintEvent(QPaintEvent *event)
166 {
167     qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
168
169     QPainter painter(this);
170
171     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
172
173     QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH,
174                              this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
175
176     QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH,
177                              BACKGROUND_BOTTOM_HEIGHT);
178
179     painter.drawPixmap(topRect, m_backgroundTopImage);
180     painter.drawPixmap(middleRect, m_backgroundMiddleImage);
181     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
182 }
183
184 void UserInfo::setAddress(const QString &address)
185 {
186     qDebug() << __PRETTY_FUNCTION__;
187
188     m_locationLabel->setText(address);
189 }
190
191 void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
192 {
193     qDebug() << __PRETTY_FUNCTION__;
194
195     m_coordinates = coordinates;
196 }
197
198 void UserInfo::setMessageText(const QString &text)
199 {
200     qDebug() << __PRETTY_FUNCTION__;
201
202     m_messageText = TextModifier::splitLongWords(m_statusTextLabel->fontMetrics(), text,
203                                                  LABEL_MAX_WIDTH);
204
205     setExpanded(false);
206 }
207
208 void UserInfo::setProfileImage(const QPixmap &image)
209 {
210     qDebug() << __PRETTY_FUNCTION__;
211
212     if(!image.isNull())
213         m_avatar->setButtonIcon(image);
214 }
215
216 void UserInfo::setExpanded(bool expanded)
217 {
218     qDebug() << __PRETTY_FUNCTION__;
219
220     if (expanded) {
221         m_statusTextLabel->setText(m_messageText);
222     } else {
223         m_statusTextLabel->setText(TextModifier::shortenText(m_statusTextLabel->fontMetrics(),
224                                                              m_messageText, LABEL_MAX_WIDTH));
225     }
226 }
227
228 void UserInfo::setTime(const QString &time)
229 {
230     qDebug() << __PRETTY_FUNCTION__;
231
232     m_updatedLabel->setText(time);
233 }
234
235 void UserInfo::setUserName(const QString &name)
236 {
237     qDebug() << __PRETTY_FUNCTION__;
238
239     m_userName = name;
240
241     m_nameLabel->setText(TextModifier::shortenText(m_nameLabel->fontMetrics(), m_userName,
242                                                    LABEL_MAX_WIDTH));
243 }