Put selection into clipboard - output format config and context menu to follow later
[quassel.git] / src / qtui / chatscene.cpp
index 797290d..8207248 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,6 +41,7 @@ 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)));
@@ -211,12 +214,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 +240,17 @@ 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 result;
+  for(int l = _selectionStart; l <= _selectionEnd; 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;
+}