Compute correct height of ChatItems.
[quassel.git] / src / qtui / chatitem.cpp
index 08d9f11..568cdcc 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 <QtGui>
 
 #include "chatitem.h"
+#include "chatlinemodel.h"
+#include "qtui.h"
 
-ChatItem::ChatItem(QGraphicsItem *parent) : QGraphicsItem(parent) {
-  _width = 0;
-  //if(_wrapMode == WordWrap) {
-  //  setFlags(QGraphicsItem::ItemClipsToShape, true);
-  //}
+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();
 }
 
 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);
+}
+
+/*
+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) {
+  if(w == _boundingRect.width()) return _boundingRect.height();
+  int h = heightForWidth(w);
+  _boundingRect.setWidth(w);
+  _boundingRect.setHeight(h);
+  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();
@@ -58,7 +106,7 @@ QString ChatItem::text() const {
 
 void ChatItem::setText(const UiStyle::StyledText &text) {
   _layout.setText(text.text);
-  _layout.setAdditionalFormats(text.formats);
+  _layout.setAdditionalFormats(text.formatList);
   layout();
 }
 
@@ -91,7 +139,9 @@ void ChatItem::layout() {
   }
   _layout.endLayout();
   update();
-}
+}    QDateTime _timestamp;
+    MsgId _msgId;
+
 
 QRectF ChatItem::boundingRect() const {
   return _layout.boundingRect();
@@ -102,6 +152,7 @@ void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
   _layout.draw(painter, QPointF(0, 0));
 
 }
+*/
 
 /*
 void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {