custom highlighting
authorsheepluva
Wed, 26 Oct 2011 05:57:00 +0200
changeset 6214 c06b7e2eb7a4
parent 6213 c0f2e0915e57
child 6215 efe2e2f7cabf
custom highlighting
QTfrontend/ui/widget/chatwidget.cpp
QTfrontend/ui/widget/chatwidget.h
--- a/QTfrontend/ui/widget/chatwidget.cpp	Wed Oct 26 02:21:44 2011 +0200
+++ b/QTfrontend/ui/widget/chatwidget.cpp	Wed Oct 26 05:57:00 2011 +0200
@@ -136,6 +136,8 @@
                 style.append(in.readLine()+"\n");
             }
             orgStyleSheet = style;
+
+            file.close();
         }
     }
 
@@ -483,7 +485,8 @@
 
     // check first character for color code and set color properly
     char c = str[0].toAscii();
-    switch (c) {
+    switch (c)
+    {
         case 3:
             cssClass = (isFriend ? "msg_FriendJoin" : "msg_UserJoin");
             break;
@@ -495,9 +498,21 @@
                 cssClass = "msg_FriendChat";
     }
 
-    bool isHL = (!nick.isEmpty() &&
-                (nick != m_userNick) &&
-                str.toLower().contains(m_hlRegExp));
+    bool isHL = false;
+
+    if ((!nick.isEmpty()) && (nick != m_userNick))
+    {
+        QString lcStr = str.toLower();
+
+        foreach (const QRegExp & hl, m_highlights)
+        {
+            if (lcStr.contains(hl))
+            {
+                isHL = true;
+                break;
+            }
+        }
+    }
 
     addLine(cssClass, formattedStr, isHL);
 }
@@ -584,8 +599,48 @@
     chatStrings.clear();
     chatNicks->clear();
     m_userNick = gameSettings->value("net/nick","").toString();
-    m_hlRegExp = QRegExp(QString("^(.* )?%1(( |[^-a-z0-9_]( |$)).*)?$").
-                            arg(QRegExp::escape(m_userNick).toLower()));
+
+    // clear and re compile regexp for highlighting
+    m_highlights.clear();
+
+    QString hlRegExp("^(.* )?%1(( |[^-a-z0-9_]( |$)).*)?$");
+    QRegExp whitespace("\\s");
+
+    m_highlights.append(QRegExp(hlRegExp.arg(m_userNick)));
+
+    QFile file(cfgdir->absolutePath() + "/" + m_userNick + "_highlight.txt");
+
+    if (file.exists() && (file.open(QIODevice::ReadOnly | QIODevice::Text)))
+    {
+        QTextStream in(&file);
+        while (!in.atEnd())
+        {
+            QString line = in.readLine();
+            QStringList list = line.split(whitespace);
+            foreach (QString word, list)
+            {
+                m_highlights.append(QRegExp(
+                                hlRegExp.arg(QRegExp::escape(word.toLower()))));
+            }
+        }
+
+        if (file.isOpen())
+            file.close();
+    }
+
+    QFile file2(cfgdir->absolutePath() + "/" + m_userNick + "_hlregexp.txt");
+
+    if (file2.exists() && (file2.open(QIODevice::ReadOnly | QIODevice::Text)))
+    {
+        QTextStream in(&file2);
+        while (!in.atEnd())
+        {
+            m_highlights.append(QRegExp(in.readLine().toLower()));
+        }
+
+        if (file2.isOpen())
+            file2.close();
+    }
 }
 
 void HWChatWidget::onKick()
@@ -788,6 +843,9 @@
         displayNotice(tr("Stylesheet imported from %1").arg(path));
         displayNotice(tr("Enter %1 if you want to use the current styleSheet in future, enter %2 to reset!").arg("/saveStyleSheet").arg("/discaredStyleSheet"));
 
+        if (file.isOpen())
+            file.close();
+
         event->acceptProposedAction();
     }
     else
--- a/QTfrontend/ui/widget/chatwidget.h	Wed Oct 26 02:21:44 2011 +0200
+++ b/QTfrontend/ui/widget/chatwidget.h	Wed Oct 26 05:57:00 2011 +0200
@@ -24,6 +24,7 @@
 #include <QListWidget>
 #include <QString>
 #include <QGridLayout>
+#include <QList>
 #include <QRegExp>
 
 #include "SDLInteraction.h"
@@ -133,7 +134,7 @@
   QString m_helloSound;
   QString m_hilightSound;
   QString m_userNick;
-  QRegExp m_hlRegExp; ///< regular expression used for highlighting messages
+  QList<QRegExp> m_highlights; ///< regular expressions used for highlighting
   bool notify;
   bool showReady;