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