- Enable chat context menu actions
authorunc0rr
Wed, 10 Oct 2012 00:21:18 +0400
changeset 7737 ff63da8a3202
parent 7736 1cf84a84ef43
child 7738 abcc7012de0b
- Enable chat context menu actions - Load/save friends/ignore list
QTfrontend/model/playerslistmodel.cpp
QTfrontend/model/playerslistmodel.h
QTfrontend/ui/widget/chatwidget.cpp
--- a/QTfrontend/model/playerslistmodel.cpp	Wed Oct 10 00:18:35 2012 +0400
+++ b/QTfrontend/model/playerslistmodel.cpp	Wed Oct 10 00:21:18 2012 +0400
@@ -135,11 +135,41 @@
                 || flagType == Ignore || flagType == RoomAdmin)
             updateSortData(mil[0]);
 
+        if(flagType == Friend)
+        {
+            if(isSet)
+                m_friendsSet.insert(nickname.toLower());
+            else
+                m_friendsSet.remove(nickname.toLower());
+
+            saveSet(m_friendsSet, "friends");
+        }
+
+        if(flagType == Ignore)
+        {
+            if(isSet)
+                m_ignoredSet.insert(nickname.toLower());
+            else
+                m_ignoredSet.remove(nickname.toLower());
+
+            saveSet(m_ignoredSet, "ignore");
+        }
+
         updateIcon(mil[0]);
     }
 }
 
 
+bool PlayersListModel::isFlagSet(const QString & nickname, StateFlag flagType)
+{
+    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
+
+    if(mil.size())
+        return mil[0].data(flagType).toBool();
+    else
+        return false;
+}
+
 void PlayersListModel::resetRoomFlags()
 {
     for(int i = rowCount() - 1; i >= 0; --i)
@@ -151,6 +181,9 @@
             setData(mi, "0", RoomFilterRole);
             setData(mi, false, RoomAdmin);
             setData(mi, false, Ready);
+
+            updateSortData(mi);
+            updateIcon(mi);
         }
     }
 }
@@ -294,6 +327,8 @@
 
 void PlayersListModel::saveSet(const QSet<QString> & set, const QString & suffix)
 {
+    qDebug("saving set");
+
     QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix);
 
     QFile txt(fileName);
--- a/QTfrontend/model/playerslistmodel.h	Wed Oct 10 00:18:35 2012 +0400
+++ b/QTfrontend/model/playerslistmodel.h	Wed Oct 10 00:21:18 2012 +0400
@@ -32,6 +32,8 @@
 
     QVariant data(const QModelIndex &index, int role) const;
     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
+    void setFlag(const QString & nickname, StateFlag flagType, bool isSet);
+    bool isFlagSet(const QString & nickname, StateFlag flagType);
 
     bool insertRow(int row, const QModelIndex &parent = QModelIndex());
     bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
@@ -42,7 +44,6 @@
     void removePlayer(const QString & nickname);
     void playerJoinedRoom(const QString & nickname);
     void playerLeftRoom(const QString & nickname);
-    void setFlag(const QString & nickname, StateFlag flagType, bool isSet);
     void resetRoomFlags();
     void setNickname(const QString & nickname);
 
--- a/QTfrontend/ui/widget/chatwidget.cpp	Wed Oct 10 00:18:35 2012 +0400
+++ b/QTfrontend/ui/widget/chatwidget.cpp	Wed Oct 10 00:21:18 2012 +0400
@@ -35,13 +35,15 @@
 #include <QTime>
 #include <QPainter>
 #include <QListView>
-
 #include <QMessageBox>
-
+#include <QModelIndexList>
+#include <QDebug>
+#include <QSortFilterProxyModel>
 
 #include "DataManager.h"
 #include "hwconsts.h"
 #include "gameuiconfig.h"
+#include "playerslistmodel.h"
 
 #include "chatwidget.h"
 
