Fixed message text cutting problem in userinfo
[situare] / src / ui / userinfo.cpp
index 96df7a3..d5e59af 100644 (file)
@@ -150,103 +150,59 @@ UserInfo::~UserInfo()
     }
 }
 
-void UserInfo::setAddress(const QString &address)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_locationLabel->setText(address);
-}
-
-void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_coordinates = coordinates;
-}
-
-void UserInfo::setMessageText(const QString &text)
+void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
 {
     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))) > LABEL_MAX_WIDTH) {
-            temp.append("\n");
-            if(temp.startsWith(QString(" ")))
-                temp.remove(0, 1);
-
-            m_expandedMessageText.append(temp);
-            temp.clear();
-        }
-    }
-    m_expandedMessageText.append(temp);
-    m_statusTextLabel->setText(m_expandedMessageText);
-
-    setText(false);
+    m_backupMessage = status;
+    m_backupFacebookPublishPolicity = publish;
 }
 
-void UserInfo::setProfileImage(const QPixmap &image)
+void UserInfo::clearUpdateLocationDialogData()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(!image.isNull())
-        m_findButton->setButtonIcon(image);
+    m_backupMessage.clear();
+    m_backupFacebookPublishPolicity = false;
 }
 
-void UserInfo::setTime(const QString &time)
+void UserInfo::findButtonClicked()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_updatedLabel->setText(time);
+    emit findUser(m_coordinates);
 }
 
-void UserInfo::setUserName(const QString &name)
+void UserInfo::messageUpdate()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_userName = name;
-    setText(false);
-}
-
-void UserInfo::setText(bool expanded)
-{
-    qDebug() << __PRETTY_FUNCTION__;
+    delete m_updateLocation;
+    m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
+                                                this);
 
-    const int EXTRA_WIDTH_FOR_LABEL = 545;
+    connect(this, SIGNAL(reverseGeoReady(QString)),
+            m_updateLocation, SLOT(setAddress(QString)));
 
-    if (expanded) {
-        m_statusTextLabel->setText(m_expandedMessageText);
-    }
-    else {
-        m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
-        m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
-                                               LABEL_MAX_WIDTH + EXTRA_WIDTH_FOR_LABEL));
-    }
-}
+    connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
+            this, SIGNAL(statusUpdate(QString,bool)));
 
-void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish)
-{
-    qDebug() << __PRETTY_FUNCTION__;
+    connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
+            this, SLOT(backupUpdateLocationDialogData(QString,bool)));
 
-    m_backupMessage = status;
-    m_backupFacebookPublishPolicity = publish;
-}
+    connect(m_updateLocation, SIGNAL(finished(int)),
+            this, SLOT(updateLocationDialogFinished(int)));
 
-void UserInfo::clearUpdateLocationDialogData()
-{
-    qDebug() << __PRETTY_FUNCTION__;
+    m_updateLocation->show();
 
-    m_backupMessage.clear();
-    m_backupFacebookPublishPolicity = false;
+    emit requestReverseGeo();
 }
 
-void UserInfo::findButtonClicked()
+void UserInfo::mousePressEvent(QMouseEvent *event)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
 
-    emit findUser(m_coordinates);
+    m_mousePosition = event->pos();
 }
 
 void UserInfo::mouseReleaseEvent(QMouseEvent *event)
@@ -270,90 +226,139 @@ void UserInfo::mouseReleaseEvent(QMouseEvent *event)
     }
 }
 
-void UserInfo::mousePressEvent(QMouseEvent *event)
+void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << event->pos();
+    qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
 
-    m_mousePosition = event->pos();
+    QPainter painter(this);
+
+    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);
+    painter.drawPixmap(middleRect, m_backgroundMiddleImage);
+    painter.drawPixmap(bottomRect, m_backgroundBottomImage);
 }
 
-void UserInfo::messageUpdate()
+void UserInfo::restoreUnsendMessage()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    delete m_updateLocation;
-    m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity,
-                                                this);
+    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();
+}
 
-    connect(this, SIGNAL(reverseGeoReady(QString)),
-            m_updateLocation, SLOT(setAddress(QString)));
+void UserInfo::setAddress(const QString &address)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
-            this, SIGNAL(statusUpdate(QString,bool)));
+    m_locationLabel->setText(address);
+}
 
-    connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)),
-            this, SLOT(backupUpdateLocationDialogData(QString,bool)));
+void UserInfo::setCoordinates(const GeoCoordinate &coordinates)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    connect(m_updateLocation, SIGNAL(finished(int)),
-            this, SLOT(updateLocationDialogFinished(int)));
+    m_coordinates = coordinates;
+}
 
-    m_updateLocation->show();
+void UserInfo::setMessageText(const QString &text)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    emit requestReverseGeo();
+    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(" ");
+    m_statusTextLabel->setText(m_messageText);
+    setText(false);
 }
 
-void UserInfo::paintEvent(QPaintEvent *aPaintEvent)
+void UserInfo::setProfileImage(const QPixmap &image)
 {
-    qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    QPainter painter(this);
+    if(!image.isNull())
+        m_findButton->setButtonIcon(image);
+}
 
-    QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT);
-///< @todo Overlaps with topRect?
-    QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH,
-                             this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT);
-///< @todo Overlaps with middleRect
-    QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH,
-                             BACKGROUND_BOTTOM_HEIGHT);
+void UserInfo::setText(bool expanded)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    painter.drawPixmap(topRect, m_backgroundTopImage);
-    painter.drawPixmap(middleRect, m_backgroundMiddleImage);
-    painter.drawPixmap(bottomRect, m_backgroundBottomImage);
+    if (expanded) {
+        m_statusTextLabel->setText(m_messageText);
+    }
+    else {
+        m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
+        m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText,
+                                               LABEL_MAX_WIDTH));
+    }
 }
 
-void UserInfo::restoreUnsendMessage()
+void UserInfo::setTime(const QString &time)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    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();
+    m_updatedLabel->setText(time);
 }
 
-QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
+void UserInfo::setUserName(const QString &name)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    const int LINE_LENGTH = 42;
+    m_userName = name;
+    setText(false);
+}
+
+QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
     QFontMetrics labelMetrics = label->fontMetrics();
     QString textParam = text;
     int index = textParam.indexOf('\n');
 
-    int textWidth = fontMetrics().width(textParam);
-
         if (index > 0) {
             textParam.truncate(index);
             textParam.append("...");
         }
 
-        if (textWidth > LABEL_MAX_WIDTH)
-            textParam.insert(LINE_LENGTH, QString("\n"));
-
    return labelMetrics.elidedText(textParam, 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)
+        {
+            temp.append(" ");
+            result.append(temp);
+            temp.clear();
+        }
+    }
+
+    result.append(temp);
+
+    return result;
+}
+
 void UserInfo::updateLocationDialogFinished(int reason)
 {
     qDebug() << __PRETTY_FUNCTION__;