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),
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);
64 QFontMetrics textMetrics = painter.fontMetrics();
66 QString expandedText = TextModifier::splitLongWords(textMetrics, text, m_subItemTextWidth);
68 ExtendedListItemStore *itemStore = new ExtendedListItemStore(expandedText);
69 itemStore->setIcon(icon);
70 itemStore->setShortenedText(shortenText(expandedText, m_subItemTextWidth,
71 ListItem::TEXT_SIZE_SMALL));
72 itemStore->setTextRect(calculateExpandedTextRect(expandedText));
74 m_subItemStoreList->append(itemStore);
77 QRect ExtendedListItem::boundingRect(const QString &text)
79 qDebug() << __PRETTY_FUNCTION__;
81 QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
83 painter.setFont(NOKIA_FONT_SMALL);
84 QFontMetrics textMetrics = painter.fontMetrics();
87 textRect.setSize(QSize(m_subItemTextWidth, textMetrics.height()));
89 QStringList lines = text.split("\n");
90 for (int i = 1; i < lines.count(); ++i) {
91 textRect.setHeight(textRect.height() + textMetrics.height());
94 QStringList words = text.split(" ");
96 for (int i = 0; i < words.count(); ++i) {
99 textLine.append(words.at(i));
101 textLine.append(" " + words.at(i));
103 if ((textMetrics.width(textLine) > m_subItemTextWidth) && !textLine.contains("\n")) {
104 textRect.setHeight(textRect.height() + textMetrics.height());
106 textLine.append(words.at(i));
113 QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
115 qDebug() << __PRETTY_FUNCTION__;
117 const int TEXT_BOTTOM_MARGIN = 2;
119 QRect textRect = boundingRect(text);
120 QRect expandedTextRect = textRect;
122 m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT + TEXT_BOTTOM_MARGIN;
123 m_expandedHeight += expandedTextRect.height() + TEXT_BOTTOM_MARGIN;
124 setSize(QSize(ITEM_WIDTH, m_normalHeight));
126 return expandedTextRect;
129 void ExtendedListItem::clearSubItems()
131 qDebug() << __PRETTY_FUNCTION__;
133 qDeleteAll(m_subItemStoreList->begin(), m_subItemStoreList->end());
134 m_subItemStoreList->clear();
136 m_expandedHeight = ITEM_MIN_HEIGHT;
137 m_normalHeight = ITEM_MIN_HEIGHT;
140 void ExtendedListItem::setSubitemTextWidth(int width)
142 qDebug() << __PRETTY_FUNCTION__;
144 m_subItemTextWidth = width;
147 void ExtendedListItem::setSelected(bool selected)
149 qDebug() << __PRETTY_FUNCTION__;
151 QListWidgetItem::setSelected(selected);
152 m_selected = selected;
153 setData(ITEM_EXPANDED_INDEX, m_selected);
156 setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_expandedHeight));
158 setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_normalHeight));
161 bool ExtendedListItem::toggleSelection()
163 qDebug() << __PRETTY_FUNCTION__;
165 setSelected(!m_selected);