Added listcommon.h file.
[situare] / src / ui / friendlistitem.cpp
index ce2ae5c..1be6a57 100644 (file)
 #include <QPaintEvent>
 #include <QLabel>
 #include <QPixmap>
-#include <QStateMachine>
-#include <QAbstractTransition>
-#include <QPropertyAnimation>
-#include <QSignalTransition>
-#include <QFontMetrics>
 #include <QFormLayout>
 #include <QSpacerItem>
+#include <QStylePainter>
+#include <math.h>
 
 #include "friendlistitem.h"
-#include "user/user.h"
+#include "../user/user.h"
+#include "imagebutton.h"
+#include "../common.h"
+#include "listcommon.h"
+
+const int WALK_DISTANCE = 5;        ///< Walk distance limit for distance icon
+const int CAR_DISTANCE = 500;       ///< Car distance limit for distance icon
+const int AEROPLANE_DISTANCE = 5000;///< Aeroplane distance limit for distance icon
+
+FriendListItem::FriendListItem()
+    : m_selected(false)
+    , m_user(0)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
+}
+
+void FriendListItem::calculateTextRects()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    //Dummy value to get painter font metrics.
+    QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
+    QPainter painter(&p);
+    painter.setFont(NOKIA_FONT_SMALL);
+    QFontMetrics smallFontMetrics = painter.fontMetrics();
+
+    QRect distanceRect = smallFontMetrics.boundingRect(m_distanceText);
+    QRect statusTextRect = smallFontMetrics.boundingRect(m_user->note());
+    QRect updatedRect = smallFontMetrics.boundingRect(m_user->timestamp());
+    QRect locationRect = smallFontMetrics.boundingRect(m_user->address());
+
+    int statusTextRectFactor = statusTextRect.width() / LABEL_MAX_WIDTH;
+    statusTextRectFactor++;
+    m_statusTextRect = QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT * statusTextRectFactor);
+    int updatedRectFactor = updatedRect.width() / LABEL_MAX_WIDTH;
+    updatedRectFactor++;
+    m_updatedRect = QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT * updatedRectFactor);
+    int locationRectFactor = locationRect.width() / LABEL_MAX_WIDTH;
+    locationRectFactor++;
+    m_locationRect = QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT * locationRectFactor);
+
+    m_expandedHeight = ITEM_MIN_HEIGHT + ((statusTextRectFactor + updatedRectFactor +
+                                           locationRectFactor - 3) * ICON_HEIGHT);
+
+    setData(DISTANCE_SIZE_HINT_INDEX, distanceRect);
+}
+
+QPointF FriendListItem::coordinates()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_user->coordinates();
+}
 
-const int IMAGE_WIDTH = 60;
-const int IMAGE_HEIGHT = 60;
+void FriendListItem::setId(const QString &id)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-const int ITEM_MIN_WIDTH = 368;
-const int ITEM_MAX_WIDTH = 368;
-const int ITEM_MIN_HEIGHT = 141;
-const int ITEM_MAX_HEIGHT = 240;
-const int ICON_MARGIN = 5;
+    ListItem::setId(id);
+}
 
-const QString BACKGROUND_PATH = QString(":/res/images/list_item.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");
+QString FriendListItem::id() const
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-const int MAXIMUM_CHARS = 32;
+    return ListItem::id();
+}
 
