Modified UserInfo to use TextModifier class.
[situare] / src / ui / userinfo.cpp
index c1d1b7f..31aaafc 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "common.h"
 #include "imagebutton.h"
+#include "textmodifier.h"
 #include "user/user.h"
 
 #include "userinfo.h"
@@ -41,8 +42,8 @@ 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;
+const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - ICON_WIDTH - 5 * MARGIN;
 
 UserInfo::UserInfo(QWidget *parent)
     : QWidget(parent),
@@ -256,15 +257,8 @@ 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(" ");
+    m_messageText = TextModifier::splitLongWords(m_statusTextLabel->fontMetrics(), text,
+                                                 LABEL_MAX_WIDTH);
 
     setExpanded(false);
 }
@@ -281,10 +275,12 @@ void UserInfo::setExpanded(bool expanded)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (expanded)
+    if (expanded) {
         m_statusTextLabel->setText(m_messageText);
-    else
-        m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText, LABEL_MAX_WIDTH));
+    } else {
+        m_statusTextLabel->setText(TextModifier::shortenText(m_statusTextLabel->fontMetrics(),
+                                                             m_messageText, LABEL_MAX_WIDTH));
+    }
 }
 
 void UserInfo::setTime(const QString &time)
@@ -300,43 +296,8 @@ void UserInfo::setUserName(const QString &name)
 
     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;
+    m_nameLabel->setText(TextModifier::shortenText(m_nameLabel->fontMetrics(), m_userName,
+                                                   LABEL_MAX_WIDTH));
 }
 
 void UserInfo::updateLocationDialogFinished(int reason)