Fixed search history settings read and write and added search history
[situare] / src / ui / listview.cpp
index c1ddaf1..40ec9c2 100644 (file)
@@ -1,3 +1,24 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
 #include <QDebug>
 
 #include "listitem.h"
 
 ListView::ListView(QWidget *parent)
     : QListWidget(parent),
-      previousItem(0)
+      m_currentItem(0)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
+    setSelectionMode(QAbstractItemView::SingleSelection);
+    setAutoFillBackground(false);
+    viewport()->setAutoFillBackground(false);
+
     connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
             this, SLOT(listItemClicked(QListWidgetItem*)));
 }
@@ -21,96 +46,225 @@ void ListView::addListItem(const QString &key, ListItem *item)
     if (!m_listItems.contains(key)) {
         addItem(item);
         m_listItems.insert(key, item);
-        qDebug() << __PRETTY_FUNCTION__ << key << " lisattiin";
     }
-    else
-        qDebug() << __PRETTY_FUNCTION__ << key << " ei lisatty";
 }
 
 void ListView::addListItemToView(ListItem *item)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     addItem(item);
 }
 
+void ListView::clearItemSelection()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    clearSelection();
+
+    if (m_currentItem)
+        m_currentItem->setSelected(false);
+}
+
 void ListView::clearList()
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
+    qDeleteAll(m_listItems.begin(), m_listItems.end());
     m_listItems.clear();
     clear();
 
-    qDebug() << __PRETTY_FUNCTION__ << count();
-    qDebug() << __PRETTY_FUNCTION__ << m_listItems.count();
+    m_currentItem = 0;
 }
 
-void ListView::clearUnused(const QStringList &userIDs)
+void ListView::clearUnused(const QStringList &itemIDs)
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
     foreach (QString key, m_listItems.keys()) {
-        if (!userIDs.contains(key)) {
+        if (!itemIDs.contains(key)) {
             ListItem *item = m_listItems.take(key);
             if (item) {
                 takeItem(row(item));
+                if (m_currentItem == item)
+                    m_currentItem = 0;
                 delete item;
             }
         }
     }
-    qDebug() << __PRETTY_FUNCTION__ << count();
-    qDebug() << __PRETTY_FUNCTION__ << m_listItems.count();
 }
 
 void ListView::clearFilter()
 {
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_filteredItemIDs.clear();
+
     foreach (ListItem *item, m_listItems)
         setItemHidden(item, false);
 }
 
-bool ListView::contains(const QString &userID)
+bool ListView::contains(const QString &itemID)
 {
-    return m_listItems.contains(userID);
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_listItems.contains(itemID);
 }
 
-void ListView::filter(const QList<QString> &userIDs)
+void ListView::filter(const QList<QString> &itemIDs)
 {
-    qDebug() << __PRETTY_FUNCTION__ << m_listItems.count();
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_filteredItemIDs = itemIDs;
 
     foreach (ListItem *item, m_listItems) {
-        if (userIDs.contains(item->id()))
-            setItemHidden(item, false);
-        else
-            setItemHidden(item, true);
+        if (itemIDs.contains(m_listItems.key(item))) {
+            item->setHidden(false);
+        } else {
+            item->setSelected(false);
+            item->setHidden(true);
+        }
     }
+
+    emit listItemSelectionChanged();
 }
 
-ListItem *ListView::takeListItemFromView(const QString &userID)
+void ListView::filter(const QString &pattern)
 {
-    ListItem *item = listItem(userID);
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_filteredItemIDs.isEmpty()) {
+        foreach (ListItem *item, m_listItems) {
+            if (item->title().contains(pattern, Qt::CaseInsensitive)) {
+                item->setHidden(false);
+            } else {
+                item->setSelected(false);
+                item->setHidden(true);
+            }
+        }
+    } else {
+        foreach (QString key, m_filteredItemIDs) {
+            ListItem *item = m_listItems.value(key);
+            if (item) {
+                if (item->title().contains(pattern, Qt::CaseInsensitive)) {
+                    item->setHidden(false);
+                } else {
+                    item->setSelected(false);
+                    item->setHidden(true);
+                }
+            }
+        }
+    }
+
+    emit listItemSelectionChanged();
+}
+
+ListItem *ListView::takeListItemFromView(const QString &itemID)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    ListItem *item = listItem(itemID);
     takeItem(row(item));
     return item;
 }
 
+bool ListView::listItemClicked(ListItem *clickedItem)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_currentItem == clickedItem) {
+        clickedItem->toggleSelection();
+    } else {
+        if (m_currentItem)
+            m_currentItem->setSelected(false);
+
+        clickedItem->setSelected(true);
+    }
+    m_currentItem = clickedItem;
+
+    emit listItemSelectionChanged();
+
+    return clickedItem->isSelected();
+}
+
 void ListView::listItemClicked(QListWidgetItem *item)
 {
-    qWarning() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    ListItem *currentItem = static_cast<ListItem*>(item);
+    ListItem *currentItem = dynamic_cast<ListItem*>(item);
 
-    if (currentItem) {
+    if (currentItem)
+        listItemClicked(currentItem);
+}
 
-        if (previousItem == currentItem) {
-            currentItem->toggleHeight();
-        }
-        else {
-            if (previousItem)
-                previousItem->setExpanded(false);
+ListItem *ListView::listItem(const QString &itemID)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-            currentItem->setExpanded(true);
-        }
+    return dynamic_cast<ListItem*>(m_listItems.value(itemID));
+}
 
-        previousItem = currentItem;
+ListItem *ListView::listItemAt(int index)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    ListItem *listItem = 0;
+
+    if (index < count())
+        listItem = dynamic_cast<ListItem*>(item(index));
+
+    return listItem;
+}
 
-        emit listItemClicked(currentItem->coordinates());
+void ListView::prependListItem(const QString &key, ListItem *item)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (!m_listItems.contains(key)) {
+        insertItem(0, item);
+        m_listItems.insert(key, item);
     }
 }
 
-ListItem *ListView::listItem(const QString &userID)
+void ListView::removeLastItem()
 {
-    return static_cast<ListItem*>(m_listItems.value(userID));
+    qDebug() << __PRETTY_FUNCTION__;
+
+    ListItem *item = listItemAt(count() - 1);
+
+    if (item) {
+        if (item) {
+            m_listItems.remove(item->title());
+            takeItem(row(item));
+            if (m_currentItem == item)
+                m_currentItem = 0;
+            delete item;
+        }
+    }
+}
+
+ListItem *ListView::selectedItem()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QList<QListWidgetItem *> selectedListItems = selectedItems();
+
+    if (!selectedListItems.isEmpty())
+        return dynamic_cast<ListItem *>(selectedListItems.first());
+    else
+        return 0;
+}
+
+void ListView::setSelectedItem(ListItem *item)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    listItemClicked(item);
+}
+
+ListView::~ListView()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    clearList();
 }