Merge branch 'master' into contact_dialog
[situare] / src / ui / friendlistitem.cpp
index f93c80e..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 "../common.h"
+#include "listcommon.h"
+
+#include "friendlistitem.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 = 220;   ///< Label maximum width
-
-FriendListItem::FriendListItem(QWidget *parent)
-    : QWidget(parent)
-    , m_expanded(false)
-    , m_user(0)
+FriendListItem::FriendListItem()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    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);
-
-    QPalette palette = this->palette();
-    palette.setColor(QPalette::Foreground, QColor::fromRgb(152, 152, 152));
-    setPalette(palette);
-    setFont(QFont( "Nokia Sans", 13, QFont::Normal));
-
-
-    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);
-    QPalette namePalette = m_nameLabel->palette();
-    namePalette.setColor(QPalette::Foreground, Qt::white);
-    m_nameLabel->setPalette(namePalette);
-    m_nameLabel->setFont(QFont( "Nokia Sans", 18, QFont::Normal));
-
-    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("QWidget#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_MIN_WIDTH, ITEM_MAX_HEIGHT);
+    setSubitemTextWidth(SUBITEM_TEXT_MAX_WIDTH);
 }
 
-void FriendListItem::setData(User *user)
+FriendListItem::~FriendListItem()
 {
     qDebug() << __PRETTY_FUNCTION__;
-
-    if (user) {
-        m_user = user;
-        shortenTexts();
-
-        m_imageLabel->setPixmap(m_user->profileImage());
-        setText(false);
-    }
 }
 
-void FriendListItem::shortenTexts()
+GeoCoordinate FriendListItem::coordinates() const
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    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);
+    return m_coordinates;
 }
 
-void FriendListItem::setText(bool expanded)
+QString FriendListItem::facebookId() const
 {
-    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);
-    }
+    return m_facebookId;
 }
 
-void FriendListItem::mousePressEvent(QMouseEvent *event)
+void FriendListItem::setAvatarImage(const QPixmap &image)
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+    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__ << " " << event->pos();
-
-    if (abs(m_mousePosition.y() - event->pos().y()) < 10) {
-        if (m_expanded) {
-            setText(false);
-            m_expanded = false;
-        }
-        else {
-            setText(true);
-            m_expanded = true;
-        }
-    }
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_coordinates = coordinates;
 }
 
-void FriendListItem::paintEvent(QPaintEvent *event)
+void FriendListItem::setDistanceIcon(double value, const QString &unit)
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
-
-//    QStyleOption option;
-//    option.init(this);
-
-//    QStylePainter painter(this);
-//    style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
-
-//    QPainter painter(this);
-
-//    painter.drawPixmap(event->rect(), QPixmap(BACKGROUND_PATH));
-
-//    painter.drawPixmap(event->rect(), QPixmap(BACKGROUND_PATH), event->rect());
-
-//    int topRectHeight = 20 - event->rect().top();
+    qDebug() << __PRETTY_FUNCTION__;
 
-//    QRect topRect;
+    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
 
-//    if (topRectHeight > 0) {
-//        topRect = QRect(event->rect().left(), event->rect().top(), 368,
-//                              topRectHeight);
+    QPixmap distanceImage;
 
-//        painter.drawPixmap(topRect, QPixmap(BACKGROUND_TOP_PATH), QRect(0, event->rect().top(), 368, topRect.height()));
-//    }
+    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");
 
-//    int middleRectHeight = event->rect().height() - topRect.height();
+    setData(DISTANCE_IMAGE_INDEX, distanceImage);
+}
 
-//    int bottomRectHeight = 15 - (148 - event->rect().height());
 
-//    if (bottomRectHeight > 0)
-//        middleRectHeight -= bottomRectHeight;
+void FriendListItem::setFacebookId(const QString &facebookId)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
+    m_facebookId = facebookId;
+}
+void FriendListItem::setUserData(User *user)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-//    QRect middleRect = QRect(event->rect().left(), topRect.bottom() + 1, 368, middleRectHeight);
+    QString unit;
+    double value;
+    user->distance(value, unit);
+    QString distanceText = QString::number(value) + " " + unit;
+    setData(DISTANCE_TEXT_DISPLAY_INDEX, distanceText);
+    setDistanceIcon(value, unit);
 
-////    painter.drawPixmap(middleRect, QPixmap(BACKGROUND_MIDDLE_PATH));
+    setFacebookId(user->userId());
 
+    //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);
 
-//    if (bottomRectHeight > 0) {
-//        QRect bottomRect = QRect(event->rect().left(), middleRect.bottom() + 1, 368,
-//                                 bottomRectHeight);
+    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());
 
-//        painter.drawPixmap(bottomRect, QPixmap(BACKGROUND_BOTTOM_PATH), QRect(0, 0, 368,
-//                                                                              bottomRectHeight));
-//    }
+    if (!user->profileImage().isNull())
+        setImage(user->profileImage());
 
-//    qDebug() << __PRETTY_FUNCTION__ << " " << topRectHeight << " " << middleRectHeight << " " <<
-//            bottomRectHeight;
+    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"));
 }