Tweak ChatItem/ChatLine layouting
[quassel.git] / src / qtui / chatscene.cpp
index bcfea00..b159dc9 100644 (file)
@@ -382,10 +382,7 @@ void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int e
 }
 
 void ChatScene::dataChanged(const QModelIndex &tl, const QModelIndex &br) {
-  // This should only be sent (currently) if the style is reloaded -> re-layout the whole scene
-  // TODO: Check range and only do partial relayouts, if appropriate
-
-  layout();
+  layout(tl.row(), br.row(), _sceneRect.width());
 }
 
 void ChatScene::updateForViewport(qreal width, qreal height) {
@@ -396,27 +393,37 @@ void ChatScene::updateForViewport(qreal width, qreal height) {
 void ChatScene::setWidth(qreal width) {
   if(width == _sceneRect.width())
     return;
-  layout(width);
+  layout(0, _lines.count()-1, width);
 }
 
-void ChatScene::layout(qreal width) {
+void ChatScene::layout(int start, int end, qreal width) {
   // clock_t startT = clock();
 
-  if(width < 0)
-    width = _sceneRect.width();
-
   // disabling the index while doing this complex updates is about
   // 2 to 10 times faster!
   //setItemIndexMethod(QGraphicsScene::NoIndex);
 
-  QList<ChatLine *>::iterator lineIter = _lines.end();
-  QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
-  qreal linePos = _sceneRect.y() + _sceneRect.height();
-  qreal contentsWidth = width - secondColumnHandle()->sceneRight();
-  while(lineIter != lineIterBegin) {
-    lineIter--;
-    (*lineIter)->setGeometryByWidth(width, contentsWidth, linePos);
+  if(end >= 0) {
+    int row = end;
+    qreal linePos = _lines.at(row)->scenePos().y() + _lines.at(row)->height();
+    qreal contentsWidth = width - secondColumnHandle()->sceneRight();
+    while(row >= start) {
+      _lines.at(row--)->setGeometryByWidth(width, contentsWidth, linePos);
+    }
+
+    if(row >= 0) {
+      // remaining items don't need geometry changes, but maybe repositioning?
+      ChatLine *line = _lines.at(row);
+      qreal offset = linePos - (line->scenePos().y() + line->height());
+      if(offset != 0) {
+        while(row >= 0) {
+          line = _lines.at(row--);
+          line->setPos(0, line->scenePos().y() + offset);
+        }
+      }
+    }
   }
+
   //setItemIndexMethod(QGraphicsScene::BspTreeIndex);
 
   updateSceneRect(width);