e215a7415b6db9b3a7db1f1fed844d586e6308ed
[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
30 #include "extendedlistitem.h"
31
32 ExtendedListItem::ExtendedListItem()
33     : m_selected(false),
34       m_expandedHeight(ITEM_MIN_HEIGHT),
35       m_normalHeight(ITEM_MIN_HEIGHT),
36       m_subItemTextWidth(0)
37
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     setSize(QSize(ITEM_WIDTH, ITEM_MIN_HEIGHT));
42
43     m_subItemStoreList = new QList<ExtendedListItemStore *>();
44     setData(SUBITEM_STORE_INDEX, qVariantFromValue((void *) m_subItemStoreList));
45 }
46
47 void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
48 {
49     qDebug() << __PRETTY_FUNCTION__;
50
51     ExtendedListItemStore *itemStore = new ExtendedListItemStore(text);
52     itemStore->setIcon(icon);
53     itemStore->setShortenedText(shortenText(text, m_subItemTextWidth, ListItem::TEXT_SIZE_SMALL));
54     itemStore->setTextRect(calculateExpandedTextRect(text));
55
56     m_subItemStoreList->append(itemStore);
57 }
58
59 QRect ExtendedListItem::boundingRect(const QString &text)
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
64     QPainter painter(&p);
65     painter.setFont(NOKIA_FONT_SMALL);
66
67     QFontMetrics textMetrics = painter.fontMetrics();
68     QRect textRect;
69     QStringList rows = text.split('\n');
70
71     foreach (QString row, rows) {
72
73         QRect textRowRect = textMetrics.boundingRect(row);
74
75         if (textRowRect.width() > textRect.width())
76             textRect.setWidth(textRowRect.width());
77
78         textRect.setHeight(textRect.height() + textRowRect.height());
79     }
80
81     return textRect;
82 }
83
84 void ExtendedListItem::clearSubItems()
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     qDeleteAll(m_subItemStoreList->begin(), m_subItemStoreList->end());
89     m_subItemStoreList->clear();
90
91     m_expandedHeight = ITEM_MIN_HEIGHT;
92     m_normalHeight = ITEM_MIN_HEIGHT;
93 }
94
95 QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
96 {
97     qDebug() << __PRETTY_FUNCTION__;
98
99     const int TEXT_BOTTOM_MARGIN = 2;
100
101     QRect textRect = boundingRect(text);
102
103     int textRectFactor = textRect.width() / m_subItemTextWidth;
104     textRectFactor += textRect.height() / SUBITEM_TEXT_ROW_HEIGHT;
105
106     QRect expandedTextRect = QRect(0, 0, m_subItemTextWidth, SUBITEM_TEXT_ROW_HEIGHT
107                                    * qMax(textRectFactor, 1));
108
109     m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT + TEXT_BOTTOM_MARGIN;
110     m_expandedHeight += expandedTextRect.height() + TEXT_BOTTOM_MARGIN;
111     setSize(QSize(ITEM_WIDTH, m_normalHeight));
112
113     return expandedTextRect;
114 }
115
116 void ExtendedListItem::setSubitemTextWidth(int width)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     m_subItemTextWidth = width;
121 }
122
123 bool ExtendedListItem::toggleSelection()
124 {
125     qDebug() << __PRETTY_FUNCTION__;
126
127     setSelected(!m_selected);
128     return m_selected;
129 }
130
131 void ExtendedListItem::setSelected(bool selected)
132 {
133     qDebug() << __PRETTY_FUNCTION__ << selected;
134
135     QListWidgetItem::setSelected(selected);
136     m_selected = selected;
137     setData(ITEM_EXPANDED_INDEX, m_selected);
138
139     if (m_selected)
140         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_expandedHeight));
141     else
142         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_normalHeight));
143 }
144
145 ExtendedListItem::~ExtendedListItem()
146 {
147     qDebug() << __PRETTY_FUNCTION__;
148
149     clearSubItems();
150     delete m_subItemStoreList;
151 }