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