Update INSTALL.cmake
[quassel.git] / src / qtui / chatitem.cpp
index c12d5c5..17189f9 100644 (file)
@@ -47,10 +47,12 @@ QVariant ChatItem::data(int role) const {
 }
 
 int ChatItem::setWidth(int w) {
+  w -= 10;
   if(w == _boundingRect.width()) return _boundingRect.height();
   int h = heightForWidth(w);
   _boundingRect.setWidth(w);
   _boundingRect.setHeight(h);
+  if(haveLayout()) updateLayout();
   return h;
 }
 
@@ -58,25 +60,27 @@ 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();
+  ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
   int lines = 1;
-  int offset = 0;
-  for(int i = 0; i < wrapList.count(); i+=2) {
-    if(wrapList.at(i+1).toUInt() - offset < width) continue;
+  qreal w = 0;
+  for(int i = 0; i < wrapList.count(); i++) {
+    w += wrapList.at(i).width;
+    if(w <= width) {
+      w += wrapList.at(i).trailing;
+      continue;
+    }
     lines++;
-    if(i > 0) {
-      if(offset < wrapList.at(i-1).toUInt()) offset = wrapList.at(i-1).toUInt();
-      else offset += width;
-    } else {
-      offset += width;
+    w = wrapList.at(i).width;
+    while(w >= width) {
+      lines++;
+      w -= width;
     }
-    i-=2;
   }
   return lines * _lineHeight;
 }
 
 void ChatItem::layout() {
-  if(_layout) return;
+  if(haveLayout()) return;
   _layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
 
   // Convert format information into a FormatRange
@@ -92,6 +96,11 @@ void ChatItem::layout() {
   }
   if(i > 0) formatRanges.last().length = _layout->text().length() - formatRanges.last().start;
   _layout->setAdditionalFormats(formatRanges);
+  updateLayout();
+}
+
+void ChatItem::updateLayout() {
+  if(!haveLayout()) layout();
 
   // Now layout
   qreal h = 0;
@@ -118,6 +127,13 @@ void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
   Q_UNUSED(option); Q_UNUSED(widget);
   layout();
   _layout->draw(painter, QPointF(0,0), QVector<QTextLayout::FormatRange>(), boundingRect());
+  painter->drawRect(boundingRect());
+  int width = 0;
+  QVariantList wrapList = data(ChatLineModel::WrapListRole).toList();
+  for(int i = 2; i < wrapList.count(); i+=2) {
+    QRect r(wrapList[i-1].toUInt(), 0, wrapList[i+1].toUInt() - wrapList[i-1].toUInt(), _lineHeight);
+    painter->drawRect(r);
+  }
 }
 
 /*