QTfrontend/chatwidget.cpp
branchhedgeroid
changeset 6023 a28be05b20bc
parent 5998 e8f44e9433f0
equal deleted inserted replaced
5943:5b2b86a37089 6023:a28be05b20bc
    28 #include <QTextStream>
    28 #include <QTextStream>
    29 #include <QMenu>
    29 #include <QMenu>
    30 #include <QCursor>
    30 #include <QCursor>
    31 #include <QScrollBar>
    31 #include <QScrollBar>
    32 #include <QItemSelectionModel>
    32 #include <QItemSelectionModel>
       
    33 #include <QStringList>
    33 
    34 
    34 #include "hwconsts.h"
    35 #include "hwconsts.h"
    35 #include "SDLs.h"
    36 #include "SDLs.h"
    36 #include "gameuiconfig.h"
    37 #include "gameuiconfig.h"
    37 #include "chatwidget.h"
    38 #include "chatwidget.h"
   108 .FriendJoin .nick { color: #d0f0d0; }\
   109 .FriendJoin .nick { color: #d0f0d0; }\
   109 .UserAction { color: #ff80ff; }\
   110 .UserAction { color: #ff80ff; }\
   110 .UserAction .nick { color: #ffa0ff; }\
   111 .UserAction .nick { color: #ffa0ff; }\
   111 .FriendAction { color: #ff00ff; }\
   112 .FriendAction { color: #ff00ff; }\
   112 .FriendAction .nick { color: #ff30ff; }\
   113 .FriendAction .nick { color: #ff30ff; }\
       
   114 .Error { color: #ff0000 }\
       
   115 .Warning { color: #ff8000 }\
       
   116 .Notice { color: #fefefe }\
   113 ";
   117 ";
   114 
   118 
   115 HWChatWidget::HWChatWidget(QWidget* parent, QSettings * gameSettings, SDLInteraction * sdli, bool notify) :
   119 HWChatWidget::HWChatWidget(QWidget* parent, QSettings * gameSettings, SDLInteraction * sdli, bool notify) :
   116   QWidget(parent),
   120   QWidget(parent),
   117   mainLayout(this)
   121   mainLayout(this)
   201         // decode nick
   205         // decode nick
   202         const QString& nick = QString::fromUtf8(QByteArray::fromBase64(link.encodedQuery()));
   206         const QString& nick = QString::fromUtf8(QByteArray::fromBase64(link.encodedQuery()));
   203         QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly);
   207         QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly);
   204         if (items.size() < 1)
   208         if (items.size() < 1)
   205             return;
   209             return;
   206         QMenu * popup = new QMenu();
   210         QMenu * popup = new QMenu(this);
   207         // selecting an item will automatically scroll there, so let's save old position
   211         // selecting an item will automatically scroll there, so let's save old position
   208         QScrollBar * scrollBar = chatNicks->verticalScrollBar();
   212         QScrollBar * scrollBar = chatNicks->verticalScrollBar();
   209         int oldScrollPos = scrollBar->sliderPosition();
   213         int oldScrollPos = scrollBar->sliderPosition();
   210         // select the nick which we want to see the actions for
   214         // select the nick which we want to see the actions for
   211         chatNicks->setCurrentItem(items[0], QItemSelectionModel::Clear);
   215         chatNicks->setCurrentItem(items[0], QItemSelectionModel::Clear);
   311     saveList(friendsList, nick.toLower() + "_friends.txt");
   315     saveList(friendsList, nick.toLower() + "_friends.txt");
   312 }
   316 }
   313 
   317 
   314 void HWChatWidget::returnPressed()
   318 void HWChatWidget::returnPressed()
   315 {
   319 {
   316     emit chatLine(chatEditLine->text());
   320     QStringList lines = chatEditLine->text().split('\n');
   317     chatEditLine->clear();
   321     chatEditLine->clear();
       
   322     foreach (const QString &line, lines)
       
   323         emit chatLine(line);
   318 }
   324 }
   319 
   325 
   320 
   326 
   321 void HWChatWidget::onChatString(const QString& str)
   327 void HWChatWidget::onChatString(const QString& str)
   322 {
   328 {
   334         if (ignoreList.contains(nick, Qt::CaseInsensitive))
   340         if (ignoreList.contains(nick, Qt::CaseInsensitive))
   335             return;
   341             return;
   336         // friends will get special treatment, of course
   342         // friends will get special treatment, of course
   337         isFriend = friendsList.contains(nick, Qt::CaseInsensitive);
   343         isFriend = friendsList.contains(nick, Qt::CaseInsensitive);
   338     }
   344     }
   339 
       
   340     if (chatStrings.size() > 250)
       
   341         chatStrings.removeFirst();
       
   342 
   345 
   343     QString formattedStr = Qt::escape(str.mid(1));
   346     QString formattedStr = Qt::escape(str.mid(1));
   344     // make hedgewars.org urls actual links
   347     // make hedgewars.org urls actual links
   345     formattedStr = formattedStr.replace(URLREGEXP, "<a href=\"http://\\3\">\\3</a>");
   348     formattedStr = formattedStr.replace(URLREGEXP, "<a href=\"http://\\3\">\\3</a>");
   346 
   349 
   362         default:
   365         default:
   363             if (isFriend)
   366             if (isFriend)
   364                 cssClass = "FriendChat";
   367                 cssClass = "FriendChat";
   365     }
   368     }
   366 
   369 
   367     formattedStr = QString("<span class=\"%2\">%1</span>").arg(formattedStr).arg(cssClass);
   370     addLine(cssClass,formattedStr);
   368 
   371 }
   369     chatStrings.append(formattedStr);
   372 
       
   373 void HWChatWidget::addLine(const QString& cssClass, QString line)
       
   374 {
       
   375     if (chatStrings.size() > 250)
       
   376         chatStrings.removeFirst();
       
   377 
       
   378     line = QString("<span class=\"%2\">%1</span>").arg(line).arg(cssClass);
       
   379 
       
   380     chatStrings.append(line);
   370 
   381 
   371     chatText->setHtml(chatStrings.join("<br>"));
   382     chatText->setHtml(chatStrings.join("<br>"));
   372 
   383 
   373     chatText->moveCursor(QTextCursor::End);
   384     chatText->moveCursor(QTextCursor::End);
   374 }
   385 }