Make sender-hash based styling ("colored nicks") work again
authorManuel Nickschas <sputnick@quassel-irc.org>
Mon, 22 Jun 2009 19:54:35 +0000 (21:54 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 6 Aug 2009 18:25:05 +0000 (20:25 +0200)
Plain messages now obtain their format depending on the sender hash.
This can be used to implement colored nicks, but with our new style engine,
we can now solve several problems like using other colors for highlighted messages.

Note that the number of hashed is currently hard-coded to 16; waiting for a nice way
to make this number configurable in a stylesheet.

src/qtui/chatlinemodelitem.cpp
src/uisupport/qssparser.cpp
src/uisupport/uistyle.cpp
src/uisupport/uistyle.h

index 0bcc7a7..1653962 100644 (file)
@@ -124,7 +124,7 @@ QVariant ChatLineModelItem::contentsData(int role) const {
 }
 
 quint32 ChatLineModelItem::messageLabel() const {
 }
 
 quint32 ChatLineModelItem::messageLabel() const {
-  quint32 label = 0;
+  quint32 label = _styledMsg.senderHash() << 16;
   if(_styledMsg.flags() & Message::Self)
     label |= UiStyle::OwnMsg;
   if(_styledMsg.flags() & Message::Highlight)
   if(_styledMsg.flags() & Message::Self)
     label |= UiStyle::OwnMsg;
   if(_styledMsg.flags() & Message::Highlight)
index d7a788e..c25bceb 100644 (file)
@@ -237,8 +237,8 @@ quint64 QssParser::parseFormatType(const QString &decl) {
               qWarning() << Q_FUNC_INFO << tr("Invalid senderhash specification: %1").arg(condValue);
               return UiStyle::Invalid;
             }
               qWarning() << Q_FUNC_INFO << tr("Invalid senderhash specification: %1").arg(condValue);
               return UiStyle::Invalid;
             }
-            if(val >= 255) {
-              qWarning() << Q_FUNC_INFO << tr("Senderhash can be at most \"fe\"!");
+            if(val >= 16) {
+              qWarning() << Q_FUNC_INFO << tr("Senderhash can be at most \"0x0f\"!");
               return UiStyle::Invalid;
             }
             fmtType |= val << 48;
               return UiStyle::Invalid;
             }
             fmtType |= val << 48;
index 229e4ba..b5e8911 100644 (file)
@@ -98,8 +98,12 @@ QTextCharFormat UiStyle::format(quint32 ftype, quint32 label) {
 
   fmt.merge(cachedFormat(key & 0x0000000000000000));  // basic
   fmt.merge(cachedFormat(key & 0x000000000000000f));  // msgtype
 
   fmt.merge(cachedFormat(key & 0x0000000000000000));  // basic
   fmt.merge(cachedFormat(key & 0x000000000000000f));  // msgtype
+  fmt.merge(cachedFormat(key & 0xffff000000000000));  // nickhash
+  fmt.merge(cachedFormat(key & 0xffff00000000000f));  // nickhash + msgtype
   fmt.merge(cachedFormat(key & 0x0000ffff00000000));  // label
   fmt.merge(cachedFormat(key & 0x0000ffff0000000f));  // label + msgtype
   fmt.merge(cachedFormat(key & 0x0000ffff00000000));  // label
   fmt.merge(cachedFormat(key & 0x0000ffff0000000f));  // label + msgtype
+  fmt.merge(cachedFormat(key & 0xffffffff00000000));  // label + nickhash
+  fmt.merge(cachedFormat(key & 0xffffffff0000000f));  // label + nickhash + msgtype
 
   // TODO: allow combinations for mirc formats and colors (each), e.g. setting a special format for "bold and italic"
   //       or "foreground 01 and background 03"
 
   // TODO: allow combinations for mirc formats and colors (each), e.g. setting a special format for "bold and italic"
   //       or "foreground 01 and background 03"
@@ -308,6 +312,10 @@ QString UiStyle::mircToInternal(const QString &mirc_) {
 UiStyle::StyledMessage::StyledMessage(const Message &msg)
   : Message(msg)
 {
 UiStyle::StyledMessage::StyledMessage(const Message &msg)
   : Message(msg)
 {
+  if(type() == Message::Plain)
+    _senderHash = 0xff;
+  else
+    _senderHash = 0x00;  // this means we never compute the hash for msgs that aren't plain
 }
 
 void UiStyle::StyledMessage::style() const {
 }
 
 void UiStyle::StyledMessage::style() const {
@@ -497,6 +505,22 @@ UiStyle::FormatType UiStyle::StyledMessage::senderFormat() const {
   }
 }
 
   }
 }
 
+// FIXME hardcoded to 16 sender hashes
+quint8 UiStyle::StyledMessage::senderHash() const {
+  if(_senderHash != 0xff)
+    return _senderHash;
+
+  QString nick = nickFromMask(sender()).toLower();
+  if(!nick.isEmpty()) {
+    int chopCount = 0;
+    while(nick.at(nick.count() - 1 - chopCount) == '_')
+      chopCount++;
+    nick.chop(chopCount);
+  }
+  quint16 hash = qChecksum(nick.toAscii().data(), nick.toAscii().size());
+  return (_senderHash = (hash & 0xf) + 1);
+}
+
 /***********************************************************************************/
 
 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList) {
 /***********************************************************************************/
 
 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList) {
index 22ade27..f031085 100644 (file)
@@ -148,6 +148,9 @@ public:
   inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
   FormatType senderFormat() const;
   const FormatList &contentsFormatList() const;
   inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
   FormatType senderFormat() const;
   const FormatList &contentsFormatList() const;
+
+  quint8 senderHash() const;
+
 protected:
   //! Styling is only needed for calls to plainContents() and contentsFormatList()
   void style() const;
 protected:
   //! Styling is only needed for calls to plainContents() and contentsFormatList()
   void style() const;
@@ -155,6 +158,7 @@ protected:
 
 private:
   mutable StyledString _contents;
 
 private:
   mutable StyledString _contents;
+  mutable quint8 _senderHash;
 };
 
 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
 };
 
 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);