-void ChatScene::setWidth(qreal w) {
- _width = w;
- _height = 0;
- foreach(ChatLine *line, _lines) {
- line->setPos(0, _height);
- _height += line->setGeometry(_width);
+void ChatScene::updateForViewport(qreal width, qreal height) {
+ _viewportHeight = height;
+ setWidth(width);
+}
+
+// setWidth is used for 2 things:
+// a) updating the scene to fit the width of the corresponding view
+// b) to update the positions of the items if a columhandle has changed it's position
+// forceReposition is true in the second case
+// this method features some codeduplication for the sake of performance
+void ChatScene::setWidth(qreal width, bool forceReposition) {
+ if(width == _sceneRect.width() && !forceReposition)
+ return;
+
+// clock_t startT = clock();
+
+ qreal linePos = _sceneRect.y() + _sceneRect.height();
+ qreal yBottom = linePos;
+ QList<ChatLine *>::iterator lineIter = _lines.end();
+ QList<ChatLine *>::iterator lineIterBegin = _lines.begin();
+ ChatLine *line = 0;
+ qreal lineHeight = 0;
+ qreal contentsWidth = width - secondColumnHandle()->sceneRight();
+
+ if(forceReposition) {
+ qreal timestampWidth = firstColumnHandle()->sceneLeft();
+ qreal senderWidth = secondColumnHandle()->sceneLeft() - firstColumnHandle()->sceneRight();
+ QPointF senderPos(firstColumnHandle()->sceneRight(), 0);
+ QPointF contentsPos(secondColumnHandle()->sceneRight(), 0);
+ while(lineIter != lineIterBegin) {
+ lineIter--;
+ line = *lineIter;
+ lineHeight = line->setColumns(timestampWidth, senderWidth, contentsWidth, senderPos, contentsPos);
+ linePos -= lineHeight;
+ line->setPos(0, linePos);
+ }
+ } else {
+ while(lineIter != lineIterBegin) {
+ lineIter--;
+ line = *lineIter;
+ lineHeight = line->setGeometryByWidth(width, contentsWidth);
+ linePos -= lineHeight;
+ line->setPos(0, linePos);
+ }