Minor changes to userinfo. Remove word wrap from name label and rename button
[situare] / src / ui / userinfo.cpp
index afb28fb..50dd2cb 100644 (file)
@@ -5,6 +5,8 @@
        Jukka Saastamoinen - jukka.saastamoinen@ixonos.com
        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 <QVBoxLayout>
-#include <QPushButton>
-#include <QPainter>
-#include <QDebug>
-#include <QPaintEvent>
-#include <QLabel>
-#include <QPixmap>
 #include <QFormLayout>
-#include <QSpacerItem>
-#include <QStylePainter>
-#include <math.h>
+#include <QLabel>
+#include <QMouseEvent>
+#include <QPainter>
+#include <QSettings>
+#include <QVBoxLayout>
 
+#include "common.h"
 #include "imagebutton.h"
-#include "avatarimage.h"
-#include "../user/user.h"
+#include "user/user.h"
+
 #include "userinfo.h"
 
+const int BACKGROUND_BOTTOM_HEIGHT = 15;
+const int BACKGROUND_TOP_HEIGHT = 20;
+const int BACKGROUND_WIDTH = 368;
+const int ICON_HEIGHT = 24;
+const int ICON_WIDTH = 24;
+const int LABEL_MAX_WIDTH = 300;
+const int MARGIN = 5;
+
 UserInfo::UserInfo(QWidget *parent)
-    : QWidget(parent)
-        , m_expanded(false)
+    : QWidget(parent),
+      m_expanded(false),
+      m_updateLocation(0)
 {
+    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);
 
-    QGridLayout *gridLayout = new QGridLayout(this);
-    gridLayout->setMargin(0);
-    gridLayout->setSpacing(0);
-
-    QHBoxLayout *topLayout = new QHBoxLayout(this);
-    topLayout->setMargin(0);
-    topLayout->setSpacing(0);
-
-    QFormLayout *infoLayout = new QFormLayout(this);
+    QFormLayout *infoLayout = new QFormLayout();
     infoLayout->setMargin(0);
     infoLayout->setSpacing(0);
 
-    QHBoxLayout *buttonLayout = new QHBoxLayout(this);
+    QHBoxLayout *buttonLayout = new QHBoxLayout();
     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);
@@ -75,51 +72,53 @@ 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_updatedLabel = new QLabel();
-    m_updatedLabel->setWordWrap(true);
     m_statusTextLabel = new QLabel();
     m_statusTextLabel->setWordWrap(true);
     m_locationLabel = new QLabel();
     m_locationLabel->setWordWrap(true);
+    m_updatedLabel = new QLabel();
+    m_updatedLabel->setWordWrap(true);
 
-    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");
+    ImageButton *updateFriendsButton = new ImageButton(":/res/images/refresh.png",
+                                                       ":/res/images/refresh_s.png",
+                                                       "", this);
+    ImageButton *updateStatusMessageButton = new ImageButton(":/res/images/send_position.png",
+                                                             ":/res/images/send_position_s.png",
+                                                             "", this);
 
     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);
 
-    topLayout->addWidget(m_nameLabel);
-
-    gridLayout->addWidget(m_imageLabel);
-
-    verticalLayout->addLayout(gridLayout, 0);
-    verticalLayout->addLayout(topLayout, 1);
+    verticalLayout->addWidget(m_avatar, 0, Qt::AlignHCenter);
+    verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
     verticalLayout->addLayout(infoLayout);
-    verticalLayout->addLayout(buttonLayout, 2 );
+    verticalLayout->addLayout(buttonLayout);
 
     connect(updateStatusMessageButton,SIGNAL(clicked()),
             this,SLOT(messageUpdate()));
+
     connect(updateFriendsButton,SIGNAL(clicked()),
-            this,SLOT(updateFriendsStatus()));
+            this, SIGNAL(refreshUserData()));
+
+    connect(m_avatar, SIGNAL(clicked()),
+            this, SLOT(findButtonClicked()));
+
+    setFixedWidth(BACKGROUND_WIDTH);
 
-    setFont(NOKIA_FONT_SMALL);
+    this->setFont(NOKIA_FONT_SMALL);
     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
     QPalette itemPalette = palette();
     itemPalette.setColor(QPalette::Foreground, COLOR_GRAY);
@@ -128,159 +127,122 @@ UserInfo::UserInfo(QWidget *parent)
     namePalette.setColor(QPalette::Foreground, Qt::white);
     m_nameLabel->setPalette(namePalette);
 
