QTfrontend/chatwidget.cpp
changeset 4884 b2006a9f0fbc
parent 4877 746ddd590dee
child 4892 b0610081ee95
equal deleted inserted replaced
4883:7cddc9201a1d 4884:b2006a9f0fbc
    24 #include <QTextDocument>
    24 #include <QTextDocument>
    25 #include <QDir>
    25 #include <QDir>
    26 #include <QSettings>
    26 #include <QSettings>
    27 #include <QFile>
    27 #include <QFile>
    28 #include <QTextStream>
    28 #include <QTextStream>
       
    29 #include <QScrollBar>
    29 
    30 
    30 #include "hwconsts.h"
    31 #include "hwconsts.h"
    31 #include "SDLs.h"
    32 #include "SDLs.h"
    32 #include "gameuiconfig.h"
    33 #include "gameuiconfig.h"
    33 #include "chatwidget.h"
    34 #include "chatwidget.h"
    34 
    35 
    35 ListWidgetNickItem::ListWidgetNickItem(const QString& nick) : QListWidgetItem(nick) {}
    36 ListWidgetNickItem::ListWidgetNickItem(const QString& nick, bool isFriend, bool isIgnored) : QListWidgetItem(nick)
       
    37 {
       
    38     this->aFriend = isFriend;
       
    39     this->isIgnored = isIgnored;
       
    40 }
       
    41 
       
    42 void ListWidgetNickItem::setFriend(bool isFriend)
       
    43 {
       
    44     this->aFriend = isFriend;
       
    45 }
       
    46 
       
    47 void ListWidgetNickItem::setIgnored(bool isIgnored)
       
    48 {
       
    49     this->isIgnored = isIgnored;
       
    50 }
       
    51 
       
    52 bool ListWidgetNickItem::isFriend()
       
    53 {
       
    54     return aFriend;
       
    55 }
       
    56 
       
    57 bool ListWidgetNickItem::ignored()
       
    58 {
       
    59     return isIgnored;
       
    60 }
    36 
    61 
    37 bool ListWidgetNickItem::operator< (const QListWidgetItem & other) const
    62 bool ListWidgetNickItem::operator< (const QListWidgetItem & other) const
    38 {
    63 {
    39     // case in-sensitive comparison of the associated strings
    64     // case in-sensitive comparison of the associated strings
    40     // chars that are no letters are sorted at the end of the list
    65     // chars that are no letters are sorted at the end of the list
       
    66 
       
    67     ListWidgetNickItem otherNick = const_cast<ListWidgetNickItem &>(dynamic_cast<const ListWidgetNickItem &>(other));
       
    68 
       
    69     // ignored always down
       
    70     if (isIgnored != otherNick.ignored())
       
    71         return !isIgnored;
       
    72 
       
    73     // friends always up
       
    74     if (aFriend != otherNick.isFriend())
       
    75         return aFriend;
    41 
    76 
    42     QString txt1 = text().toLower();
    77     QString txt1 = text().toLower();
    43     QString txt2 = other.text().toLower();
    78     QString txt2 = other.text().toLower();
    44 
    79 
    45     bool firstIsShorter = (txt1.size() < txt2.size());
    80     bool firstIsShorter = (txt1.size() < txt2.size());
   168     for(int i = 0; i < list.size(); i++)
   203     for(int i = 0; i < list.size(); i++)
   169         stream << list[i] << endl;
   204         stream << list[i] << endl;
   170     txt.close();
   205     txt.close();
   171 }
   206 }
   172 
   207 
   173 void HWChatWidget::updateIcon(QListWidgetItem *item)
   208 void HWChatWidget::updateNickItem(QListWidgetItem *nickItem)
   174 {
   209 {
   175     QString nick = item->text();
   210     QString nick = nickItem->text();
   176 
   211     ListWidgetNickItem * item = dynamic_cast<ListWidgetNickItem*>(nickItem);
   177     if(ignoreList.contains(nick, Qt::CaseInsensitive))
   212 
       
   213     item->setFriend(friendsList.contains(nick, Qt::CaseInsensitive));
       
   214     item->setIgnored(ignoreList.contains(nick, Qt::CaseInsensitive));
       
   215 
       
   216     if(item->ignored())
   178     {
   217     {
   179         item->setIcon(QIcon(showReady ? (item->data(Qt::UserRole).toBool() ? ":/res/chat_ignore_on.png" : ":/res/chat_ignore_off.png") : ":/res/chat_ignore.png"));
   218         item->setIcon(QIcon(showReady ? (item->data(Qt::UserRole).toBool() ? ":/res/chat_ignore_on.png" : ":/res/chat_ignore_off.png") : ":/res/chat_ignore.png"));
   180         item->setForeground(Qt::gray);
   219         item->setForeground(Qt::gray);
   181     }
   220     }
   182     else if(friendsList.contains(nick, Qt::CaseInsensitive))
   221     else if(item->isFriend())
   183     {
   222     {
   184         item->setIcon(QIcon(showReady ? (item->data(Qt::UserRole).toBool() ? ":/res/chat_friend_on.png" : ":/res/chat_friend_off.png") : ":/res/chat_friend.png"));
   223         item->setIcon(QIcon(showReady ? (item->data(Qt::UserRole).toBool() ? ":/res/chat_friend_on.png" : ":/res/chat_friend_off.png") : ":/res/chat_friend.png"));
   185         item->setForeground(Qt::green);
   224         item->setForeground(Qt::green);
   186     }
   225     }
   187     else
   226     else
   189         item->setIcon(QIcon(showReady ? (item->data(Qt::UserRole).toBool() ? ":/res/chat_default_on.png" : ":/res/chat_default_off.png") : ":/res/chat_default.png"));
   228         item->setIcon(QIcon(showReady ? (item->data(Qt::UserRole).toBool() ? ":/res/chat_default_on.png" : ":/res/chat_default_off.png") : ":/res/chat_default.png"));
   190         item->setForeground(QBrush(QColor(0xff, 0xcc, 0x00)));
   229         item->setForeground(QBrush(QColor(0xff, 0xcc, 0x00)));
   191     }
   230     }
   192 }
   231 }
   193 
   232 
   194 void HWChatWidget::updateIcons()
   233 void HWChatWidget::updateNickItems()
   195 {
   234 {
   196     for(int i = 0; i < chatNicks->count(); i++)
   235     for(int i = 0; i < chatNicks->count(); i++)
   197         updateIcon(chatNicks->item(i));
   236         updateNickItem(chatNicks->item(i));
       
   237 
       
   238     chatNicks->sortItems();
   198 }
   239 }
   199 
   240 
   200 void HWChatWidget::loadLists(const QString & nick)
   241 void HWChatWidget::loadLists(const QString & nick)
   201 {
   242 {
   202     loadList(ignoreList, nick.toLower() + "_ignore.txt");
   243     loadList(ignoreList, nick.toLower() + "_ignore.txt");
   203     loadList(friendsList, nick.toLower() + "_friends.txt");
   244     loadList(friendsList, nick.toLower() + "_friends.txt");
   204     updateIcons();
   245     updateNickItems();
   205 }
   246 }
   206 
   247 
   207 void HWChatWidget::saveLists(const QString & nick)
   248 void HWChatWidget::saveLists(const QString & nick)
   208 {
   249 {
   209     saveList(ignoreList, nick.toLower() + "_ignore.txt");
   250     saveList(ignoreList, nick.toLower() + "_ignore.txt");
   264     chatText->moveCursor(QTextCursor::End);
   305     chatText->moveCursor(QTextCursor::End);
   265 }
   306 }
   266 
   307 
   267 void HWChatWidget::nickAdded(const QString& nick, bool notifyNick)
   308 void HWChatWidget::nickAdded(const QString& nick, bool notifyNick)
   268 {
   309 {
   269     QListWidgetItem * item = new ListWidgetNickItem(nick);
   310     QListWidgetItem * item = new ListWidgetNickItem(nick, friendsList.contains(nick, Qt::CaseInsensitive), ignoreList.contains(nick, Qt::CaseInsensitive));
   270     updateIcon(item);
   311     updateNickItem(item);
   271     chatNicks->addItem(item);
   312     chatNicks->addItem(item);
   272 
   313 
   273     if(notifyNick && notify && gameSettings->value("frontend/sound", true).toBool()) {
   314     if(notifyNick && notify && gameSettings->value("frontend/sound", true).toBool()) {
   274        Mix_PlayChannel(-1, sound[rand()%4], 0);
   315        Mix_PlayChannel(-1, sound[rand()%4], 0);
   275     }
   316     }
   330         ignoreList.removeAll(curritem->text().toLower());
   371         ignoreList.removeAll(curritem->text().toLower());
   331         onChatString(HWChatWidget::tr("%1 *** %2 has been removed from your ignore list").arg('\x03').arg(curritem->text()));
   372         onChatString(HWChatWidget::tr("%1 *** %2 has been removed from your ignore list").arg('\x03').arg(curritem->text()));
   332     }
   373     }
   333     else // not on list - add
   374     else // not on list - add
   334     {
   375     {
       
   376         // don't consider ignored people friends
       
   377         if(friendsList.contains(curritem->text(), Qt::CaseInsensitive))
       
   378             emit onFriend();
       
   379 
       
   380         // scroll down on first ignore added so that people see where that nick went to
       
   381         if (ignoreList.isEmpty())
       
   382             emit chatNicks->verticalScrollBar()->setValue(chatNicks->verticalScrollBar()->maximum());
       
   383 
   335         ignoreList << curritem->text().toLower();
   384         ignoreList << curritem->text().toLower();
   336         onChatString(HWChatWidget::tr("%1 *** %2 has been added to your ignore list").arg('\x03').arg(curritem->text()));
   385         onChatString(HWChatWidget::tr("%1 *** %2 has been added to your ignore list").arg('\x03').arg(curritem->text()));
   337     }
   386     }
   338     updateIcon(curritem); // update icon
   387     updateNickItem(curritem); // update icon/sort order/etc
       
   388     chatNicks->sortItems();
   339     chatNickSelected(0); // update context menu
   389     chatNickSelected(0); // update context menu
   340 }
   390 }
   341 
   391 
   342 void HWChatWidget::onFriend()
   392 void HWChatWidget::onFriend()
   343 {
   393 {
   350         friendsList.removeAll(curritem->text().toLower());
   400         friendsList.removeAll(curritem->text().toLower());
   351         onChatString(HWChatWidget::tr("%1 *** %2 has been removed from your friends list").arg('\x03').arg(curritem->text()));
   401         onChatString(HWChatWidget::tr("%1 *** %2 has been removed from your friends list").arg('\x03').arg(curritem->text()));
   352     }
   402     }
   353     else // not on list - add
   403     else // not on list - add
   354     {
   404     {
       
   405         // don't ignore the new friend
       
   406         if(ignoreList.contains(curritem->text(), Qt::CaseInsensitive))
       
   407             emit onIgnore();
       
   408 
       
   409         // scroll up on first friend added so that people see where that nick went to
       
   410         if (friendsList.isEmpty())
       
   411             emit chatNicks->verticalScrollBar()->setValue(chatNicks->verticalScrollBar()->minimum());
       
   412 
   355         friendsList << curritem->text().toLower();
   413         friendsList << curritem->text().toLower();
   356         onChatString(HWChatWidget::tr("%1 *** %2 has been added to your friends list").arg('\x03').arg(curritem->text()));
   414         onChatString(HWChatWidget::tr("%1 *** %2 has been added to your friends list").arg('\x03').arg(curritem->text()));
   357     }
   415     }
   358     updateIcon(curritem); // update icon
   416     updateNickItem(curritem); // update icon/sort order/etc
       
   417     chatNicks->sortItems();
   359     chatNickSelected(0); // update context menu
   418     chatNickSelected(0); // update context menu
   360 }
   419 }
   361 
   420 
   362 void HWChatWidget::chatNickDoubleClicked(QListWidgetItem * item)
   421 void HWChatWidget::chatNickDoubleClicked(QListWidgetItem * item)
   363 {
   422 {
   409         qWarning("Bug: cannot find user in chat");
   468         qWarning("Bug: cannot find user in chat");
   410         return;
   469         return;
   411     }
   470     }
   412 
   471 
   413     items[0]->setData(Qt::UserRole, isReady); // bulb status
   472     items[0]->setData(Qt::UserRole, isReady); // bulb status
   414     updateIcon(items[0]);
   473     updateNickItem(items[0]);
   415 
   474 
   416     // ensure we're still showing the status bulbs
   475     // ensure we're still showing the status bulbs
   417     showReady = true;
   476     showReady = true;
   418 }
   477 }
   419 
   478