2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jussi Laitinen - jussi.laitinen@ixonos.com
7 Situare is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
11 Situare is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Situare; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 #include <QFontMetrics>
26 #include "../common.h"
27 #include "extendedlistitemstore.h"
28 #include "listcommon.h"
29 #include "textmodifier.h"
31 #include "extendedlistitem.h"
33 ExtendedListItem::ExtendedListItem()
35 m_expandedHeight(ITEM_MIN_HEIGHT),
36 m_normalHeight(ITEM_MIN_HEIGHT),
37 m_subItemTextWidth(SUBITEM_TEXT_MAX_WIDTH)
40 qDebug() << __PRETTY_FUNCTION__;
42 setSize(QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
44 m_subItemStoreList = new QList<ExtendedListItemStore *>();
45 setData(SUBITEM_STORE_INDEX, qVariantFromValue((void *) m_subItemStoreList));
48 ExtendedListItem::~ExtendedListItem()
50 qDebug() << __PRETTY_FUNCTION__;
53 delete m_subItemStoreList;
56 void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
58 qDebug() << __PRETTY_FUNCTION__;
60 QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
62 painter.setFont(NOKIA_FONT_SMALL);
63 QFontMetrics textMetrics = painter.fontMetrics();
65 QString expandedText = TextModifier::splitLongWords(textMetrics, text, m_subItemTextWidth);
67 ExtendedListItemStore *itemStore = new ExtendedListItemStore(expandedText);
68 itemStore->setIcon(icon);
69 itemStore->setShortenedText(shortenText(text, m_subItemTextWidth, ListItem::TEXT_SIZE_SMALL));
70 itemStore->setTextRect(calculateExpandedTextRect(expandedText));
72 m_subItemStoreList->append(itemStore);
75 QRect ExtendedListItem::boundingRect(const QString &text)
77 qDebug() << __PRETTY_FUNCTION__;
79 QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
81 painter.setFont(NOKIA_FONT_SMALL);
83 QFontMetrics textMetrics = painter.fontMetrics();
85 int rowHeight = textMetrics.ascent() + textMetrics.descent();
86 textRect.setSize(QSize(m_subItemTextWidth, rowHeight));
88 QStringList words = text.split(QRegExp("\\b"));
90 foreach (QString word, words) {
92 if (word.contains("\n")) {
93 foreach (QChar c, word) {
95 textRect.setHeight(textRect.height() + rowHeight);
100 if (textMetrics.width(rowText) > m_subItemTextWidth) {
101 textRect.setHeight(textRect.height() + rowHeight);
102 rowText = word.simplified();
110 QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
112 qDebug() << __PRETTY_FUNCTION__;
114 const int TEXT_BOTTOM_MARGIN = 2;
116 QRect expandedTextRect = boundingRect(text);
118 m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT + TEXT_BOTTOM_MARGIN;
119 m_expandedHeight += expandedTextRect.height() + TEXT_BOTTOM_MARGIN;
120 setSize(QSize(ITEM_WIDTH, m_normalHeight));
122 return expandedTextRect;
125 void ExtendedListItem::clearSubItems()
127 qDebug() << __PRETTY_FUNCTION__;
129 qDeleteAll(m_subItemStoreList->begin(), m_subItemStoreList->end());
130 m_subItemStoreList->clear();
132 m_expandedHeight = ITEM_MIN_HEIGHT;
133 m_normalHeight = ITEM_MIN_HEIGHT;
136 void ExtendedListItem::setSubitemTextWidth(int width)
138 qDebug() << __PRETTY_FUNCTION__;
140 m_subItemTextWidth = width;
143 void ExtendedListItem::setSelected(bool selected)
145 qDebug() << __PRETTY_FUNCTION__;
147 QListWidgetItem::setSelected(selected);
148 m_selected = selected;
149 setData(ITEM_EXPANDED_INDEX, m_selected);
152 setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_expandedHeight));
154 setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_normalHeight));
157 bool ExtendedListItem::toggleSelection()
159 qDebug() << __PRETTY_FUNCTION__;
161 setSelected(!m_selected);