QTfrontend/ui/widget/chatwidget.cpp
changeset 6180 0992fc5a4ad9
parent 6178 affa860f2983
child 6181 c739b503ae31
equal deleted inserted replaced
6179:b529f88d37d0 6180:0992fc5a4ad9
    28 #include <QMenu>
    28 #include <QMenu>
    29 #include <QCursor>
    29 #include <QCursor>
    30 #include <QScrollBar>
    30 #include <QScrollBar>
    31 #include <QItemSelectionModel>
    31 #include <QItemSelectionModel>
    32 #include <QStringList>
    32 #include <QStringList>
       
    33 #include <QRegExp>
       
    34 
    33 
    35 
    34 #include "HWDataManager.h"
    36 #include "HWDataManager.h"
    35 #include "hwconsts.h"
    37 #include "hwconsts.h"
    36 #include "gameuiconfig.h"
    38 #include "gameuiconfig.h"
    37 
    39 
    94     }
    96     }
    95 
    97 
    96     return firstIsShorter;
    98     return firstIsShorter;
    97 }
    99 }
    98 
   100 
    99 
   101 QString * HWChatWidget::s_styleSheet = NULL;
   100 HWChatWidget::HWChatWidget(QWidget* parent, QSettings * gameSettings, bool notify) :
   102 QStringList * HWChatWidget::s_displayNone = NULL;
   101   QWidget(parent),
   103 
   102   mainLayout(this)
   104 QString & HWChatWidget::styleSheet()
   103 {
   105 {
   104     this->gameSettings = gameSettings;
   106     if (s_styleSheet != NULL)
   105     this->notify = notify;
   107         return *s_styleSheet;
   106     if(notify && gameSettings->value("frontend/sound", true).toBool())
   108 
   107         helloSound = HWDataManager::instance().findFileForRead(
   109     // initialize
   108                         "Sounds/voices/Classic/Hello.ogg");
   110     s_styleSheet = new QString();
   109 
   111 
   110     mainLayout.setSpacing(1);
   112     // getting a reference
   111     mainLayout.setMargin(1);
   113     QString & style = *s_styleSheet;
   112     mainLayout.setSizeConstraint(QLayout::SetMinimumSize);
       
   113     mainLayout.setColumnStretch(0, 76);
       
   114     mainLayout.setColumnStretch(1, 24);
       
   115 
       
   116     chatEditLine = new SmartLineEdit(this);
       
   117     chatEditLine->addCommands(QStringList("/me"));
       
   118     chatEditLine->setMaxLength(300);
       
   119     connect(chatEditLine, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
       
   120 
       
   121     mainLayout.addWidget(chatEditLine, 2, 0);
       
   122 
       
   123     chatText = new QTextBrowser(this);
       
   124 
       
   125     QString style;
       
   126 
   114 
   127     // load external stylesheet if there is any
   115     // load external stylesheet if there is any
   128     QFile extFile(HWDataManager::instance().findFileForRead("css/chat.css"));
   116     QFile extFile(HWDataManager::instance().findFileForRead("css/chat.css"));
   129 
   117 
   130     QFile resFile(":/res/css/chat.css");
   118     QFile resFile(":/res/css/chat.css");
   140             if(!line.isEmpty())
   128             if(!line.isEmpty())
   141                 style.append(line);
   129                 style.append(line);
   142         }
   130         }
   143     }
   131     }
   144 
   132 
   145     chatText->document()->setDefaultStyleSheet(style);
   133     // prepare for MAGIC :D
       
   134 
       
   135     // matches (multi-)whitespaces (for replacement with simple space)
       
   136     QRegExp ws("\\s+");
       
   137 
       
   138     // matches comments (for removal)
       
   139     QRegExp rem("/\\*([^*]|\\*(?!/))*\\*/");
       
   140 
       
   141     // strip comments and multi-whitespaces to compress the style-sheet a bit
       
   142     style = style.remove(rem).replace(ws," ");
       
   143 
       
   144 
       
   145     // now let's see what messages the user does not want to be displayed
       
   146     // by checking for display:none; (since QTextBrowser does not support it)
       
   147 
       
   148     // MOAR MAGIC :DDD
       
   149 
       
   150     // matches definitions lacking display:none; (for removal)
       
   151     QRegExp displayed(
       
   152         "([^{}]*\\{)(?!([^}]*;)* ?display ?: ?none ?(;[^}]*)?\\})[^}]*\\}");
       
   153 
       
   154     // matches all {...} and , (used as seperator for splitting into names)
       
   155     QRegExp split(" *(\\{[^}]*\\}|,) *");
       
   156 
       
   157     // matches class names that are referenced without hierachy
       
   158     QRegExp nohierarchy("^.[^ .]+$");
       
   159 
       
   160     QStringList victims = QString(style).
       
   161                                 remove(displayed). // remove visible stuff
       
   162                                 split(split). // get a list of the names
       
   163                                 filter(nohierarchy). // only direct class names
       
   164                                 replaceInStrings(QRegExp("^."),""); // crop .
       
   165 
       
   166     victims.removeDuplicates();
       
   167 
       
   168     s_displayNone = new QStringList(victims);
       
   169 
       
   170 
       
   171     return style;
       
   172 }
       
   173 
       
   174 void HWChatWidget::displayError(const QString & message)
       
   175 {
       
   176     addLine("msg_Error", message);
       
   177     // scroll to the end
       
   178     chatText->moveCursor(QTextCursor::End);
       
   179 }
       
   180 
       
   181 
       
   182 void HWChatWidget::displayNotice(const QString & message)
       
   183 {
       
   184     addLine("msg_Notice", message);
       
   185 }
       
   186 
       
   187 
       
   188 void HWChatWidget::displayWarning(const QString & message)
       
   189 {
       
   190     addLine("msg_Warning", message);
       
   191 }
       
   192 
       
   193 
       
   194 HWChatWidget::HWChatWidget(QWidget* parent, QSettings * gameSettings, bool notify) :
       
   195   QWidget(parent),
       
   196   mainLayout(this)
       
   197 {
       
   198     this->gameSettings = gameSettings;
       
   199     this->notify = notify;
       
   200     if(notify && gameSettings->value("frontend/sound", true).toBool())
       
   201         helloSound = HWDataManager::instance().findFileForRead(
       
   202                         "Sounds/voices/Classic/Hello.ogg");
       
   203 
       
   204     mainLayout.setSpacing(1);
       
   205     mainLayout.setMargin(1);
       
   206     mainLayout.setSizeConstraint(QLayout::SetMinimumSize);
       
   207     mainLayout.setColumnStretch(0, 76);
       
   208     mainLayout.setColumnStretch(1, 24);
       
   209 
       
   210     chatEditLine = new SmartLineEdit(this);
       
   211     chatEditLine->addCommands(QStringList("/me"));
       
   212     chatEditLine->setMaxLength(300);
       
   213     connect(chatEditLine, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
       
   214 
       
   215     mainLayout.addWidget(chatEditLine, 2, 0);
       
   216 
       
   217     chatText = new QTextBrowser(this);
       
   218 
       
   219     chatText->document()->setDefaultStyleSheet(styleSheet());
   146 
   220 
   147     chatText->setMinimumHeight(20);
   221     chatText->setMinimumHeight(20);
   148     chatText->setMinimumWidth(10);
   222     chatText->setMinimumWidth(10);
   149     chatText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
   223     chatText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
   150     chatText->setOpenLinks(false);
   224     chatText->setOpenLinks(false);
   363     // "link" nick, but before that encode it in base64 to make sure it can't intefere with html/url syntax
   437     // "link" nick, but before that encode it in base64 to make sure it can't intefere with html/url syntax
   364     // the nick is put as querystring as putting it as host would convert it to it's lower case variant
   438     // the nick is put as querystring as putting it as host would convert it to it's lower case variant
   365     if(!nick.isEmpty())
   439     if(!nick.isEmpty())
   366         formattedStr.replace("|nick|",QString("<a href=\"hwnick://?%1\" class=\"nick\">%2</a>").arg(QString(nick.toUtf8().toBase64())).arg(nick));
   440         formattedStr.replace("|nick|",QString("<a href=\"hwnick://?%1\" class=\"nick\">%2</a>").arg(QString(nick.toUtf8().toBase64())).arg(nick));
   367 
   441 
   368     QString cssClass("UserChat");
   442     QString cssClass("msg_UserChat");
   369 
   443 
   370     // check first character for color code and set color properly
   444     // check first character for color code and set color properly
   371     switch (str[0].toAscii()) {
   445     switch (str[0].toAscii()) {
   372         case 3:
   446         case 3:
   373             cssClass = (isFriend ? "FriendJoin" : "UserJoin");
   447             cssClass = (isFriend ? "msg_FriendJoin" : "msg_UserJoin");
   374             break;
   448             break;
   375         case 2:
   449         case 2:
   376             cssClass = (isFriend ? "FriendAction" : "UserAction");
   450             cssClass = (isFriend ? "msg_FriendAction" : "msg_UserAction");
   377             break;
   451             break;
   378         default:
   452         default:
   379             if (isFriend)
   453             if (isFriend)
   380                 cssClass = "FriendChat";
   454                 cssClass = "msg_FriendChat";
   381     }
   455     }
   382 
   456 
   383     addLine(cssClass,formattedStr);
   457     addLine(cssClass,formattedStr);
   384 }
   458 }
   385 
   459 
   386 void HWChatWidget::addLine(const QString& cssClass, QString line)
   460 void HWChatWidget::addLine(const QString & cssClass, QString line)
   387 {
   461 {
       
   462     if (s_displayNone->contains(cssClass))
       
   463         return; // the css forbids us to display this line
       
   464 
   388     if (chatStrings.size() > 250)
   465     if (chatStrings.size() > 250)
   389         chatStrings.removeFirst();
   466         chatStrings.removeFirst();
   390 
   467 
   391     line = QString("<span class=\"%2\">%1</span>").arg(line).arg(cssClass);
   468     line = QString("<span class=\"%2\">%1</span>").arg(line).arg(cssClass);
   392 
   469