Merge branch 'master' into contact_dialog
[situare] / src / ui / friendlistitem.cpp
index 06ec988..9a8c67a 100644 (file)
    USA.
 */
 
-#include <QVBoxLayout>
-#include <QPushButton>
 #include <QPainter>
 #include <QDebug>
-#include <QPaintEvent>
-#include <QLabel>
 #include <QPixmap>
-#include <QFormLayout>
-#include <QSpacerItem>
-#include <QStylePainter>
-#include <math.h>
 
-#include "friendlistitem.h"
 #include "../user/user.h"
-#include "imagebutton.h"
 #include "../common.h"
+#include "listcommon.h"
 
-const int ITEM_WIDTH = 368;
-const int ICON_HEIGHT = 24;     ///< Icon height
-const int ICON_WIDTH = 24;       ///< Icon width
-const int IMAGE_HEIGHT = 64; ///< Friend image height
-const int IMAGE_WIDTH = 64;  ///< Friend image width
-const int ITEM_MIN_HEIGHT = 141; ///< Minimum height 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_WIDTH - 3*MARGIN - IMAGE_WIDTH;
-
-/**
-* @var LABEL_MAX_WIDTH
-*
-* @brief All label's maximum width
-*/
-const int LABEL_MAX_WIDTH = ITEM_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH - MARGIN;
-
-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
+#include "friendlistitem.h"
 
 FriendListItem::FriendListItem()
-    : m_expanded(false)
-    , m_user(0)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
-
-//    m_distanceTextLabel = new QLabel();
-//    m_distanceTextLabel->setFixedHeight(ICON_HEIGHT);
-
-//    m_distanceImageLabel = new QLabel();
-//    m_distanceImageLabel->setFixedSize(ICON_WIDTH, ICON_HEIGHT);
-
-//    m_findButton = new ImageButton(this);
-
-//    m_updatedLabel = new QLabel();
-//    m_updatedLabel->setWordWrap(true);
-//    m_statusTextLabel = new QLabel();
-//    m_statusTextLabel->setWordWrap(true);
-//    m_locationLabel = new QLabel();
-//    m_locationLabel->setWordWrap(true);
-
-//    distanceLayout->addWidget(m_distanceImageLabel, 0, Qt::AlignRight);
-//    distanceLayout->addWidget(m_distanceTextLabel, 0, Qt::AlignRight);
-
-//    infoLayout->addRow(envelopeLabel, m_statusTextLabel);
-//    infoLayout->addRow(compassLabel, m_locationLabel);
-//    infoLayout->addRow(clockLabel, m_updatedLabel);
-
-//    topLayout->addWidget(m_findButton);
-//    topLayout->addWidget(m_nameLabel, 1);
-//    topLayout->addLayout(distanceLayout);
-
-//    bottomLayout->addSpacing(IMAGE_WIDTH);
-//    bottomLayout->addLayout(infoLayout);
-
-//    layout->addLayout(topLayout, 0);
-//    layout->addLayout(bottomLayout, 1);
-
-//    setMinimumSize(ITEM_WIDTH, ITEM_MIN_HEIGHT);
-//    setMaximumWidth(ITEM_WIDTH);
-
-//    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()));
+    setSubitemTextWidth(SUBITEM_TEXT_MAX_WIDTH);
 }
 
-void FriendListItem::calculateTextRects()
+FriendListItem::~FriendListItem()
 {
-    QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
-
-    QRect statusTextRect = otherLabelsMetrics.boundingRect(QRect(0, 0, LABEL_MAX_WIDTH,
-                                                                             1000), Qt::AlignLeft,
-                                                                       m_user->note());
-    QRect updatedRect = otherLabelsMetrics.boundingRect(QRect(0, 0, LABEL_MAX_WIDTH,
-                                                                             1000), Qt::AlignLeft,
-                                                                       m_user->timestamp());
-    QRect locationRect = otherLabelsMetrics.boundingRect(QRect(0, 0, LABEL_MAX_WIDTH,
-                                                                             1000), Qt::AlignLeft,
-                                                                       m_user->address());
-
-
-    qDebug() << m_shortenedStatusText;
-
-    int statusTextFactor = statusTextRect.width() / LABEL_MAX_WIDTH;
-    statusTextFactor++;
-    m_statusTextRect = QRect(0, 0, LABEL_MAX_WIDTH, 24 * statusTextFactor);
-    int updatedFactor = updatedRect.width() / LABEL_MAX_WIDTH;
-    updatedFactor++;
-    m_updatedRect = QRect(0, 0, LABEL_MAX_WIDTH, 24 * updatedFactor);
-    int locationFactor = locationRect.width() / LABEL_MAX_WIDTH;
-    locationFactor++;
-    m_locationRect = QRect(0, 0, LABEL_MAX_WIDTH, 24 * locationFactor);
-
-    qDebug() << statusTextRect;
-    qDebug() << updatedRect;
-    qDebug() << locationRect;
-
-    qDebug() << m_statusTextRect;
-    qDebug() << m_updatedRect;
-    qDebug() << m_locationRect;
-
-    m_expandedHeight = ITEM_MIN_HEIGHT + ((statusTextFactor + updatedFactor + locationFactor - 3)*ICON_HEIGHT);
+    qDebug() << __PRETTY_FUNCTION__;
 }
 
-void FriendListItem::setId(const QString &id)
+GeoCoordinate FriendListItem::coordinates() const
 {
     qDebug() << __PRETTY_FUNCTION__;
+
+    return m_coordinates;
 }
 
