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