-FriendListItem::FriendListItem(QWidget *parent)
-    : QWidget(parent)
-    , m_expanded(false)
+void FriendListItem::setUserData(User *user)
 {
-    QVBoxLayout *layout = new QVBoxLayout(this);
-    this->setLayout(layout);
-
-    QHBoxLayout *topLayout = new QHBoxLayout(this);
-    topLayout->setMargin(0);
-    topLayout->setSpacing(0);
-
-    QHBoxLayout *bottomLayout = new QHBoxLayout(this);
-    bottomLayout->setMargin(0);
-    bottomLayout->setSpacing(0);
-
-    m_infoWidget = new QWidget(this);
-
-    QFormLayout *infoLayout = new QFormLayout(this);
-    infoLayout->setMargin(0);
-    infoLayout->setSpacing(0);
-    m_infoWidget->setLayout(infoLayout);
-
-    QLabel *clockLabel = new QLabel(this);
-    clockLabel->setPixmap(QPixmap(CLOCK_PATH));
-    clockLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
-    QLabel *envelopeLabel = new QLabel(this);
-    envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
-    envelopeLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
-    QLabel *compassLabel = new QLabel(this);
-    compassLabel->setPixmap(QPixmap(COMPASS_PATH));
-    compassLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
-
-    m_imageLabel = new QLabel(this);
-    m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
-    m_nameLabel = new QLabel("", this);
-    m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
-    m_updatedLabel = new QLabel("", this);
-    m_updatedLabel->setWordWrap(true);
-    m_statusTextLabel = new QLabel("", this);
-    m_statusTextLabel->setWordWrap(true);
-    m_locationLabel = new QLabel("", this);
-    m_locationLabel->setWordWrap(true);
-
-    infoLayout->addRow(clockLabel, m_updatedLabel);
-    infoLayout->addRow(envelopeLabel, m_statusTextLabel);
-    infoLayout->addRow(compassLabel, m_locationLabel);
-
-    topLayout->addWidget(m_imageLabel);
-    topLayout->addWidget(m_nameLabel);
-
-    QLabel *tmp = new QLabel(this);
-    bottomLayout->addSpacerItem(new QSpacerItem(IMAGE_WIDTH, IMAGE_HEIGHT));
-    bottomLayout->addWidget(m_infoWidget, 1);
-
-    layout->addLayout(topLayout, 0);
-    layout->addLayout(bottomLayout, 1);
-
-    this->setObjectName("listItem");
-    m_infoWidget->setObjectName("infoWidget");
-    m_nameLabel->setObjectName("nameLabel");
-    this->setStyleSheet("#listItem { border-image: url(:/res/images/list_item.png) 20%; " \
-                        "border-width: 20px 14px 16px 14px; } " \
-                        "QLabel { font-size: 13pt; color: #989898; }" \
-                        "#nameLabel { font-size: 18pt; color: #ffffff }");
-
-    this->setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
-    this->setMaximumSize(ITEM_MAX_WIDTH, ITEM_MAX_HEIGHT);
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(user) {
+        m_user = user;
+
+        setId(m_user->userId());
+
+        if (!m_user->profileImage().isNull())
+            setData(AVATAR_IMAGE_INDEX, m_user->profileImage());
+
+        QString unit;
+        double value;
+        m_user->distance(value, unit);
+        m_distanceText = QString::number(value) + " " + unit;
+        setData(DISTANCE_TEXT_DISPLAY_INDEX, m_distanceText);
+        setDistanceIcon(value, unit);
+
+        shortenTexts();
+        calculateTextRects();
+        setText(false);
+    }
 }
 