-    m_nameLabel->setText("");
-
-    this->setMaximumHeight(300);
-
     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");
+
+    restoreUnsendMessage();
 }
 
+UserInfo::~UserInfo()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
 
-void UserInfo::setUserName(const QString &name)
+    if (!m_backupMessage.isEmpty()) {
+        settings.setValue(USER_UNSEND_MESSAGE, m_backupMessage.toAscii());
+        settings.setValue(USER_UNSEND_MESSAGE_PUBLISH, m_backupFacebookPublishPolicity);
+    } else {
+        settings.remove(USER_UNSEND_MESSAGE);
+        settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
+    }
+}
+
+void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
 {
     qDebug() << __PRETTY_FUNCTION__;
-    qDebug() << name;
 
-    m_nameLabel->setText(name);
+    m_backupMessage = status;
+    m_backupFacebookPublishPolicity = publish;
+}
 
-//     shortenTexts();
+void UserInfo::clearUpdateLocationDialogData()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_backupMessage.clear();
+    m_backupFacebookPublishPolicity = false;
 }
 
-void UserInfo::setAvatar(const QPixmap &image)
+void UserInfo::collapse()
 {
-//    m_avatar = image;
-//   // m_imageLabel->setPixmap(AvatarImage::create(m_avatar.profileImage()));
-//    //m_imageLabel->setPixmap(AvatarImage::create(m_avatar.profileImage()));
-//    m_imageLabel->setPixmap(m_avatar);
+    qDebug() << __PRETTY_FUNCTION__;
+
+    setExpanded(false);
 }
 
+void UserInfo::findButtonClicked()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-void UserInfo::setMessageText(const QString &text)
+    emit findUser(m_coordinates);
+}
+
+void UserInfo::messageUpdate()
 {
-    if(m_messageText == text)
-      return;
-    m_messageText = text;
-    m_statusTextLabel->setText(m_messageText);
-//    shortenTexts();
+    qDebug() << __PRETTY_FUNCTION__;
+
+    delete m_updateLocation;
+    m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
+                                                this);
+
+    connect(this, SIGNAL(reverseGeoReady(QString)),
+            m_updateLocation, SLOT(setAddress(QString)));
+
+    connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
+            this, SIGNAL(statusUpdate(QString, bool)));
+
+    connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)),
+            this, SLOT(backupUpdateLocationDialogData(QString, bool)));
+
+    connect(m_updateLocation, SIGNAL(finished(int)),
+            this, SLOT(updateLocationDialogFinished(int)));
+
+    m_updateLocation->show();
+
+    emit requestReverseGeo();
 }
 
-void UserInfo::setAddress(const QString &addr)
+void UserInfo::mousePressEvent(QMouseEvent *event)
 {
-    if(m_address == addr)
-      return;
-    m_address = addr;
-    m_locationLabel->setText(m_address);
-//    shortenTexts();
+    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+
+    m_mousePosition = event->pos();
 }
 
-void UserInfo::setTime(const QString &tim)
+void UserInfo::mouseReleaseEvent(QMouseEvent *event)
 {
-    if(m_time == tim)
-      return;
-    m_time = tim;
-    m_updatedLabel->setText(m_time);
-//    shortenTexts();
+    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+
+    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;
+        }
+    }
 }
 
-//void UserInfo::shortenTexts()
-//{
-//    qDebug() << __PRETTY_FUNCTION__;
-//
-//    QFontMetrics nameLabelMetrics = m_nameLabel->fontMetrics();
-//    QFontMetrics otherLabelsMetrics = m_locationLabel->fontMetrics();
-//
-//    QString name = m_userName;
-//    QString updated = m_time;
-//    QString statusText = m_messageText;
-//    QString location = m_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("...");
-//    }
-//
-//    m_shortenedName = nameLabelMetrics.elidedText(name, Qt::ElideRight, LABEL_MAX_WIDTH + 30);
-//    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 UserInfo::setText(bool expanded)
-//{
-//    qDebug() << __PRETTY_FUNCTION__;
-//
-//    if (expanded) {
-//        m_nameLabel->setText(m_userName);
-//        m_updatedLabel->setText(m_time);
-//        m_statusTextLabel->setText(m_messageText);
-//        m_locationLabel->setText(m_address);
-//    }
-//    else {
-//        m_nameLabel->setText(m_shortenedName);
-//        m_updatedLabel->setText(m_shortenedUpdated);
-//        m_statusTextLabel->setText(m_shortenedStatusText);
-//        m_locationLabel->setText(m_shortenedLocation);
-//    }
-//}
-
-//void UserInfo::mouseReleaseEvent(QMouseEvent *event)
-//{
-//    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
-//
-//    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;
-//        }
-//    }
-//}
-void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
+void UserInfo::paintEvent(QPaintEvent *event)
 {
-//    Q_UNUSED(aPaintEvent);
-//
-//    QStyleOption option;
-//    option.init(this);
-//    QStylePainter painter(this);
-//
-//   // Temporary border
-//    painter.setPen(Qt::red);
-//    painter.drawRect(this->rect().x(), this->rect().y(), this->rect().width() - 1, this->rect().height() - 1);
-//
-//    style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
-//
-    qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
+    qDebug() << __PRETTY_FUNCTION__ << " " << event->rect();
 
     QPainter painter(this);
-    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,
+
+    QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
+
+    QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH,
+                             this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
+
+    QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH,
                              BACKGROUND_BOTTOM_HEIGHT);
 
     painter.drawPixmap(topRect, m_backgroundTopImage);
