QTfrontend/ui/widget/chatwidget.cpp
branchwebgl
changeset 8833 c13ebed437cb
parent 8797 b7e0b7a18e3a
child 8887 539380a498e4
equal deleted inserted replaced
8450:404ddce27b23 8833:c13ebed437cb
    29 #include <QTime>
    29 #include <QTime>
    30 #include <QListView>
    30 #include <QListView>
    31 #include <QModelIndexList>
    31 #include <QModelIndexList>
    32 #include <QSortFilterProxyModel>
    32 #include <QSortFilterProxyModel>
    33 #include <QMenu>
    33 #include <QMenu>
       
    34 #include <QScrollBar>
    34 
    35 
    35 #include "DataManager.h"
    36 #include "DataManager.h"
    36 #include "hwconsts.h"
    37 #include "hwconsts.h"
    37 #include "gameuiconfig.h"
    38 #include "gameuiconfig.h"
    38 #include "playerslistmodel.h"
    39 #include "playerslistmodel.h"
    39 
    40 #include "HWApplication.h"
    40 #include "chatwidget.h"
    41 #include "chatwidget.h"
    41 
    42 
    42 
    43 
    43 QString * HWChatWidget::s_styleSheet = NULL;
    44 QString * HWChatWidget::s_styleSheet = NULL;
    44 QStringList * HWChatWidget::s_displayNone = NULL;
    45 QStringList * HWChatWidget::s_displayNone = NULL;
   154 }
   155 }
   155 
   156 
   156 void HWChatWidget::displayError(const QString & message)
   157 void HWChatWidget::displayError(const QString & message)
   157 {
   158 {
   158     addLine("msg_Error", " !!! " + message);
   159     addLine("msg_Error", " !!! " + message);
   159     // scroll to the end
       
   160     chatText->moveCursor(QTextCursor::End);
       
   161 }
   160 }
   162 
   161 
   163 
   162 
   164 void HWChatWidget::displayNotice(const QString & message)
   163 void HWChatWidget::displayNotice(const QString & message)
   165 {
   164 {
   166     addLine("msg_Notice", " *** " + message);
   165     addLine("msg_Notice", " *** " + message);
   167     // scroll to the end
       
   168     chatText->moveCursor(QTextCursor::End);
       
   169 }
   166 }
   170 
   167 
   171 
   168 
   172 void HWChatWidget::displayWarning(const QString & message)
   169 void HWChatWidget::displayWarning(const QString & message)
   173 {
   170 {
   174     addLine("msg_Warning", " *!* " + message);
   171     addLine("msg_Warning", " *!* " + message);
   175     // scroll to the end
       
   176     chatText->moveCursor(QTextCursor::End);
       
   177 }
   172 }
   178 
   173 
   179 
   174 
   180 HWChatWidget::HWChatWidget(QWidget* parent, bool notify) :
   175 HWChatWidget::HWChatWidget(QWidget* parent, bool notify) :
   181     QWidget(parent),
   176     QWidget(parent),
   449 void HWChatWidget::addLine(const QString & cssClass, QString line, bool isHighlight)
   444 void HWChatWidget::addLine(const QString & cssClass, QString line, bool isHighlight)
   450 {
   445 {
   451     if (s_displayNone->contains(cssClass))
   446     if (s_displayNone->contains(cssClass))
   452         return; // the css forbids us to display this line
   447         return; // the css forbids us to display this line
   453 
   448 
       
   449     beforeContentAdd();
       
   450 
   454     if (chatStrings.size() > 250)
   451     if (chatStrings.size() > 250)
   455         chatStrings.removeFirst();
   452         chatStrings.removeFirst();
   456 
   453 
   457     if (s_isTimeStamped)
   454     if (s_isTimeStamped)
   458     {
   455     {
   465 
   462 
   466     if (isHighlight)
   463     if (isHighlight)
   467     {
   464     {
   468         line = QString("<span class=\"highlight\">%1</span>").arg(line);
   465         line = QString("<span class=\"highlight\">%1</span>").arg(line);
   469         SDLInteraction::instance().playSoundFile(m_hilightSound);
   466         SDLInteraction::instance().playSoundFile(m_hilightSound);
       
   467         HWApplication::alert(this, 800);
   470     }
   468     }
   471 
   469 
   472     chatStrings.append(line);
   470     chatStrings.append(line);
   473 
   471 
   474     chatText->setHtml("<html><body>"+chatStrings.join("<br>")+"</body></html>");
   472     chatText->setHtml("<html><body>"+chatStrings.join("<br>")+"</body></html>");
   475 
   473 
   476     chatText->moveCursor(QTextCursor::End);
   474     afterContentAdd();
   477 }
   475 }
   478 
   476 
   479 void HWChatWidget::onServerMessage(const QString& str)
   477 void HWChatWidget::onServerMessage(const QString& str)
   480 {
   478 {
       
   479     beforeContentAdd();
       
   480 
   481     if (chatStrings.size() > 250)
   481     if (chatStrings.size() > 250)
   482         chatStrings.removeFirst();
   482         chatStrings.removeFirst();
   483 
   483 
   484     chatStrings.append("<hr>" + str + "<hr>");
   484     chatStrings.append("<hr>" + str + "<hr>");
   485 
   485 
   486     chatText->setHtml("<html><body>"+chatStrings.join("<br>")+"</body></html>");
   486     chatText->setHtml("<html><body>"+chatStrings.join("<br>")+"</body></html>");
   487 
   487 
   488     chatText->moveCursor(QTextCursor::End);
   488     afterContentAdd();
   489 }
   489 }
   490 
   490 
   491 
   491 
   492 void HWChatWidget::nickAdded(const QString & nick, bool notifyNick)
   492 void HWChatWidget::nickAdded(const QString & nick, bool notifyNick)
   493 {
   493 {
   899     foreach(QAction * action, chatNicks->actions())
   899     foreach(QAction * action, chatNicks->actions())
   900         m_nicksMenu->addAction(action);
   900         m_nicksMenu->addAction(action);
   901 
   901 
   902     m_nicksMenu->popup(chatNicks->mapToGlobal(pos));
   902     m_nicksMenu->popup(chatNicks->mapToGlobal(pos));
   903 }
   903 }
       
   904 
       
   905 void HWChatWidget::beforeContentAdd()
       
   906 {
       
   907     m_scrollBarPos = chatText->verticalScrollBar()->value();
       
   908     m_scrollToBottom = m_scrollBarPos == chatText->verticalScrollBar()->maximum();
       
   909 }
       
   910 
       
   911 void HWChatWidget::afterContentAdd()
       
   912 {
       
   913     if(m_scrollToBottom)
       
   914     {
       
   915         chatText->verticalScrollBar()->setValue(chatText->verticalScrollBar()->maximum());
       
   916         chatText->moveCursor(QTextCursor::End);
       
   917     } else
       
   918     {
       
   919         chatText->verticalScrollBar()->setValue(m_scrollBarPos);
       
   920     }
       
   921 }