Get rid of deprecated chat widget stuff
authorunc0rr
Wed, 10 Oct 2012 23:30:34 +0400
changeset 7741 d216f0599291
parent 7740 076f57836ee3
child 7742 e5e529dcc9bb
Get rid of deprecated chat widget stuff
QTfrontend/hwform.cpp
QTfrontend/ui/widget/chatwidget.cpp
QTfrontend/ui/widget/chatwidget.h
--- a/QTfrontend/hwform.cpp	Wed Oct 10 23:15:58 2012 +0400
+++ b/QTfrontend/hwform.cpp	Wed Oct 10 23:30:34 2012 +0400
@@ -626,19 +626,6 @@
     {
         config->reloadVideosValues();
     }
-
-    // load and save ignore/friends lists
-    if (lastid == ID_PAGE_NETGAME) // leaving a room
-        ui.pageNetGame->pChatWidget->saveLists(ui.pageOptions->editNetNick->text());
-    else if(lastid == ID_PAGE_ROOMSLIST) // leaving the lobby
-        ui.pageRoomsList->chatWidget->saveLists(ui.pageOptions->editNetNick->text());
-
-    if (id == ID_PAGE_NETGAME) // joining a room
-        ui.pageNetGame->pChatWidget->loadLists(ui.pageOptions->editNetNick->text());
-// joining the lobby
-    else if (id == ID_PAGE_ROOMSLIST)
-        ui.pageRoomsList->chatWidget->loadLists(ui.pageOptions->editNetNick->text());
-
 }
 
 void HWForm::GoToPage(int id)
--- a/QTfrontend/ui/widget/chatwidget.cpp	Wed Oct 10 23:15:58 2012 +0400
+++ b/QTfrontend/ui/widget/chatwidget.cpp	Wed Oct 10 23:30:34 2012 +0400
@@ -49,128 +49,6 @@
 #include "chatwidget.h"
 
 
-ListWidgetNickItem::ListWidgetNickItem(const QString& nick, bool isFriend, bool isIgnored) : QListWidgetItem(nick)
-{
-    setData(Friend, isFriend);
-    setData(Ignore, isIgnored);
-}
-
-void ListWidgetNickItem::setData(StateFlag role, const QVariant &value)
-{
-    QListWidgetItem::setData(role, value);
-
-    updateIcon();
-}
-
-bool ListWidgetNickItem::operator< (const QListWidgetItem & other) const
-{
-    // case in-sensitive comparison of the associated strings
-    // chars that are no letters are sorted at the end of the list
-
-    ListWidgetNickItem otherNick = const_cast<ListWidgetNickItem &>(dynamic_cast<const ListWidgetNickItem &>(other));
-
-    // ignored always down
-    if (data(Ignore).toBool() != otherNick.data(Ignore).toBool())
-        return !data(Ignore).toBool();
-
-    // friends always up
-    if (data(Friend).toBool() != otherNick.data(Friend).toBool())
-        return data(Friend).toBool();
-
-    QString txt1 = text().toLower();
-    QString txt2 = other.text().toLower();
-
-    bool firstIsShorter = (txt1.size() < txt2.size());
-    int len = firstIsShorter?txt1.size():txt2.size();
-
-    for (int i = 0; i < len; i++)
-    {
-        if (txt1[i] == txt2[i])
-            continue;
-        if (txt1[i].isLetter() != txt2[i].isLetter())
-            return txt1[i].isLetter();
-        return (txt1[i] < txt2[i]);
-    }
-
-    return firstIsShorter;
-}
-
-void ListWidgetNickItem::updateIcon()
-{
-    quint32 iconNum = 0;
-
-    QList<bool> flags;
-    flags
-        << data(Ready).toBool()
-        << data(ServerAdmin).toBool()
-        << data(RoomAdmin).toBool()
-        << data(Registered).toBool()
-        << data(Friend).toBool()
-        << data(Ignore).toBool()
-        ;
-
-    for(int i = flags.size() - 1; i >= 0; --i)
-        if(flags[i])
-            iconNum |= 1 << i;
-
-    if(m_icons().contains(iconNum))
-    {
-        setIcon(m_icons().value(iconNum));
-    }
-    else
-    {
-        QPixmap result(24, 16);
-        result.fill(Qt::transparent);
-
-        QPainter painter(&result);
-
-        if(data(Ready).toBool())
-            painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png"));
-
-        QString mainIconName(":/res/chat/");
-
-        if(data(RoomAdmin).toBool())
-            mainIconName += "roomadmin";
-        else if(data(ServerAdmin).toBool())
-            mainIconName += "serveradmin";
-        else
-            mainIconName += "hedgehog";
-
-        if(!data(Registered).toBool())
-            mainIconName += "_gray";
-
-        painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png"));
-
-        if(data(Ignore).toBool())
-            painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png"));
-        else
-        if(data(Friend).toBool())
-            painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png"));
-
-        painter.end();
-
-        QIcon icon(result);
-
-        setIcon(icon);
-        m_icons().insert(iconNum, icon);
-    }
-
-    if(data(Ignore).toBool())
-        setForeground(Qt::gray);
-    else
-    if(data(Friend).toBool())
-        setForeground(Qt::green);
-    else
-        setForeground(QBrush(QColor(0xff, 0xcc, 0x00)));
-}
-
-QHash<quint32, QIcon> & ListWidgetNickItem::m_icons()
-{
-    static QHash<quint32, QIcon> iconsCache;
-
-    return iconsCache;
-}
-
 QString * HWChatWidget::s_styleSheet = NULL;
 QStringList * HWChatWidget::s_displayNone = NULL;
 bool HWChatWidget::s_isTimeStamped = true;
