From cdda3f15a097a7260615b2c3e46f43b719f21b99 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Sat, 31 Mar 2012 15:06:47 +0200 Subject: [PATCH] added option to ignore unregistered users in chat; switched UserList data storage to QMap to speed up lookups; issue #28 fixed --- cockatrice/cockatrice.qrc | 1 + cockatrice/resources/icon_settings.svg | 93 ++++++++++++++++++++++++++ cockatrice/src/playerlistwidget.cpp | 4 +- cockatrice/src/settingscache.cpp | 9 +++ cockatrice/src/settingscache.h | 4 ++ cockatrice/src/tab_room.cpp | 44 +++++++++++- cockatrice/src/tab_room.h | 5 +- cockatrice/src/tab_userlists.cpp | 2 +- cockatrice/src/userlist.cpp | 67 +++++++++---------- cockatrice/src/userlist.h | 5 +- 10 files changed, 191 insertions(+), 43 deletions(-) create mode 100644 cockatrice/resources/icon_settings.svg diff --git a/cockatrice/cockatrice.qrc b/cockatrice/cockatrice.qrc index 2ae36655a..a3c850c1b 100644 --- a/cockatrice/cockatrice.qrc +++ b/cockatrice/cockatrice.qrc @@ -21,6 +21,7 @@ resources/phases/icon_phase_main2.svg resources/phases/icon_phase_cleanup.svg resources/phases/icon_phase_nextturn.svg + resources/icon_settings.svg resources/hand.svg resources/pencil.svg resources/icon_search.svg diff --git a/cockatrice/resources/icon_settings.svg b/cockatrice/resources/icon_settings.svg new file mode 100644 index 000000000..f29af4d0f --- /dev/null +++ b/cockatrice/resources/icon_settings.svg @@ -0,0 +1,93 @@ + + + + + + + Ruota dentata grigia + + + + unsorted + + + + + Open Clip Art Library + + + + + Architetto Francesco Rollandin + + + + + Architetto Francesco Rollandin + + + + image/svg+xml + + + en + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/cockatrice/src/playerlistwidget.cpp b/cockatrice/src/playerlistwidget.cpp index 2684e3903..689dc876d 100644 --- a/cockatrice/src/playerlistwidget.cpp +++ b/cockatrice/src/playerlistwidget.cpp @@ -183,11 +183,11 @@ void PlayerListWidget::showContextMenu(const QPoint &pos, const QModelIndex &ind menu->addAction(aChat); if ((userLevel & ServerInfo_User::IsRegistered) && (tabSupervisor->getUserLevel() & ServerInfo_User::IsRegistered)) { menu->addSeparator(); - if (tabSupervisor->getUserListsTab()->getBuddyList()->userInList(userName)) + if (tabSupervisor->getUserListsTab()->getBuddyList()->getUsers().contains(userName)) menu->addAction(aRemoveFromBuddyList); else menu->addAction(aAddToBuddyList); - if (tabSupervisor->getUserListsTab()->getIgnoreList()->userInList(userName)) + if (tabSupervisor->getUserListsTab()->getIgnoreList()->getUsers().contains(userName)) menu->addAction(aRemoveFromIgnoreList); else menu->addAction(aAddToIgnoreList); diff --git a/cockatrice/src/settingscache.cpp b/cockatrice/src/settingscache.cpp index 927be9450..9597acd99 100644 --- a/cockatrice/src/settingscache.cpp +++ b/cockatrice/src/settingscache.cpp @@ -36,6 +36,8 @@ SettingsCache::SettingsCache() soundPath = settings->value("sound/path").toString(); priceTagFeature = settings->value("deckeditor/pricetags", false).toBool(); + + ignoreUnregisteredUsers = settings->value("chat/ignore_unregistered", false).toBool(); } void SettingsCache::setCustomTranslationFile(const QString &_customTranslationFile) @@ -202,3 +204,10 @@ void SettingsCache::setPriceTagFeature(int _priceTagFeature) priceTagFeature = _priceTagFeature; settings->setValue("deckeditor/pricetags", priceTagFeature); } + +void SettingsCache::setIgnoreUnregisteredUsers(bool _ignoreUnregisteredUsers) +{ + ignoreUnregisteredUsers = _ignoreUnregisteredUsers; + settings->setValue("chat/ignore_unregistered", ignoreUnregisteredUsers); + emit ignoreUnregisteredUsersChanged(); +} diff --git a/cockatrice/src/settingscache.h b/cockatrice/src/settingscache.h index 2bcbfd661..b467a6787 100644 --- a/cockatrice/src/settingscache.h +++ b/cockatrice/src/settingscache.h @@ -23,6 +23,7 @@ signals: void invertVerticalCoordinateChanged(); void minPlayersForMultiColumnLayoutChanged(); void soundPathChanged(); + void ignoreUnregisteredUsersChanged(); private: QSettings *settings; @@ -42,6 +43,7 @@ private: bool soundEnabled; QString soundPath; bool priceTagFeature; + bool ignoreUnregisteredUsers; public: SettingsCache(); QString getCustomTranslationFile() const { return customTranslationFile; } @@ -69,6 +71,7 @@ public: bool getSoundEnabled() const { return soundEnabled; } QString getSoundPath() const { return soundPath; } bool getPriceTagFeature() const { return priceTagFeature; } + bool getIgnoreUnregisteredUsers() const { return ignoreUnregisteredUsers; } public slots: void setCustomTranslationFile(const QString &_customTranslationFile); void setLang(const QString &_lang); @@ -95,6 +98,7 @@ public slots: void setSoundEnabled(int _soundEnabled); void setSoundPath(const QString &_soundPath); void setPriceTagFeature(int _priceTagFeature); + void setIgnoreUnregisteredUsers(bool _ignoreUnregisteredUsers); }; extern SettingsCache *settingsCache; diff --git a/cockatrice/src/tab_room.cpp b/cockatrice/src/tab_room.cpp index 6cc78603e..513634e03 100644 --- a/cockatrice/src/tab_room.cpp +++ b/cockatrice/src/tab_room.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "tab_supervisor.h" #include "tab_room.h" @@ -15,6 +16,7 @@ #include "abstractclient.h" #include "chatview.h" #include "gameselector.h" +#include "settingscache.h" #include "get_pb_extension.h" #include "pb/room_commands.pb.h" @@ -46,9 +48,20 @@ TabRoom::TabRoom(TabSupervisor *_tabSupervisor, AbstractClient *_client, ServerI sayLabel->setBuddy(sayEdit); connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(sendMessage())); + QMenu *chatSettingsMenu = new QMenu(this); + aIgnoreUnregisteredUsers = chatSettingsMenu->addAction(QString()); + aIgnoreUnregisteredUsers->setCheckable(true); + connect(aIgnoreUnregisteredUsers, SIGNAL(triggered()), this, SLOT(actIgnoreUnregisteredUsers())); + connect(settingsCache, SIGNAL(ignoreUnregisteredUsersChanged()), this, SLOT(ignoreUnregisteredUsersChanged())); + QToolButton *chatSettingsButton = new QToolButton; + chatSettingsButton->setIcon(QIcon(":/resources/icon_settings.svg")); + chatSettingsButton->setMenu(chatSettingsMenu); + chatSettingsButton->setPopupMode(QToolButton::InstantPopup); + QHBoxLayout *sayHbox = new QHBoxLayout; sayHbox->addWidget(sayLabel); sayHbox->addWidget(sayEdit); + sayHbox->addWidget(chatSettingsButton); QVBoxLayout *chatVbox = new QVBoxLayout; chatVbox->addWidget(chatView); @@ -96,6 +109,7 @@ void TabRoom::retranslateUi() chatGroupBox->setTitle(tr("Chat")); tabMenu->setTitle(tr("&Room")); aLeaveRoom->setText(tr("&Leave room")); + aIgnoreUnregisteredUsers->setText(tr("&Ignore unregistered users in chat")); } void TabRoom::closeRequest() @@ -137,6 +151,16 @@ void TabRoom::actLeaveRoom() deleteLater(); } +void TabRoom::actIgnoreUnregisteredUsers() +{ + settingsCache->setIgnoreUnregisteredUsers(!settingsCache->getIgnoreUnregisteredUsers()); +} + +void TabRoom::ignoreUnregisteredUsersChanged() +{ + aIgnoreUnregisteredUsers->setChecked(settingsCache->getIgnoreUnregisteredUsers()); +} + void TabRoom::processRoomEvent(const RoomEvent &event) { switch (static_cast(getPbExtension(event))) { @@ -168,8 +192,24 @@ void TabRoom::processLeaveRoomEvent(const Event_LeaveRoom &event) void TabRoom::processRoomSayEvent(const Event_RoomSay &event) { - if (!tabSupervisor->getUserListsTab()->getIgnoreList()->userInList(QString::fromStdString(event.name()))) - chatView->appendMessage(QString::fromStdString(event.name()), QString::fromStdString(event.message())); + QString senderName = QString::fromStdString(event.name()); + if (tabSupervisor->getUserListsTab()->getIgnoreList()->getUsers().contains(senderName)) + return; + UserListTWI *twi = userList->getUsers().value(senderName); + QColor senderColor; + if (twi && (senderName != QString::fromStdString(ownUser->name()))) { + ServerInfo_User::UserLevelFlags userLevel = static_cast(twi->getUserLevel()); + if (userLevel & ServerInfo_User::IsModerator) + senderColor = Qt::darkMagenta; + else if (userLevel & ServerInfo_User::IsRegistered) + senderColor = Qt::darkGreen; + else { + if (settingsCache->getIgnoreUnregisteredUsers()) + return; + senderColor = QColor(0, 0, 254); + } + } + chatView->appendMessage(QString::fromStdString(event.name()), QString::fromStdString(event.message()), senderColor); emit userEvent(false); } diff --git a/cockatrice/src/tab_room.h b/cockatrice/src/tab_room.h index 16ed082f4..b54afc95e 100644 --- a/cockatrice/src/tab_room.h +++ b/cockatrice/src/tab_room.h @@ -42,14 +42,17 @@ private: QGroupBox *chatGroupBox; QAction *aLeaveRoom; + QAction *aIgnoreUnregisteredUsers; QString sanitizeHtml(QString dirty) const; signals: void roomClosing(TabRoom *tab); void openMessageDialog(const QString &userName, bool focus); private slots: void sendMessage(); - void actLeaveRoom(); void sayFinished(const Response &response); + void actLeaveRoom(); + void actIgnoreUnregisteredUsers(); + void ignoreUnregisteredUsersChanged(); void processListGamesEvent(const Event_ListGames &event); void processJoinRoomEvent(const Event_JoinRoom &event); diff --git a/cockatrice/src/tab_userlists.cpp b/cockatrice/src/tab_userlists.cpp index ab489b5e2..156b61012 100644 --- a/cockatrice/src/tab_userlists.cpp +++ b/cockatrice/src/tab_userlists.cpp @@ -123,7 +123,7 @@ void TabUserLists::ignoreListReceived(const QList &_ignoreList) void TabUserLists::processAddToListEvent(const Event_AddToList &event) { const ServerInfo_User &info = event.user_info(); - bool online = allUsersList->userInList(QString::fromStdString(info.name())); + bool online = allUsersList->getUsers().contains(QString::fromStdString(info.name())); QString list = QString::fromStdString(event.list_name()); UserList *userList = 0; if (list == "buddy") diff --git a/cockatrice/src/userlist.cpp b/cockatrice/src/userlist.cpp index 706fbedcb..5ae1da20b 100644 --- a/cockatrice/src/userlist.cpp +++ b/cockatrice/src/userlist.cpp @@ -174,6 +174,16 @@ UserListTWI::UserListTWI() { } +QString UserListTWI::getUserName() const +{ + return data(2, Qt::UserRole).toString(); +} + +int UserListTWI::getUserLevel() const +{ + return data(0, Qt::UserRole).toInt(); +} + bool UserListTWI::operator<(const QTreeWidgetItem &other) const { // Sort by online/offline @@ -224,16 +234,11 @@ void UserList::retranslateUi() void UserList::processUserInfo(const ServerInfo_User &user, bool online) { - QTreeWidgetItem *item = 0; - for (int i = 0; i < userTree->topLevelItemCount(); ++i) { - QTreeWidgetItem *temp = userTree->topLevelItem(i); - if (temp->data(2, Qt::UserRole) == QString::fromStdString(user.name())) { - item = temp; - break; - } - } + const QString userName = QString::fromStdString(user.name()); + UserListTWI *item = users.value(userName); if (!item) { item = new UserListTWI; + users.insert(userName, item); userTree->addTopLevelItem(item); if (online) ++onlineCount; @@ -254,15 +259,16 @@ void UserList::processUserInfo(const ServerInfo_User &user, bool online) bool UserList::deleteUser(const QString &userName) { - for (int i = 0; i < userTree->topLevelItemCount(); ++i) - if (userTree->topLevelItem(i)->data(2, Qt::UserRole) == userName) { - QTreeWidgetItem *item = userTree->takeTopLevelItem(i); - if (item->data(0, Qt::UserRole + 1).toBool()) - --onlineCount; - delete item; - updateCount(); - return true; - } + UserListTWI *twi = users.value(userName); + if (twi) { + users.remove(userName); + userTree->takeTopLevelItem(userTree->indexOfTopLevelItem(twi)); + if (twi->data(0, Qt::UserRole + 1).toBool()) + --onlineCount; + delete twi; + updateCount(); + return true; + } return false; } @@ -283,13 +289,9 @@ void UserList::setUserOnline(QTreeWidgetItem *item, bool online) void UserList::setUserOnline(const QString &userName, bool online) { - for (int i = 0; i < userTree->topLevelItemCount(); ++i) { - QTreeWidgetItem *item = userTree->topLevelItem(i); - if (item->data(2, Qt::UserRole) == userName) { - setUserOnline(item, online); - break; - } - } + UserListTWI *twi = users.value(userName); + if (twi) + setUserOnline(twi, online); } void UserList::updateCount() @@ -361,8 +363,9 @@ void UserList::banUser_dialogFinished() void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index) { - const QString &userName = index.sibling(index.row(), 2).data(Qt::UserRole).toString(); - ServerInfo_User::UserLevelFlags userLevel = static_cast(index.sibling(index.row(), 0).data(Qt::UserRole).toInt()); + UserListTWI *twi = static_cast(userTree->topLevelItem(index.row())); + const QString &userName = twi->getUserName(); + ServerInfo_User::UserLevelFlags userLevel = static_cast(twi->getUserLevel()); QAction *aUserName = new QAction(userName, this); aUserName->setEnabled(false); @@ -383,11 +386,11 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index) menu->addAction(aChat); if ((userLevel & ServerInfo_User::IsRegistered) && (tabSupervisor->getUserLevel() & ServerInfo_User::IsRegistered)) { menu->addSeparator(); - if (tabSupervisor->getUserListsTab()->getBuddyList()->userInList(userName)) + if (tabSupervisor->getUserListsTab()->getBuddyList()->getUsers().contains(userName)) menu->addAction(aRemoveFromBuddyList); else menu->addAction(aAddToBuddyList); - if (tabSupervisor->getUserListsTab()->getIgnoreList()->userInList(userName)) + if (tabSupervisor->getUserListsTab()->getIgnoreList()->getUsers().contains(userName)) menu->addAction(aRemoveFromIgnoreList); else menu->addAction(aAddToIgnoreList); @@ -465,14 +468,6 @@ void UserList::showContextMenu(const QPoint &pos, const QModelIndex &index) delete aBan; } -bool UserList::userInList(const QString &userName) const -{ - for (int i = 0; i < userTree->topLevelItemCount(); ++i) - if (userTree->topLevelItem(i)->data(2, Qt::UserRole) == userName) - return true; - return false; -} - void UserList::sortItems() { userTree->sortItems(1, Qt::AscendingOrder); diff --git a/cockatrice/src/userlist.h b/cockatrice/src/userlist.h index f81b9a398..19de8b0ae 100644 --- a/cockatrice/src/userlist.h +++ b/cockatrice/src/userlist.h @@ -48,6 +48,8 @@ public: class UserListTWI : public QTreeWidgetItem { public: UserListTWI(); + QString getUserName() const; + int getUserLevel() const; bool operator<(const QTreeWidgetItem &other) const; }; @@ -56,6 +58,7 @@ class UserList : public QGroupBox { public: enum UserListType { AllUsersList, RoomList, BuddyList, IgnoreList }; private: + QMap users; TabSupervisor *tabSupervisor; AbstractClient *client; UserListType type; @@ -82,7 +85,7 @@ public: void processUserInfo(const ServerInfo_User &user, bool online); bool deleteUser(const QString &userName); void setUserOnline(const QString &userName, bool online); - bool userInList(const QString &userName) const; + const QMap &getUsers() const { return users; } void showContextMenu(const QPoint &pos, const QModelIndex &index); void sortItems(); };