Made a few cosmetic changes
[situare] / src / ui / friendlistitem.cpp
index f93c80e..e3e5996 100644 (file)
 
 #include "friendlistitem.h"
 #include "../user/user.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
+#include "avatarimage.h"
+#include "imagebutton.h"
 
 FriendListItem::FriendListItem(QWidget *parent)
     : QWidget(parent)
@@ -60,7 +44,9 @@ FriendListItem::FriendListItem(QWidget *parent)
     qDebug() << __PRETTY_FUNCTION__;
 
     QVBoxLayout *layout = new QVBoxLayout(this);
-    this->setLayout(layout);
+    layout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
+    layout->setSpacing(0);
+    setLayout(layout);
 
     QHBoxLayout *topLayout = new QHBoxLayout();
     topLayout->setMargin(0);
@@ -73,32 +59,32 @@ FriendListItem::FriendListItem(QWidget *parent)
     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));
-
+    infoLayout->setLabelAlignment(Qt::AlignTop);
 
     QLabel *clockLabel = new QLabel();
-    clockLabel->setPixmap(QPixmap(CLOCK_PATH));
-    clockLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
+    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(ENVELOPE_PATH));
-    envelopeLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
+    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(COMPASS_PATH));
-    compassLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
+    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);
-    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_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);
@@ -107,56 +93,98 @@ FriendListItem::FriendListItem(QWidget *parent)
     m_locationLabel = new QLabel();
     m_locationLabel->setWordWrap(true);
 
-    infoLayout->addRow(clockLabel, m_updatedLabel);
     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
     infoLayout->addRow(compassLabel, m_locationLabel);
+    infoLayout->addRow(clockLabel, m_updatedLabel);
 
     topLayout->addWidget(m_imageLabel);
-    topLayout->addWidget(m_nameLabel);
+    topLayout->addWidget(m_nameLabel, 1);
+    topLayout->addWidget(m_distanceLabel);
 
-    bottomLayout->addSpacing(IMAGE_WIDTH);
-    bottomLayout->addLayout(infoLayout, 1);
+    bottomLayout->addWidget(m_findButton, 0, Qt::AlignTop);
+    bottomLayout->addLayout(infoLayout);
 
     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);
+
+    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()));
 }
 
 void FriendListItem::setData(User *user)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (user) {
-        m_user = user;
-        shortenTexts();
+    m_user = user;
 
-        m_imageLabel->setPixmap(m_user->profileImage());
-        setText(false);
-    }
+    m_imageLabel->setPixmap(AvatarImage::create(m_user->profileImage()));
+
+    QString unit;
+    double value;
+    user->distance(value, unit);
+    m_distanceLabel->setText(QString::number(value) + " " + unit);
+
+    shortenTexts();
+    setText(false);
 }
 
 void FriendListItem::shortenTexts()
 {
     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);
+    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("...");
+    }
+
+    int distanceLabelWidth = otherLabelsMetrics.width(m_distanceLabel->text());
+    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);
 }
 
 void FriendListItem::setText(bool expanded)
@@ -164,16 +192,20 @@ void FriendListItem::setText(bool expanded)
     qDebug() << __PRETTY_FUNCTION__;
 
     if (expanded) {
-        m_nameLabel->setText(m_user->name());
+        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);
     }
 }
 
@@ -188,7 +220,8 @@ void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
 
-    if (abs(m_mousePosition.y() - event->pos().y()) < 10) {
+    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;
@@ -204,51 +237,20 @@ void FriendListItem::paintEvent(QPaintEvent *event)
 {
     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();
-
-//    QRect topRect;
+    QPainter painter(this);
 
-//    if (topRectHeight > 0) {
-//        topRect = QRect(event->rect().left(), event->rect().top(), 368,
-//                              topRectHeight);
+    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, QPixmap(BACKGROUND_TOP_PATH), QRect(0, event->rect().top(), 368, topRect.height()));
-//    }
-
-//    int middleRectHeight = event->rect().height() - topRect.height();
-
-//    int bottomRectHeight = 15 - (148 - event->rect().height());
-
-//    if (bottomRectHeight > 0)
-//        middleRectHeight -= bottomRectHeight;
-
-
-//    QRect middleRect = QRect(event->rect().left(), topRect.bottom() + 1, 368, middleRectHeight);
-
-////    painter.drawPixmap(middleRect, QPixmap(BACKGROUND_MIDDLE_PATH));
-
-
-//    if (bottomRectHeight > 0) {
-//        QRect bottomRect = QRect(event->rect().left(), middleRect.bottom() + 1, 368,
-//                                 bottomRectHeight);
-
-//        painter.drawPixmap(bottomRect, QPixmap(BACKGROUND_BOTTOM_PATH), QRect(0, 0, 368,
-//                                                                              bottomRectHeight));
-//    }
-
-//    qDebug() << __PRETTY_FUNCTION__ << " " << topRectHeight << " " << middleRectHeight << " " <<
-//            bottomRectHeight;
+    painter.drawPixmap(topRect, m_backgroundTopImage);
+    painter.drawPixmap(middleRect, m_backgroundMiddleImage);
+    painter.drawPixmap(bottomRect, m_backgroundBottomImage);
+}
 
+void FriendListItem::findButtonClicked()
+{
+    emit findFriend(m_user->coordinates());
 }