6da9b6ba152a34e8a6f254b7de3f1dbfad413357
[situare] / src / ui / listview.h
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 #ifndef LISTVIEW_H
23 #define LISTVIEW_H
24
25 #include <QListWidget>
26
27 class GeoCoordinate;
28 class ListItem;
29
30 /**
31 * @brief View for ListItems.
32 *
33 * ListView is used to show ListItems in list view. Items can be added, removed or
34 * filtered.
35 *
36 * @author Jussi Laitinen - jussi.laitinen (at) ixonos.com
37 */
38 class ListView : public QListWidget
39 {
40     Q_OBJECT
41
42 public:
43     /**
44     * @brief Constructor.
45     *
46     * @param parent QWidget
47     */
48     ListView(QWidget *parent = 0);
49
50     /**
51     * @brief Destructor.
52     *
53     * Calls ListView::clearList().
54     */
55     ~ListView();
56
57 /******************************************************************************
58 * MEMBER FUNCTIONS AND SLOTS
59 ******************************************************************************/
60 public:
61     /**
62     * @brief Add item to view and item list.
63     *
64     * @param key user ID
65     * @param item item to add to view and list
66     */
67     void addListItem(const QString &key, ListItem *item);
68
69     /**
70     * @brief Adds item to view.
71     *
72     * @param item FriendListItem
73     */
74     void addListItemToView(ListItem *item);
75
76     /**
77     * @brief Clear unused items from view.
78     *
79     * Clears items which are not in item ID's list from the view and items list.
80     *
81     * @param itemIDs list of item ID's to keep in list view
82     */
83     void clearUnused(const QStringList &itemIDs);
84
85     /**
86     * @brief Clears filtering from list.
87     *
88     * Clears m_filteredItemIDs and shows all items.
89     */
90     void clearFilter();
91
92     /**
93     * @brief Deselects all selected items.
94     */
95     void clearItemSelection();
96
97     /**
98     * @brief Clears list.
99     *
100     * Items are removed from view and item list.
101     */
102     void clearList();
103
104     /**
105     * @brief Checks if view contains item with userID.
106     *
107     * @param userID user's ID
108     * @return true if view contains item, false otherwise
109     */
110     bool contains(const QString &userID);
111
112     /**
113     * @brief Filters list by item IDs.
114     *
115     * Hide all items that are not in the itemIDs list.
116     *
117     * @param itemIDs item ID's for items that are shown
118     */
119     void filter(const QList<QString> &itemIDs);
120
121     /**
122     * @brief Filters list by text pattern.
123     *
124     * Filtering uses item names. If filtering by item IDs is on, filters only those items.
125     *
126     * @param pattern text pattern to filter
127     */
128     void filter(const QString &pattern);
129
130     /**
131     * @brief Takes item from view.
132     *
133     * Item is not deleted.
134     *
135     * @param itemID item's ID
136     * @return ListItem
137     */
138     ListItem *takeListItemFromView(const QString &itemID);
139
140     /**
141     * @brief Returns ListItem with itemID.
142     *
143     * @param itemID item's ID
144     * @return ListItem
145     */
146     ListItem *listItem(const QString &itemID);
147
148     /**
149     * @brief Returns ListItem by index.
150     *
151     * @param index item's index
152     * @return ListItem
153     */
154     ListItem *listItemAt(int index);
155
156     /**
157     * @brief Returns selected ListItem.
158     *
159     * @return ListItem if there is selected, 0 otherwise
160     */
161     ListItem *selectedItem();
162
163     /**
164     * @brief Sets selected item.
165     *
166     * @param item ListItem to select
167     */
168     void setSelectedItem(ListItem *item);
169
170 protected slots:
171     /**
172     * @brief Slot for list item clicked.
173     *
174     * Toggles items selection state and emits listItemClicked signal.
175     * @param clickedItem clicked ListItem
176     * @return true if item was selected, false otherwise
177     */
178     virtual bool listItemClicked(ListItem *clickedItem);
179
180 private slots:
181     /**
182     * @brief Slot for list item clicked.
183     *
184     * Calls listItemClicked(ListItem *item)
185     * @param item QListWidgetItem
186     */
187     void listItemClicked(QListWidgetItem *item);
188
189 signals:
190     void listItemSelectionChanged();
191
192 /*******************************************************************************
193  * DATA MEMBERS
194  ******************************************************************************/
195 private:
196     QHash<QString, ListItem *> m_listItems; ///< List of items in this view. Key = user ID
197
198     QList<QString> m_filteredItemIDs;       ///< List of filtered item IDs
199
200     ListItem *m_currentItem;               ///< Previously selected item
201 };
202
203 #endif // LISTVIEW_H