Added QSizePolicy to the labels in user info
[situare] / src / ui / userinfo.cpp
index 50dd2cb..de6e209 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),
@@ -60,10 +61,6 @@ UserInfo::UserInfo(QWidget *parent)
     infoLayout->setMargin(0);
     infoLayout->setSpacing(0);
 
-    QHBoxLayout *buttonLayout = new QHBoxLayout();
-    buttonLayout->setMargin(0);
-    buttonLayout->setSpacing(0);
-
     QLabel *envelopeLabel = new QLabel();
     envelopeLabel->setPixmap(QPixmap(":/res/images/envelope.png"));
     envelopeLabel->setContentsMargins(0, 0, MARGIN, 0);
@@ -83,20 +80,13 @@ UserInfo::UserInfo(QWidget *parent)
 
     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);
-
-    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);
+    m_updatedLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
 
     infoLayout->addRow(envelopeLabel, m_statusTextLabel);
     infoLayout->addRow(compassLabel, m_locationLabel);
@@ -105,13 +95,6 @@ UserInfo::UserInfo(QWidget *parent)
     verticalLayout->addWidget(m_avatar, 0, Qt::AlignHCenter);
     verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter);
     verticalLayout->addLayout(infoLayout);
-    verticalLayout->addLayout(buttonLayout);
-
-    connect(updateStatusMessageButton,SIGNAL(clicked()),
-            this,SLOT(messageUpdate()));
-
-    connect(updateFriendsButton,SIGNAL(clicked()),
-            this, SIGNAL(refreshUserData()));
 
     connect(m_avatar, SIGNAL(clicked()),
             this, SLOT(findButtonClicked()));
@@ -277,15 +260,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);
 }
@@ -302,10 +278,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)
@@ -321,43 +299,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)