Modified FriendListItem constructor.
[situare] / src / ui / friendlistitem.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QVBoxLayout>
23 #include <QPushButton>
24 #include <QPainter>
25 #include <QDebug>
26 #include <QPaintEvent>
27 #include <QLabel>
28 #include <QPixmap>
29 #include <QStateMachine>
30 #include <QAbstractTransition>
31 #include <QPropertyAnimation>
32 #include <QSignalTransition>
33 #include <QFontMetrics>
34 #include <QFormLayout>
35 #include <QSpacerItem>
36
37 #include "friendlistitem.h"
38 #include "user/user.h"
39
40 const int IMAGE_WIDTH = 60;
41 const int IMAGE_HEIGHT = 60;
42
43 const int ITEM_MIN_WIDTH = 368;
44 const int ITEM_MAX_WIDTH = 368;
45 const int ITEM_MIN_HEIGHT = 141;
46 const int ITEM_MAX_HEIGHT = 240;
47 const int ICON_MARGIN = 5;
48
49 const QString BACKGROUND_PATH = QString(":/res/images/list_item.png");
50 const QString CLOCK_PATH = QString(":/res/images/clock.png");
51 const QString ENVELOPE_PATH = QString(":/res/images/envelope.png");
52 const QString COMPASS_PATH = QString(":/res/images/compass.png");
53
54 const int MAXIMUM_CHARS = 32;
55
56 FriendListItem::FriendListItem(QWidget *parent)
57     : QWidget(parent)
58     , m_expanded(false)
59 {
60     QVBoxLayout *layout = new QVBoxLayout(this);
61     this->setLayout(layout);
62
63     QHBoxLayout *topLayout = new QHBoxLayout(this);
64     topLayout->setMargin(0);
65     topLayout->setSpacing(0);
66
67     QHBoxLayout *bottomLayout = new QHBoxLayout(this);
68     bottomLayout->setMargin(0);
69     bottomLayout->setSpacing(0);
70
71     m_infoWidget = new QWidget(this);
72
73     QFormLayout *infoLayout = new QFormLayout(this);
74     infoLayout->setMargin(0);
75     infoLayout->setSpacing(0);
76     m_infoWidget->setLayout(infoLayout);
77
78     QLabel *clockLabel = new QLabel(this);
79     clockLabel->setPixmap(QPixmap(CLOCK_PATH));
80     clockLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
81     QLabel *envelopeLabel = new QLabel(this);
82     envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
83     envelopeLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
84     QLabel *compassLabel = new QLabel(this);
85     compassLabel->setPixmap(QPixmap(COMPASS_PATH));
86     compassLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
87
88     m_imageLabel = new QLabel(this);
89     m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
90     m_nameLabel = new QLabel("", this);
91     m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
92     m_updatedLabel = new QLabel("", this);
93     m_updatedLabel->setWordWrap(true);
94     m_statusTextLabel = new QLabel("", this);
95     m_statusTextLabel->setWordWrap(true);
96     m_locationLabel = new QLabel("", this);
97     m_locationLabel->setWordWrap(true);
98
99     infoLayout->addRow(clockLabel, m_updatedLabel);
100     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
101     infoLayout->addRow(compassLabel, m_locationLabel);
102
103     topLayout->addWidget(m_imageLabel);
104     topLayout->addWidget(m_nameLabel);
105
106     QLabel *tmp = new QLabel(this);
107     bottomLayout->addSpacerItem(new QSpacerItem(IMAGE_WIDTH, IMAGE_HEIGHT));
108     bottomLayout->addWidget(m_infoWidget, 1);
109
110     layout->addLayout(topLayout, 0);
111     layout->addLayout(bottomLayout, 1);
112
113     this->setObjectName("listItem");
114     m_infoWidget->setObjectName("infoWidget");
115     m_nameLabel->setObjectName("nameLabel");
116     this->setStyleSheet("#listItem { border-image: url(:/res/images/list_item.png) 20%; " \
117                         "border-width: 20px 14px 16px 14px; } " \
118                         "QLabel { font-size: 13pt; color: #989898; }" \
119                         "#nameLabel { font-size: 18pt; color: #ffffff }");
120
121     this->setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
122     this->setMaximumSize(ITEM_MAX_WIDTH, ITEM_MAX_HEIGHT);
123 }
124
125 void FriendListItem::setData(const User &user)
126 {
127     m_user = user;
128
129     shortenTexts();
130
131     m_imageLabel->setPixmap(QPixmap(":/res/images/dummy_Avatar.png"));
132     m_nameLabel->setText(m_shortenedName);
133     m_updatedLabel->setText(m_shortenedUpdated);
134     m_statusTextLabel->setText(m_shortenedStatusText);
135     m_locationLabel->setText(m_shortenedLocation);
136     m_image = user.profileImageUrl().toString();
137
138 }
139
140 void FriendListItem::shortenTexts()
141 {
142     if (m_user.name().length() > MAXIMUM_CHARS) {
143         m_shortenedName = m_user.name().left(MAXIMUM_CHARS-3).append("...");
144     }
145     else {
146          m_shortenedName = m_user.name();
147     }
148     if (m_user.timestamp().length() > MAXIMUM_CHARS) {
149         m_shortenedUpdated = m_user.timestamp().left(MAXIMUM_CHARS-3).append("...");
150     }
151     else {
152         m_shortenedUpdated = m_user.timestamp();
153     }
154     if (m_user.note().length() > MAXIMUM_CHARS) {
155         m_shortenedStatusText = m_user.note().left(MAXIMUM_CHARS-3).append("...");
156     }
157     else {
158         m_shortenedStatusText = m_user.note();
159     }
160     if (m_user.address().length() > MAXIMUM_CHARS) {
161         m_shortenedLocation = m_user.address().left(MAXIMUM_CHARS-3).append("...");
162     }
163     else {
164         m_shortenedLocation = m_user.address();
165     }
166 }
167
168 void FriendListItem::toggleHeight()
169 {
170     if (m_expanded) {
171         m_nameLabel->setText(m_shortenedName);
172         m_updatedLabel->setText(m_shortenedUpdated);
173         m_statusTextLabel->setText(m_shortenedStatusText);
174         m_locationLabel->setText(m_shortenedLocation);
175         m_expanded = false;
176
177     }
178     else {
179         m_nameLabel->setText(m_user.name());
180         m_updatedLabel->setText(m_user.timestamp());
181         m_statusTextLabel->setText(m_user.note());
182         m_locationLabel->setText(m_user.address());
183         m_expanded = true;
184     }
185 }
186
187 void FriendListItem::mousePressEvent(QMouseEvent *event)
188 {
189     m_mousePosition = event->pos();
190 }
191
192 void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196     if (event->pos() == m_mousePosition)
197         toggleHeight();
198 }
199
200 void FriendListItem::paintEvent(QPaintEvent *)
201 {
202     QStyleOption option;
203     option.init(this);
204
205     QPainter painter(this);
206     style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
207 }