Added TextModifier class.
[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
64     QFontMetrics textMetrics = painter.fontMetrics();
65
66     QString expandedText = TextModifier::splitLongWords(textMetrics, text, m_subItemTextWidth);
67
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));
73
74     m_subItemStoreList->append(itemStore);
75 }
76
77 QRect ExtendedListItem::boundingRect(const QString &text)
78 {
79     qWarning() << __PRETTY_FUNCTION__ << text;
80
81     QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
82     QPainter painter(&p);
83     painter.setFont(NOKIA_FONT_SMALL);
84
85     QFontMetrics textMetrics = painter.fontMetrics();
86     QRect textRect;
87     textRect.setSize(QSize(m_subItemTextWidth, textMetrics.height()));
88
89     QStringList rows = text.split('\n');
90
91     for (int i = 1; i < rows.count(); ++i) {
92         qWarning() << "jep";
93         QRect textRowRect = textMetrics.boundingRect(rows.at(i));
94         textRect.setHeight(textRect.height() + textRowRect.height());
95     }
96
97     QStringList words = text.split(" ");
98     QString rowText;
99     for (int i = 0; i < words.count(); ++i) {
100         if (i == words.count() -1)
101             rowText.append(words.at(i));
102         else
103             rowText.append(words.at(i) + " ");
104         if (textMetrics.width(rowText) > m_subItemTextWidth) {
105             textRect.setHeight(textRect.height() + textMetrics.height());
106             rowText = words.at(i);
107             qWarning() << "hep";
108         }
109     }
110
111     //textRect.setHeight(textRect.height() + 5);
112 //    qWarning() << textRect.width() << textRect.height();
113     return textRect;
114 }
115
116 QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     const int TEXT_BOTTOM_MARGIN = 2;
121
122     QRect textRect = boundingRect(text);
123     QRect expandedTextRect = textRect;
124 //    int textRectFactor = textRect.width() / m_subItemTextWidth;
125 //    textRectFactor += textRect.height() / SUBITEM_TEXT_ROW_HEIGHT;
126
127 //    QRect expandedTextRect = QRect(0, 0, m_subItemTextWidth, SUBITEM_TEXT_ROW_HEIGHT
128 //                                   * qMax(textRectFactor, 1));
129
130     m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT + TEXT_BOTTOM_MARGIN;
131     m_expandedHeight += expandedTextRect.height() + TEXT_BOTTOM_MARGIN;
132     setSize(QSize(ITEM_WIDTH, m_normalHeight));
133
134     return expandedTextRect;
135 }
136
137 void ExtendedListItem::clearSubItems()
138 {
139     qDebug() << __PRETTY_FUNCTION__;
140
141     qDeleteAll(m_subItemStoreList->begin(), m_subItemStoreList->end());
142     m_subItemStoreList->clear();
143
144     m_expandedHeight = ITEM_MIN_HEIGHT;
145     m_normalHeight = ITEM_MIN_HEIGHT;
146 }
147
148 void ExtendedListItem::setSubitemTextWidth(int width)
149 {
150     qDebug() << __PRETTY_FUNCTION__;
151
152     m_subItemTextWidth = width;
153 }
154
155 void ExtendedListItem::setSelected(bool selected)
156 {
157     qDebug() << __PRETTY_FUNCTION__;
158
159     QListWidgetItem::setSelected(selected);
160     m_selected = selected;
161     setData(ITEM_EXPANDED_INDEX, m_selected);
162
163     if (m_selected)
164         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_expandedHeight));
165     else
166         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_normalHeight));
167 }
168
169 bool ExtendedListItem::toggleSelection()
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     setSelected(!m_selected);
174     return m_selected;
175 }