Added new classes, ExtendedListItem, -Delegate and -Store.
[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;
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     m_subItemStoreList = new QList<ExtendedListItemStore *>();
45     setData(SUBITEM_STORE_INDEX, qVariantFromValue((void *) m_subItemStoreList));
46 }
47
48 void ExtendedListItem::addSubItem(const QString &text, const QPixmap &icon)
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     ExtendedListItemStore *itemStore = new ExtendedListItemStore(text);
53     itemStore->setIcon(icon);
54     itemStore->setShortenedText(shortenText(text, m_subItemTextWidth, ListItem::TEXT_SIZE_SMALL));
55     itemStore->setTextRect(calculateExpandedTextRect(text));
56
57     m_subItemStoreList->append(itemStore);
58 }
59
60 QRect ExtendedListItem::calculateExpandedTextRect(const QString &text)
61 {
62     qDebug() << __PRETTY_FUNCTION__;
63
64     QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
65     QPainter painter(&p);
66     painter.setFont(NOKIA_FONT_SMALL);
67     QFontMetrics textMetrics = painter.fontMetrics();
68
69     QRect textRect = textMetrics.boundingRect(text);
70     qWarning() << textRect.width() << textRect.height();
71     int textRectFactor = textRect.width() / m_subItemTextWidth;
72     textRectFactor++;
73     QRect expandedTextRect = QRect(0, 0, m_subItemTextWidth, SUBITEM_TEXT_ROW_HEIGHT
74                                    * textRectFactor);
75     qWarning() << expandedTextRect.width() << expandedTextRect.height();
76     m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT;
77     m_expandedHeight += expandedTextRect.height();
78
79     setSize(QSize(ITEM_WIDTH, m_normalHeight));
80
81     return expandedTextRect;
82 }
83
84 void ExtendedListItem::setSubitemTextWidth(int width)
85 {
86     m_subItemTextWidth = width;
87 }
88
89 bool ExtendedListItem::toggleSelection()
90 {
91     qWarning() << __PRETTY_FUNCTION__;
92
93     setSelected(!m_selected);
94     return m_selected;
95 }
96
97 void ExtendedListItem::setSelected(bool selected)
98 {
99     qWarning() << __PRETTY_FUNCTION__;
100
101     m_selected = selected;
102     setData(ITEM_EXPANDED_INDEX, m_selected);
103
104     if (m_selected) {
105         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_expandedHeight));
106     } else {
107         setData(ITEM_SIZE_HINT_INDEX, QSize(ITEM_WIDTH, m_normalHeight));
108     }
109 }