YAY! Colors are back!
[quassel.git] / src / qtui / chatitem.cpp
index 08d9f11..c12d5c5 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 <QFontMetrics>
 #include <QGraphicsSceneMouseEvent>
-#include <QTextCursor>
-#include <QTextDocument>
-
-#include <QtGui>
+#include <QPainter>
+#include <QTextLayout>
 
 #include "chatitem.h"
-
-ChatItem::ChatItem(QGraphicsItem *parent) : QGraphicsItem(parent) {
-  _width = 0;
-  //if(_wrapMode == WordWrap) {
-  //  setFlags(QGraphicsItem::ItemClipsToShape, true);
-  //}
+#include "chatlinemodel.h"
+#include "qtui.h"
+
+ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
+  QFontMetricsF *metrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
+  _lineHeight = metrics->lineSpacing();
+  _lineLeading = metrics->leading();
+  _layout = 0;
 }
 
 ChatItem::~ChatItem() {
 
 }
 
-void ChatItem::setWidth(int w) {
-  _width = w;
-  layout();
+QVariant ChatItem::data(int role) const {
+  if(!_index.isValid()) {
+    qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
+    return QVariant();
+  }
+  return _index.data(role);
 }
 
-void ChatItem::setTextOption(const QTextOption &option) {
-  _textOption = option;
-  layout();
+int ChatItem::setWidth(int w) {
+  if(w == _boundingRect.width()) return _boundingRect.height();
+  int h = heightForWidth(w);
+  _boundingRect.setWidth(w);
+  _boundingRect.setHeight(h);
+  return h;
 }
 
-QTextOption ChatItem::textOption() const {
-  return _textOption;
+int ChatItem::heightForWidth(int width) {
+  if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
+    return _lineHeight; // only contents can be multi-line
+
+  QVariantList wrapList = data(ChatLineModel::WrapListRole).toList();
+  int lines = 1;
+  int offset = 0;
+  for(int i = 0; i < wrapList.count(); i+=2) {
+    if(wrapList.at(i+1).toUInt() - offset < width) continue;
+    lines++;
+    if(i > 0) {
+      if(offset < wrapList.at(i-1).toUInt()) offset = wrapList.at(i-1).toUInt();
+      else offset += width;
+    } else {
+      offset += width;
+    }
+    i-=2;
+  }
+  return lines * _lineHeight;
 }
 
-QString ChatItem::text() const {
-  return _layout.text();
+void ChatItem::layout() {
+  if(_layout) return;
+  _layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
+
+  // Convert format information into a FormatRange
+  QList<QTextLayout::FormatRange> formatRanges;
+  UiStyle::FormatList formatList = data(MessageModel::FormatRole).value<UiStyle::FormatList>();
+  QTextLayout::FormatRange range;
+  int i = 0;
+  for(i = 0; i < formatList.count(); i++) {
+    range.format = QtUi::style()->mergedFormat(formatList.at(i).second);
+    range.start = formatList.at(i).first;
+    if(i > 0) formatRanges.last().length = range.start - formatRanges.last().start;
+    formatRanges.append(range);
+  }
+  if(i > 0) formatRanges.last().length = _layout->text().length() - formatRanges.last().start;
+  _layout->setAdditionalFormats(formatRanges);
+
+  // Now layout
+  qreal h = 0;
+  _layout->beginLayout();
+  forever {
+    QTextLine line = _layout->createLine();
+    if (!line.isValid())
+      break;
+
+    line.setLineWidth(width());
+    h += _lineLeading;
+    line.setPosition(QPointF(0, h));
+    h += line.height();
+  }
+  _layout->endLayout();
 }
 
-void ChatItem::setText(const UiStyle::StyledText &text) {
-  _layout.setText(text.text);
-  _layout.setAdditionalFormats(text.formats);
+void ChatItem::clearLayout() {
+  delete _layout;
+  _layout = 0;
+}
+
+void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+  Q_UNUSED(option); Q_UNUSED(widget);
   layout();
+  _layout->draw(painter, QPointF(0,0), QVector<QTextLayout::FormatRange>(), boundingRect());
 }
 
+/*
 void ChatItem::layout() {
   if(!_layout.additionalFormats().count()) return; // no text set
   if(_width <= 0) return;
@@ -91,7 +150,9 @@ void ChatItem::layout() {
   }
   _layout.endLayout();
   update();
-}
+}    QDateTime _timestamp;
+    MsgId _msgId;
+
 
 QRectF ChatItem::boundingRect() const {
   return _layout.boundingRect();
@@ -102,6 +163,7 @@ void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
   _layout.draw(painter, QPointF(0, 0));
 
 }
+*/
 
 /*
 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
@@ -114,6 +176,3 @@ void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
   } else QGraphicsTextItem::mouseMoveEvent(event);
 }
 */
-
-
-