Modified FriendListItem.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Tue, 6 Jul 2010 12:57:33 +0000 (15:57 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Tue, 6 Jul 2010 12:57:33 +0000 (15:57 +0300)
src/ui/friendlistitem.cpp
src/ui/friendlistitem.h
src/ui/friendlistitemdelegate.cpp
src/ui/listitem.cpp
src/ui/listitem.h
src/ui/listview.cpp
src/ui/listview.h

index d835d65..3da1105 100644 (file)
@@ -36,9 +36,7 @@
 #include "imagebutton.h"
 #include "../common.h"
 
-const int BACKGROUND_BOTTOM_HEIGHT = 15;
-const int BACKGROUND_TOP_HEIGHT = 20;
-const int BACKGROUND_WIDTH = 368;
+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
@@ -53,26 +51,27 @@ const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height togglin
 *
 * @brief Name label's maximum width
 */
-const int NAME_LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3*MARGIN - IMAGE_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 = BACKGROUND_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_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
 
-FriendListItem::FriendListItem(QWidget *parent)
-    : ListItem(parent)
-    , m_expanded(false)
+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);
 
@@ -105,8 +104,8 @@ FriendListItem::FriendListItem(QWidget *parent)
 //    layout->addLayout(topLayout, 0);
 //    layout->addLayout(bottomLayout, 1);
 
-//    setMinimumSize(BACKGROUND_WIDTH, ITEM_MIN_HEIGHT);
-//    setMaximumWidth(BACKGROUND_WIDTH);
+//    setMinimumSize(ITEM_WIDTH, ITEM_MIN_HEIGHT);
+//    setMaximumWidth(ITEM_WIDTH);
 
 //    setFont(NOKIA_FONT_SMALL);
 //    m_nameLabel->setFont(NOKIA_FONT_NORMAL);
@@ -125,6 +124,54 @@ FriendListItem::FriendListItem(QWidget *parent)
 //        this, SLOT(findButtonClicked()));
 }
 
+void FriendListItem::calculateTextRects()
+{
+    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);
+}
+
+void FriendListItem::setId(const QString &id)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+}
+
+QString FriendListItem::id()
+{
+    return m_user->userId();
+}
+
 void FriendListItem::setUserData(User *user)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -133,15 +180,17 @@ void FriendListItem::setUserData(User *user)
         m_user = user;
 
         if (!m_user->profileImage().isNull())
-            setData(Qt::DisplayRole, m_user->profileImage());
+            setData(Qt::DecorationRole, m_user->profileImage());
 
-//        QString unit;
-//        double value;
-//        m_user->distance(value, unit);
-//        m_distanceTextLabel->setText(QString::number(value) + " " + unit);
-//        setDistanceIcon(value, unit);
+        QString unit;
+        double value;
+        m_user->distance(value, unit);
+        m_distanceText = QString::number(value) + " " + unit;
+        setData(Qt::UserRole + 3, m_distanceText);
+        setDistanceIcon(value, unit);
 
         shortenTexts();
+        calculateTextRects();
         setText(false);
     }
 }
@@ -159,7 +208,9 @@ void FriendListItem::setDistanceIcon(double value, const QString &unit)
     else
         distanceImage.load(":/res/images/rocket_icon_gray.png");
 
-    m_distanceImageLabel->setPixmap(distanceImage);
+    qDebug() << __PRETTY_FUNCTION__ << distanceImage.isNull();
+
+    setData(Qt::UserRole + 4, distanceImage);
 }
 
 void FriendListItem::shortenTexts()
@@ -196,40 +247,88 @@ void FriendListItem::shortenTexts()
         location.append("...");
     }
 
-    int distanceLabelWidth = otherLabelsMetrics.width(m_distanceTextLabel->text());
+    int distanceLabelWidth = otherLabelsMetrics.width(m_distanceText) + 24;
     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_shortenedLocation = otherLabelsMetrics.elidedText(location, Qt::ElideRight, LABEL_MAX_WIDTH);
+                                                          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));
+}
+
+QString FriendListItem::elideText(int width, const QString &text)
+{
+    QFontMetrics otherLabelsMetrics = QFontMetrics(NOKIA_FONT_SMALL);
+    int widthSum = 0;
+    QString t = text;
+
+    for (int i = 0; i < text.size(); ++i) {
+        QChar c = text.at(i);
+
+        widthSum += otherLabelsMetrics.width(c);
+
+        if (widthSum > width) {
+            t.truncate(i - 3);
+            t.append("...");
+            break;
+        }
+    }
+
+    return t;
 }
 
 void FriendListItem::setText(bool expanded)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
