Integrated friendlistview with dev.
[situare] / src / ui / friendlistitem.cpp
index 71a27f4..ce2ae5c 100644 (file)
 */
 
 #include <QVBoxLayout>
+#include <QPushButton>
+#include <QPainter>
+#include <QDebug>
+#include <QPaintEvent>
+#include <QLabel>
+#include <QPixmap>
+#include <QStateMachine>
+#include <QAbstractTransition>
+#include <QPropertyAnimation>
+#include <QSignalTransition>
+#include <QFontMetrics>
+#include <QFormLayout>
+#include <QSpacerItem>
 
 #include "friendlistitem.h"
+#include "user/user.h"
 
-FriendListItem::FriendListItem()
+const int IMAGE_WIDTH = 60;
+const int IMAGE_HEIGHT = 60;
+
+const int ITEM_MIN_WIDTH = 368;
+const int ITEM_MAX_WIDTH = 368;
+const int ITEM_MIN_HEIGHT = 141;
+const int ITEM_MAX_HEIGHT = 240;
+const int ICON_MARGIN = 5;
+
+const QString BACKGROUND_PATH = QString(":/res/images/list_item.png");
+const QString CLOCK_PATH = QString(":/res/images/clock.png");
+const QString ENVELOPE_PATH = QString(":/res/images/envelope.png");
+const QString COMPASS_PATH = QString(":/res/images/compass.png");
+
+const int MAXIMUM_CHARS = 32;
+
+FriendListItem::FriendListItem(QWidget *parent)
+    : QWidget(parent)
+    , m_expanded(false)
+{
+    QVBoxLayout *layout = new QVBoxLayout(this);
+    this->setLayout(layout);
+
+    QHBoxLayout *topLayout = new QHBoxLayout(this);
+    topLayout->setMargin(0);
+    topLayout->setSpacing(0);
+
+    QHBoxLayout *bottomLayout = new QHBoxLayout(this);
+    bottomLayout->setMargin(0);
+    bottomLayout->setSpacing(0);
+
+    m_infoWidget = new QWidget(this);
+
+    QFormLayout *infoLayout = new QFormLayout(this);
+    infoLayout->setMargin(0);
+    infoLayout->setSpacing(0);
+    m_infoWidget->setLayout(infoLayout);
+
+    QLabel *clockLabel = new QLabel(this);
+    clockLabel->setPixmap(QPixmap(CLOCK_PATH));
+    clockLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
+    QLabel *envelopeLabel = new QLabel(this);
+    envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
+    envelopeLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
+    QLabel *compassLabel = new QLabel(this);
+    compassLabel->setPixmap(QPixmap(COMPASS_PATH));
+    compassLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
+
+    m_imageLabel = new QLabel(this);
+    m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
+    m_nameLabel = new QLabel("", this);
+    m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
+    m_updatedLabel = new QLabel("", this);
+    m_updatedLabel->setWordWrap(true);
+    m_statusTextLabel = new QLabel("", this);
+    m_statusTextLabel->setWordWrap(true);
+    m_locationLabel = new QLabel("", this);
+    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);
+
+    QLabel *tmp = new QLabel(this);
+    bottomLayout->addSpacerItem(new QSpacerItem(IMAGE_WIDTH, IMAGE_HEIGHT));
+    bottomLayout->addWidget(m_infoWidget, 1);
+
+    layout->addLayout(topLayout, 0);
+    layout->addLayout(bottomLayout, 1);
+
+    this->setObjectName("listItem");
+    m_infoWidget->setObjectName("infoWidget");
+    m_nameLabel->setObjectName("nameLabel");
+    this->setStyleSheet("#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 }");
+
+    this->setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
+    this->setMaximumSize(ITEM_MAX_WIDTH, ITEM_MAX_HEIGHT);
+}
+
+void FriendListItem::setData(const User &user)
+{
+    m_user = user;
+
+    shortenTexts();
+
+    m_imageLabel->setPixmap(m_user.profileImage());
+    m_nameLabel->setText(m_shortenedName);
+    m_updatedLabel->setText(m_shortenedUpdated);
+    m_statusTextLabel->setText(m_shortenedStatusText);
+    m_locationLabel->setText(m_shortenedLocation);
+    m_image = user.profileImageUrl().toString();
+
+}
+
+void FriendListItem::shortenTexts()
+{
+    if (m_user.name().length() > MAXIMUM_CHARS) {
+        m_shortenedName = m_user.name().left(MAXIMUM_CHARS-3).append("...");
+    }
+    else {
+         m_shortenedName = m_user.name();
+    }
+    if (m_user.timestamp().length() > MAXIMUM_CHARS) {
+        m_shortenedUpdated = m_user.timestamp().left(MAXIMUM_CHARS-3).append("...");
+    }
+    else {
+        m_shortenedUpdated = m_user.timestamp();
+    }
+    if (m_user.note().length() > MAXIMUM_CHARS) {
+        m_shortenedStatusText = m_user.note().left(MAXIMUM_CHARS-3).append("...");
+    }
+    else {
+        m_shortenedStatusText = m_user.note();
+    }
+    if (m_user.address().length() > MAXIMUM_CHARS) {
+        m_shortenedLocation = m_user.address().left(MAXIMUM_CHARS-3).append("...");
+    }
+    else {
+        m_shortenedLocation = m_user.address();
+    }
+}
+
+void FriendListItem::toggleHeight()
+{
+    if (m_expanded) {
+        m_nameLabel->setText(m_shortenedName);
+        m_updatedLabel->setText(m_shortenedUpdated);
+        m_statusTextLabel->setText(m_shortenedStatusText);
+        m_locationLabel->setText(m_shortenedLocation);
+        m_expanded = false;
+
+    }
+    else {
+        m_nameLabel->setText(m_user.name());
+        m_updatedLabel->setText(m_user.timestamp());
+        m_statusTextLabel->setText(m_user.note());
+        m_locationLabel->setText(m_user.address());
+        m_expanded = true;
+    }
+}
+
+void FriendListItem::mousePressEvent(QMouseEvent *event)
+{
+    m_mousePosition = event->pos();
+}
+
+void FriendListItem::mouseReleaseEvent(QMouseEvent *event)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (event->pos() == m_mousePosition)
+        toggleHeight();
+}
+
+void FriendListItem::paintEvent(QPaintEvent *)
+{
+    QStyleOption option;
+    option.init(this);
+
+    QPainter painter(this);
+    style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
 }