Sorted includes alphabetically.
[situare] / src / ui / friendlistitem.cpp
index cac8d4c..f2e4e05 100644 (file)
    USA.
 */
 
-#include <QVBoxLayout>
-#include <QPushButton>
 #include <QPainter>
 #include <QDebug>
-#include <QPaintEvent>
-#include <QLabel>
 #include <QPixmap>
-#include <QFormLayout>
-#include <QSpacerItem>
 
-#include "friendlistitem.h"
 #include "../user/user.h"
+#include "../common.h"
+#include "listcommon.h"
 
-const QString BACKGROUND_PATH = ":/res/images/list_item.png";   ///< Background image path
-const QString CLOCK_PATH = ":/res/images/clock.png";        ///< Clock image path
-const QString COMPASS_PATH = ":/res/images/compass.png";    ///< Compass image path
-const QString ENVELOPE_PATH = ":/res/images/envelope.png";  ///< Envelope image path
-
-const int ICON_MARGIN = 5;   ///< Icon margin
-const int IMAGE_HEIGHT = 60; ///< Friend image height
-const int IMAGE_WIDTH = 60;  ///< Friend image width
-
-const int ITEM_MAX_HEIGHT = 240; ///< Maximum height for item
-const int ITEM_MAX_WIDTH = 368;  ///< Maximum width for item
-const int ITEM_MIN_HEIGHT = 141; ///< Minimum height for item
-const int ITEM_MIN_WIDTH = 368;  ///< Minimum width for item
-const int LABEL_MAX_WIDTH = 350;   ///< Label maximum width
-
-FriendListItem::FriendListItem(QWidget *parent)
-    : QWidget(parent)
-    , m_expanded(false)
-    , m_user(0)
-{
-    qDebug() << __PRETTY_FUNCTION__;
+#include "friendlistitem.h"
 
-    QVBoxLayout *layout = new QVBoxLayout(this);
-    this->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);
-    infoLayout->setSpacing(0);
-
-    QLabel *clockLabel = new QLabel();
-    clockLabel->setPixmap(QPixmap(CLOCK_PATH));
-    clockLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
-    QLabel *envelopeLabel = new QLabel();
-    envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
-    envelopeLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
-    QLabel *compassLabel = new QLabel();
-    compassLabel->setPixmap(QPixmap(COMPASS_PATH));
-    compassLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
-
-    m_imageLabel = new QLabel();
-    m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
-    m_nameLabel = new QLabel();
-    m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
-    m_updatedLabel = new QLabel();
-    m_updatedLabel->setWordWrap(true);
-    m_statusTextLabel = new QLabel();
-    m_statusTextLabel->setWordWrap(true);
-    m_locationLabel = new QLabel();
-    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);
-
-    bottomLayout->addSpacing(IMAGE_WIDTH);
-    bottomLayout->addLayout(infoLayout, 1);
-
-    layout->addLayout(topLayout, 0);
-    layout->addLayout(bottomLayout, 1);
-
-    setObjectName("listItem");
-    m_nameLabel->setObjectName("nameLabel");
-
-    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 }");
-
-    setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
-    setMaximumSize(ITEM_MAX_WIDTH, ITEM_MAX_HEIGHT);
-}
+const int AEROPLANE_DISTANCE = 5000;///< Aeroplane distance limit for distance icon
+const int CAR_DISTANCE = 500;       ///< Car distance limit for distance icon
+const int WALK_DISTANCE = 5;        ///< Walk distance limit for distance icon
 
-void FriendListItem::setData(User *user)
+FriendListItem::FriendListItem()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (user) {
-        m_user = user;
-        shortenTexts();
-
-        m_imageLabel->setPixmap(m_user->profileImage());
-        setText(false);
-    }
+    setSubitemTextWidth(SUBITEM_TEXT_MAX_WIDTH);
 }
 
-void FriendListItem::shortenTexts()
+GeoCoordinate FriendListItem::coordinates()
 {
-    qDebug() << __PRETTY_FUNCTION__ << m_user->address().length();
-
-    m_shortenedName = m_nameLabel->fontMetrics()
-                      .elidedText(m_user->name(), Qt::ElideRight, LABEL_MAX_WIDTH);
-    m_shortenedUpdated = m_updatedLabel->fontMetrics()
-                         .elidedText(m_user->timestamp(), Qt::ElideRight, LABEL_MAX_WIDTH);
-    m_shortenedStatusText = m_statusTextLabel->fontMetrics()
-                            .elidedText(m_user->note(), Qt::ElideRight, LABEL_MAX_WIDTH);
-    m_shortenedLocation = m_locationLabel->fontMetrics()
-                          .elidedText(m_user->address(), Qt::ElideRight, LABEL_MAX_WIDTH);
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_coordinates;
 }
 
-void FriendListItem::setText(bool expanded)
+void FriendListItem::setUserData(User *user)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (expanded) {
-        m_nameLabel->setText(m_user->name());
-        m_updatedLabel->setText(m_user->timestamp());
-        m_statusTextLabel->setText(m_user->note());
-        m_locationLabel->setText(m_user->address());
-    }
-    else {
-        m_nameLabel->setText(m_shortenedName);
-        m_updatedLabel->setText(m_shortenedUpdated);
-        m_statusTextLabel->setText(m_shortenedStatusText);
-        m_locationLabel->setText(m_shortenedLocation);
+    if(user) {
+
+        QString unit;
+        double value;
+        user->distance(value, unit);
+        QString distanceText = QString::number(value) + " " + unit;
+        setData(DISTANCE_TEXT_DISPLAY_INDEX, distanceText);
+        setDistanceIcon(value, unit);
+
+        //Dummy value to get painter font metrics.
+        QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
+        QPainter painter(&p);
+        painter.setFont(NOKIA_FONT_SMALL);
+        QFontMetrics distanceTextFontMetrics = painter.fontMetrics();
+        QRect distanceRect = distanceTextFontMetrics.boundingRect(distanceText);
+
+        setData(DISTANCE_SIZE_HINT_INDEX, distanceRect);
+        setName(shortenText(user->name(), NAME_TEXT_MAX_WIDTH - distanceRect.width() + MARGIN,
+                            ListItem::TEXT_SIZE_NORMAL));
+        setCoordinates(user->coordinates());
+
+        if (!user->profileImage().isNull())
+            setImage(user->profileImage());
+
+        clearSubItems();
+
+        addSubItem(user->note(), QPixmap(":/res/images/envelope.png"));
+        addSubItem(user->address(), QPixmap(":/res/images/compass.png"));
+        addSubItem(user->timestamp(), QPixmap(":/res/images/clock.png"));
     }
 }
 
-void FriendListItem::mousePressEvent(QMouseEvent *event)
+void FriendListItem::setAvatarImage(const QPixmap &image)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_mousePosition = event->pos();
+    if(!image.isNull())
+        setImage(image);
 }
 
-void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
+void FriendListItem::setCoordinates(const GeoCoordinate &coordinates)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (event->pos() == m_mousePosition) {
-        if (m_expanded) {
-            setText(false);
-            m_expanded = false;
-        }
-        else {
-            setText(true);
-            m_expanded = true;
-        }
-    }
+    m_coordinates = coordinates;
 }
 
-void FriendListItem::paintEvent(QPaintEvent *)
+void FriendListItem::setDistanceIcon(double value, const QString &unit)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QStyleOption option;
-    option.init(this);
+    QPixmap distanceImage;
 
-    QPainter painter(this);
-    style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
+    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);
+}
+
+FriendListItem::~FriendListItem()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 }