Sorted includes alphabetically.
[situare] / src / ui / friendlistitem.cpp
index f99aad8..f2e4e05 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 "avatarimage.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 MARGIN = 5;   ///< Icon margin
-const int IMAGE_HEIGHT = 60; ///< Friend image height
-const int IMAGE_WIDTH = 60;  ///< Friend image width
+#include "../common.h"
+#include "listcommon.h"
 
-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
+#include "friendlistitem.h"
 
-const QColor COLOR_GRAY = QColor(152, 152, 152);
+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
 
-FriendListItem::FriendListItem(QWidget *parent)
-    : QWidget(parent)
-    , m_expanded(false)
-    , m_user(0)
+FriendListItem::FriendListItem()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QVBoxLayout *layout = new QVBoxLayout(this);
-    layout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
-    layout->setSpacing(0);
-    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, MARGIN, 0);
-    QLabel *envelopeLabel = new QLabel();
-    envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
-    envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
-    QLabel *compassLabel = new QLabel();
-    compassLabel->setPixmap(QPixmap(COMPASS_PATH));
-    compassLabel->setContentsMargins(0, 0, MARGIN, 0);
-
-    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_distanceLabel->setAlignment(Qt::AlignHCenter | Qt::AlignRight);
-//    m_distanceLabel->setMinimumWidth(IMAGE_WIDTH + 15);
-//    m_distanceLabel->setMaximumWidth(IMAGE_WIDTH + 15);
-
-    QLabel *button = new QLabel();
-    button->setPixmap(QPixmap("res/images/show_position.png"));
-    button->setFixedSize(IMAGE_WIDTH, 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(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(button, 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);
+    setSubitemTextWidth(SUBITEM_TEXT_MAX_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);
+GeoCoordinate FriendListItem::coordinates()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    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");
+    return m_coordinates;
 }
 
-void FriendListItem::setData(User *user)
+void FriendListItem::setUserData(User *user)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (user) {
-        m_user = user;
-        shortenTexts();
-        m_imageLabel->setPixmap(AvatarImage::make(m_user->profileImage()));
+    if(user) {
+
         QString unit;
         double value;
         user->distance(value, unit);
-        m_distanceLabel->setText(QString::number(value) + " " + unit);
-        setText(false);
+        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::shortenTexts()
+void FriendListItem::setAvatarImage(const QPixmap &image)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
-    QFontMetrics otherLabelsMetrics = m_updatedLabel->fontMetrics();
-
-    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("...");
-    }
-
-
-    m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, LABEL_MAX_WIDTH + 30);
-    m_shortenedUpdated = otherLabelsMetrics.elidedText(updated, Qt::ElideRight, LABEL_MAX_WIDTH);
-    m_shortenedStatusText = otherLabelsMetrics.elidedText(statusText, Qt::ElideRight,
-                                                          LABEL_MAX_WIDTH);
-    m_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
+    if(!image.isNull())
+        setImage(image);
 }
 
-void FriendListItem::setText(bool expanded)
+void FriendListItem::setCoordinates(const GeoCoordinate &coordinates)
 {
     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);
-    }
+    m_coordinates = coordinates;
 }
 
-void FriendListItem::mousePressEvent(QMouseEvent *event)
+void FriendListItem::setDistanceIcon(double value, const QString &unit)
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_mousePosition = event->pos();
-}
+    QPixmap distanceImage;
 
-void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
-{
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+    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");
 
-    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(DISTANCE_IMAGE_INDEX, distanceImage);
 }
 
-void FriendListItem::paintEvent(QPaintEvent *event)
+FriendListItem::~FriendListItem()
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
-
-    QPainter painter(this);
-
-    QRect topRect = QRect(0, 0, event->rect().width(), 20);
-    QRect middleRect = QRect(0, topRect.bottom(), event->rect().width(),
-                             height() - 20 - 15);
-    QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), event->rect().width(),
-                             15);
-
-    painter.drawPixmap(topRect, m_backgroundTopImage);
-    painter.drawPixmap(middleRect, m_backgroundMiddleImage);
-    painter.drawPixmap(bottomRect, m_backgroundBottomImage);
+    qDebug() << __PRETTY_FUNCTION__;
 }