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