Fixing selections, also prevent from crashing in some circumstances
[quassel.git] / src / qtui / chatscene.cpp
index 797290d..8f36e1d 100644 (file)
@@ -18,6 +18,8 @@
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#include <QApplication>
+#include <QClipboard>
 #include <QGraphicsSceneMouseEvent>
 #include <QPersistentModelIndex>
 
@@ -39,9 +41,11 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject
   _width = 0;
   _selectingItem = 0;
   _isSelecting = false;
+  _selectionStart = -1;
   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
 
   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
+  connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset()));
   for(int i = 0; i < model->rowCount(); i++) {
     ChatLine *line = new ChatLine(model->index(i, 0));
     _lines.append(line);
@@ -73,7 +77,6 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject
 
 ChatScene::~ChatScene() {
 
-
 }
 
 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
@@ -103,6 +106,15 @@ void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
   }
 }
 
+void ChatScene::modelReset() {
+  foreach(ChatLine *line, _lines) {
+    removeItem(line);
+    delete line;
+  }
+  _lines.clear();
+  setSceneRect(QRectF(0, 0, _width, 0));
+}
+
 void ChatScene::setWidth(qreal w) {
   _width = w;
   _height = 0;
@@ -211,12 +223,25 @@ void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
 }
 
 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
-  qDebug() << "pressed";
-  QGraphicsScene::mousePressEvent(event);
+  if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
+    for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
+      _lines[l]->setSelected(false);
+    }
+    _selectionStart = -1;
+    event->accept();
+  } else {
+    QGraphicsScene::mousePressEvent(event);
+  }
 }
 
 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
   if(_isSelecting) {
+#   ifdef Q_WS_X11
+      QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
+#   endif
+//# else
+      QApplication::clipboard()->setText(selectionToString());
+//# endif
     _isSelecting = false;
     event->accept();
   } else {
@@ -224,3 +249,23 @@ void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
   }
 }
 
+//!\brief Convert current selection to human-readable string.
+QString ChatScene::selectionToString() const {
+  //TODO Make selection format configurable!
+  if(!_isSelecting) return QString();
+  int start = qMin(_selectionStart, _selectionEnd);
+  int end = qMax(_selectionStart, _selectionEnd);
+  if(start < 0 || end >= _lines.count()) {
+    qDebug() << "Invalid selection range:" << start << end;
+    return QString();
+  }
+  QString result;
+  for(int l = start; l <= end; l++) {
+    if(_selectionMinCol == ChatLineModel::TimestampColumn)
+      result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " ";
+    if(_selectionMinCol <= ChatLineModel::SenderColumn)
+      result += _lines[l]->item(ChatLineModel::SenderColumn)->data(MessageModel::DisplayRole).toString() + " ";
+    result += _lines[l]->item(ChatLineModel::ContentsColumn)->data(MessageModel::DisplayRole).toString() + "\n";
+  }
+  return result;
+}