#include <QPaintEvent>
#include <QLabel>
#include <QPixmap>
-#include <QStateMachine>
-#include <QAbstractTransition>
-#include <QPropertyAnimation>
-#include <QSignalTransition>
+#include <QFormLayout>
+#include <QSpacerItem>
+#include <QStylePainter>
+#include <math.h>
#include "friendlistitem.h"
-#include "user/user.h"
-
-const int BACKGROUND_TOP_WIDTH = 368;
-const int BACKGROUND_TOP_HEIGHT = 20;
-const int BACKGROUND_MIDDLE_X = 0;
-const int BACKGROUND_MIDDLE_Y = BACKGROUND_TOP_HEIGHT;
-const int BACKGROUND_MIDDLE_WIDTH = 368;
-const int BACKGROUND_MIDDLE_HEIGHT = 106;
-const int BACKGROUND_BOTTOM_X = 0;
-const int BACKGROUND_BOTTOM_Y = BACKGROUND_MIDDLE_Y + BACKGROUND_MIDDLE_HEIGHT;
-const int BACKGROUND_BOTTOM_WIDTH = 368;
-const int BACKGROUND_BOTTOM_HEIGHT = 15;
-const int IMAGE_X = 9;
-const int IMAGE_Y = 0;
-const int IMAGE_WIDTH = 60;
-const int IMAGE_HEIGHT = 60;
-const int CLOCK_X = 75;
-const int CLOCK_Y = 53;
-const int ENVELOPE_X = 75;
-const int ENVELOPE_Y = 77;
-const int COMPASS_X = 75;
-const int COMPASS_Y = 101;
-const int ICON_WIDTH = 24;
-const int ICON_HEIGHT = 24;
-
-const QString BACKGROUND_TOP_PATH = QString(":/res/images/list_item_top.png");
-const QString BACKGROUND_MIDDLE_PATH = QString(":/res/images/list_item_middle.png");
-const QString BACKGROUND_BOTTOM_PATH = QString(":/res/images/list_item_bottom.png");
-const QString CLOCK_PATH = QString(":/res/images/clock.png");
-const QString ENVELOPE_PATH = QString(":/res/images/envelope.png");
-const QString COMPASS_PATH = QString(":/res/images/compass.png");
+#include "../user/user.h"
+#include "avatarimage.h"
+#include "imagebutton.h"
FriendListItem::FriendListItem(QWidget *parent)
: QWidget(parent)
+ , m_expanded(false)
+ , m_user(0)
{
- QWidget *childWidget = new QWidget(this);
- QPalette palette = childWidget->palette();
- palette.setColor(QPalette::Foreground, QColor::fromRgb(152, 152, 152));
- childWidget->setPalette(palette);
- QFont font = QFont("Nokia Sans", 13, QFont::Normal);
- childWidget->setFont(font);
-
- QGridLayout *infoLayout = new QGridLayout(this);
- infoLayout->setSpacing(0);
+ qDebug() << __PRETTY_FUNCTION__;
+
+ QVBoxLayout *layout = new QVBoxLayout(this);
+ layout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
+ layout->setSpacing(0);
+ setLayout(layout);
+
+ QHBoxLayout *topLayout = new QHBoxLayout();
+ topLayout->setMargin(0);
+ topLayout->setSpacing(0);
+
+ QHBoxLayout *bottomLayout = new QHBoxLayout();
+ bottomLayout->setMargin(0);
+ bottomLayout->setSpacing(0);
+
+ QFormLayout *infoLayout = new QFormLayout();
infoLayout->setMargin(0);
- childWidget->setLayout(infoLayout);
+ infoLayout->setSpacing(0);
+ infoLayout->setLabelAlignment(Qt::AlignTop);
+
+ QLabel *clockLabel = new QLabel();
+ clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
+ clockLabel->setContentsMargins(0, 0, MARGIN, 0);
+ clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
+ QLabel *envelopeLabel = new QLabel();
+ envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
+ envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
+ envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
+ QLabel *compassLabel = new QLabel();
+ compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
+ compassLabel->setContentsMargins(0, 0, MARGIN, 0);
+ compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
- QLabel *clockLabel = new QLabel(this);
- clockLabel->setPixmap(QPixmap(CLOCK_PATH));
- QLabel *envelopeLabel = new QLabel(this);
- envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
- QLabel *compassLabel = new QLabel(this);
- compassLabel->setPixmap(QPixmap(COMPASS_PATH));
+ m_imageLabel = new QLabel();
+ m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
- m_updatedLabel = new QLabel("", this);
+ m_nameLabel = new QLabel();
+ m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
+
+ m_distanceLabel = new QLabel();
+ m_distanceLabel->setFixedHeight(IMAGE_HEIGHT);
+
+ m_findButton = new ImageButton(this, ":/res/images/show_position.png",
+ ":/res/images/show_position_s.png");
+
+ m_updatedLabel = new QLabel();
m_updatedLabel->setWordWrap(true);
- m_statusTextLabel = new QLabel("", this);
+ m_statusTextLabel = new QLabel();
m_statusTextLabel->setWordWrap(true);
- m_locationLabel = new QLabel("", this);
+ m_locationLabel = new QLabel();
m_locationLabel->setWordWrap(true);
- infoLayout->setColumnStretch(1, 1);
- infoLayout->setColumnMinimumWidth(0, 28);
- infoLayout->setRowMinimumHeight(0, 28);
-
- infoLayout->addWidget(clockLabel, 0, 0, 1, 1, Qt::AlignTop);
- infoLayout->addWidget(m_updatedLabel, 0, 1);
- infoLayout->addWidget(envelopeLabel, 1, 0, 1, 1, Qt::AlignTop);
- infoLayout->addWidget(m_statusTextLabel, 1, 1);
- infoLayout->addWidget(compassLabel, 2, 0, 1, 1, Qt::AlignTop);
- infoLayout->addWidget(m_locationLabel, 2, 1);
-
- childWidget->setGeometry(75, 53, 368-75-5, 141-53-5);
-
- this->setMinimumSize(368, 141);
-
-// QStateMachine machine;
-// QState *state1 = new QState(&machine);
-// QState *state2 = new QState(&machine);
-// machine.setInitialState(state1);
-
-// QPushButton *btn = new QPushButton(this);
-
-// //state1->assignProperty(childWidget, "geometry", QRect(0, 0, 150, 50));
-// state1->assignProperty(childWidget, "pos", QPointF(0, 0));
-// //state2->assignProperty(childWidget, "geometry", QRect(0, 0, 150, 100));
-// state2->assignProperty(childWidget, "pos", QPointF(100, 100));
-//
-//
-// QAbstractTransition *t1 = state1->addTransition(btn, SIGNAL(clicked()), state2);
-// //t1->addAnimation(new QPropertyAnimation(childWidget, "geometry"));
-// t1->addAnimation(new QPropertyAnimation(childWidget, "pos"));
-//
-// QAbstractTransition *t2 = state2->addTransition(btn, SIGNAL(clicked()), state1);
-// //t2->addAnimation(new QPropertyAnimation(childWidget, "geometry"));
-// t2->addAnimation(new QPropertyAnimation(childWidget, "pos"));
-//
-// machine.start();
+ infoLayout->addRow(envelopeLabel, m_statusTextLabel);
+ infoLayout->addRow(compassLabel, m_locationLabel);
+ infoLayout->addRow(clockLabel, m_updatedLabel);
+
+ topLayout->addWidget(m_imageLabel);
+ topLayout->addWidget(m_nameLabel, 1);
+ topLayout->addWidget(m_distanceLabel);
+
+ bottomLayout->addWidget(m_findButton, 0, Qt::AlignTop);
+ bottomLayout->addLayout(infoLayout);
+
+ layout->addLayout(topLayout, 0);
+ layout->addLayout(bottomLayout, 1);
+
+ setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
+ setMaximumSize(ITEM_MIN_WIDTH, ITEM_MAX_HEIGHT);
+
+ setFont(NOKIA_FONT_SMALL);
+ m_nameLabel->setFont(NOKIA_FONT_NORMAL);
+ QPalette itemPalette = palette();
+ itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
+ setPalette(itemPalette);
+ QPalette namePalette = m_nameLabel->palette();
+ namePalette.setColor(QPalette::Foreground, Qt::white);
+ m_nameLabel->setPalette(namePalette);
+
+ m_backgroundTopImage.load(":/res/images/list_item_top.png");
+ m_backgroundMiddleImage.load(":/res/images/list_item_middle.png");
+ m_backgroundBottomImage.load(":/res/images/list_item_bottom.png");
+
+ connect(m_findButton, SIGNAL(clicked()),
+ this, SLOT(findButtonClicked()));
}
-void FriendListItem::setData(const User &user)
+void FriendListItem::setData(User *user)
{
- m_name = user.name();
- m_updatedLabel->setText(user.timestamp());
- m_statusTextLabel->setText(user.note());
- m_locationLabel->setText(user.address());
- m_image = user.profileImageUrl().toString();
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_user = user;
+
+ m_imageLabel->setPixmap(AvatarImage::create(m_user->profileImage()));
+
+ QString unit;
+ double value;
+ user->distance(value, unit);
+ m_distanceLabel->setText(QString::number(value) + " " + unit);
+
+ shortenTexts();
+ setText(false);
+}
+
+void FriendListItem::shortenTexts()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
+ QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
+
+ QString name = m_user->name();
+ QString updated = m_user->timestamp();
+ QString statusText = m_user->note();
+ QString location = m_user->address();
+
+ int nameIndex = name.indexOf('\n');
+ int updatedIndex = updated.indexOf('\n');
+ int statusTextIndex = statusText.indexOf('\n');
+ int locationIndex = location.indexOf('\n');
+
+ if (nameIndex > 0) {
+ name.truncate(nameIndex);
+ name.append("...");
+ }
+ if (updatedIndex > 0) {
+ updated.truncate(updatedIndex);
+ updated.append("...");
+ }
+ if (statusTextIndex > 0) {
+ statusText.truncate(statusTextIndex);
+ statusText.append("...");
+ }
+ if (locationIndex > 0) {
+ location.truncate(locationIndex);
+ location.append("...");
+ }
+
+ int distanceLabelWidth = otherLabelsMetrics.width(m_distanceLabel->text());
+ m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
+ - distanceLabelWidth);
+ m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
+ m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
+ LABEL_MAX_WIDTH);
+ m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
+}
+
+void FriendListItem::setText(bool expanded)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (expanded) {
+ setUpdatesEnabled(false);
+ m_nameLabel->setText(m_shortenedName);
+ m_updatedLabel->setText(m_user->timestamp());
+ m_statusTextLabel->setText(m_user->note());
+ m_locationLabel->setText(m_user->address());
+ setUpdatesEnabled(true);
+ }
+ else {
+ setUpdatesEnabled(false);
+ m_nameLabel->setText(m_shortenedName);
+ m_updatedLabel->setText(m_shortenedUpdated);
+ m_statusTextLabel->setText(m_shortenedStatusText);
+ m_locationLabel->setText(m_shortenedLocation);
+ setUpdatesEnabled(true);
+ }
}
void FriendListItem::mousePressEvent(QMouseEvent *event)
{
- emit changeState();
+ qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+
+ m_mousePosition = event->pos();
+}
+
+void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
+{
+ qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+
+ if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) &&
+ (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) {
+ if (m_expanded) {
+ setText(false);
+ m_expanded = false;
+ }
+ else {
+ setText(true);
+ m_expanded = true;
+ }
+ }
}
void FriendListItem::paintEvent(QPaintEvent *event)
{
+ qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
+
QPainter painter(this);
- //QRect itemRect = event->rect();
- QRect itemRect = QRect(0, 0, 368, 141);
- qDebug() << event->rect();
- //Draw background
- QRect topRect = QRect(itemRect.left(), itemRect.top(), BACKGROUND_TOP_WIDTH,
- BACKGROUND_TOP_HEIGHT);
- painter.drawPixmap(topRect, QPixmap(BACKGROUND_TOP_PATH));
-
- QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_MIDDLE_WIDTH,
- BACKGROUND_MIDDLE_HEIGHT);
- painter.drawPixmap(middleRect, QPixmap(BACKGROUND_MIDDLE_PATH));
-
- if (itemRect.height() >= (141 - 15)) {
- QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_BOTTOM_WIDTH,
- BACKGROUND_BOTTOM_HEIGHT);
- painter.drawPixmap(bottomRect, QPixmap(BACKGROUND_BOTTOM_PATH));
- }
- //Draw image
- QString image = QString(":/res/images/dummy_Avatar.png");
- QRect imageRect = QRect(itemRect.left() + IMAGE_X, itemRect.top() + IMAGE_Y,
- IMAGE_WIDTH, IMAGE_HEIGHT);
- painter.drawPixmap(imageRect, QPixmap(image));
-
- //Draw name
- QRect nameRect = QRect(itemRect.left() + IMAGE_WIDTH + 11, itemRect.top() + 15, itemRect.width(),
- itemRect.height());
- QPen whitePen(QColor(Qt::white), 1, Qt::SolidLine);
- painter.setPen(whitePen);
- painter.setFont(QFont( "Nokia Sans", 18, QFont::Normal));
- painter.drawText(nameRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, m_name, &nameRect);
+ QRect topRect = QRect(0, 0, ITEM_MIN_WIDTH, BACKGROUND_TOP_HEIGHT);
+ QRect middleRect = QRect(0, topRect.bottom(), ITEM_MIN_WIDTH,
+ height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
+ QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), ITEM_MIN_WIDTH,
+ BACKGROUND_BOTTOM_HEIGHT);
+
+ painter.drawPixmap(topRect, m_backgroundTopImage);
+ painter.drawPixmap(middleRect, m_backgroundMiddleImage);
+ painter.drawPixmap(bottomRect, m_backgroundBottomImage);
+}
+
+void FriendListItem::findButtonClicked()
+{
+ emit findFriend(m_user->coordinates());
}