Merge branch 'master' into new_panels_with_context_buttons
[situare] / src / ui / userinfo.cpp
index b2d5d1c..d7e8f33 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 "user/user.h"
+
 #include "userinfo.h"
 
-const int BACKGROUND_WIDTH = 240; ///< Width for item
-const int BACKGROUND_TOP_HEIGHT = 16;
-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;    ///< Profile image height
-const int IMAGE_WIDTH = 60;     ///< Profile image width
-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
+const int BACKGROUND_WIDTH = 368;           ///< Width for item
+const int BACKGROUND_TOP_HEIGHT = 20;       ///< Height for item top
+const int BACKGROUND_BOTTOM_HEIGHT = 15;    ///< Height for item bottom
+const int ICON_HEIGHT = 24;                 ///< Icon height
+const int ICON_WIDTH = 24;                  ///< Icon width
+const int MARGIN = 5;                       ///< Icon margin
+const int LINE_LENGTH = 27;                 ///< Line length
+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
@@ -47,80 +53,59 @@ const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height togglin
 */
 const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - ICON_WIDTH + 130;
 
-/**
-* @brief Name label's maximum width
-*
-* @var NAME_LABEL_MAX_WIDTH
-*/
-const int NAME_LABEL_MAX_WIDTH = LABEL_MAX_WIDTH + 70;
-
 UserInfo::UserInfo(QWidget *parent)
     : QWidget(parent),
-      m_expanded(false)
-{    
+      m_expanded(false),
+      m_updateLocation(0)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
     QVBoxLayout *verticalLayout = new QVBoxLayout(this);
     verticalLayout->setContentsMargins(MARGIN * 2, 0, MARGIN * 2, MARGIN * 2);
     verticalLayout->setSpacing(0);
-    this->setLayout(verticalLayout);
+    setLayout(verticalLayout);
 
     QFormLayout *infoLayout = new QFormLayout();
     infoLayout->setMargin(0);
     infoLayout->setSpacing(0);
 
-    QHBoxLayout *buttonLayout = new QHBoxLayout();
-    buttonLayout->setMargin(0);
-    buttonLayout->setSpacing(0);
-
-    QLabel *envelopeLabel = new QLabel(this);
+    QLabel *envelopeLabel = new QLabel();
     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
     envelopeLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
-    QLabel *compassLabel = new QLabel(this);
+    QLabel *compassLabel = new QLabel();
     compassLabel->setPixmap(QPixmap(":/res/images/compass.png"));
     compassLabel->setContentsMargins(0, 0, MARGIN, 0);
     compassLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT);
-    QLabel *clockLabel = new QLabel(this);
+    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(this);
-    m_imageLabel->setFixedSize(IMAGE_WIDTH, IMAGE_HEIGHT);
+    m_findButton = new ImageButton();
 
-    m_nameLabel = new QLabel(this);
+    m_nameLabel = new QLabel();
     m_nameLabel->setWordWrap(true);
-    m_nameLabel->setFixedHeight(IMAGE_HEIGHT);
 
-    m_statusTextLabel = new QLabel(this);
+    m_statusTextLabel = new QLabel();
     m_statusTextLabel->setWordWrap(true);
-    m_locationLabel = new QLabel(this);
+    m_locationLabel = new QLabel();
     m_locationLabel->setWordWrap(true);
-    m_updatedLabel = new QLabel(this);
+    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");
-
-    buttonLayout->addWidget(updateFriendsButton);
-    buttonLayout->addWidget(updateStatusMessageButton);
-
     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_findButton, 0, Qt::AlignHCenter);
     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
-    verticalLayout->addLayout(infoLayout, 1);
-    verticalLayout->addLayout(buttonLayout);
+    verticalLayout->addLayout(infoLayout);
 
-    connect(updateStatusMessageButton,SIGNAL(clicked()),
-            this,SLOT(messageUpdate()));
-    connect(updateFriendsButton,SIGNAL(clicked()),
-            this, SIGNAL(refreshUserData()));
+    connect(m_findButton, SIGNAL(clicked()),
+            this, SLOT(findButtonClicked()));
 
-    this->setFixedWidth(BACKGROUND_WIDTH);
+    setFixedWidth(BACKGROUND_WIDTH);
 
     this->setFont(NOKIA_FONT_SMALL);
     m_nameLabel->setFont(NOKIA_FONT_NORMAL);
@@ -131,24 +116,40 @@ UserInfo::UserInfo(QWidget *parent)
     namePalette.setColor(QPalette::Foreground, Qt::white);
     m_nameLabel->setPalette(namePalette);
 
-    this->m_backgroundTopImage.load(":/res/images/user_info_item_top.png");
-    this->m_backgroundMiddleImage.load(":/res/images/user_info_item_middle.png");
-    this->m_backgroundBottomImage.load(":/res/images/user_info_item_bottom.png");
+    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();
 }
 