@@ -784,104 +786,119 @@
 
 void HWChatWidget::onKick()
 {
-    /*QListWidgetItem * curritem = chatNicks->currentItem();
-    if (curritem)
-        emit kick(curritem->text());*/
+    QModelIndexList mil = chatNicks->selectionModel()->selectedRows();
+
+    if(mil.size())
+        emit kick(mil[0].data().toString());
 }
 
 void HWChatWidget::onBan()
 {
-    /*QListWidgetItem * curritem = chatNicks->currentItem();
-    if (curritem)
-        emit ban(curritem->text());*/
+    QModelIndexList mil = chatNicks->selectionModel()->selectedRows();
+
+    if(mil.size())
+        emit ban(mil[0].data().toString());
 }
 
 void HWChatWidget::onInfo()
 {
-    /*QListWidgetItem * curritem = chatNicks->currentItem();
-    if (curritem)
-        emit info(curritem->text());*/
+    QModelIndexList mil = chatNicks->selectionModel()->selectedRows();
+
+    if(mil.size())
+        emit info(mil[0].data().toString());
 }
 
 void HWChatWidget::onFollow()
 {
-    /*QListWidgetItem * curritem = chatNicks->currentItem();
-    if (curritem)
-        emit follow(curritem->text());*/
+    QModelIndexList mil = chatNicks->selectionModel()->selectedRows();
+
+    if(mil.size())
+        emit follow(mil[0].data().toString());
 }
 
 void HWChatWidget::onIgnore()
 {
-    /*QListWidgetItem * curritem = chatNicks->currentItem();
-    QString nick = "";
-    if(curritem != NULL)
-        nick = curritem->text();
+    QModelIndexList mil = chatNicks->selectionModel()->selectedRows();
+
+    QString nick;
+    if(mil.size())
+        nick = mil[0].data().toString();
     else
         nick = m_clickedNick;
 
-    if(ignoreList.contains(nick, Qt::CaseInsensitive)) // already on list - remove him
+    QSortFilterProxyModel * playersSortFilterModel = qobject_cast<QSortFilterProxyModel *>(chatNicks->model());
+    if(!playersSortFilterModel)
+        return;
+
+    PlayersListModel * players = qobject_cast<PlayersListModel *>(playersSortFilterModel->sourceModel());
+
+    if(!players)
+        return;
+
+    if(players->isFlagSet(nick, PlayersListModel::Ignore))
     {
-        ignoreList.removeAll(nick.toLower());
+        players->setFlag(nick, PlayersListModel::Ignore, false);
         chatEditLine->addNickname(nick);
         displayNotice(tr("%1 has been removed from your ignore list").arg(linkedNick(nick)));
     }
     else // not on list - add
     {
         // don't consider ignored people friends
-        if(friendsList.contains(nick, Qt::CaseInsensitive))
+        if(players->isFlagSet(nick, PlayersListModel::Friend))
             emit onFriend();
 
-        // scroll down on first ignore added so that people see where that nick went to
-        if (ignoreList.isEmpty())
-            chatNicks->scrollToBottom();
-
-        ignoreList << nick.toLower();
+        players->setFlag(nick, PlayersListModel::Ignore, true);
         chatEditLine->removeNickname(nick);
         displayNotice(tr("%1 has been added to your ignore list").arg(linkedNick(nick)));
     }
 
-    if(curritem != NULL)
+    if(mil.size())
     {
-        updateNickItem(curritem); // update icon/sort order/etc
-        chatNicks->sortItems();
+        chatNicks->scrollTo(chatNicks->selectionModel()->selectedRows()[0]);
         chatNickSelected(0); // update context menu
-    }*/
+    }
 }
 
 void HWChatWidget::onFriend()
 {
-    /*QListWidgetItem * curritem = chatNicks->currentItem();
-    QString nick = "";
-    if(curritem != NULL)
-        nick = curritem->text();
+    QModelIndexList mil = chatNicks->selectionModel()->selectedRows();
+
+    QString nick;
+    if(mil.size())
+        nick = mil[0].data().toString();
     else
         nick = m_clickedNick;
 
-    if(friendsList.contains(nick, Qt::CaseInsensitive)) // already on list - remove him
+    QSortFilterProxyModel * playersSortFilterModel = qobject_cast<QSortFilterProxyModel *>(chatNicks->model());
+    if(!playersSortFilterModel)
+        return;
+
+    PlayersListModel * players = qobject_cast<PlayersListModel *>(playersSortFilterModel->sourceModel());
+
+    if(!players)
+        return;
+
+    if(players->isFlagSet(nick, PlayersListModel::Friend))
     {
-        friendsList.removeAll(nick.toLower());
+        players->setFlag(nick, PlayersListModel::Friend, false);
+        chatEditLine->removeNickname(nick);
         displayNotice(tr("%1 has been removed from your friends list").arg(linkedNick(nick)));
     }
     else // not on list - add
     {
-        // don't ignore the new friend
-        if(ignoreList.contains(nick, Qt::CaseInsensitive))
+        if(players->isFlagSet(nick, PlayersListModel::Ignore))
             emit onIgnore();
 
-        // scroll up on first friend added so that people see where that nick went to
-        if (friendsList.isEmpty())
-            chatNicks->scrollToTop();
-
-        friendsList << nick.toLower();
+        players->setFlag(nick, PlayersListModel::Friend, true);
+        chatEditLine->addNickname(nick);
         displayNotice(tr("%1 has been added to your friends list").arg(linkedNick(nick)));
     }
 
-    if(curritem != NULL)
+    if(mil.size())
     {
-        updateNickItem(curritem); // update icon/sort order/etc
-        chatNicks->sortItems();
+        chatNicks->scrollTo(chatNicks->selectionModel()->selectedRows()[0]);
         chatNickSelected(0); // update context menu
-    }*/
+    }
 }
 
 void HWChatWidget::chatNickDoubleClicked(QListWidgetItem * item)