some changes to the css... display:none; works now (e.g. you can use it for user join/leave messages that are not your friends)
authorsheepluva
Sat, 22 Oct 2011 06:55:14 +0200
changeset 6180 0992fc5a4ad9
parent 6179 b529f88d37d0
child 6181 c739b503ae31
some changes to the css... display:none; works now (e.g. you can use it for user join/leave messages that are not your friends)
QTfrontend/hwform.cpp
QTfrontend/res/css/chat.css
QTfrontend/ui/page/pageadmin.cpp
QTfrontend/ui/page/pagenetgame.cpp
QTfrontend/ui/page/pagenetgame.h
QTfrontend/ui/page/pageroomslist.cpp
QTfrontend/ui/page/pageroomslist.h
QTfrontend/ui/widget/chatwidget.cpp
QTfrontend/ui/widget/chatwidget.h
--- a/QTfrontend/hwform.cpp	Sat Oct 22 00:49:55 2011 -0400
+++ b/QTfrontend/hwform.cpp	Sat Oct 22 06:55:14 2011 +0200
@@ -769,19 +769,19 @@
             ShowErrorMessage(errmsg);
             // no break
         case ID_PAGE_NETGAME:
-            ui.pageNetGame->pChatWidget->addLine("Error",errmsg);
+            ui.pageNetGame->displayError(errmsg);
             break;
         default:
-        ui.pageRoomsList->chatWidget->addLine("Error",errmsg);
+        ui.pageRoomsList->displayError(errmsg);
     }
 }
 
 void HWForm::NetWarning(const QString & wrnmsg)
 {
     if (ui.Pages->currentIndex() == ID_PAGE_NETGAME || ui.Pages->currentIndex() == ID_PAGE_INGAME)
-        ui.pageNetGame->pChatWidget->addLine("Warning",wrnmsg);
+        ui.pageNetGame->displayWarning(wrnmsg);
     else
-        ui.pageRoomsList->chatWidget->addLine("Warning",wrnmsg);
+        ui.pageRoomsList->displayWarning(wrnmsg);
 }
 
 void HWForm::_NetConnect(const QString & hostName, quint16 port, QString nick)
@@ -1257,7 +1257,7 @@
     {
         GoBack();
         if (!reason.isEmpty())
-            ui.pageRoomsList->chatWidget->addLine("Notice",reason);
+            ui.pageRoomsList->displayNotice(reason);
     }
     else
         qWarning("Left room while not in room");
--- a/QTfrontend/res/css/chat.css	Sat Oct 22 00:49:55 2011 -0400
+++ b/QTfrontend/res/css/chat.css	Sat Oct 22 06:55:14 2011 +0200
@@ -1,21 +1,34 @@
 /*
  * see http://doc.qt.nokia.com/4.5/richtext-html-subset.html#css-properties
+ *
+ * In the QTfrontend of hedgewars also display:none; will work for class names
+ * that start with msg_ - as long as they are referenced directly and not
+ * within any hierachy.
  */
 