+    setData(Qt::DisplayRole, m_shortenedName);
+
     if (expanded) {
-        //setUpdatesEnabled(false);
-        setData(Qt::UserRole, m_shortenedName);
+        setData(Qt::UserRole, m_user->note());
         setData(Qt::UserRole + 1, m_user->timestamp());
-        setData(Qt::UserRole + 2, m_user->note());
-        setData(Qt::UserRole + 3, m_user->address());
-        //setUpdatesEnabled(true);
+        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 {
-        //setUpdatesEnabled(false);
-        setData(Qt::UserRole, m_shortenedName);
+        setData(Qt::UserRole, m_shortenedStatusText);
         setData(Qt::UserRole + 1, m_shortenedUpdated);
-        setData(Qt::UserRole + 2, m_shortenedStatusText);
-        setData(Qt::UserRole + 3, m_shortenedLocation);
-        //setUpdatesEnabled(true);
+        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));
     }
 }
 
-void FriendListItem::findButtonClicked()
+void FriendListItem::toggleHeight()
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    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));
+    }
+}
+
+void FriendListItem::setExpanded(bool expanded)
+{
+    m_expanded = expanded;
 
-    emit findFriend(m_user->coordinates());
+    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));
+    }
 }
index d4f4433..8faece2 100644 (file)
@@ -36,7 +36,7 @@
 */
 class FriendListItem : public ListItem
 {
-    Q_OBJECT
+    //Q_OBJECT
 
 public:
     /**
@@ -46,7 +46,7 @@ public:
     * for this class.
     * @param parent parent widget
     */
-    FriendListItem(QWidget *parent = 0);
+    FriendListItem();
 
 
 /******************************************************************************
@@ -60,6 +60,12 @@ public:
     */
     void setUserData(User *user);
 
+    void toggleHeight();
+    void setExpanded(bool expanded);
+
+    void setId(const QString &id);
+    QString id() const;
+
 private:
     /**
     * @brief Set distance icon.
@@ -85,22 +91,25 @@ private:
     */
     void shortenTexts();
 
-private slots:
-    /**
-    * @brief Slot for find button click
-    */
-    void findButtonClicked();
+    QString elideText(int width, const QString &text);
+
+    void calculateTextRects();
+//private slots:
+//    /**
+//    * @brief Slot for find button click
+//    */
+//    void findButtonClicked();
 
 /******************************************************************************
 * SIGNALS
 ******************************************************************************/
-signals:
-    /**
-    * @brief Signal for finding friend.
-    *
-    * @param coordinates friend's geo coordinates
-    */
-    void findFriend(const QPointF &coordinates);
+//signals:
+//    /**
+//    * @brief Signal for finding friend.
+//    *
+//    * @param coordinates friend's geo coordinates
+//    */
+//    void findFriend(const QPointF &coordinates);
 
 /******************************************************************************
 * DATA MEMBERS
@@ -108,10 +117,15 @@ signals:
 private:
     bool m_expanded;                ///< Item expanded state
     QPoint m_mousePosition;         ///< Current mouse press position
+    QRect m_locationRect;
+    QRect m_statusTextRect;
+    QRect m_updatedRect;
+    int m_expandedHeight;
     QString m_shortenedLocation;    ///< Shortened location text
     QString m_shortenedName;        ///< Shortened name text
     QString m_shortenedStatusText;  ///< Shortened status text
     QString m_shortenedUpdated;     ///< Shortened updated text
+    QString m_distanceText;         ///< Distance text
     User *m_user;                   ///< User data
 };
 
index f985b41..ad5dce3 100644 (file)
@@ -2,11 +2,33 @@
 #include <QPainter>
 
 #include "friendlistitemdelegate.h"
+#include "../common.h"
 
-const int IMAGE_HEIGHT = 64; ///< Friend image height
-const int IMAGE_WIDTH = 64;  ///< Friend image width
+const int BACKGROUND_BOTTOM_HEIGHT = 15;
+const int BACKGROUND_TOP_HEIGHT = 20;
+const int BACKGROUND_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 = BACKGROUND_WIDTH - 3*MARGIN - IMAGE_WIDTH;
+
+/**
+* @var LABEL_MAX_WIDTH
+*
+* @brief All label's maximum width
+*/
+const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH;
 
 FriendListItemDelegate::FriendListItemDelegate()
 {
@@ -24,18 +46,48 @@ void FriendListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem
 
     ListItemDelegate::paint(painter, option, index);
 
+    painter->setPen(COLOR_GRAY);
+    painter->setFont(NOKIA_FONT_SMALL);
+
+    QString statusText = index.data(Qt::UserRole).toString();
+    QString address = index.data(Qt::UserRole + 1).toString();
+    QString timestamp = index.data(Qt::UserRole + 2).toString();
+    QString distance = index.data(Qt::UserRole + 3).toString();
+    QPixmap distanceIcon = QPixmap(qvariant_cast<QPixmap>(index.data(Qt::UserRole + 4)));
+
     QRect itemRect = option.rect;
 
-    QRect envelopeRect = QRect(itemRect.left() + IMAGE_WIDTH + 15, itemRect.top() + IMAGE_HEIGHT,
+    QPoint distanceIconPoint = QPoint(BACKGROUND_WIDTH - ICON_WIDTH - 15, itemRect.top() + 5);
+    QRect distanceRect = index.data(Qt::SizeHintRole + 4).toRect();
+    distanceRect.translate(BACKGROUND_WIDTH - distanceRect.width(), itemRect.top() + ICON_HEIGHT);
+    qDebug() << __PRETTY_FUNCTION__ << distanceRect << distance;
+    painter->drawPixmap(distanceIconPoint, distanceIcon);
+    painter->drawText(distanceRect, Qt::TextWordWrap, distance);
+
+    QRect envelopeRect = QRect(itemRect.left() + IMAGE_WIDTH + 15, itemRect.top() + IMAGE_HEIGHT - 10,
                                ICON_WIDTH, ICON_HEIGHT);
-    QRect compassRect = QRect(envelopeRect.left(), envelopeRect.bottom() + 5, ICON_WIDTH,
-                              ICON_HEIGHT);
-    QRect clockRect = QRect(envelopeRect.left(), compassRect.bottom() + 5, ICON_WIDTH,
-                              ICON_HEIGHT);
+    QRect statusTextRect = index.data(Qt::SizeHintRole + 1).toRect();
+    statusTextRect.translate(envelopeRect.right() + 5, envelopeRect.top());
 
     painter->drawPixmap(envelopeRect, m_envelopeImage);
+    painter->drawText(statusTextRect, Qt::TextWordWrap, statusText);
+
+
+    QRect compassRect = QRect(envelopeRect.left(), statusTextRect.bottom() + 2, ICON_WIDTH,
+                              ICON_HEIGHT);
+    QRect locationRect = index.data(Qt::SizeHintRole + 2).toRect();
+    locationRect.translate(compassRect.right() + 5, compassRect.top());
+
     painter->drawPixmap(compassRect, m_compassImage);
+    painter->drawText(locationRect, Qt::TextWordWrap, address);
+
+
+    QRect clockRect = QRect(envelopeRect.left(), locationRect.bottom() + 2, ICON_WIDTH, ICON_HEIGHT);
+    QRect timestampRect = index.data(Qt::SizeHintRole + 3).toRect();
+    timestampRect.translate(clockRect.right() + 5, clockRect.top());
+
     painter->drawPixmap(clockRect, m_clockImage);
+    painter->drawText(timestampRect, Qt::TextWordWrap, timestamp);
 }
 
 QSize FriendListItemDelegate::sizeHint(const QStyleOptionViewItem &option,
index 389c8c9..ac17ef3 100644 (file)
@@ -6,8 +6,6 @@ ListItem::ListItem()
     : m_expanded(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
-
-    setData(Qt::SizeHintRole, QSize(368, 161));
 }
 
 QPixmap ListItem::image() const
@@ -35,25 +33,3 @@ void ListItem::setName(const QString &name)
 
     m_name = name;
 }
-
-void ListItem::toggleHeight()
-{
-    if (m_expanded) {
-        m_expanded = false;
-        setData(Qt::SizeHintRole, QSize(368, 161));
-    }
-    else {
-        m_expanded = true;
-        setData(Qt::SizeHintRole, QSize(368, 241));
-    }
-}
-
-void ListItem::setExpanded(bool expanded)
-{
-    m_expanded = expanded;
-
-    if (m_expanded)
-        setData(Qt::SizeHintRole, QSize(368, 241));
-    else
-        setData(Qt::SizeHintRole, QSize(368, 161));
-}
index 75a7680..0017bc2 100644 (file)
@@ -14,8 +14,10 @@ public:
     QString name() const;
     void setImage(const QPixmap &image);
     void setName(const QString &neame);
-    void toggleHeight();
-    void setExpanded(bool expanded);
+    virtual void toggleHeight() = 0;
+    virtual void setExpanded(bool expanded) = 0;
+    virtual QString id() const = 0;
+    virtual void setId(const QString &id) = 0;
 
 private:
     bool m_expanded;
@@ -24,6 +26,4 @@ private:
     QString m_name;
 };
 
-Q_DECLARE_METATYPE(ListItem)
-
 #endif // LISTITEM_H
index 984bc80..408439e 100644 (file)
@@ -34,6 +34,7 @@ void ListView::listItemClicked(QListWidgetItem *item)
         }
 
         previousItem = currentItem;
+        emit listItemClicked(currentItem->id());
     }
 }
 
index 6f3c595..99dbd0b 100644 (file)
@@ -16,6 +16,7 @@ public:
  ******************************************************************************/
 
 signals:
+    void listItemClicked(const QString &id);
 
 public slots: