Rework selections to make them not fail with weird button combinations
authorManuel Nickschas <sputnick@quassel-irc.org>
Mon, 11 Aug 2008 21:56:45 +0000 (23:56 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Wed, 13 Aug 2008 10:24:28 +0000 (12:24 +0200)
src/qtui/chatitem.cpp
src/qtui/chatscene.cpp
src/qtui/chatscene.h

index 655d1fd..7a11a66 100644 (file)
@@ -239,7 +239,7 @@ void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
 }
 
 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
-  if(_selectionMode != NoSelection && event->buttons() == Qt::LeftButton) {
+  if(_selectionMode != NoSelection && !event->buttons() & Qt::LeftButton) {
     _selectionEnd = posToCursor(event->pos());
     QString selection
         = data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, _selectionEnd), qAbs(_selectionStart - _selectionEnd));
index 1af7e85..7ba7d39 100644 (file)
@@ -179,7 +179,7 @@ void ChatScene::setSelectingItem(ChatItem *item) {
 }
 
 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
-  _selectionStart = _selectionEnd = item->row();
+  _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
   _selectionStartCol = _selectionMinCol = item->column();
   _isSelecting = true;
   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
@@ -206,26 +206,31 @@ void ChatScene::updateSelection(const QPointF &pos) {
     }
   }
 
-  if(curRow > _selectionEnd && curRow > _selectionStart) {  // select further towards bottom
-    for(int l = _selectionEnd + 1; l <= curRow; l++) {
+  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);
-    }
-  } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
-    for(int l = _selectionEnd; l < curRow; l++) {
-      _lines[l]->setSelected(false);
-    }
-  } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
-    for(int l = _selectionEnd; l > curRow; l--) {
+  }
+  if(newstart > _selectionStart) {
+    for(int l = _selectionStart; l < newstart; l++)
       _lines[l]->setSelected(false);
-    }
-  } else if(curRow < _selectionEnd && curRow < _selectionStart) {
-    for(int l = _selectionEnd - 1; l >= curRow; l--) {
+  }
+  if(newend > _selectionEnd) {
+    for(int l = _selectionEnd+1; l <= newend; l++)
       _lines[l]->setSelected(true, minColumn);
-    }
   }
-  _selectionEnd = curRow;
+  if(newend < _selectionEnd) {
+    for(int l = newend+1; l <= _selectionEnd; l++)
+      _lines[l]->setSelected(false);
+  }
+
+  _selectionStart = newstart;
+  _selectionEnd = newend;
+  _lastSelectionRow = curRow;
 
-  if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
+  if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
     _lines[curRow]->setSelected(false);
     _isSelecting = false;
     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
@@ -254,7 +259,7 @@ void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
 }
 
 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
-  if(_isSelecting && event->buttons() == Qt::LeftButton) {
+  if(_isSelecting && !event->buttons() & Qt::LeftButton) {
 #   ifdef Q_WS_X11
       QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
 #   endif
index de6add9..17c009c 100644 (file)
@@ -97,6 +97,7 @@ class ChatScene : public QGraphicsScene {
     int _selectionStartCol, _selectionMinCol;
     int _selectionStart;
     int _selectionEnd;
+    int _firstSelectionRow, _lastSelectionRow;
     bool _isSelecting;
 
     bool _fetchingBacklog;