Make the style engine fix a little less obscure by copying the string explicitely...
[quassel.git] / src / uisupport / uistyle.cpp
index 8c3afaa..e04a0bd 100644 (file)
@@ -1,11 +1,11 @@
 /***************************************************************************
- *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
+ *   (at your option) version 3.                                           *
  *                                                                         *
  *   This program is distributed in the hope that it will be useful,       *
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  ***************************************************************************/
 
 #include "uistyle.h"
+#include "uistylesettings.h"
 
-UiStyle::UiStyle() {
+UiStyle::UiStyle(const QString &settingsKey) : _settingsKey(settingsKey) {
   // Default format
   QTextCharFormat def;
   def.setForeground(QBrush("#000000"));
-  def.setFont(QFont("Verdana",9));
-
-  _formats = QVector<QTextCharFormat>(NumFormatTypes, def);
+  //def.setFont(QFont("Courier", 10));
+  def.font().setStyleHint(QFont::TypeWriter);
+  _defaultFormats = QVector<QTextCharFormat>(NumFormatTypes, def);
+  _customFormats = QVector<QTextCharFormat>(NumFormatTypes, QTextFormat().toCharFormat());
+
+  // Load saved custom formats
+  UiStyleSettings s(_settingsKey);
+  foreach(FormatType type, s.availableFormats()) {
+    _customFormats[type] = s.customFormat(type);
+  }
 
   // Initialize color codes according to mIRC "standard"
   QStringList colors;
@@ -68,19 +76,22 @@ UiStyle::UiStyle() {
     _formatCodes[QString("%Dcf%1").arg(idx)] = (FormatType)(FgCol00 + i);
     _formatCodes[QString("%Dcb%1").arg(idx)] = (FormatType)(BgCol00 + i);
     QTextCharFormat fgf, bgf;
-    fgf.setForeground(QBrush(QColor(colors[i]))); _formats[FgCol00 + i] = fgf;
-    bgf.setBackground(QBrush(QColor(colors[i]))); _formats[BgCol00 + i] = bgf;
+    fgf.setForeground(QBrush(QColor(colors[i]))); setFormat((FormatType)(FgCol00 + i), fgf, Settings::Default);
+    bgf.setBackground(QBrush(QColor(colors[i]))); setFormat((FormatType)(BgCol00 + i), bgf, Settings::Default);
+    //FIXME fix the havoc caused by ColorSettingsPage
+    setFormat((FormatType)(FgCol00 + i), fgf, Settings::Custom);
+    setFormat((FormatType)(BgCol00 + i), bgf, Settings::Custom);
   }
 
   // Set a few more standard formats
   QTextCharFormat bold; bold.setFontWeight(QFont::Bold);
-  setFormat(Bold, bold);
+  setFormat(Bold, bold, Settings::Default);
 
   QTextCharFormat italic; italic.setFontItalic(true);
-  setFormat(Italic, italic);
+  setFormat(Italic, italic, Settings::Default);
 
   QTextCharFormat underline; underline.setFontUnderline(true);
-  setFormat(Underline, underline);
+  setFormat(Underline, underline, Settings::Default);
 
   // All other formats should be defined in derived classes.
 }
@@ -89,12 +100,24 @@ UiStyle::~ UiStyle() {
   
 }
 
-void UiStyle::setFormat(FormatType ftype, QTextCharFormat fmt) {
-  _formats[ftype] = fmt;
+void UiStyle::setFormat(FormatType ftype, QTextCharFormat fmt, Settings::Mode mode) {
+  if(mode == Settings::Default) {
+    _defaultFormats[ftype] = fmt;
+  } else {
+    UiStyleSettings s(_settingsKey);
+    if(fmt != _defaultFormats[ftype]) {
+      _customFormats[ftype] = fmt;
+      s.setCustomFormat(ftype, fmt);
+    } else {
+      _customFormats[ftype] = QTextFormat().toCharFormat();
+      s.removeCustomFormat(ftype);
+    }
+  }
 }
 
-QTextCharFormat UiStyle::format(FormatType ftype) const {
-  return _formats[ftype];
+QTextCharFormat UiStyle::format(FormatType ftype, Settings::Mode mode) const {
+  if(mode == Settings::Custom && _customFormats[ftype].isValid()) return _customFormats[ftype];
+  else return _defaultFormats[ftype];
 }
 
 UiStyle::FormatType UiStyle::formatType(const QString & code) const {
@@ -106,14 +129,18 @@ QString UiStyle::formatCode(FormatType ftype) const {
   return _formatCodes.key(ftype);
 }
 
-UiStyle::StyledText UiStyle::styleString(QString s) {
+UiStyle::StyledText UiStyle::styleString(const QString &_s) {
+  QString s = _s;  // we can't use call-by-value since this seems to maybe screw up Qt's implicit sharing somehow
+                   // at least invalid formats are created if we do that
+
   StyledText result;
   QList<FormatType> fmtList;
   fmtList.append(None);
   QTextLayout::FormatRange curFmtRng;
   curFmtRng.format = format(None);
+  curFmtRng.start = 0;
   result.formats.append(curFmtRng);
-  int pos = 0; int length;
+  int pos = 0; int length = 0;
   int fgCol = -1, bgCol = -1;  // marks current mIRC color
   for(;;) {
     pos = s.indexOf('%', pos);
@@ -136,6 +163,8 @@ UiStyle::StyledText UiStyle::styleString(QString s) {
         length = 4;
       } else {
         int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue();
+        //TODO: use 99 as transparent color (re mirc color "standard")
+        color &= 0x0f;
         int *colptr; FormatType coltype;
         if(s[pos+3] == 'f') { // foreground
           colptr = &fgCol; coltype = FgCol00;
@@ -160,16 +189,20 @@ UiStyle::StyledText UiStyle::styleString(QString s) {
       fmtList.clear(); fmtList.append(None);
       curFmtRng.format = format(None);
       fgCol = bgCol = -1;
-      length = 1;
+      length = 2;
     } else if(s[pos+1] == 'R') { // reverse
       // TODO: implement reverse formatting
 
-      length = 1;
+      length = 2;
     } else { // all others are toggles
       QString code = QString("%") + s[pos+1];
       if(s[pos+1] == 'D') code += s[pos+2];
       FormatType ftype = formatType(code);
-      Q_ASSERT(ftype != Invalid);
+      if(ftype == Invalid) {
+        qWarning(qPrintable(QString("Invalid format code in string: %1").arg(s)));
+        continue;
+      }
+      //Q_ASSERT(ftype != Invalid);
       length = code.length();
       if(!fmtList.contains(ftype)) {
         // toggle it on