-void UserInfo::setUserName(const QString &name)
+UserInfo::~UserInfo()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_userName = name;
-    setText(false);
+    QSettings settings(DIRECTORY_NAME, FILE_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::setProfileImage(const QPixmap &image)
+void UserInfo::setAddress(const QString &address)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_locationLabel->setText(address);
+}
+
+void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_imageLabel->setPixmap(image);
+    m_coordinates = coordinates;
 }
 
 void UserInfo::setMessageText(const QString &text)
@@ -156,14 +157,28 @@ void UserInfo::setMessageText(const QString &text)
     qDebug() << __PRETTY_FUNCTION__;
 
     m_messageText = text;
+    m_expandedMessageText.clear();
+    QString temp = "";
+    for(int i=0;i < text.length();i++) {
+        if(fontMetrics().width(temp.append(text.at(i))) > 170) {
+            temp.append("\n");
+            if(temp.startsWith(QString(" ")))
+                temp.remove(0, 1);
+
+            m_expandedMessageText.append(temp);
+            temp.clear();
+        }
+    }
+    m_expandedMessageText.append(temp);
     setText(false);
 }
 
-void UserInfo::setAddress(const QString &address)
+void UserInfo::setProfileImage(const QPixmap &image)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_locationLabel->setText(address);
+    if(!image.isNull())
+        m_findButton->setButtonIcon(image);
 }
 
 void UserInfo::setTime(const QString &time)
@@ -173,21 +188,12 @@ void UserInfo::setTime(const QString &time)
     m_updatedLabel->setText(time);
 }
 
-QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
+void UserInfo::setUserName(const QString &name)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QFontMetrics labelMetrics = label->fontMetrics();
-
-    QString textParam = text;
-    int index = textParam.indexOf('\n');
-
-    if (index > 0) {
-        textParam.truncate(index);
-        textParam.append("...");
-    }
-
-   return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
+    m_userName = name;
+    setText(false);
 }
 
 void UserInfo::setText(bool expanded)
@@ -195,8 +201,7 @@ void UserInfo::setText(bool expanded)
     qDebug() << __PRETTY_FUNCTION__;
 
     if (expanded) {
-        m_nameLabel->setText(m_userName);
-        m_statusTextLabel->setText(m_messageText);
+        m_statusTextLabel->setText(m_expandedMessageText);
     }
     else {
         m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
@@ -205,6 +210,29 @@ void UserInfo::setText(bool expanded)
     }
 }
 
+void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_backupMessage = status;
+    m_backupFacebookPublishPolicity = publish;
+}
+
+void UserInfo::clearUpdateLocationDialogData()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_backupMessage.clear();
+    m_backupFacebookPublishPolicity = false;
+}
+
+void UserInfo::findButtonClicked()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    emit findUser(m_coordinates);
+}
+
 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
@@ -221,6 +249,39 @@ void UserInfo::mouseReleaseEvent(QMouseEvent *event)
         }
     }
 }
+
+void UserInfo::mousePressEvent(QMouseEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+
+    m_mousePosition = event->pos();
+}
+
+void UserInfo::messageUpdate()
+{
+    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::paintEvent(QPaintEvent *aPaintEvent)
 {
     qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
@@ -228,8 +289,10 @@ void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
     QPainter painter(this);
 
     QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
+///< @todo Overlaps with topRect?
     QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
-                             height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
+                             this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
+///< @todo Overlaps with middleRect
     QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
                              BACKGROUND_BOTTOM_HEIGHT);
 
@@ -238,26 +301,55 @@ void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
     painter.drawPixmap(bottomRect, m_backgroundBottomImage);
 }
 
-void UserInfo::mousePressEvent(QMouseEvent *event)
+void UserInfo::restoreUnsendMessage()
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_mousePosition = event->pos();
+    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::messageUpdate()
+QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    UpdateLocationDialog updateLocationDialog(this);
-
-    emit requestReverseGeo();
+    QFontMetrics labelMetrics = label->fontMetrics();
+    QString textParam = text;
+    int index = textParam.indexOf('\n');
+    int textWidth = fontMetrics().width(textParam);
 
-    connect(this, SIGNAL(reverseGeoReady(QString)),
-            &updateLocationDialog, SLOT(setAddress(QString)));
-    connect(&updateLocationDialog, SIGNAL(statusUpdate(QString, bool)),
-            this, SIGNAL(statusUpdate(QString, bool)));
+        if (index > 0) {
+            textParam.truncate(index);
+            textParam.append("...");
+        }
+            if (textWidth > 250) ///< @todo magic number
+                textParam.insert(LINE_LENGTH, QString("\n"));
 
-    updateLocationDialog.exec();
+   return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth);
 }
 
+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;
+    }
+}