Added comments to friendlistitem, friendlistview and listviewscreen.
[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 FriendListItem::FriendListItem(QWidget *parent)
41     : QWidget(parent)
42     , m_expanded(false)
43 {
44     QVBoxLayout *layout = new QVBoxLayout(this);
45     this->setLayout(layout);
46
47     QHBoxLayout *topLayout = new QHBoxLayout();
48     topLayout->setMargin(0);
49     topLayout->setSpacing(0);
50
51     QHBoxLayout *bottomLayout = new QHBoxLayout();
52     bottomLayout->setMargin(0);
53     bottomLayout->setSpacing(0);
54     m_infoWidget = new QWidget(this);
55
56     QFormLayout *infoLayout = new QFormLayout();
57     infoLayout->setMargin(0);
58     infoLayout->setSpacing(0);
59     m_infoWidget->setLayout(infoLayout);
60
61     QLabel *clockLabel = new QLabel(this);
62     clockLabel->setPixmap(QPixmap(CLOCK_PATH));
63     clockLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
64     QLabel *envelopeLabel = new QLabel(this);
65     envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
66     envelopeLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
67     QLabel *compassLabel = new QLabel(this);
68     compassLabel->setPixmap(QPixmap(COMPASS_PATH));
69     compassLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
70
71     m_imageLabel = new QLabel(this);
72     m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
73     m_nameLabel = new QLabel("", this);
74     m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
75     m_updatedLabel = new QLabel("", this);
76     m_updatedLabel->setWordWrap(true);
77     m_statusTextLabel = new QLabel("", this);
78     m_statusTextLabel->setWordWrap(true);
79     m_locationLabel = new QLabel("", this);
80     m_locationLabel->setWordWrap(true);
81
82     infoLayout->addRow(clockLabel, m_updatedLabel);
83     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
84     infoLayout->addRow(compassLabel, m_locationLabel);
85
86     topLayout->addWidget(m_imageLabel);
87     topLayout->addWidget(m_nameLabel);
88
89     bottomLayout->addSpacerItem(new QSpacerItem(IMAGE_WIDTH, IMAGE_HEIGHT));
90     bottomLayout->addWidget(m_infoWidget, 1);
91
92     layout->addLayout(topLayout, 0);
93     layout->addLayout(bottomLayout, 1);
94
95     this->setObjectName("listItem");
96     m_infoWidget->setObjectName("infoWidget");
97     m_nameLabel->setObjectName("nameLabel");
98
99     this->setStyleSheet("#listItem { border-image: url(:/res/images/list_item.png) 20%; " \
100                         "border-width: 20px 14px 16px 14px; } " \
101                         "QLabel { font-size: 13pt; color: #989898; }" \
102                         "#nameLabel { font-size: 18pt; color: #ffffff }");
103
104     this->setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
105     this->setMaximumSize(ITEM_MAX_WIDTH, ITEM_MAX_HEIGHT);
106 }
107
108 void FriendListItem::setData(const User &user)
109 {
110     m_user = user;
111
112     shortenTexts();
113
114     m_imageLabel->setPixmap(m_user.profileImage());
115     m_nameLabel->setText(m_shortenedName);
116     m_updatedLabel->setText(m_shortenedUpdated);
117     m_statusTextLabel->setText(m_shortenedStatusText);
118     m_locationLabel->setText(m_shortenedLocation);
119
120 }
121
122 void FriendListItem::shortenTexts()
123 {
124     if (m_user.name().length() > MAXIMUM_CHARS) {
125         m_shortenedName = m_user.name().left(MAXIMUM_CHARS-3).append("...");
126     }
127     else {
128          m_shortenedName = m_user.name();
129     }
130     if (m_user.timestamp().length() > MAXIMUM_CHARS) {
131         m_shortenedUpdated = m_user.timestamp().left(MAXIMUM_CHARS-3).append("...");
132     }
133     else {
134         m_shortenedUpdated = m_user.timestamp();
135     }
136     if (m_user.note().length() > MAXIMUM_CHARS) {
137         m_shortenedStatusText = m_user.note().left(MAXIMUM_CHARS-3).append("...");
138     }
139     else {
140         m_shortenedStatusText = m_user.note();
141     }
142     if (m_user.address().length() > MAXIMUM_CHARS) {
143         m_shortenedLocation = m_user.address().left(MAXIMUM_CHARS-3).append("...");
144     }
145     else {
146         m_shortenedLocation = m_user.address();
147     }
148 }
149
150 void FriendListItem::toggleHeight()
151 {
152     if (m_expanded) {
153         m_nameLabel->setText(m_shortenedName);
154         m_updatedLabel->setText(m_shortenedUpdated);
155         m_statusTextLabel->setText(m_shortenedStatusText);
156         m_locationLabel->setText(m_shortenedLocation);
157         m_expanded = false;
158
159     }
160     else {
161         m_nameLabel->setText(m_user.name());
162         m_updatedLabel->setText(m_user.timestamp());
163         m_statusTextLabel->setText(m_user.note());
164         m_locationLabel->setText(m_user.address());
165         m_expanded = true;
166     }
167 }
168
169 void FriendListItem::mousePressEvent(QMouseEvent *event)
170 {
171     m_mousePosition = event->pos();
172 }
173
174 void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
175 {
176     qDebug() << __PRETTY_FUNCTION__;
177
178     if (event->pos() == m_mousePosition)
179         toggleHeight();
180 }
181
182 void FriendListItem::paintEvent(QPaintEvent *)
183 {
184     QStyleOption option;
185     option.init(this);
186
187     QPainter painter(this);
188     style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
189 }