Added listcommon.h file.
[situare] / src / ui / friendlistitem.cpp
index af5f7ba..1be6a57 100644 (file)
 
 #include "friendlistitem.h"
 #include "../user/user.h"
+#include "imagebutton.h"
+#include "../common.h"
+#include "listcommon.h"
 
-const QString BACKGROUND_PATH = ":/res/images/list_item.png";   ///< Background image path
-const QString BACKGROUND_TOP_PATH = ":/res/images/list_item_top.png";   ///< Background image path
-const QString BACKGROUND_MIDDLE_PATH = ":/res/images/list_item_middle.png";   ///< Background image path
-const QString BACKGROUND_BOTTOM_PATH = ":/res/images/list_item_bottom.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 = 247;   ///< Label maximum width
-
-const int MOUSE_PRESS_AREA_WIDTH = 20;  ///< Area width for item height toggling
-const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling
-
-const QFont NOKIA_FONT_NORMAL = QFont( "Nokia Sans", 18, QFont::Normal);    ///< Normal font
-const QFont NOKIA_FONT_SMALL = QFont( "Nokia Sans", 13, QFont::Normal);     ///< Small font
-
-/**
-* @var STYLESHEET
-* @brief Stylesheet for FriendListItem
-*/
-const QString STYLESHEET = "QWidget#listItem { border-image: url(:/res/images/list_item.png) 20%; " \
-                           "border-width: 20px 14px 16px 14px; } " \
-                           "QLabel { color: #989898; }" \
-                           "#nameLabel { color: #ffffff }";
-
-FriendListItem::FriendListItem(QWidget *parent)
-    : QWidget(parent)
-    , m_expanded(false)
+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__;
 
-    QVBoxLayout *layout = new QVBoxLayout(this);
-    this->setLayout(layout);
-
-    QHBoxLayout *topLayout = new QHBoxLayout();
-    topLayout->setMargin(0);
-    topLayout->setSpacing(0);
+    setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
+}
 
-    QHBoxLayout *bottomLayout = new QHBoxLayout();
-    bottomLayout->setMargin(0);
-    bottomLayout->setSpacing(0);
+void FriendListItem::calculateTextRects()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    QFormLayout *infoLayout = new QFormLayout();
-    infoLayout->setMargin(0);
-    infoLayout->setSpacing(0);
+    //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);
+}
 
-    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);
+QPointF FriendListItem::coordinates()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_imageLabel = new QLabel();
-    m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
+    return m_user->coordinates();
+}
 
-    m_nameLabel = new QLabel();
-    m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
-    m_nameLabel->setFont(NOKIA_FONT_NORMAL);
+void FriendListItem::setId(const QString &id)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_updatedLabel = new QLabel();
-    m_updatedLabel->setWordWrap(true);
-    m_statusTextLabel = new QLabel();
-    m_statusTextLabel->setWordWrap(true);
-    m_locationLabel = new QLabel();
-    m_locationLabel->setWordWrap(true);
+    ListItem::setId(id);
+}
 
-    infoLayout->addRow(clockLabel, m_updatedLabel);
-    infoLayout->addRow(envelopeLabel, m_statusTextLabel);
-    infoLayout->addRow(compassLabel, m_locationLabel);
+QString FriendListItem::id() const
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    topLayout->addWidget(m_imageLabel);
-    topLayout->addWidget(m_nameLabel);
+    return ListItem::id();
+}
 
-    bottomLayout->addSpacing(IMAGE_WIDTH);
-    bottomLayout->addLayout(infoLayout, 1);
+void FriendListItem::setUserData(User *user)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    layout->addLayout(topLayout, 0);
-    layout->addLayout(bottomLayout, 1);
+    if(user) {
+        m_user = user;
 
-    setObjectName("listItem");
-    m_nameLabel->setObjectName("nameLabel");
+        setId(m_user->userId());
 
-    setFont(NOKIA_FONT_SMALL);
+        if (!m_user->profileImage().isNull())
+            setData(AVATAR_IMAGE_INDEX, m_user->profileImage());
 
-    setStyleSheet(STYLESHEET);
+        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);
 
-    setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
-    setMaximumSize(ITEM_MIN_WIDTH, ITEM_MAX_HEIGHT);
-    this->setAutoFillBackground(false);
+        shortenTexts();
+        calculateTextRects();
+        setText(false);
+    }
 }
 
-void FriendListItem::setData(User *user)
+void FriendListItem::setDistanceIcon(double value, const QString &unit)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (user) {
-        m_user = user;
-        shortenTexts();
+    QPixmap distanceImage;
 
-        m_imageLabel->setPixmap(m_user->profileImage());
-        setText(false);
-    }
+    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()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
-    QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
+    //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();
@@ -185,12 +184,15 @@ void FriendListItem::shortenTexts()
         location.append("...");
     }
 
-
-    m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, LABEL_MAX_WIDTH + 30);
-    m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
+    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::setText(bool expanded)
@@ -198,50 +200,46 @@ void FriendListItem::setText(bool expanded)
     qDebug() << __PRETTY_FUNCTION__;
 
     if (expanded) {
-        m_nameLabel->setText(m_shortenedName);
-        m_updatedLabel->setText(m_user->timestamp());
-        m_statusTextLabel->setText(m_user->note());
-        m_locationLabel->setText(m_user->address());
+        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_shortenedName);
-        m_updatedLabel->setText(m_shortenedUpdated);
-        m_statusTextLabel->setText(m_shortenedStatusText);
-        m_locationLabel->setText(m_shortenedLocation);
+        setData(STATUS_TEXT_DISPLAY_INDEX, m_shortenedStatusText);
+        setData(LOCATION_DISPLAY_INDEX, m_shortenedLocation);
+        setData(UPDATED_DISPLAY_INDEX, m_shortenedUpdated);
+
+        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::mousePressEvent(QMouseEvent *event)
+bool FriendListItem::toggleSelection()
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
-
-    m_mousePosition = event->pos();
-}
+    qDebug() << __PRETTY_FUNCTION__;
 
-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;
-        }
-    }
+    setSelected(!m_selected);
+    return m_selected;
 }
 
-void FriendListItem::paintEvent(QPaintEvent *event)
+void FriendListItem::setSelected(bool selected)
 {
-//    qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    QStyleOption option;
-    option.init(this);
+    m_selected = selected;
 
-    QStylePainter 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));
+    }
 }