Adding a Q_ASSERT to see if that catches the remaining selection segfaults
[quassel.git] / src / qtui / chatscene.cpp
index 7ba7d39..33a13c5 100644 (file)
@@ -44,7 +44,6 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject
     _model(model),
     _singleBufferScene(false),
     _selectingItem(0),
-    _lastItem(0),
     _selectionStart(-1),
     _isSelecting(false),
     _fetchingBacklog(false)
@@ -113,6 +112,15 @@ void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
     _lines[i]->setRow(i);
   }
 
+  // update selection
+  if(_selectionStart >= 0) {
+    int offset = end - start + 1;
+    if(_selectionStart >= start) _selectionStart += offset;
+    if(_selectionEnd >= start) _selectionEnd += offset;
+    if(_firstSelectionRow >= start) _firstSelectionRow += offset;
+    if(_lastSelectionRow >= start) _lastSelectionRow += offset;
+  }
+
   if(h > 0) {
     _height += h;
     for(int i = end+1; i < _lines.count(); i++) {
@@ -205,10 +213,8 @@ void ChatScene::updateSelection(const QPointF &pos) {
       _lines[l]->setSelected(true, minColumn);
     }
   }
-
   int newstart = qMin(curRow, _firstSelectionRow);
   int newend = qMax(curRow, _firstSelectionRow);
-
   if(newstart < _selectionStart) {
     for(int l = newstart; l < _selectionStart; l++)
       _lines[l]->setSelected(true, minColumn);
@@ -233,6 +239,8 @@ void ChatScene::updateSelection(const QPointF &pos) {
   if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
     _lines[curRow]->setSelected(false);
     _isSelecting = false;
+    Q_ASSERT(_selectingItem); // this seems to not always be true, but I have no idea why
+                              // adding this assert to make sure the occasional segfault is caused by this
     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
   }
 }
@@ -260,12 +268,7 @@ void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
 
 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
   if(_isSelecting && !event->buttons() & Qt::LeftButton) {
-#   ifdef Q_WS_X11
-      QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
-#   endif
-//# else
-      QApplication::clipboard()->setText(selectionToString());
-//# endif
+    putToClipboard(selectionToString());
     _isSelecting = false;
     event->accept();
   } else {
@@ -273,6 +276,16 @@ void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
   }
 }
 
+void ChatScene::putToClipboard(const QString &selection) {
+  // TODO Configure clipboards
+#   ifdef Q_WS_X11
+  QApplication::clipboard()->setText(selection, QClipboard::Selection);
+#   endif
+//# else
+  QApplication::clipboard()->setText(selection);
+//# endif
+}
+
 //!\brief Convert current selection to human-readable string.
 QString ChatScene::selectionToString() const {
   //TODO Make selection format configurable!