-void FriendListItem::setData(const User &user)
+void FriendListItem::setDistanceIcon(double value, const QString &unit)
 {
-    m_user = user;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    shortenTexts();
+    QPixmap distanceImage;
 
-    m_imageLabel->setPixmap(m_user.profileImage());
-    m_nameLabel->setText(m_shortenedName);
-    m_updatedLabel->setText(m_shortenedUpdated);
-    m_statusTextLabel->setText(m_shortenedStatusText);
-    m_locationLabel->setText(m_shortenedLocation);
-    m_image = user.profileImageUrl().toString();
+    if ((unit == "m") || (value < WALK_DISTANCE))
+        distanceImage.load(":/res/images/walk_icon_gray.png");
+    else if (value < CAR_DISTANCE)
+        distanceImage.load(":/res/images/car_icon_gray.png");
+    else if (value < AEROPLANE_DISTANCE)
+        distanceImage.load(":/res/images/aeroplane_icon_gray.png");
+    else
+        distanceImage.load(":/res/images/rocket_icon_gray.png");
 
+    setData(DISTANCE_IMAGE_INDEX, distanceImage);
 }
 
 void FriendListItem::shortenTexts()
 {
-    if (m_user.name().length() > MAXIMUM_CHARS) {
-        m_shortenedName = m_user.name().left(MAXIMUM_CHARS-3).append("...");
-    }
-    else {
-         m_shortenedName = m_user.name();
-    }
-    if (m_user.timestamp().length() > MAXIMUM_CHARS) {
-        m_shortenedUpdated = m_user.timestamp().left(MAXIMUM_CHARS-3).append("...");
-    }
-    else {
-        m_shortenedUpdated = m_user.timestamp();
-    }
-    if (m_user.note().length() > MAXIMUM_CHARS) {
-        m_shortenedStatusText = m_user.note().left(MAXIMUM_CHARS-3).append("...");
+    qDebug() << __PRETTY_FUNCTION__;
+
+    //Dummy value to get painter font metrics.
+    QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
+    QPainter painter(&p);
+    painter.setFont(NOKIA_FONT_NORMAL);
+    QFontMetrics nameLabelMetrics = painter.fontMetrics();
+    painter.setFont(NOKIA_FONT_SMALL);
+    QFontMetrics otherLabelsMetrics = painter.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("...");
     }
-    else {
-        m_shortenedStatusText = m_user.note();
+    if (updatedIndex > 0) {
+        updated.truncate(updatedIndex);
+        updated.append("...");
     }
-    if (m_user.address().length() > MAXIMUM_CHARS) {
-        m_shortenedLocation = m_user.address().left(MAXIMUM_CHARS-3).append("...");
+    if (statusTextIndex > 0) {
+        statusText.truncate(statusTextIndex);
+        statusText.append("...");
     }
-    else {
-        m_shortenedLocation = m_user.address();
+    if (locationIndex > 0) {
+        location.truncate(locationIndex);
+        location.append("...");
     }
+
+    int distanceLabelWidth = otherLabelsMetrics.width(m_distanceText) + MARGIN;
+    m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
+                                                  - distanceLabelWidth);
+    m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
+                                                          LABEL_MAX_WIDTH);
+    m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
+    m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
+
+    setData(NAME_DISPLAY_INDEX, m_shortenedName);
 }
 
-void FriendListItem::toggleHeight()
+void FriendListItem::setText(bool expanded)
 {
-    if (m_expanded) {
-        m_nameLabel->setText(m_shortenedName);
-        m_updatedLabel->setText(m_shortenedUpdated);
-        m_statusTextLabel->setText(m_shortenedStatusText);
-        m_locationLabel->setText(m_shortenedLocation);
-        m_expanded = false;
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (expanded) {
+        setData(STATUS_TEXT_DISPLAY_INDEX, m_user->note());
+        setData(LOCATION_DISPLAY_INDEX, m_user->address());
+        setData(UPDATED_DISPLAY_INDEX, m_user->timestamp());
+
+        setData(STATUS_TEXT_SIZE_HINT_INDEX, m_statusTextRect);
+        setData(LOCATION_SIZE_HINT_INDEX, m_locationRect);
+        setData(UPDATED_SIZE_HINT_INDEX, m_updatedRect);
 
     }
     else {
-        m_nameLabel->setText(m_user.name());
-        m_updatedLabel->setText(m_user.timestamp());
-        m_statusTextLabel->setText(m_user.note());
-        m_locationLabel->setText(m_user.address());
-        m_expanded = true;
-    }
-}
+        setData(STATUS_TEXT_DISPLAY_INDEX, m_shortenedStatusText);
+        setData(LOCATION_DISPLAY_INDEX, m_shortenedLocation);
+        setData(UPDATED_DISPLAY_INDEX, m_shortenedUpdated);
 
-void FriendListItem::mousePressEvent(QMouseEvent *event)
-{
-    m_mousePosition = event->pos();
+        setData(STATUS_TEXT_SIZE_HINT_INDEX, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
+        setData(LOCATION_SIZE_HINT_INDEX, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
+        setData(UPDATED_SIZE_HINT_INDEX, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
+    }
 }
 
-void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
+bool FriendListItem::toggleSelection()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (event->pos() == m_mousePosition)
-        toggleHeight();
+    setSelected(!m_selected);
+    return m_selected;
 }
 
-void FriendListItem::paintEvent(QPaintEvent *)
+void FriendListItem::setSelected(bool selected)
 {
-    QStyleOption option;
-    option.init(this);
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_selected = selected;
 
-    QPainter painter(this);
-    style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
+    if (m_selected) {
+        setText(true);
+        setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_expandedHeight));
+    }
+    else {
+        setText(false);
+        setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
+    }
 }