Added TextModifier class.
[situare] / src / ui / extendedlistitem.cpp
index 6d6e1f8..868857f 100644 (file)
@@ -26,6 +26,7 @@
 #include "../common.h"
 #include "extendedlistitemstore.h"
 #include "listcommon.h"
+#include "textmodifier.h"
 
 #include "extendedlistitem.h"
 
@@ -56,17 +57,26 @@ void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    ExtendedListItemStore *itemStore = new ExtendedListItemStore(text);
+    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);
+
+    ExtendedListItemStore *itemStore = new ExtendedListItemStore(expandedText);
     itemStore->setIcon(icon);
-    itemStore->setShortenedText(shortenText(text, m_subItemTextWidth, ListItem::TEXT_SIZE_SMALL));
-    itemStore->setTextRect(calculateExpandedTextRect(text));
+    itemStore->setShortenedText(shortenText(expandedText, m_subItemTextWidth,
+                                            ListItem::TEXT_SIZE_SMALL));
+    itemStore->setTextRect(calculateExpandedTextRect(expandedText));
 
     m_subItemStoreList->append(itemStore);
 }
 
 QRect ExtendedListItem::boundingRect(const QString &text)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__ << text;
 
     QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
     QPainter painter(&p);
@@ -74,18 +84,32 @@ QRect ExtendedListItem::boundingRect(const QString &text)
 
     QFontMetrics textMetrics = painter.fontMetrics();
     QRect textRect;
-    QStringList rows = text.split('\n');
-
-    foreach (QString row, rows) {
+    textRect.setSize(QSize(m_subItemTextWidth, textMetrics.height()));
 
-        QRect textRowRect = textMetrics.boundingRect(row);
-
-        if (textRowRect.width() > textRect.width())
-            textRect.setWidth(textRowRect.width());
+    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;
+    for (int i = 0; i < words.count(); ++i) {
+        if (i == words.count() -1)
+            rowText.append(words.at(i));
+        else
+            rowText.append(words.at(i) + " ");
+        if (textMetrics.width(rowText) > m_subItemTextWidth) {
+            textRect.setHeight(textRect.height() + textMetrics.height());
+            rowText = words.at(i);
+            qWarning() << "hep";
+        }
+    }
+
+    //textRect.setHeight(textRect.height() + 5);
+//    qWarning() << textRect.width() << textRect.height();
     return textRect;
 }
 
@@ -96,12 +120,12 @@ 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;
 
-    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 = QRect(0, 0, m_subItemTextWidth, SUBITEM_TEXT_ROW_HEIGHT
+//                                   * qMax(textRectFactor, 1));
 
     m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT + TEXT_BOTTOM_MARGIN;
     m_expandedHeight += expandedTextRect.height() + TEXT_BOTTOM_MARGIN;