Added listcommon.h file.
[situare] / src / ui / friendlistitem.cpp
index 60c82e2..1be6a57 100644 (file)
 #include "friendlistitem.h"
 #include "../user/user.h"
 #include "imagebutton.h"
-#include "common.h"
-
-const int BACKGROUND_TOP_HEIGHT = 20;
-const int BACKGROUND_BOTTOM_HEIGHT = 15;
-const int ICON_HEIGHT = 24;     ///< Icon height
-const int ICON_WIDTH = 24;       ///< Icon width
-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 MARGIN = 5;   ///< Icon margin
-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
-/**
-* @var NAME_LABEL_MAX_WIDTH
-*
-* @brief Name label's maximum width
-*/
-const int NAME_LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH;
-/**
-* @var LABEL_MAX_WIDTH
-*
-* @brief All label's maximum width
-*/
-const int LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
+#include "../common.h"
+#include "listcommon.h"
 
-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);
-    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);
-    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);
-
-    m_imageLabel = new QLabel();
-    m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
-
-    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();
-    m_statusTextLabel->setWordWrap(true);
-    m_locationLabel = new QLabel();
-    m_locationLabel->setWordWrap(true);
-
-    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()));
+    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);
 }
 
-void FriendListItem::setData(User *user)
+QPointF FriendListItem::coordinates()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_user = user;
+    return m_user->coordinates();
+}
+
+void FriendListItem::setId(const QString &id)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    ListItem::setId(id);
+}
+
+QString FriendListItem::id() const
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return ListItem::id();
+}
+
+void FriendListItem::setUserData(User *user)
+{
+    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);
+    }
+}
 
-    m_imageLabel->setPixmap(m_user->profileImage());
+void FriendListItem::setDistanceIcon(double value, const QString &unit)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    QString unit;
-    double value;
-    user->distance(value, unit);
-    m_distanceLabel->setText(QString::number(value) + " " + unit);
+    QPixmap distanceImage;
 
-    shortenTexts();
-    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();
@@ -204,13 +184,15 @@ void FriendListItem::shortenTexts()
         location.append("...");
     }
 
-    int distanceLabelWidth = otherLabelsMetrics.width(m_distanceLabel->text());
+    int distanceLabelWidth = otherLabelsMetrics.width(m_distanceText) + MARGIN;
     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_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)
@@ -218,69 +200,46 @@ 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);
-    }
-}
+        setData(STATUS_TEXT_DISPLAY_INDEX, m_user->note());
+        setData(LOCATION_DISPLAY_INDEX, m_user->address());
+        setData(UPDATED_DISPLAY_INDEX, m_user->timestamp());
 
-void FriendListItem::mousePressEvent(QMouseEvent *event)
-{
-    qDebug() << __PRETTY_FUNCTION__;
+        setData(STATUS_TEXT_SIZE_HINT_INDEX, m_statusTextRect);
+        setData(LOCATION_SIZE_HINT_INDEX, m_locationRect);
+        setData(UPDATED_SIZE_HINT_INDEX, m_updatedRect);
 
-    m_mousePosition = event->pos();
-}
-
-void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
-{
-    qDebug() << __PRETTY_FUNCTION__;
+    }
+    else {
+        setData(STATUS_TEXT_DISPLAY_INDEX, m_shortenedStatusText);
+        setData(LOCATION_DISPLAY_INDEX, m_shortenedLocation);
+        setData(UPDATED_DISPLAY_INDEX, m_shortenedUpdated);
 
-    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;
-        }
+        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::paintEvent(QPaintEvent *event)
+bool FriendListItem::toggleSelection()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    Q_UNUSED(event);
-
-    QPainter painter(this);
-
-    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);
+    setSelected(!m_selected);
+    return m_selected;
 }
 
-void FriendListItem::findButtonClicked()
+void FriendListItem::setSelected(bool selected)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    emit findFriend(m_user->coordinates());
+    m_selected = selected;
+
+    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));
+    }
 }