Moved location update logic to new class called UpdateLocation.
[situare] / src / ui / userinfo.cpp
index e8c8614..9301e6c 100644 (file)
@@ -6,6 +6,7 @@
        Jussi Laitinen - jussi.laitinen@ixonos.com
        Katri Kaikkonen - katri.kaikkonen@ixonos.com
        Henri Lampela - henri.lampela@ixonos.com
+       Ville Tiensuu - ville.tiensuu@ixonos.com
 
    Situare is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    USA.
 */
 
+#include <QFormLayout>
+#include <QLabel>
+#include <QMouseEvent>
+#include <QPainter>
+#include <QSettings>
+#include <QVBoxLayout>
+
+#include "common.h"
 #include "imagebutton.h"
-#include "../user/user.h"
+#include "textmodifier.h"
+#include "user/user.h"
+
 #include "userinfo.h"
 
-const int BACKGROUND_TOP_HEIGHT = 20;
 const int BACKGROUND_BOTTOM_HEIGHT = 15;
-const QColor COLOR_GRAY = QColor(152, 152, 152);    ///< Gray color
-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
-const int ICON_HEIGHT = 24;     ///< Icon height
-const int ICON_WIDTH = 24;       ///< Icon width
-const int IMAGE_HEIGHT = 60; ///< Friend image height
-const int IMAGE_WIDTH = 60;  ///< Friend image width
-const int ITEM_MIN_WIDTH = 250;  ///< Minimum width 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 LABEL_MAX_WIDTH
-*
-* @brief All label's maximum width
-*/
-const int LABEL_MAX_WIDTH = ITEM_MIN_WIDTH - 3*MARGIN - ICON_WIDTH;
-
-const int NAME_LABEL_MAX_WIDTH = LABEL_MAX_WIDTH + 30;
+const int BACKGROUND_TOP_HEIGHT = 20;
+const int BACKGROUND_WIDTH = 368;
+const int ICON_HEIGHT = 24;
+const int ICON_WIDTH = 24;
+const int MARGIN = 5;
+const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - ICON_WIDTH - 5 * MARGIN;
 
 UserInfo::UserInfo(QWidget *parent)
-    : QWidget(parent)
-        , m_expanded(false)
+    : QWidget(parent),
+      m_expanded(false)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
-    verticalLayout->setContentsMargins(MARGIN, 0, MARGIN*2, MARGIN*2);
+    verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
     verticalLayout->setSpacing(0);
     setLayout(verticalLayout);
 
-    QFormLayout *infoLayout = new QFormLayout(this);
+    QFormLayout *infoLayout = new QFormLayout();
     infoLayout->setMargin(0);
     infoLayout->setSpacing(0);
 
-    QHBoxLayout *buttonLayout = new QHBoxLayout(this);
-    buttonLayout->setMargin(0);
-    buttonLayout->setSpacing(0);
-
-    QLabel *clockLabel = new QLabel();
-    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(":/res/images/envelope.png"));
     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
@@ -78,44 +68,37 @@ UserInfo::UserInfo(QWidget *parent)
     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
+    QLabel *clockLabel = new QLabel();
+    clockLabel->setPixmap(QPixmap(":/res/images/clock.png"));
+    clockLabel->setContentsMargins(0, 0, MARGIN, 0);
+    clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
 
-    m_imageLabel = new QLabel();
-    m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
-    m_imageLabel->setAlignment(Qt::AlignHCenter);
+    m_avatar = new ImageButton();
 
     m_nameLabel = new QLabel();
-    m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
-    m_nameLabel->setAlignment(Qt::AlignHCenter);
 
-    m_updatedLabel = new QLabel();
-    m_updatedLabel->setWordWrap(true);
     m_statusTextLabel = new QLabel();
     m_statusTextLabel->setWordWrap(true);
+    m_statusTextLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
     m_locationLabel = new QLabel();
     m_locationLabel->setWordWrap(true);
+    m_locationLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
+    m_updatedLabel = new QLabel();
+    m_updatedLabel->setWordWrap(true);
+    m_updatedLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
 
-    ImageButton *updateFriendsButton = new ImageButton(this, ":/res/images/refresh.png",
-                                                             ":/res/images/refresh_s.png");
-    ImageButton *updateStatusMessageButton = new ImageButton(this, ":/res/images/send_position.png",
-                                                                   ":/res/images/send_position_s.png");
-
-    buttonLayout->addWidget(updateFriendsButton);
-    buttonLayout->addWidget(updateStatusMessageButton);
-
-    infoLayout->addRow(compassLabel, m_locationLabel);
     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
+    infoLayout->addRow(compassLabel, m_locationLabel);
     infoLayout->addRow(clockLabel, m_updatedLabel);
 
-    verticalLayout->addWidget(m_imageLabel, 0, Qt::AlignHCenter);
-
-    verticalLayout->addWidget(m_nameLabel, 1);
+    verticalLayout->addWidget(m_avatar, 0, Qt::AlignHCenter);
+    verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
     verticalLayout->addLayout(infoLayout);
-    verticalLayout->addLayout(buttonLayout, 2);
 