-QString FriendListItem::id() const
+QString FriendListItem::facebookId() const
 {
-    return m_user->userId();
+    return m_facebookId;
 }
 
-void FriendListItem::setUserData(User *user)
+void FriendListItem::setAvatarImage(const QPixmap &image)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(user) {
-        m_user = user;
-
-        if (!m_user->profileImage().isNull())
-            setData(Qt::DecorationRole, m_user->profileImage());
+    if(!image.isNull())
+        setImage(image);
+}
 
-        QString unit;
-        double value;
-        m_user->distance(value, unit);
-        m_distanceText = QString::number(value) + " " + unit;
-        setData(Qt::UserRole + 3, m_distanceText);
-        setDistanceIcon(value, unit);
+void FriendListItem::setCoordinates(const GeoCoordinate &coordinates)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-        shortenTexts();
-        calculateTextRects();
-        setText(false);
-    }
+    m_coordinates = coordinates;
 }
 
 void FriendListItem::setDistanceIcon(double value, const QString &unit)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
+    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
+
     QPixmap distanceImage;
 
     if ((unit == "m") || (value < WALK_DISTANCE))
@@ -208,127 +87,47 @@ void FriendListItem::setDistanceIcon(double value, const QString &unit)
     else
         distanceImage.load(":/res/images/rocket_icon_gray.png");
 
-    qDebug() << __PRETTY_FUNCTION__ << distanceImage.isNull();
-
-    setData(Qt::UserRole + 4, distanceImage);
+    setData(DISTANCE_IMAGE_INDEX, distanceImage);
 }
 
-void FriendListItem::shortenTexts()
+
+void FriendListItem::setFacebookId(const QString &facebookId)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QFontMetrics nameLabelMetrics = QFontMetrics(NOKIA_FONT_NORMAL);
-    QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
-
-    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_distanceText) + 24;
-    m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, NAME_LABEL_MAX_WIDTH
-                                                  - distanceLabelWidth);
-    m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
-                                                          LABEL_MAX_WIDTH - 70);
-    m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH - 70);
-    m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH - 70);
-
-    setData(Qt::SizeHintRole + 4, QRect(0, 0, distanceLabelWidth, 24));
+    m_facebookId = facebookId;
 }
-
-QString FriendListItem::elideText(int width, const QString &text)
+void FriendListItem::setUserData(User *user)
 {
-    QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
-    int widthSum = 0;
-    QString t = text;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    for (int i = 0; i < text.size(); ++i) {
-        QChar c = text.at(i);
+    QString unit;
+    double value;
+    user->distance(value, unit);
+    QString distanceText = QString::number(value) + " " + unit;
+    setData(DISTANCE_TEXT_DISPLAY_INDEX, distanceText);
+    setDistanceIcon(value, unit);
 
-        widthSum += otherLabelsMetrics.width(c);
+    setFacebookId(user->userId());
 
-        if (widthSum > width) {
-            t.truncate(i - 3);
-            t.append("...");
-            break;
-        }
-    }
+    //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);
 
-    return t;
-}
-
-void FriendListItem::setText(bool expanded)
-{
-    qDebug() << __PRETTY_FUNCTION__;
+    setData(DISTANCE_SIZE_HINT_INDEX, distanceRect);
+    setTitle(shortenText(user->name(), NAME_TEXT_MAX_WIDTH - distanceRect.width() - MARGIN * 2,
+                        ListItem::TEXT_SIZE_NORMAL));
+    setCoordinates(user->coordinates());
 
-    setData(Qt::DisplayRole, m_shortenedName);
-
-    if (expanded) {
-        setData(Qt::UserRole, m_user->note());
-        setData(Qt::UserRole + 1, m_user->timestamp());
-        setData(Qt::UserRole + 2, m_user->address());
-
-        setData(Qt::SizeHintRole + 1, m_statusTextRect);
-        setData(Qt::SizeHintRole + 2, m_updatedRect);
-        setData(Qt::SizeHintRole + 3, m_locationRect);
-    }
-    else {
-        setData(Qt::UserRole, m_shortenedStatusText);
-        setData(Qt::UserRole + 1, m_shortenedUpdated);
-        setData(Qt::UserRole + 2, m_shortenedLocation);
-
-        setData(Qt::SizeHintRole + 1, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
-        setData(Qt::SizeHintRole + 2, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
-        setData(Qt::SizeHintRole + 3, QRect(0, 0, LABEL_MAX_WIDTH, ICON_HEIGHT));
-    }
-}
+    if (!user->profileImage().isNull())
+        setImage(user->profileImage());
 
-void FriendListItem::toggleHeight()
-{
-    if (m_expanded) {
-        m_expanded = false;
-        setText(false);
-        setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
-    }
-    else {
-        m_expanded = true;
-        setText(true);
-        setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, m_expandedHeight));
-    }
-}
+    clearSubItems();
 
-void FriendListItem::setExpanded(bool expanded)
-{
-    m_expanded = expanded;
-
-    if (m_expanded) {
-        setText(true);
-        setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, m_expandedHeight));
-    }
-    else {
-        setText(false);
-        setData(Qt::SizeHintRole, QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
-    }
+    addSubItem(user->note(), QPixmap(":/res/images/envelope.png"));
+    addSubItem(user->address(), QPixmap(":/res/images/compass.png"));
+    addSubItem(user->timestamp(), QPixmap(":/res/images/clock.png"));
 }