Modified UserInfo to use TextModifier class.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Wed, 1 Sep 2010 09:12:15 +0000 (12:12 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Wed, 1 Sep 2010 09:12:15 +0000 (12:12 +0300)
src/ui/extendedlistitem.cpp
src/ui/friendlistitem.cpp
src/ui/listcommon.h
src/ui/textmodifier.h
src/ui/userinfo.cpp
src/ui/userinfo.h

index c83384a..644caec 100644 (file)
@@ -60,14 +60,14 @@ void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
     QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
     QPainter painter(&p);
     painter.setFont(NOKIA_FONT_SMALL);
-
     QFontMetrics textMetrics = painter.fontMetrics();
 
-    QString expandedText = TextModifier::splitLongWords(textMetrics, text, m_subItemTextWidth);
+    QString expandedText = TextModifier::splitLongWords(textMetrics, text,
+                                                        m_subItemTextWidth);
 
     ExtendedListItemStore *itemStore = new ExtendedListItemStore(expandedText);
     itemStore->setIcon(icon);
-    itemStore->setShortenedText(shortenText(expandedText, m_subItemTextWidth,
+    itemStore->setShortenedText(shortenText(text, m_subItemTextWidth,
                                             ListItem::TEXT_SIZE_SMALL));
     itemStore->setTextRect(calculateExpandedTextRect(expandedText));
 
@@ -76,7 +76,7 @@ void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
 
 QRect ExtendedListItem::boundingRect(const QString &text)
 {
-    qWarning() << __PRETTY_FUNCTION__ << text;
+    qDebug() << __PRETTY_FUNCTION__;
 
     QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
     QPainter painter(&p);
@@ -84,32 +84,26 @@ QRect ExtendedListItem::boundingRect(const QString &text)
 
     QFontMetrics textMetrics = painter.fontMetrics();
     QRect textRect;
-    textRect.setSize(QSize(m_subItemTextWidth, textMetrics.height()));
-
-    QStringList rows = text.split('\n');
-
-//    for (int i = 1; i < rows.count(); ++i) {
-//        qWarning() << "jep";
-//        QRect textRowRect = textMetrics.boundingRect(rows.at(i));
-//        textRect.setHeight(textRect.height() + textRowRect.height());
-//    }
-
-    QStringList words = text.split(" ");
-    QString rowText = words.at(0);
-    for (int i = 1; i < words.count(); ++i) {
-        if (rowText.contains("\n")) {
-            textRect.setHeight(textRect.height() + textMetrics.height());
-            //rowText.clear();
-            rowText = words.at(i);
+    int rowHeight = textMetrics.ascent() + textMetrics.descent();
+    textRect.setSize(QSize(m_subItemTextWidth, rowHeight));
+
+    QStringList words = text.split(QRegExp("\\b"));
+    QString rowText;
+    foreach (QString word, words) {
+
+        if (word.contains("\n")) {
+            foreach (QChar c, word) {
+                if (c == '\n')
+                    textRect.setHeight(textRect.height() + rowHeight);
+            }
+            rowText.clear();
+        } else {
+            rowText.append(word);
+            if (textMetrics.width(rowText) > m_subItemTextWidth) {
+                textRect.setHeight(textRect.height() + rowHeight);
+                rowText = word.simplified();
+            }
         }
-        else if (textMetrics.width(rowText) > m_subItemTextWidth) {
-            textRect.setHeight(textRect.height() + textMetrics.height());
-            rowText = words.at(i);
-            rowText.remove("\n");
-            qWarning() << "hep";
-        }
-        else
-            rowText.append(" " + words.at(i));
     }
 
     return textRect;
@@ -121,13 +115,7 @@ QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
 
     const int TEXT_BOTTOM_MARGIN = 2;
 
-    QRect textRect = boundingRect(text);
-    QRect expandedTextRect = textRect;
-//    int textRectFactor = textRect.width() / m_subItemTextWidth;
-//    textRectFactor += textRect.height() / SUBITEM_TEXT_ROW_HEIGHT;
-
-//    QRect expandedTextRect = QRect(0, 0, m_subItemTextWidth, SUBITEM_TEXT_ROW_HEIGHT
-//                                   * qMax(textRectFactor, 1));
+    QRect expandedTextRect = boundingRect(text);
 
     m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT + TEXT_BOTTOM_MARGIN;
     m_expandedHeight += expandedTextRect.height() + TEXT_BOTTOM_MARGIN;
index cd0fc9d..395b9c1 100644 (file)
@@ -104,8 +104,7 @@ void FriendListItem::setUserData(User *user)
     QRect distanceRect = distanceTextFontMetrics.boundingRect(distanceText);
 
     setData(DISTANCE_SIZE_HINT_INDEX, distanceRect);
-    QString nameTemp = QString("Pitka nimi pitka nimi pitka nimi");
-    setTitle(shortenText(nameTemp, NAME_TEXT_MAX_WIDTH - distanceRect.width() - MARGIN * 2,
+    setTitle(shortenText(user->name(), NAME_TEXT_MAX_WIDTH - distanceRect.width() - MARGIN * 2,
                         ListItem::TEXT_SIZE_NORMAL));
     setCoordinates(user->coordinates());
 
@@ -114,27 +113,7 @@ void FriendListItem::setUserData(User *user)
 
     clearSubItems();
 
-    QString noteTemp = QString("Joillakin lehdillä on tosi tarkat rajat painon kanssa, ja emme "
-                               "menneet Playboyn pyytämiin \n\nkoekuvauksiinkaan, kun he sanoivat, "
-                               "että se on ainoa mahdollisuus. Jos on esim. liian ruskettunut, "
-                               "niin he karsivat saman tien pois. Nain jälkikäteen ajatellen minua "
-                               "on jäänyt harmittamaan, ettemme menneet koekuvaukseen kun kerran "
-                               "pyydettiin! Kun Julia pääsi kyseisen lehden Grapevine-sivulle "
-                               "vuonna 2003, meille molemmille jäi kytemään ajatus jatkosta. "
-                               "Olemme ihmetelleet, miksi Suomen lehdistö sivuutti asian vain "
-                               "nopeasti. Vaikka kyseessä ei ollutkaan monen kuvan kuvasarja "
-                               "Juliasta, hän silti poseerasi varsinaisessa Playboy-lehdessä.");
-    QString noteTemp2 = QString("hei\nhei\n\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\n"
-                                "hei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei");
-
-    static int index = 0;
-
-//    if (index % 2)
-        addSubItem(user->note(), QPixmap(":/res/images/envelope.png"));
-//    else
-//        addSubItem(noteTemp2, QPixmap(":/res/images/envelope.png"));
+    addSubItem(user->note(), QPixmap(":/res/images/envelope.png"));
     addSubItem(user->address(), QPixmap(":/res/images/compass.png"));
     addSubItem(user->timestamp(), QPixmap(":/res/images/clock.png"));
-
-    index++;
 }
index bc8b416..e975123 100644 (file)
@@ -48,7 +48,7 @@ const int NAME_TEXT_MAX_WIDTH = ITEM_WIDTH - 3 * MARGIN - IMAGE_WIDTH;
 const int SUBITEM_TEXT_MAX_WIDTH = ITEM_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH
                                    - MARGIN * 2;
 
-const int SUBITEM_TEXT_ROW_HEIGHT = 22;     ///< Subitems text row height
+const int SUBITEM_TEXT_ROW_HEIGHT = 21;     ///< Subitems text row height
 
 const int TITLE_DISPLAY_INDEX = Qt::DisplayRole;
 const int AVATAR_IMAGE_INDEX = Qt::DecorationRole;
index ad3cb79..d88e9a9 100644 (file)
@@ -11,11 +11,40 @@ class TextModifier : public QObject
 public:
     TextModifier(QObject *parent = 0);
 
+    /**
+    * @brief Shortens text to fit.
+    *
+    * Text shortening is defined by text maximum width.
+    *
+    * @param fontMetrics font metrics to use
+    * @param text text to shorten
+    * @param textMaxWidth maximum width for text
+    */
     static QString shortenText(const QFontMetrics fontMetrics, const QString &text,
                                int textMaxWidth);
 
+    /**
+     * @brief Splits too long word.
+     *
+     * Splits long word to several by adding extra spaces
+     *
+     * @param fontMetrics font metrics to use
+     * @param word long word to be splitted
+     * @param textMaxWidth maximum width for word
+     * @returns splitted word
+     */
     static QString splitWord(const QFontMetrics fontMetrics, const QString &word, int textMaxWidth);
 
+    /**
+     * @brief Splits long words from text.
+     *
+     * Splits long words to several by adding extra spaces
+     *
+     * @param fontMetrics font metrics to use
+     * @param text text to be splitted
+     * @param textMaxWidth maximum width for word
+     * @returns text with long words splitted
+     */
     static QString splitLongWords(const QFontMetrics fontMetrics, const QString &text,
                                   int textMaxWidth);
 };
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)
index e6553e6..a7b52c9 100644 (file)
@@ -155,26 +155,6 @@ private:
      */
     void setExpanded(bool expanded);
 
-    /**
-     * @brief Elides long text
-     *
-     * @param label get the fontmetrics from the label
-     * @param text long text to be shortened
-     * @param textMaxWidth label width
-     * @returns shortened text
-     */
-    QString shortenText(const QLabel *label, const QString &text, int textMaxWidth);
-
-    /**
-     * @brief Split too long words.
-     *
-     * Splits long word to several by adding extra spaces
-     *
-     * @param word long word to be splitted
-     * @returns splitted word
-     */
-    QString splitWord(const QString &word) const;
-
 private slots:
     /**
      * @brief Slot for collapse user info