-    connect(updateStatusMessageButton,SIGNAL(clicked()),
-            this,SLOT(messageUpdate()));
-    connect(updateFriendsButton,SIGNAL(clicked()),
-            this, SIGNAL(refreshUserData()));
+    connect(m_avatar, SIGNAL(clicked()),
+            this, SLOT(findButtonClicked()));
+
+    setFixedWidth(BACKGROUND_WIDTH);
 
     this->setFont(NOKIA_FONT_SMALL);
     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
@@ -129,132 +112,130 @@ UserInfo::UserInfo(QWidget *parent)
     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");
-
-    this->setMinimumWidth(ITEM_MIN_WIDTH);
 }
 
-void UserInfo::setUserName(const QString &name)
+UserInfo::~UserInfo()
 {
     qDebug() << __PRETTY_FUNCTION__;
-
-    m_userName = name;
-    setText(false);
 }
 
-void UserInfo::setAvatar(const QPixmap &image)
+void UserInfo::collapse()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_imageLabel->setPixmap(image);
+    setExpanded(false);
 }
 
-void UserInfo::setMessageText(const QString &text)
+void UserInfo::findButtonClicked()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_messageText = text;
-    setText(false);
+    emit findUser(m_coordinates);
 }
 
-void UserInfo::setAddress(const QString &addr)
+void UserInfo::mousePressEvent(QMouseEvent *event)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
 
-    m_locationLabel->setText(addr);
+    m_mousePosition = event->pos();
 }
 
-void UserInfo::setTime(const QString &tim)
+void UserInfo::mouseReleaseEvent(QMouseEvent *event)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
 
-    m_updatedLabel->setText(tim);
+    const int MOUSE_PRESS_AREA_HEIGHT = 20;
+    const int MOUSE_PRESS_AREA_WIDTH = 20;
+
+    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) {
+            setExpanded(false);
+            m_expanded = false;
+        }
+        else {
+            setExpanded(true);
+            m_expanded = true;
+        }
+    }
 }
 
-QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
+void UserInfo::paintEvent(QPaintEvent *event)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
 
-    QFontMetrics labelMetrics = label->fontMetrics();
+    QPainter painter(this);
 
-    QString textParam = text;
-    int index = textParam.indexOf('\n');
+    QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
 
-    if (index > 0) {
-        textParam.truncate(index);
-        textParam.append("...");
-    }
+    QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH,
+                             this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
 
-   return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
+    QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH,
+                             BACKGROUND_BOTTOM_HEIGHT);
+
+    painter.drawPixmap(topRect, m_backgroundTopImage);
+    painter.drawPixmap(middleRect, m_backgroundMiddleImage);
+    painter.drawPixmap(bottomRect, m_backgroundBottomImage);
 }
 
-void UserInfo::setText(bool expanded)
+void UserInfo::setAddress(const QString &address)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (expanded) {
-        m_nameLabel->setText(m_userName);
-        m_statusTextLabel->setText(m_messageText);
-    }
-    else {
-        m_nameLabel->setText(shortenText(m_nameLabel, m_userName, NAME_LABEL_MAX_WIDTH));
-        m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
-                                               LABEL_MAX_WIDTH));
-    }
+    m_locationLabel->setText(address);
 }
 
-void UserInfo::mouseReleaseEvent(QMouseEvent *event)
+void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    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;
-        }
-    }
+    m_coordinates = coordinates;
 }
-void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
+
+void UserInfo::setMessageText(const QString &text)
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    QPainter painter(this);
+    m_messageText = TextModifier::splitLongWords(m_statusTextLabel->fontMetrics(), text,
+                                                 LABEL_MAX_WIDTH);
 
-    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);
+    setExpanded(false);
+}
 
-    painter.drawPixmap(topRect, m_backgroundTopImage);
-    painter.drawPixmap(middleRect, m_backgroundMiddleImage);
-    painter.drawPixmap(bottomRect, m_backgroundBottomImage);
+void UserInfo::setProfileImage(const QPixmap &image)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(!image.isNull())
+        m_avatar->setButtonIcon(image);
 }
 
-void UserInfo::mousePressEvent(QMouseEvent *event)
+void UserInfo::setExpanded(bool expanded)
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_mousePosition = event->pos();
+    if (expanded) {
+        m_statusTextLabel->setText(m_messageText);
+    } else {
+        m_statusTextLabel->setText(TextModifier::shortenText(m_statusTextLabel->fontMetrics(),
+                                                             m_messageText, LABEL_MAX_WIDTH));
+    }
 }
 
-void UserInfo::messageUpdate()
+void UserInfo::setTime(const QString &time)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    UpdateLocationDialog updateLocationDialog(this);
+    m_updatedLabel->setText(time);
+}
 
-    emit requestReverseGeo();
+void UserInfo::setUserName(const QString &name)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    connect(this, SIGNAL(reverseGeoReady(QString)),
-            &updateLocationDialog, SLOT(setAddress(QString)));
-    connect(&updateLocationDialog, SIGNAL(statusUpdate(QString, bool)),
-            this, SIGNAL(statusUpdate(QString, bool)));
+    m_userName = name;
 
-    updateLocationDialog.exec();
+    m_nameLabel->setText(TextModifier::shortenText(m_nameLabel->fontMetrics(), m_userName,
+                                                   LABEL_MAX_WIDTH));
 }
-