Alphabetising
[situare] / src / ui / extendedlistitem.cpp
index 4644b42..6d6e1f8 100644 (file)
 #include <QPainter>
 
 #include "../common.h"
-#include "listcommon.h"
 #include "extendedlistitemstore.h"
+#include "listcommon.h"
 
 #include "extendedlistitem.h"
 
-const int SUBITEM_TEXT_ROW_HEIGHT = ICON_HEIGHT;
-
 ExtendedListItem::ExtendedListItem()
     : m_selected(false),
       m_expandedHeight(ITEM_MIN_HEIGHT),
@@ -41,10 +39,19 @@ ExtendedListItem::ExtendedListItem()
     qDebug() << __PRETTY_FUNCTION__;
 
     setSize(QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
+
     m_subItemStoreList = new QList<ExtendedListItemStore *>();
     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__;
@@ -57,53 +64,88 @@ void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
     m_subItemStoreList->append(itemStore);
 }
 
-QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
+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;
+}
+
+QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    const int TEXT_BOTTOM_MARGIN = 2;
+
+    QRect textRect = boundingRect(text);
 
-    QRect textRect = textMetrics.boundingRect(text);
-    qWarning() << textRect.width() << textRect.height();
     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);
-    qWarning() << expandedTextRect.width() << expandedTextRect.height();
-    m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT;
-    m_expandedHeight += expandedTextRect.height();
+                                   * qMax(textRectFactor, 1));
 
+    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()
 {
-    m_subItemTextWidth = width;
+    qDebug() << __PRETTY_FUNCTION__;
+
+    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)
 {
-    qWarning() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    setSelected(!m_selected);
-    return m_selected;
+    m_subItemTextWidth = width;
 }
 
 void ExtendedListItem::setSelected(bool selected)
 {
-    qWarning() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
+    QListWidgetItem::setSelected(selected);
     m_selected = selected;
     setData(ITEM_EXPANDED_INDEX, m_selected);
 
-    if (m_selected) {
+    if (m_selected)
         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_expandedHeight));
-    } else {
+    else
         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_normalHeight));
-    }
+}
+
+bool ExtendedListItem::toggleSelection()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    setSelected(!m_selected);
+    return m_selected;
 }