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