1ecb362670c4eed9efd26083bf60233c74921a52
[situare] / src / ui / extendedlistitem.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
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.
10
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.
15
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,
19    USA.
20 */
21
22 #include <QDebug>
23 #include <QFontMetrics>
24 #include <QPainter>
25
26 #include "../common.h"
27 #include "extendedlistitemstore.h"
28 #include "listcommon.h"
29 #include "textmodifier.h"
30
31 #include "extendedlistitem.h"
32
33 ExtendedListItem::ExtendedListItem()
34     : m_selected(false),
35       m_expandedHeight(ITEM_MIN_HEIGHT),
36       m_normalHeight(ITEM_MIN_HEIGHT),
37       m_subItemTextWidth(0)
38
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41
42     setSize(QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
43
44     m_subItemStoreList = new QList<ExtendedListItemStore *>();
45     setData(SUBITEM_STORE_INDEX, qVariantFromValue((void *) m_subItemStoreList));
46 }
47
48 ExtendedListItem::~ExtendedListItem()
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     clearSubItems();
53     delete m_subItemStoreList;
54 }
55
56 void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
57 {
58     qDebug() << __PRETTY_FUNCTION__;
59
60     QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
61     QPainter painter(&p);
62     painter.setFont(NOKIA_FONT_SMALL);
63     QFontMetrics textMetrics = painter.fontMetrics();
64
65     QString expandedText = TextModifier::splitLongWords(textMetrics, text, m_subItemTextWidth);
66
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));
71
72     m_subItemStoreList->append(itemStore);
73 }
74
75 QRect ExtendedListItem::boundingRect(const QString &text)
76 {
77     qDebug() << __PRETTY_FUNCTION__;
78
79     QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
80     QPainter painter(&p);
81     painter.setFont(NOKIA_FONT_SMALL);
82
83     QFontMetrics textMetrics = painter.fontMetrics();
84     QRect textRect;
85     int rowHeight = textMetrics.ascent() + textMetrics.descent();
86     textRect.setSize(QSize(m_subItemTextWidth, rowHeight));
87
88     QStringList words = text.split(QRegExp("\\b"));
89     QString rowText;
90     foreach (QString word, words) {
91
92         if (word.contains("\n")) {
93             foreach (QChar c, word) {
94                 if (c == '\n')
95                     textRect.setHeight(textRect.height() + rowHeight);
96             }
97             rowText.clear();
98         } else {
99             rowText.append(word);
100             if (textMetrics.width(rowText) > m_subItemTextWidth) {
101                 textRect.setHeight(textRect.height() + rowHeight);
102                 rowText = word.simplified();
103             }
104         }
105     }
106
107     return textRect;
108 }
109
110 QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
111 {
112     qDebug() << __PRETTY_FUNCTION__;
113
114     const int TEXT_BOTTOM_MARGIN = 2;
115
116     QRect expandedTextRect = boundingRect(text);
117
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));
121
122     return expandedTextRect;
123 }
124
125 void ExtendedListItem::clearSubItems()
126 {
127     qDebug() << __PRETTY_FUNCTION__;
128
129     qDeleteAll(m_subItemStoreList->begin(), m_subItemStoreList->end());
130     m_subItemStoreList->clear();
131
132     m_expandedHeight = ITEM_MIN_HEIGHT;
133     m_normalHeight = ITEM_MIN_HEIGHT;
134 }
135
136 void ExtendedListItem::setSubitemTextWidth(int width)
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     m_subItemTextWidth = width;
141 }
142
143 void ExtendedListItem::setSelected(bool selected)
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     QListWidgetItem::setSelected(selected);
148     m_selected = selected;
149     setData(ITEM_EXPANDED_INDEX, m_selected);
150
151     if (m_selected)
152         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_expandedHeight));
153     else
154         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_normalHeight));
155 }
156
157 bool ExtendedListItem::toggleSelection()
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160
161     setSelected(!m_selected);
162     return m_selected;
163 }