Make the newly arrived topicbutton display background colors and font styles.
[quassel.git] / src / qtui / chatline-old.cpp
index c26da15..2c9cb0d 100644 (file)
 #include "network.h"
 #include "qtui.h"
 
-//! Construct a ChatLine object from a message.
+//! Construct a ChatLineOld object from a message.
 /**
  * \param m   The message to be layouted and rendered
  */
-ChatLine::ChatLine(Message m) {
+ChatLineOld::ChatLineOld(Message m) {
   hght = 0;
   //networkName = m.buffer.network();
   //bufferName = m.buffer.buffer();
@@ -37,17 +37,12 @@ ChatLine::ChatLine(Message m) {
   formatMsg(msg);
 }
 
-ChatLine::~ChatLine() {
+ChatLineOld::~ChatLineOld() {
 
 }
 
-void ChatLine::formatMsg(Message msg) {
-  const Network *net = Client::network(msg.bufferInfo().networkId());
-  if(net) {
-    QRegExp nickRegExp("^(.*\\W)?"+net->myNick()+"(\\W.*)?$");
-    if((msg.type() == Message::Plain || msg.type() == Message::Notice || msg.type() == Message::Action) && nickRegExp.exactMatch(msg.text()))
-      isHighlight = true;
-  }
+void ChatLineOld::formatMsg(Message msg) {
+  isHighlight = msg.flags() & Message::Highlight;
   QTextOption tsOption, senderOption, textOption;
   styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
   styledSender = QtUi::style()->styleString(msg.formattedSender());
@@ -56,13 +51,33 @@ void ChatLine::formatMsg(Message msg) {
 }
 
 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
-// We don't know yet if we keep this implementation of ChatLine, so I won't bother making this actually nice.
-// Also, the additional format is ignored for now, which means that you won't see a selection...
-// FIXME TODO
-QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(const UiStyle::StyledText &fs, QTextLayout::FormatRange additional) {
+// We don't know yet if we keep this implementation of ChatLineOld, so I won't bother making this actually nice.
+QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(UiStyle::StyledText fs, QTextLayout::FormatRange additional) {
   QList<FormatRange> ranges;
 
+  if(additional.length > 0) {
+    for(int i = 0; i < fs.formats.count(); i++) {
+      int oldend = fs.formats[i].start + fs.formats[i].length - 1;
+      int addend = additional.start + additional.length - 1;
+      if(oldend < additional.start) continue;
+      fs.formats[i].length = additional.start - fs.formats[i].start;
+      QTextLayout::FormatRange addfmtrng = fs.formats[i];
+      addfmtrng.format.merge(additional.format);
+      addfmtrng.start = additional.start;
+      addfmtrng.length = qMin(oldend, addend) - additional.start + 1;
+      fs.formats.insert(++i, addfmtrng);
+      if(addend == oldend) break;
+      if(addend < oldend) {
+        QTextLayout::FormatRange restfmtrng = fs.formats[i-1];
+        restfmtrng.start = addend + 1;
+        restfmtrng.length = oldend - addend;
+        fs.formats.insert(++i, restfmtrng);
+        break;
+      }
+    }
+  }
   foreach(QTextLayout::FormatRange f, fs.formats) {
+    if(f.length <= 0) continue;
     FormatRange range;
     range.start = f.start;
     range.length = f.length;
@@ -71,40 +86,10 @@ QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(const UiStyle::StyledTex
     range.height = metrics.lineSpacing();
     ranges.append(range);
   }
-  /*
-  QList<QTextLayout::FormatRange> formats = fs.formats;
-  formats.append(additional);
-  int cur = -1;
-  FormatRange range, lastrange;
-  for(int i = 0; i < fs.text.length(); i++) {
-    QTextCharFormat format;
-    foreach(QTextLayout::FormatRange f, formats) {
-      if(i >= f.start && i < f.start + f.length) format.merge(f.format);
-    }
-    if(cur < 0) {
-      range.start = 0; range.length = 1; range.format= format;
-      cur = 0;
-    } else {
-      if(format == range.format) range.length++;
-      else {
-        QFontMetrics metrics(range.format.font());
-        range.height = metrics.lineSpacing();
-        ranges.append(range);
-        range.start = i; range.length = 1; range.format = format;
-        cur++;
-      }
-    }
-  }
-  if(cur >= 0) {
-    QFontMetrics metrics(range.format.font());
-    range.height = metrics.lineSpacing();
-    ranges.append(range);
-  }
-  */
   return ranges;
 }
 
-void ChatLine::setSelection(SelectionMode mode, int start, int end) {
+void ChatLineOld::setSelection(SelectionMode mode, int start, int end) {
   selectionMode = mode;
   //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
   QPalette pal = QApplication::palette();
@@ -121,9 +106,7 @@ void ChatLine::setSelection(SelectionMode mode, int start, int end) {
       textSel.format.setBackground(pal.brush(QPalette::Highlight));
       textSel.start = selectionStart;
       textSel.length = selectionEnd - selectionStart;
-      //textFormat.append(textSel);
       textFormat = calcFormatRanges(styledText, textSel);
-      foreach(FormatRange fr, textFormat);
       break;
     case Full:
       tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
@@ -142,32 +125,32 @@ void ChatLine::setSelection(SelectionMode mode, int start, int end) {
   }
 }
 
-MsgId ChatLine::msgId() const {
+MsgId ChatLineOld::msgId() const {
   return msg.msgId();
 }
 
-BufferInfo ChatLine::bufferInfo() const {
+BufferInfo ChatLineOld::bufferInfo() const {
   return msg.bufferInfo();
 }
 
-QDateTime ChatLine::timestamp() const {
+QDateTime ChatLineOld::timestamp() const {
   return msg.timestamp();
 }
 
-QString ChatLine::sender() const {
+QString ChatLineOld::sender() const {
   return styledSender.text;
 }
 
-QString ChatLine::text() const {
+QString ChatLineOld::text() const {
   return styledText.text;
 }
 
-bool ChatLine::isUrl(int c) const {
+bool ChatLineOld::isUrl(int c) const {
   if(c < 0 || c >= charUrlIdx.count()) return false;;
   return charUrlIdx[c] >= 0;
 }
 
-QUrl ChatLine::getUrl(int c) const {
+QUrl ChatLineOld::getUrl(int c) const {
   if(c < 0 || c >= charUrlIdx.count()) return QUrl();
   int i = charUrlIdx[c];
   if(i >= 0) return styledText.urls[i].url;
@@ -176,10 +159,10 @@ QUrl ChatLine::getUrl(int c) const {
 
 //!\brief Return the cursor position for the given coordinate pos.
 /**
- * \param pos The position relative to the ChatLine
+ * \param pos The position relative to the ChatLineOld
  * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
  */
-int ChatLine::posToCursor(QPointF pos) {
+int ChatLineOld::posToCursor(QPointF pos) {
   if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
   qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
   if(pos.x() < textStart) return -1;
@@ -197,7 +180,7 @@ int ChatLine::posToCursor(QPointF pos) {
   return 0;
 }
 
-void ChatLine::precomputeLine() {
+void ChatLineOld::precomputeLine() {
   tsFormat = calcFormatRanges(styledTimeStamp);
   senderFormat = calcFormatRanges(styledSender);
   textFormat = calcFormatRanges(styledText);
@@ -250,7 +233,7 @@ void ChatLine::precomputeLine() {
   if(wr.start >= 0) words.append(wr);
 }
 
-qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) {
+qreal ChatLineOld::layout(qreal tsw, qreal senderw, qreal textw) {
   tsWidth = tsw; senderWidth = senderw; textWidth = textw;
   if(textw <= 0) return minHeight;
   lineLayouts.clear(); LineLayout line;
@@ -315,8 +298,8 @@ qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) {
   return hght;
 }
 
-//!\brief Draw ChatLine on the given QPainter at the given position.
-void ChatLine::draw(QPainter *p, const QPointF &pos) {
+//!\brief Draw ChatLineOld on the given QPainter at the given position.
+void ChatLineOld::draw(QPainter *p, const QPointF &pos) {
   QPalette pal = QApplication::palette();
 
   if(selectionMode == Full) {
@@ -326,7 +309,7 @@ void ChatLine::draw(QPainter *p, const QPointF &pos) {
   } else {
     if(isHighlight) {
       p->setPen(Qt::NoPen);
-      p->setBrush(pal.brush(QPalette::AlternateBase));
+      p->setBrush(QColor("lightcoral") /*pal.brush(QPalette::AlternateBase) */);
       p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
     }
     if(selectionMode == Partial) {