@@ -288,23 +250,136 @@ void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
 }
 
+void UserInfo::restoreUnsendMessage()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-void UserInfo::mousePressEvent(QMouseEvent *event)
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString();
+    m_backupFacebookPublishPolicity = settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool();
+}
+
+void UserInfo::setAddress(const QString &address)
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_mousePosition = event->pos();
+    m_locationLabel->setText(address);
 }
 
-void UserInfo::messageUpdate()
+void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_coordinates = coordinates;
+}
+
+void UserInfo::setMessageText(const QString &text)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QStringList list;
+    list = text.split(' ');
+
+    for (int i = 0; i < list.count(); i++) {
+        if (fontMetrics().width(list.at(i)) > LABEL_MAX_WIDTH)
+            list.replace(i, splitWord(list.at(i)));
+    }
+
+    m_messageText = list.join(" ");
+
+    setExpanded(false);
+}
+
+void UserInfo::setProfileImage(const QPixmap &image)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(!image.isNull())
+        m_avatar->setButtonIcon(image);
+}
+
+void UserInfo::setExpanded(bool expanded)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (expanded)
+        m_statusTextLabel->setText(m_messageText);
+    else
+        m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText, LABEL_MAX_WIDTH));
+}
+
+void UserInfo::setTime(const QString &time)
 {
     qDebug() << __PRETTY_FUNCTION__;
-    m_locationDialog = new UpdateLocationDialog(this);
-    m_locationDialog->show();
+
+    m_updatedLabel->setText(time);
 }
 
-void UserInfo::updateFriendsStatus()
+void UserInfo::setUserName(const QString &name)
 {
     qDebug() << __PRETTY_FUNCTION__;
-    //emit launchUpdateFriendsStatus();
+
+    m_userName = name;
+
+    m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
+}
+
+QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QFontMetrics labelMetrics = label->fontMetrics();
+    QString copiedText = text;
+    int index = copiedText.indexOf('\n');
+
+    if (index >= 0) {
+        copiedText.truncate(index);
+        copiedText.append("...");
+    }
+
+    return labelMetrics.elidedText(copiedText, Qt::ElideRight, textMaxWidth);
+}
+
+QString UserInfo::splitWord(const QString &word) const
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QString result;
+    QString temp;
+
+    for (int i = 0; i < word.length(); i++) {
+        if (fontMetrics().width(temp.append(word.at(i))) > LABEL_MAX_WIDTH) {
+            result.append(temp.left(temp.length() - 1));
+            result.append(" ");
+            temp.remove(0, temp.length() - 1);
+        }
+    }
+
+    result.append(temp);
+
+    return result;
+}
+
+void UserInfo::updateLocationDialogFinished(int reason)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    Q_UNUSED(reason);
+
+    if (m_updateLocation) {
+        disconnect(this, SIGNAL(reverseGeoReady(QString)),
+                m_updateLocation, SLOT(setAddress(QString)));
+
+        disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
+                this, SIGNAL(statusUpdate(QString,bool)));
+
+        disconnect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
+                this, SLOT(backupUpdateLocationDialogData(QString,bool)));
+
+        disconnect(m_updateLocation, SIGNAL(finished(int)),
+                this, SLOT(updateLocationDialogFinished(int)));
+
+        m_updateLocation->deleteLater();
+        m_updateLocation = 0;
+    }
 }