@@ -479,86 +357,6 @@
     m_autoKickEnabled = enabled;
 }
 
-void HWChatWidget::loadList(QStringList & list, const QString & file)
-{
-    list.clear();
-    QFile txt(cfgdir->absolutePath() + "/" + file);
-    if(!txt.open(QIODevice::ReadOnly))
-        return;
-    QTextStream stream(&txt);
-    stream.setCodec("UTF-8");
-
-    while(!stream.atEnd())
-    {
-        QString str = stream.readLine();
-        if(str.startsWith(";") || str.length() == 0)
-            continue;
-        list << str.trimmed();
-    }
-    //readd once we require newer Qt than 4.4
-    //list.removeDuplicates();
-    txt.close();
-}
-
-void HWChatWidget::saveList(QStringList & list, const QString & file)
-{
-    QFile txt(cfgdir->absolutePath() + "/" + file);
-
-    // list empty? => rather have no file for the list than an empty one
-    if (list.isEmpty())
-    {
-        if (txt.exists())
-        {
-            // try to remove file, if successful we're done here.
-            if (txt.remove())
-                return;
-        }
-        else
-            // there is no file
-            return;
-    }
-
-    if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate))
-        return;
-    QTextStream stream(&txt);
-    stream.setCodec("UTF-8");
-
-    stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl;
-    for(int i = 0; i < list.size(); i++)
-        stream << list[i] << endl;
-    txt.close();
-}
-
-void HWChatWidget::updateNickItem(QListWidgetItem *nickItem)
-{
-    QString nick = nickItem->text();
-    ListWidgetNickItem * item = dynamic_cast<ListWidgetNickItem*>(nickItem);
-
-    item->setData(ListWidgetNickItem::Friend, QVariant(friendsList.contains(nick, Qt::CaseInsensitive)));
-    item->setData(ListWidgetNickItem::Ignore, QVariant(ignoreList.contains(nick, Qt::CaseInsensitive)));
-}
-
-void HWChatWidget::updateNickItems()
-{
-    /*for(int i = 0; i < chatNicks->count(); i++)
-        updateNickItem(chatNicks->item(i));
-
-    chatNicks->sortItems();*/
-}
-
-void HWChatWidget::loadLists(const QString & nick)
-{
-    loadList(ignoreList, nick.toLower() + "_ignore.txt");
-    loadList(friendsList, nick.toLower() + "_friends.txt");
-    updateNickItems();
-}
-
-void HWChatWidget::saveLists(const QString & nick)
-{
-    saveList(ignoreList, nick.toLower() + "_ignore.txt");
-    saveList(friendsList, nick.toLower() + "_friends.txt");
-}
-
 
 void HWChatWidget::returnPressed()
 {
@@ -599,17 +397,24 @@
 
 void HWChatWidget::onChatString(const QString& nick, const QString& str)
 {
-    bool isFriend = false;
+    QSortFilterProxyModel * playersSortFilterModel = qobject_cast<QSortFilterProxyModel *>(chatNicks->model());
+    if(!playersSortFilterModel)
+        return;
+
+    PlayersListModel * players = qobject_cast<PlayersListModel *>(playersSortFilterModel->sourceModel());
+
+    if(!players)
+        return;
 
     if (!nick.isEmpty())
     {
         // don't show chat lines that are from ignored nicks
-        if (ignoreList.contains(nick, Qt::CaseInsensitive))
+        if (players->isFlagSet(nick, PlayersListModel::Ignore))
             return;
-        // friends will get special treatment, of course
-        isFriend = friendsList.contains(nick, Qt::CaseInsensitive);
     }
 
+    bool isFriend = (!nick.isEmpty()) && players->isFlagSet(nick, PlayersListModel::Friend);
+
     QString formattedStr = Qt::escape(str.mid(1));
     // make hedgewars.org urls actual links
     formattedStr = formattedStr.replace(URLREGEXP, "<a href=\"http://\\3\">\\3</a>");
@@ -697,9 +502,19 @@
     chatText->moveCursor(QTextCursor::End);
 }
 
+
 void HWChatWidget::nickAdded(const QString & nick, bool notifyNick)
 {
-    bool isIgnored = ignoreList.contains(nick, Qt::CaseInsensitive);
+    QSortFilterProxyModel * playersSortFilterModel = qobject_cast<QSortFilterProxyModel *>(chatNicks->model());
+    if(!playersSortFilterModel)
+        return;
+
+    PlayersListModel * players = qobject_cast<PlayersListModel *>(playersSortFilterModel->sourceModel());
+
+    if(!players)
+        return;
+
+    bool isIgnored = players->isFlagSet(nick, PlayersListModel::Ignore);
 
     if (isIgnored && m_isAdmin && m_autoKickEnabled)
     {
@@ -707,10 +522,6 @@
         return;
     }
 
-    QListWidgetItem * item = new ListWidgetNickItem(nick, friendsList.contains(nick, Qt::CaseInsensitive), isIgnored);
-    updateNickItem(item);
-    /*chatNicks->addItem(item);*/
-
     if ((!isIgnored) && (nick != m_userNick)) // don't auto-complete own name
         chatEditLine->addNickname(nick);
 
@@ -727,10 +538,7 @@
 {
     chatEditLine->removeNickname(nick);
 
-    /*foreach(QListWidgetItem * item, chatNicks->findItems(nick, Qt::MatchExactly))
-        chatNicks->takeItem(chatNicks->row(item));*/
-
-    //emit nickCountUpdate(chatNicks->count());
+    emit nickCountUpdate(chatNicks->model()->rowCount());
 }
 
 void HWChatWidget::clear()
@@ -858,10 +666,7 @@
     }
 
     if(mil.size())
-    {
         chatNicks->scrollTo(chatNicks->selectionModel()->selectedRows()[0]);
-        chatNickSelected(); // update context menu
-    }
 }
 
 void HWChatWidget::onFriend()
