Alphabetising
[situare] / src / ui / extendedlistitem.cpp
index a8974b8..6d6e1f8 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),
@@ -46,6 +44,14 @@ ExtendedListItem::ExtendedListItem()
     setData(SUBITEM_STORE_INDEX, qVariantFromValue((void *) m_subItemStoreList));
 }
 
+ExtendedListItem::~ExtendedListItem()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    clearSubItems();
+    delete m_subItemStoreList;
+}
+
 void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -58,58 +64,75 @@ void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
     m_subItemStoreList->append(itemStore);
 }
 
-void ExtendedListItem::clearSubItems()
+QRect ExtendedListItem::boundingRect(const QString &text)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    qDeleteAll(m_subItemStoreList->begin(), m_subItemStoreList->end());
-    m_subItemStoreList->clear();
+    QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
+    QPainter painter(&p);
+    painter.setFont(NOKIA_FONT_SMALL);
 
-    m_expandedHeight = ITEM_MIN_HEIGHT;
-    m_normalHeight = ITEM_MIN_HEIGHT;
+    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;
 }
 
 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);
+                                   * qMax(textRectFactor, 1));
 
-    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;
 }
 
-void ExtendedListItem::setSubitemTextWidth(int width)
+void ExtendedListItem::clearSubItems()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_subItemTextWidth = width;
+    qDeleteAll(m_subItemStoreList->begin(), m_subItemStoreList->end());
+    m_subItemStoreList->clear();
+
+    m_expandedHeight = ITEM_MIN_HEIGHT;
+    m_normalHeight = ITEM_MIN_HEIGHT;
 }
 
-bool ExtendedListItem::toggleSelection()
+void ExtendedListItem::setSubitemTextWidth(int width)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    setSelected(!m_selected);
-    return m_selected;
+    m_subItemTextWidth = width;
 }
 
 void ExtendedListItem::setSelected(bool selected)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
+    QListWidgetItem::setSelected(selected);
     m_selected = selected;
     setData(ITEM_EXPANDED_INDEX, m_selected);
 
@@ -119,10 +142,10 @@ void ExtendedListItem::setSelected(bool selected)
         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_normalHeight));
 }
 
-ExtendedListItem::~ExtendedListItem()
+bool ExtendedListItem::toggleSelection()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    clearSubItems();
-    delete m_subItemStoreList;
+    setSelected(!m_selected);
+    return m_selected;
 }