Properly re-layout on resizing
[quassel.git] / src / qtui / chatitem.cpp
index c518c5f..6efe8c2 100644 (file)
 
 #include <QFontMetrics>
 #include <QGraphicsSceneMouseEvent>
-#include <QTextCursor>
-#include <QTextDocument>
-
-#include <QtGui>
+#include <QPainter>
+#include <QTextLayout>
 
 #include "chatitem.h"
+#include "chatlinemodel.h"
+#include "qtui.h"
 
 ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
-  _width = _height = 0;
+  QFontMetricsF *metrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
+  _lineHeight = metrics->lineSpacing();
+  _lineLeading = metrics->leading();
+  _layout = 0;
 }
 
 ChatItem::~ChatItem() {
@@ -43,47 +46,89 @@ QVariant ChatItem::data(int role) const {
   return _index.data(role);
 }
 
-QRectF ChatItem::boundingRect() const {
-  return QRectF(0, 0, _width, _height);
-}
-
-void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
-  Q_UNUSED(option); Q_UNUSED(widget);
-
-  painter->drawText(boundingRect(), data(MessageModel::DisplayRole).toString());
-  painter->setPen(Qt::DotLine);
-  painter->drawRect(boundingRect());
-}
-
-
-
 int ChatItem::setWidth(int w) {
-  _width = w;
-  _height = 20; // FIXME
-  return _height;
+  w -= 10;
+  if(w == _boundingRect.width()) return _boundingRect.height();
+  int h = heightForWidth(w);
+  _boundingRect.setWidth(w);
+  _boundingRect.setHeight(h);
+  if(haveLayout()) updateLayout();
+  return h;
 }
 
-/*
+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;
+}
 
-void ChatItem::setTextOption(const QTextOption &option) {
-  _textOption = option;
-  layout();
+void ChatItem::layout() {
+  if(haveLayout()) 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);
+  updateLayout();
 }
 
-QTextOption ChatItem::textOption() const {
-  return _textOption;
+void ChatItem::updateLayout() {
+  if(!haveLayout()) layout();
+
+  // 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();
 }
 
-QString ChatItem::text() const {
-  return _layout.text();
+void ChatItem::clearLayout() {
+  delete _layout;
+  _layout = 0;
 }
 
-void ChatItem::setText(const UiStyle::StyledText &text) {
-  _layout.setText(text.text);
-  _layout.setAdditionalFormats(text.formatList);
+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());
+  painter->drawRect(boundingRect());
 }
 
+/*
 void ChatItem::layout() {
   if(!_layout.additionalFormats().count()) return; // no text set
   if(_width <= 0) return;
@@ -139,6 +184,3 @@ void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
   } else QGraphicsTextItem::mouseMoveEvent(event);
 }
 */
-
-
-