Make UiStyle::styleString() and friends static
authorManuel Nickschas <sputnick@quassel-irc.org>
Thu, 18 Jun 2009 08:21:56 +0000 (10:21 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 6 Aug 2009 18:25:05 +0000 (20:25 +0200)
styleString() actually just converts our internal format codes into a list
of FormatTypes, which are only used as keys for getting a particular format from
a particular style. Hence, styleString() itself can be made static. Amongst other things,
this has the advantage that StyledMessage's lazy styling can be encapsulated properly, and
there is no need to externally ensure that the message has been styled.

src/uisupport/uistyle.cpp
src/uisupport/uistyle.h

index 7cae8b3..a29d0c5 100644 (file)
@@ -25,6 +25,8 @@
 #include "uisettings.h"
 #include "util.h"
 
+QHash<QString, UiStyle::FormatType> UiStyle::_formatCodes;
+
 UiStyle::UiStyle() {
   // register FormatList if that hasn't happened yet
   // FIXME I don't think this actually avoids double registration... then again... does it hurt?
@@ -135,7 +137,7 @@ QFontMetricsF *UiStyle::fontMetrics(quint32 ftype, quint32 label) {
   return (_metricsCache[key] = new QFontMetricsF(format(ftype, label).font()));
 }
 
-UiStyle::FormatType UiStyle::formatType(Message::Type msgType) const {
+UiStyle::FormatType UiStyle::formatType(Message::Type msgType) {
   switch(msgType) {
     case Message::Plain:
       return PlainMsg;
@@ -170,12 +172,12 @@ UiStyle::FormatType UiStyle::formatType(Message::Type msgType) const {
   return ErrorMsg;
 }
 
-UiStyle::FormatType UiStyle::formatType(const QString & code) const {
+UiStyle::FormatType UiStyle::formatType(const QString & code) {
   if(_formatCodes.contains(code)) return _formatCodes.value(code);
   return Invalid;
 }
 
-QString UiStyle::formatCode(FormatType ftype) const {
+QString UiStyle::formatCode(FormatType ftype) {
   return _formatCodes.key(ftype);
 }
 
@@ -258,7 +260,7 @@ UiStyle::StyledString UiStyle::styleString(const QString &s_, quint32 baseFormat
   return result;
 }
 
-QString UiStyle::mircToInternal(const QString &mirc_) const {
+QString UiStyle::mircToInternal(const QString &mirc_) {
   QString mirc = mirc_;
   mirc.replace('%', "%%");      // escape % just to be sure
   mirc.replace('\x02', "%B");
@@ -308,11 +310,11 @@ UiStyle::StyledMessage::StyledMessage(const Message &msg)
 {
 }
 
-void UiStyle::StyledMessage::style(UiStyle *style) const {
+void UiStyle::StyledMessage::style() const {
   QString user = userFromMask(sender());
   QString host = hostFromMask(sender());
   QString nick = nickFromMask(sender());
-  QString txt = style->mircToInternal(contents());
+  QString txt = UiStyle::mircToInternal(contents());
   QString bufferName = bufferInfo().bufferName();
   bufferName.replace('%', "%%"); // well, you _can_ have a % in a buffername apparently... -_-
 
@@ -377,7 +379,21 @@ void UiStyle::StyledMessage::style(UiStyle *style) const {
     default:
       t = tr("[%1]").arg(txt);
   }
-  _contents = style->styleString(t, style->formatType(type()));
+  _contents = UiStyle::styleString(t, UiStyle::formatType(type()));
+}
+
+const QString &UiStyle::StyledMessage::plainContents() const {
+  if(_contents.plainText.isNull())
+    style();
+
+  return _contents.plainText;
+}
+
+const UiStyle::FormatList &UiStyle::StyledMessage::contentsFormatList() const {
+  if(_contents.plainText.isNull())
+    style();
+
+  return _contents.formatList;
 }
 
 QString UiStyle::StyledMessage::decoratedTimestamp() const {
index c55aa1c..a163809 100644 (file)
@@ -102,10 +102,9 @@ public:
 
   class StyledMessage;
 
-  StyledString styleString(const QString &string, quint32 baseFormat = None);
-  QString mircToInternal(const QString &) const;
-
-  FormatType formatType(Message::Type msgType) const;
+  static FormatType formatType(Message::Type msgType);
+  static StyledString styleString(const QString &string, quint32 baseFormat = None);
+  static QString mircToInternal(const QString &);
 
   QTextCharFormat format(quint32 formatType, quint32 messageLabel = 0);
   QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel = 0);
@@ -126,34 +125,32 @@ protected:
   void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
   void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
 
-  FormatType formatType(const QString &code) const;
-  QString formatCode(FormatType) const;
+  static FormatType formatType(const QString &code);
+  static QString formatCode(FormatType);
 
 private:
   QFont _defaultFont;
   QHash<quint64, QTextCharFormat> _formatCache;
   QHash<quint64, QFontMetricsF *> _metricsCache;
-  QHash<QString, FormatType> _formatCodes;
+  static QHash<QString, FormatType> _formatCodes;
 };
 
 class UiStyle::StyledMessage : public Message {
 public:
   explicit StyledMessage(const Message &message);
 
-  //! Styling is only needed for calls to plainContents() and contentsFormatList()
-  // StyledMessage can't style lazily by itself, as it doesn't know the used style
-  bool inline needsStyling() const { return _contents.plainText.isNull(); }
-  void style(UiStyle *style) const;
-
-
   QString decoratedTimestamp() const;
   QString plainSender() const;             //!< Nickname (no decorations) for Plain and Notice, empty else
   QString decoratedSender() const;
-  inline const QString &plainContents() const { return _contents.plainText; }
+  const QString &plainContents() const;
 
   inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
   FormatType senderFormat() const;
-  inline const FormatList &contentsFormatList() const { return _contents.formatList; }
+  const FormatList &contentsFormatList() const;
+protected:
+  //! Styling is only needed for calls to plainContents() and contentsFormatList()
+  void style() const;
+
 
 private:
   mutable StyledString _contents;