#include "friendlistitem.h"
#include "user/user.h"
-const int BACKGROUND_TOP_WIDTH = 368;
-const int BACKGROUND_TOP_HEIGHT = 20;
-const int BACKGROUND_MIDDLE_X = 0;
-const int BACKGROUND_MIDDLE_Y = BACKGROUND_TOP_HEIGHT;
-const int BACKGROUND_MIDDLE_WIDTH = 368;
-const int BACKGROUND_MIDDLE_HEIGHT = 106;
-const int BACKGROUND_BOTTOM_X = 0;
-const int BACKGROUND_BOTTOM_Y = BACKGROUND_MIDDLE_Y + BACKGROUND_MIDDLE_HEIGHT;
-const int BACKGROUND_BOTTOM_WIDTH = 368;
-const int BACKGROUND_BOTTOM_HEIGHT = 15;
-const int IMAGE_X = 9;
-const int IMAGE_Y = 0;
const int IMAGE_WIDTH = 60;
const int IMAGE_HEIGHT = 60;
-const int CLOCK_X = 75;
-const int CLOCK_Y = 53;
-const int ENVELOPE_X = 75;
-const int ENVELOPE_Y = 77;
-const int COMPASS_X = 75;
-const int COMPASS_Y = 101;
-const int ICON_WIDTH = 24;
-const int ICON_HEIGHT = 24;
-
-const QString BACKGROUND_TOP_PATH = QString(":/res/images/list_item_top.png");
-const QString BACKGROUND_MIDDLE_PATH = QString(":/res/images/list_item_middle.png");
-const QString BACKGROUND_BOTTOM_PATH = QString(":/res/images/list_item_bottom.png");
+
+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)
{
- QGridLayout *layout = new QGridLayout(this);
+ QVBoxLayout *layout = new QVBoxLayout(this);
this->setLayout(layout);
- m_childWidget = new QWidget(this);
- QPalette palette = m_childWidget->palette();
- palette.setColor(QPalette::Foreground, QColor::fromRgb(152, 152, 152));
- m_childWidget->setPalette(palette);
- QFont font = QFont("Nokia Sans", 13, QFont::Normal);
- m_childWidget->setFont(font);
+ 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);
- m_childWidget->setLayout(infoLayout);
+ infoLayout->setMargin(0);
+ infoLayout->setSpacing(0);
+ m_infoWidget->setLayout(infoLayout);
QLabel *clockLabel = new QLabel(this);
clockLabel->setPixmap(QPixmap(CLOCK_PATH));
- clockLabel->setMinimumSize(ICON_WIDTH, ICON_HEIGHT);
+ clockLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
QLabel *envelopeLabel = new QLabel(this);
envelopeLabel->setPixmap(QPixmap(ENVELOPE_PATH));
- envelopeLabel->setMinimumSize(ICON_WIDTH, ICON_HEIGHT);
+ envelopeLabel->setContentsMargins(0, 0, ICON_MARGIN, 0);
QLabel *compassLabel = new QLabel(this);
compassLabel->setPixmap(QPixmap(COMPASS_PATH));
- compassLabel->setMinimumSize(ICON_WIDTH, ICON_HEIGHT);
+ 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_updatedLabel->setMinimumWidth(ICON_WIDTH);
m_statusTextLabel = new QLabel("", this);
m_statusTextLabel->setWordWrap(true);
- m_statusTextLabel->setMinimumWidth(ICON_WIDTH);
m_locationLabel = new QLabel("", this);
m_locationLabel->setWordWrap(true);
- m_locationLabel->setMinimumWidth(ICON_WIDTH);
infoLayout->addRow(clockLabel, m_updatedLabel);
infoLayout->addRow(envelopeLabel, m_statusTextLabel);
infoLayout->addRow(compassLabel, m_locationLabel);
- infoLayout->setLabelAlignment(Qt::AlignTop);
- infoLayout->setFormAlignment(Qt::AlignCenter);
+ topLayout->addWidget(m_imageLabel);
+ topLayout->addWidget(m_nameLabel);
- QPalette childPal = m_childWidget->palette();
- childPal.setColor(QPalette::Background, Qt::red);
- m_childWidget->setPalette(childPal);
+ QLabel *tmp = new QLabel(this);
+ bottomLayout->addSpacerItem(new QSpacerItem(IMAGE_WIDTH, IMAGE_HEIGHT));
+ bottomLayout->addWidget(m_infoWidget, 1);
- //m_childWidget->setGeometry(75, 53, 368-75-5, 141-53-5);
-
- layout->addWidget(m_imageLabel, 0, 0);
- layout->addWidget(m_nameLabel, 0, 1);
- layout->addWidget(m_childWidget, 1, 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; }");
-
- this->setMinimumSize(368, 141);
+ "border-width: 20px 14px 16px 14px; } " \
+ "QLabel { font-size: 13pt; color: #989898; }" \
+ "#nameLabel { font-size: 18pt; color: #ffffff }");
-// this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+ this->setMinimumSize(ITEM_MIN_WIDTH, ITEM_MIN_HEIGHT);
+ this->setMaximumSize(ITEM_MAX_WIDTH, ITEM_MAX_HEIGHT);
}
void FriendListItem::setData(const User &user)
{
- m_imageLabel->setPixmap(QPixmap(":/res/images/dummy_Avatar.png"));
- m_nameLabel->setText(user.name());
- m_updatedLabel->setText(user.timestamp());
- m_statusTextLabel->setText(user.note());
- m_locationLabel->setText(user.address());
+ 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();
- QFontMetrics fontMetrics = QFontMetrics(QFont("Nokia Sans", 13, QFont::Normal));
+}
- qDebug() << "Status text metrics: " << fontMetrics.boundingRect(user.note());
+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)
{
qDebug() << __PRETTY_FUNCTION__;
- if (event->pos() == m_mousePosition) {
-
- emit changeState();
-
- if (m_expanded) {
- m_expanded = false;
- this->setGeometry(0, 0, this->width(), 141);
- m_childWidget->setGeometry(75, 53, 368-75-5, 141-53-5);
- m_childWidget->updateGeometry();
-
- }
- else {
- m_expanded = true;
- this->setGeometry(0, 0, this->width(), 200);
- m_childWidget->setGeometry(75, 53, 368-75-5, 250-53-5);
- m_childWidget->updateGeometry();
- }
-
- updateGeometry();
- emit changeState();
- }
+ if (event->pos() == m_mousePosition)
+ toggleHeight();
}
-QSize FriendListItem::sizeHint() const
+void FriendListItem::paintEvent(QPaintEvent *)
{
- qDebug() << __PRETTY_FUNCTION__ << " " << m_expanded;
-
- if (m_expanded)
- return QSize(this->width(), 250);
- else
- return QSize(this->width(), 141);
-}
+ QStyleOption option;
+ option.init(this);
-void FriendListItem::paintEvent(QPaintEvent *event)
-{
- QStyleOption opt;
- opt.init(this);
QPainter painter(this);
- style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
-
- QRect itemRect = event->rect();
-
-// if (m_expanded)
-// itemRect = QRect(0, 0, 568, 200);
-// else
-// itemRect = QRect(0, 0, 568, 141);
-//
-// //Draw background
-// QRect topRect = QRect(itemRect.left(), itemRect.top(), BACKGROUND_TOP_WIDTH,
-// BACKGROUND_TOP_HEIGHT);
-// painter.drawPixmap(topRect, QPixmap(BACKGROUND_TOP_PATH));
-// qDebug() << "Top rect : " << topRect;
-//
-// QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_MIDDLE_WIDTH,
-// itemRect.height() - BACKGROUND_BOTTOM_HEIGHT - BACKGROUND_TOP_HEIGHT);
-// painter.drawPixmap(middleRect, QPixmap(BACKGROUND_MIDDLE_PATH));
-// qDebug() << "Middle rect: " << middleRect;
-//
-// QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_BOTTOM_WIDTH,
-// BACKGROUND_BOTTOM_HEIGHT);
-// painter.drawPixmap(bottomRect, QPixmap(BACKGROUND_BOTTOM_PATH));
-// qDebug() << "Bottom rect: " << bottomRect;
-//
-// //Draw image
-// QString image = QString(":/res/images/dummy_Avatar.png");
-// QRect imageRect = QRect(itemRect.left() + IMAGE_X, itemRect.top() + IMAGE_Y,
-// IMAGE_WIDTH, IMAGE_HEIGHT);
-// painter.drawPixmap(imageRect, QPixmap(image));
-//
-// //Draw name
-// QRect nameRect = QRect(itemRect.left() + IMAGE_WIDTH + 11, itemRect.top() + 15, itemRect.width(),
-// itemRect.height());
-// QPen whitePen(QColor(Qt::white), 1, Qt::SolidLine);
-// painter.setPen(whitePen);
-// painter.setFont(QFont( "Nokia Sans", 18, QFont::Normal));
-// painter.drawText(nameRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, m_name, &nameRect);
-
-// //Draw image
-// QString image = QString(":/res/images/dummy_Avatar.png");
-// QRect imageRect = QRect(IMAGE_X, IMAGE_Y,
-// IMAGE_WIDTH, IMAGE_HEIGHT);
-// painter.drawPixmap(imageRect, QPixmap(image));
-//
-// //Draw name
-// QRect nameRect = QRect(IMAGE_WIDTH + 11, 15, itemRect.width(),
-// 50);
-// QPen whitePen(QColor(Qt::white), 1, Qt::SolidLine);
-// painter.setPen(whitePen);
-// painter.setFont(QFont( "Nokia Sans", 18, QFont::Normal));
-// painter.drawText(nameRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, m_name, &nameRect);
+ style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
}