Added ExtendedListItem::boundingRect method.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 12 Aug 2010 13:11:53 +0000 (16:11 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 12 Aug 2010 13:11:53 +0000 (16:11 +0300)
src/ui/extendedlistitem.cpp
src/ui/extendedlistitem.h
src/ui/extendedlistitemdelegate.cpp
src/ui/listcommon.h
tests/ui/friendlistitem/testfriendlistitem.cpp

index a8974b8..158e746 100644 (file)
@@ -29,8 +29,6 @@
 
 #include "extendedlistitem.h"
 
-const int SUBITEM_TEXT_ROW_HEIGHT = ICON_HEIGHT;    ///< Sub item text row height
-
 ExtendedListItem::ExtendedListItem()
     : m_selected(false),
       m_expandedHeight(ITEM_MIN_HEIGHT),
@@ -58,6 +56,31 @@ void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
     m_subItemStoreList->append(itemStore);
 }
 
+QRect ExtendedListItem::boundingRect(const QString &text)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
+    QPainter painter(&p);
+    painter.setFont(NOKIA_FONT_SMALL);
+
+    QFontMetrics textMetrics = painter.fontMetrics();
+    QRect textRect;
+    QStringList rows = text.split('\n');
+
+    foreach (QString row, rows) {
+
+        QRect textRowRect = textMetrics.boundingRect(row);
+
+        if (textRowRect.width() > textRect.width())
+            textRect.setWidth(textRowRect.width());
+
+        textRect.setHeight(textRect.height() + textRowRect.height());
+    }
+
+    return textRect;
+}
+
 void ExtendedListItem::clearSubItems()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -73,19 +96,18 @@ QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
-    QPainter painter(&p);
-    painter.setFont(NOKIA_FONT_SMALL);
-    QFontMetrics textMetrics = painter.fontMetrics();
+    const int TEXT_BOTTOM_MARGIN = 2;
+
+    QRect textRect = boundingRect(text);
 
-    QRect textRect = textMetrics.boundingRect(text);
     int textRectFactor = textRect.width() / m_subItemTextWidth;
-    textRectFactor++;
+    textRectFactor += textRect.height() / SUBITEM_TEXT_ROW_HEIGHT;
+
     QRect expandedTextRect = QRect(0, 0, m_subItemTextWidth, SUBITEM_TEXT_ROW_HEIGHT
                                    * textRectFactor);
 
-    m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT;
-    m_expandedHeight += expandedTextRect.height();
+    m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT + TEXT_BOTTOM_MARGIN;
+    m_expandedHeight += expandedTextRect.height() + TEXT_BOTTOM_MARGIN;
     setSize(QSize(ITEM_WIDTH, m_normalHeight));
 
     return expandedTextRect;
index 5c59c0a..0522c6e 100644 (file)
@@ -93,6 +93,15 @@ public:
 
 private:
     /**
+    * @brief Returns text's bounding rect.
+    *
+    * Takes into account line breaks in text.
+    *
+    * @param text text which rect is calculated
+    */
+    QRect boundingRect(const QString &text);
+
+    /**
     * @brief Calculates text rects.
     *
     * Rect width is defined by sub item text width.
index 50bc9d6..ee1b6f3 100644 (file)
@@ -65,7 +65,7 @@ void ExtendedListItemDelegate::paint(QPainter *painter, const QStyleOptionViewIt
                 text = itemStore->text();
             }
             else {
-                subItemTextRect.setHeight(ICON_HEIGHT);
+                subItemTextRect.setHeight(SUBITEM_TEXT_ROW_HEIGHT);
                 text = itemStore->shortenedText();
             }
 
@@ -82,7 +82,7 @@ void ExtendedListItemDelegate::paint(QPainter *painter, const QStyleOptionViewIt
                 painter->drawPixmap(iconRect, itemStore->icon());
 
             painter->drawText(subItemTextRect, Qt::TextWrapAnywhere, text);
-            previousSubItemTextRectBottom = subItemTextRect.bottom();
+            previousSubItemTextRectBottom = subItemTextRect.bottom() + ICON_MARGIN;
         }
     }
 }
index 1b01d34..bc8b416 100644 (file)
@@ -48,6 +48,8 @@ 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 TITLE_DISPLAY_INDEX = Qt::DisplayRole;
 const int AVATAR_IMAGE_INDEX = Qt::DecorationRole;
 
index 7fc2713..68bd23d 100644 (file)
@@ -103,12 +103,12 @@ void TestFriendListItem::toggleSelection()
     QVERIFY(subItems);
     QCOMPARE(subItems->count(), 3);
 
-    QCOMPARE(subItems->at(0)->textRect(), QRect(0, 0, 250, 72));
-    QCOMPARE(subItems->at(1)->textRect(), QRect(0, 0, 250, 24));
-    QCOMPARE(subItems->at(2)->textRect(), QRect(0, 0, 250, 48));
+    QCOMPARE(subItems->at(0)->textRect(), QRect(0, 0, 250, 66));
+    QCOMPARE(subItems->at(1)->textRect(), QRect(0, 0, 250, 22));
+    QCOMPARE(subItems->at(2)->textRect(), QRect(0, 0, 250, 44));
 
     friendListItem->toggleSelection();
-    QCOMPARE(friendListItem->data(ITEM_SIZE_HINT_INDEX).toSize(), QSize(368, 142 + 3 * 24));
+    QCOMPARE(friendListItem->data(ITEM_SIZE_HINT_INDEX).toSize(), QSize(368, 142 + 3 * 22));
     QCOMPARE(friendListItem->data(ITEM_EXPANDED_INDEX).toBool(), true);
 
     friendListItem->toggleSelection();