Modified FriendListItem::paint.
[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
34 #include "friendlistitem.h"
35 #include "user/user.h"
36
37 const int BACKGROUND_TOP_WIDTH = 368;
38 const int BACKGROUND_TOP_HEIGHT = 20;
39 const int BACKGROUND_MIDDLE_X = 0;
40 const int BACKGROUND_MIDDLE_Y = BACKGROUND_TOP_HEIGHT;
41 const int BACKGROUND_MIDDLE_WIDTH = 368;
42 const int BACKGROUND_MIDDLE_HEIGHT = 106;
43 const int BACKGROUND_BOTTOM_X = 0;
44 const int BACKGROUND_BOTTOM_Y = BACKGROUND_MIDDLE_Y + BACKGROUND_MIDDLE_HEIGHT;
45 const int BACKGROUND_BOTTOM_WIDTH = 368;
46 const int BACKGROUND_BOTTOM_HEIGHT = 15;
47 const int IMAGE_X = 9;
48 const int IMAGE_Y = 0;
49 const int IMAGE_WIDTH = 60;
50 const int IMAGE_HEIGHT = 60;
51 const int CLOCK_X = 75;
52 const int CLOCK_Y = 53;
53 const int ENVELOPE_X = 75;
54 const int ENVELOPE_Y = 77;
55 const int COMPASS_X = 75;
56 const int COMPASS_Y = 101;
57 const int ICON_WIDTH = 24;
58 const int ICON_HEIGHT = 24;
59
60 const QString BACKGROUND_TOP_PATH = QString(":/res/images/list_item_top.png");
61 const QString BACKGROUND_MIDDLE_PATH = QString(":/res/images/list_item_middle.png");
62 const QString BACKGROUND_BOTTOM_PATH = QString(":/res/images/list_item_bottom.png");
63 const QString CLOCK_PATH = QString(":/res/images/clock.png");
64 const QString ENVELOPE_PATH = QString(":/res/images/envelope.png");
65 const QString COMPASS_PATH = QString(":/res/images/compass.png");
66
67 FriendListItem::FriendListItem(QWidget *parent)
68     : QWidget(parent)
69     , m_expanded(false)
70 {
71     QWidget *childWidget = new QWidget(this);
72     QPalette palette = childWidget->palette();
73     palette.setColor(QPalette::Foreground, QColor::fromRgb(152, 152, 152));
74     childWidget->setPalette(palette);
75     QFont font = QFont("Nokia Sans", 13, QFont::Normal);
76     childWidget->setFont(font);
77
78     QGridLayout *infoLayout = new QGridLayout(this);
79     infoLayout->setSpacing(0);
80     infoLayout->setMargin(0);
81     childWidget->setLayout(infoLayout);
82
83     QLabel *clockLabel = new QLabel(this);
84     clockLabel->setPixmap(QPixmap(CLOCK_PATH));
85     QLabel *envelopeLabel = new QLabel(this);
86     envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
87     QLabel *compassLabel = new QLabel(this);
88     compassLabel->setPixmap(QPixmap(COMPASS_PATH));
89
90     m_updatedLabel = new QLabel("", this);
91     m_updatedLabel->setWordWrap(true);
92     m_statusTextLabel = new QLabel("", this);
93     m_statusTextLabel->setWordWrap(true);
94     m_locationLabel = new QLabel("", this);
95     m_locationLabel->setWordWrap(true);
96
97     infoLayout->setColumnStretch(1, 1);
98     infoLayout->setColumnMinimumWidth(0, 28);
99     infoLayout->setRowMinimumHeight(0, 28);
100
101     infoLayout->addWidget(clockLabel, 0, 0, 1, 1, Qt::AlignTop);
102     infoLayout->addWidget(m_updatedLabel, 0, 1);
103     infoLayout->addWidget(envelopeLabel, 1, 0, 1, 1, Qt::AlignTop);
104     infoLayout->addWidget(m_statusTextLabel, 1, 1);
105     infoLayout->addWidget(compassLabel, 2, 0, 1, 1, Qt::AlignTop);
106     infoLayout->addWidget(m_locationLabel, 2, 1);
107
108     childWidget->setGeometry(75, 53, 368-75-5, 141-53-5);
109
110 //    QStateMachine machine;
111 //    QState *state1 = new QState(&machine);
112 //    QState *state2 = new QState(&machine);
113 //    machine.setInitialState(state1);
114
115 //    QPushButton *btn = new QPushButton(this);
116
117 //    //state1->assignProperty(childWidget, "geometry", QRect(0, 0, 150, 50));
118 //    state1->assignProperty(childWidget, "pos", QPointF(0, 0));
119 //    //state2->assignProperty(childWidget, "geometry", QRect(0, 0, 150, 100));
120 //    state2->assignProperty(childWidget, "pos", QPointF(100, 100));
121 //
122 //
123 //    QAbstractTransition *t1 = state1->addTransition(btn, SIGNAL(clicked()), state2);
124 //    //t1->addAnimation(new QPropertyAnimation(childWidget, "geometry"));
125 //    t1->addAnimation(new QPropertyAnimation(childWidget, "pos"));
126 //
127 //    QAbstractTransition *t2 = state2->addTransition(btn, SIGNAL(clicked()), state1);
128 //    //t2->addAnimation(new QPropertyAnimation(childWidget, "geometry"));
129 //    t2->addAnimation(new QPropertyAnimation(childWidget, "pos"));
130 //
131 //    machine.start();
132 }
133
134 void FriendListItem::setData(const User &user)
135 {
136     m_name = user.name();
137     m_updatedLabel->setText(user.timestamp());
138     m_statusTextLabel->setText(user.note());
139     m_locationLabel->setText(user.address());
140     m_image = user.profileImageUrl().toString();
141 }
142
143 void FriendListItem::mousePressEvent(QMouseEvent *event)
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     emit changeState();
148
149     if (m_expanded)
150         m_expanded = false;
151     else
152         m_expanded = true;
153
154     updateGeometry();
155 }
156
157 QSize FriendListItem::sizeHint() const
158 {
159     qDebug() << __PRETTY_FUNCTION__ << " " << m_expanded;
160
161     if (m_expanded)
162         return QSize(368, 200);
163     else
164         return QSize(368, 141);
165 }
166
167 void FriendListItem::paintEvent(QPaintEvent *event)
168 {
169     QPainter painter(this);
170     QRect itemRect = event->rect();
171
172     if (m_expanded)
173         itemRect = QRect(0, 0, 368, 200);
174     else
175         itemRect = QRect(0, 0, 368, 141);
176
177     //Draw background
178     QRect topRect = QRect(itemRect.left(), itemRect.top(), BACKGROUND_TOP_WIDTH,
179                           BACKGROUND_TOP_HEIGHT);
180     painter.drawPixmap(topRect, QPixmap(BACKGROUND_TOP_PATH));
181
182     QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_MIDDLE_WIDTH,
183                              itemRect.height());
184     painter.drawPixmap(middleRect, QPixmap(BACKGROUND_MIDDLE_PATH));
185     qDebug() << "Middle rect: " << middleRect;
186
187     if (itemRect.height() >= (141 - 15)) {
188         QRect bottomRect = QRect(topRect.left(), middleRect.bottom() - 14, BACKGROUND_BOTTOM_WIDTH,
189                                  BACKGROUND_BOTTOM_HEIGHT);
190         painter.drawPixmap(bottomRect, QPixmap(BACKGROUND_BOTTOM_PATH));
191     }
192
193     //Draw image
194     QString image = QString(":/res/images/dummy_Avatar.png");
195     QRect imageRect = QRect(itemRect.left() + IMAGE_X, itemRect.top() + IMAGE_Y,
196                                 IMAGE_WIDTH, IMAGE_HEIGHT);
197     painter.drawPixmap(imageRect, QPixmap(image));
198
199     //Draw name
200     QRect nameRect = QRect(itemRect.left() + IMAGE_WIDTH + 11, itemRect.top() + 15, itemRect.width(),
201                            itemRect.height());
202     QPen whitePen(QColor(Qt::white), 1, Qt::SolidLine);
203     painter.setPen(whitePen);
204     painter.setFont(QFont( "Nokia Sans", 18, QFont::Normal));
205     painter.drawText(nameRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, m_name, &nameRect);
206 }