@@ -900,12 +705,9 @@
     }
 
     if(mil.size())
-    {
         chatNicks->scrollTo(chatNicks->selectionModel()->selectedRows()[0]);
-        chatNickSelected(); // update context menu
-    }
 }
-
+/*
 void HWChatWidget::chatNickDoubleClicked(QListWidgetItem * item)
 {
     if (item != NULL)
@@ -915,42 +717,7 @@
     QList<QAction *> actions = chatNicks->actions();
     actions.first()->activate(QAction::Trigger);
 }
-
-void HWChatWidget::chatNickSelected()
-{
-}
-
-void HWChatWidget::setStatus(const QString & nick, ListWidgetNickItem::StateFlag flag, bool status)
-{
-    /*QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly);
-
-    if (items.size() == 1)
-    {
-        items[0]->setData(flag, status);
-        updateNickItem(items[0]);
-    }*/
-}
-
-void HWChatWidget::setReadyStatus(const QString & nick, bool isReady)
-{
-    setStatus(nick, ListWidgetNickItem::Ready, isReady);
-}
-
-void HWChatWidget::setAdminStatus(const QString & nick, bool isAdmin)
-{
-    setStatus(nick, ListWidgetNickItem::ServerAdmin, isAdmin);
-}
-
-void HWChatWidget::setRoomMasterStatus(const QString & nick, bool isAdmin)
-{
-    setStatus(nick, ListWidgetNickItem::RoomAdmin, isAdmin);
-}
-
-void HWChatWidget::setRegisteredStatus(const QStringList & nicks, bool isRegistered)
-{
-    foreach(const QString & nick, nicks)
-        setStatus(nick, ListWidgetNickItem::Registered, isRegistered);
-}
+*/
 
 void HWChatWidget::adminAccess(bool b)
 {
--- a/QTfrontend/ui/widget/chatwidget.h	Wed Oct 10 23:15:58 2012 +0400
+++ b/QTfrontend/ui/widget/chatwidget.h	Wed Oct 10 23:30:34 2012 +0400
@@ -40,29 +40,6 @@
 class QAbstractItemModel;
 class QMenu;
 
-/// Class for custom nickname sorting
-class ListWidgetNickItem : public QListWidgetItem
-{
-    public:
-        enum StateFlag {
-            Ready       = Qt::UserRole,
-            ServerAdmin = Qt::UserRole + 1,
-            RoomAdmin   = Qt::UserRole + 2,
-            Registered  = Qt::UserRole + 3,
-            Friend      = Qt::UserRole + 4,
-            Ignore      = Qt::UserRole + 5
-        };
-
-        ListWidgetNickItem(const QString & nick, bool isFriend, bool isIgnored);
-        void setData(StateFlag role, const QVariant &value);
-        bool operator<(const QListWidgetItem & other) const;        
-
-    private:
-        QHash<quint32, QIcon> & m_icons();
-        void updateIcon();
-};
-
-
 /**
  * @brief Chat widget.
  *
@@ -78,11 +55,8 @@
 
     public:
         HWChatWidget(QWidget* parent, QSettings * gameSettings, bool notify);
-        void loadLists(const QString & nick);
-        void saveLists(const QString & nick);
         void setIgnoreListKick(bool enabled); ///< automatically kick people on ignore list (if possible)
         void setShowFollow(bool enabled);
-        QStringList ignoreList, friendsList;
         static const QString & styleSheet();
         void displayError(const QString & message);
         void displayNotice(const QString & message);
@@ -103,10 +77,6 @@
 
         static void setStyleSheet(const QString & styleSheet = "");
 
-        void loadList(QStringList & list, const QString & file);
-        void saveList(QStringList & list, const QString & file);
-        void updateNickItem(QListWidgetItem *item);
-        void updateNickItems();
         void addLine(const QString & cssClass, QString line, bool isHighlight = false);
         bool parseCommand(const QString & line);
         void discardStyleSheet();
@@ -120,10 +90,6 @@
         void nickAdded(const QString& nick, bool notifyNick);
         void nickRemoved(const QString& nick);
         void clear();
-        void setReadyStatus(const QString & nick, bool isReady);
-        void setAdminStatus(const QString & nick, bool isAdmin);
-        void setRoomMasterStatus(const QString & nick, bool isAdmin);
-        void setRegisteredStatus(const QStringList & nicks, bool isRegistered);
         void adminAccess(bool);
 
     signals:
@@ -157,8 +123,6 @@
         bool notify;
         bool m_autoKickEnabled;
 
-        void setStatus(const QString & nick, ListWidgetNickItem::StateFlag flag, bool status);
-
     private slots:
         void returnPressed();
         void onBan();
@@ -167,8 +131,6 @@
         void onFollow();
         void onIgnore();
         void onFriend();
-        void chatNickDoubleClicked(QListWidgetItem * item);
-        void chatNickSelected();
         void linkClicked(const QUrl & link);
         void nicksContextMenuRequested(const QPoint & pos);
 };