+/* links */
 a { color:#c8c8ff; }
+
+/* nick names (they are also sometimes linked) */
 .nick { text-decoration: none; }
-.UserChat {  }
-.UserChat .nick { color:#ffec20; }
-.FriendChat { color: #08e008; }
-.FriendChat .nick { color: #20ff20; }
-.UserJoin { color: #c0c0c0; }
-.UserJoin .nick { color: #d0d0d0; }
-.FriendJoin { color: #c0e0c0; }
-.FriendJoin .nick { color: #d0f0d0; }
-.UserAction { color: #ff80ff; }
-.UserAction .nick { color: #ffa0ff; }
-.FriendAction { color: #ff00ff; }
-.FriendAction .nick { color: #ff30ff; }
-.Error { color: #ff0000 }
-.Warning { color: #ff8000 }
-.Notice { color: #fefefe }
+
+/* various chat messages */
+.msg_UserChat { }
+.msg_UserChat .nick { color:#ffec20; }
+.msg_FriendChat { color: #08e008; }
+.msg_FriendChat .nick { color: #20ff20; }
+.msg_UserJoin { color: #c0c0c0; }
+.msg_UserJoin .nick { color: #d0d0d0; }
+.msg_FriendJoin { color: #c0f0c0; }
+.msg_FriendJoin .nick { color: #d8f0d8; }
+.msg_UserAction { color: #ff80ff; }
+.msg_UserAction .nick { color: #ffa0ff;}
+.msg_FriendAction { color: #ff00ff; }
+.msg_FriendAction .nick { color: #ff30ff; }
+/* uncomment next line to disable join and leave messages of non-friends */
+/* .msg_UserJoin { display:none; } */
+
+/* frontend messages */
+.msg_Error { color: #ff0000; }
+.msg_Warning { color: #ff8000; }
+.msg_Notice { color: #fefefe; }
--- a/QTfrontend/ui/page/pageadmin.cpp	Sat Oct 22 00:49:55 2011 -0400
+++ b/QTfrontend/ui/page/pageadmin.cpp	Sat Oct 22 06:55:14 2011 +0200
@@ -64,8 +64,7 @@
 
     tb = new QTextBrowser(this);
     tb->setOpenExternalLinks(true);
-    // TODO this work-around to apply chat css to preview won't work anymore
-    // tb->document()->setDefaultStyleSheet(HWChatWidget::STYLE);
+    tb->document()->setDefaultStyleSheet(HWChatWidget::styleSheet());
     pageLayout->addWidget(tb, 4, 1, 1, 2);
     
     // 5
--- a/QTfrontend/ui/page/pagenetgame.cpp	Sat Oct 22 00:49:55 2011 -0400
+++ b/QTfrontend/ui/page/pagenetgame.cpp	Sat Oct 22 06:55:14 2011 +0200
@@ -116,6 +116,24 @@
 
 }
 
+
+void PageNetGame::displayError(const QString & message)
+{
+    pChatWidget->displayError(message);
+}
+
+
+void PageNetGame::displayNotice(const QString & message)
+{
+    pChatWidget->displayNotice(message);
+}
+
+void PageNetGame::displayWarning(const QString & message)
+{
+    pChatWidget->displayWarning(message);
+}
+
+
 void PageNetGame::setReadyStatus(bool isReady)
 {
     if(isReady)
--- a/QTfrontend/ui/page/pagenetgame.h	Sat Oct 22 00:49:55 2011 -0400
+++ b/QTfrontend/ui/page/pagenetgame.h	Sat Oct 22 06:55:14 2011 +0200
@@ -40,6 +40,10 @@
      */
     void setRoomName(const QString & roomName);
 
+    void displayError(const QString & message);
+    void displayNotice(const QString & message);
+    void displayWarning(const QString & message);
+
     QPushButton *BtnGo;
     QPushButton *BtnMaster;
     QPushButton *BtnStart;
--- a/QTfrontend/ui/page/pageroomslist.cpp	Sat Oct 22 00:49:55 2011 -0400
+++ b/QTfrontend/ui/page/pageroomslist.cpp	Sat Oct 22 06:55:14 2011 +0200
@@ -168,6 +168,24 @@
     gameInLobby = false;
 }
 
+
+void PageRoomsList::displayError(const QString & message)
+{
+    chatWidget->displayError(message);
+}
+
+
+void PageRoomsList::displayNotice(const QString & message)
+{
+    chatWidget->displayNotice(message);
+}
+
+void PageRoomsList::displayWarning(const QString & message)
+{
+    chatWidget->displayWarning(message);
+}
+
+
 void PageRoomsList::setAdmin(bool flag)
 {
     BtnAdmin->setVisible(flag);
--- a/QTfrontend/ui/page/pageroomslist.h	Sat Oct 22 00:49:55 2011 -0400
+++ b/QTfrontend/ui/page/pageroomslist.h	Sat Oct 22 06:55:14 2011 +0200
@@ -30,6 +30,9 @@
 
 public:
     PageRoomsList(QWidget* parent, QSettings * config);
+    void displayError(const QString & message);
+    void displayNotice(const QString & message);
+    void displayWarning(const QString & message);
 
     QLineEdit * roomName;
     QLineEdit * searchText;
--- a/QTfrontend/ui/widget/chatwidget.cpp	Sat Oct 22 00:49:55 2011 -0400
+++ b/QTfrontend/ui/widget/chatwidget.cpp	Sat Oct 22 06:55:14 2011 +0200
@@ -30,6 +30,8 @@
 #include <QScrollBar>
 #include <QItemSelectionModel>
 #include <QStringList>
+#include <QRegExp>
+
 
 #include "HWDataManager.h"
 #include "hwconsts.h"
@@ -96,6 +98,98 @@
     return firstIsShorter;
 }
 
+QString * HWChatWidget::s_styleSheet = NULL;
+QStringList * HWChatWidget::s_displayNone = NULL;
+
+QString & HWChatWidget::styleSheet()
+{
+    if (s_styleSheet != NULL)
+        return *s_styleSheet;
+
+    // initialize
+    s_styleSheet = new QString();
+
+    // getting a reference
+    QString & style = *s_styleSheet;
+
+    // load external stylesheet if there is any
+    QFile extFile(HWDataManager::instance().findFileForRead("css/chat.css"));
+
+    QFile resFile(":/res/css/chat.css");
+
+    QFile & file = (extFile.exists()?extFile:resFile);
+
+    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
+    {
+        QTextStream in(&file);
+        while (!in.atEnd())
+        {
+            QString line = in.readLine();
+            if(!line.isEmpty())
+                style.append(line);
+        }
+    }
+
+    // prepare for MAGIC :D
+
+    // matches (multi-)whitespaces (for replacement with simple space)
+    QRegExp ws("\\s+");
+
+    // matches comments (for removal)
+    QRegExp rem("/\\*([^*]|\\*(?!/))*\\*/");
+
+    // strip comments and multi-whitespaces to compress the style-sheet a bit
+    style = style.remove(rem).replace(ws," ");
+
+
+    // now let's see what messages the user does not want to be displayed
+    // by checking for display:none; (since QTextBrowser does not support it)
+
+    // MOAR MAGIC :DDD
+
+    // matches definitions lacking display:none; (for removal)
+    QRegExp displayed(
+        "([^{}]*\\{)(?!([^}]*;)* ?display ?: ?none ?(;[^}]*)?\\})[^}]*\\}");
+
+    // matches all {...} and , (used as seperator for splitting into names)
+    QRegExp split(" *(\\{[^}]*\\}|,) *");
+
+    // matches class names that are referenced without hierachy
+    QRegExp nohierarchy("^.[^ .]+$");
+
+    QStringList victims = QString(style).
+                                remove(displayed). // remove visible stuff
+                                split(split). // get a list of the names
+                                filter(nohierarchy). // only direct class names
+                                replaceInStrings(QRegExp("^."),""); // crop .
+
+    victims.removeDuplicates();
+
+    s_displayNone = new QStringList(victims);
+
+
+    return style;
+}
+
+void HWChatWidget::displayError(const QString & message)
+{
+    addLine("msg_Error", message);
+    // scroll to the end
+    chatText->moveCursor(QTextCursor::End);
+}
+
+
+void HWChatWidget::displayNotice(const QString & message)
+{
+    addLine("msg_Notice", message);
+}
+
+
+void HWChatWidget::displayWarning(const QString & message)
+{
+    addLine("msg_Warning", message);
+}
+
 
 HWChatWidget::HWChatWidget(QWidget* parent, QSettings * gameSettings, bool notify) :
   QWidget(parent),
@@ -122,27 +216,7 @@
 
     chatText = new QTextBrowser(this);
 
-    QString style;
-
-    // load external stylesheet if there is any
-    QFile extFile(HWDataManager::instance().findFileForRead("css/chat.css"));
-
-    QFile resFile(":/res/css/chat.css");
-
-    QFile & file = (extFile.exists()?extFile:resFile);
-
-    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
-    {
-        QTextStream in(&file);
-        while (!in.atEnd())
-        {
-            QString line = in.readLine();
-            if(!line.isEmpty())
-                style.append(line);
-        }
-    }
-
-    chatText->document()->setDefaultStyleSheet(style);
+    chatText->document()->setDefaultStyleSheet(styleSheet());
 
     chatText->setMinimumHeight(20);
     chatText->setMinimumWidth(10);
@@ -365,26 +439,29 @@
     if(!nick.isEmpty())
         formattedStr.replace("|nick|",QString("<a href=\"hwnick://?%1\" class=\"nick\">%2</a>").arg(QString(nick.toUtf8().toBase64())).arg(nick));
 
-    QString cssClass("UserChat");
+    QString cssClass("msg_UserChat");
 
     // check first character for color code and set color properly
     switch (str[0].toAscii()) {
         case 3:
-            cssClass = (isFriend ? "FriendJoin" : "UserJoin");
+            cssClass = (isFriend ? "msg_FriendJoin" : "msg_UserJoin");
             break;
         case 2:
-            cssClass = (isFriend ? "FriendAction" : "UserAction");
+            cssClass = (isFriend ? "msg_FriendAction" : "msg_UserAction");
             break;
         default:
             if (isFriend)
-                cssClass = "FriendChat";
+                cssClass = "msg_FriendChat";
     }
 
     addLine(cssClass,formattedStr);
 }
 
-void HWChatWidget::addLine(const QString& cssClass, QString line)
+void HWChatWidget::addLine(const QString & cssClass, QString line)
 {
+    if (s_displayNone->contains(cssClass))
+        return; // the css forbids us to display this line
+
     if (chatStrings.size() > 250)
         chatStrings.removeFirst();
 
--- a/QTfrontend/ui/widget/chatwidget.h	Sat Oct 22 00:49:55 2011 -0400
+++ b/QTfrontend/ui/widget/chatwidget.h	Sat Oct 22 06:55:14 2011 +0200
@@ -62,15 +62,21 @@
   void saveLists(const QString & nick);
   void setShowReady(bool s);
   void setShowFollow(bool enabled);
-  void addLine(const QString & cssClass, QString line);
-  static const char* STYLE;
   QStringList ignoreList, friendsList;
+  static QString & styleSheet();
+  void displayError(const QString & message);
+  void displayNotice(const QString & message);
+  void displayWarning(const QString & message);
 
 private:
+  static QString * s_styleSheet;
+  static QStringList * s_displayNone;
+
   void loadList(QStringList & list, const QString & file);
   void saveList(QStringList & list, const QString & file);
   void updateNickItem(QListWidgetItem *item);
   void updateNickItems();
+  void addLine(const QString & cssClass, QString line);
   static const QRegExp URLREGEXP;
 
  public slots: