-void ChatScene::modelReset() {
- foreach(ChatLine *line, _lines) {
- removeItem(line);
- delete line;
+void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
+ Q_UNUSED(parent);
+
+ qreal h = 0; // total height of removed items;
+
+ bool atTop = (start == 0);
+ bool atBottom = (end == _lines.count() - 1);
+
+ // right now this method doesn't properly handle messages that are removed at an arbitrary point
+ Q_ASSERT(atBottom || atTop);
+
+ // remove items from scene
+ QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
+ int lineCount = start;
+ while(lineIter != _lines.end() && lineCount <= end) {
+ h += (*lineIter)->height();
+ delete *lineIter;
+ lineIter = _lines.erase(lineIter);
+ lineCount++;
+ }
+
+ // update rows of remaining chatlines
+ for(int i = start; i < _lines.count(); i++) {
+ _lines.at(i)->setRow(i);
+ }
+
+ // update sceneRect
+ if(atBottom) {
+ setSceneRect(sceneRect().adjusted(0, 0, 0, -h));
+ } else {
+ setSceneRect(sceneRect().adjusted(0, h, 0, 0));