+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);
+
+ // clear selection
+ if (_selectingItem) {
+ int row = _selectingItem->row();
+ if (row >= start && row <= end)
+ setSelectingItem(nullptr);
+ }
+
+ // remove items from scene
+ QList<ChatLine*>::iterator lineIter = _lines.begin() + start;
+ int lineCount = start;
+ while (lineIter != _lines.end() && lineCount <= end) {
+ if ((*lineIter) == markerLine()->chatLine())
+ markerLine()->setChatLine(nullptr);
+ 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 selection
+ if (_selectionStart >= 0) {
+ int offset = end - start + 1;
+ if (_selectionStart >= start)
+ _selectionStart = qMax(_selectionStart - offset, start);
+ if (_selectionEnd >= start)
+ _selectionEnd -= offset;
+ if (_firstSelectionRow >= start)
+ _firstSelectionRow -= offset;
+
+ if (_selectionEnd < _selectionStart) {
+ _isSelecting = false;
+ _selectionStart = -1;
+ }
+ }
+
+ // neither removing at bottom or top means we have to move items...
+ if (!(atTop || atBottom)) {
+ qreal offset = h;
+ int moveStart = 0;
+ int moveEnd = _lines.count() - 1;
+ if (start < _lines.count() - start) {
+ // move top part
+ moveEnd = start - 1;
+ }
+ else {
+ // move bottom part
+ moveStart = start;
+ offset = -offset;
+ }
+ ChatLine* line = nullptr;
+ for (int i = moveStart; i <= moveEnd; i++) {
+ line = _lines.at(i);
+ line->setPos(0, line->pos().y() + offset);
+ }
+ }
+
+ Q_ASSERT(start == 0 || start >= _lines.count()
+ || _lines.at(start - 1)->pos().y() + _lines.at(start - 1)->height() == _lines.at(start)->pos().y());
+
+ // update sceneRect
+ // when searching for the first non-date-line we have to take into account that our
+ // model still contains the just removed lines so we cannot simply call updateSceneRect()
+ int numRows = model()->rowCount();
+ QModelIndex firstLineIdx;
+ _firstLineRow = -1;
+ bool needOffset = false;
+ do {
+ _firstLineRow++;
+ if (_firstLineRow >= start && _firstLineRow <= end) {
+ _firstLineRow = end + 1;
+ needOffset = true;
+ }
+ firstLineIdx = model()->index(_firstLineRow, 0);
+ } while ((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) == Message::DayChange && _firstLineRow < numRows);
+
+ if (needOffset)
+ _firstLineRow -= end - start + 1;
+ updateSceneRect();