Properly determine if mouse cursor is over selection in all cases
[quassel.git] / src / qtui / chatscene.cpp
index dc38854..af6ddc3 100644 (file)
@@ -22,6 +22,7 @@
 #include <QClipboard>
 #include <QDrag>
 #include <QGraphicsSceneMouseEvent>
+#include <QMenu>
 #include <QPersistentModelIndex>
 #include <QWebView>
 
@@ -32,6 +33,7 @@
 #include "client.h"
 #include "clientbacklogmanager.h"
 #include "columnhandleitem.h"
+#include "iconloader.h"
 #include "messagefilter.h"
 #include "qtui.h"
 #include "qtuistyle.h"
@@ -553,7 +555,7 @@ bool ChatScene::isPosOverSelection(const QPointF &pos) const {
   if(hasGlobalSelection()) {
     int row = chatItem->row();
     if(row >= qMin(_selectionStart, _selectionEnd) && row <= qMax(_selectionStart, _selectionEnd))
-      return true;
+      return columnByScenePos(pos) >= _selectionMinCol;
   } else {
     return chatItem->isPosOverSelection(chatItem->mapFromScene(pos));
   }
@@ -571,6 +573,19 @@ bool ChatScene::isScrollingAllowed() const {
 
 /******** MOUSE HANDLING **************************************************************************/
 
+void ChatScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
+  QPointF pos = event->scenePos();
+  QMenu menu;
+
+  if(isPosOverSelection(pos))
+    menu.addAction(SmallIcon("edit-copy"), tr("Copy Selection"),
+                    this, SLOT(selectionToClipboard()),
+                    QKeySequence::Copy);
+
+  menu.exec(event->screenPos());
+
+}
+
 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
   if(event->buttons() == Qt::LeftButton) {
     if(!_clickHandled && (event->scenePos() - _clickPos).toPoint().manhattanLength() >= QApplication::startDragDistance()) {
@@ -635,7 +650,7 @@ void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
     } else {
       // no click -> drag or selection move
       if(isGloballySelecting()) {
-        putToClipboard(selection());
+        selectionToClipboard(QClipboard::Selection);
         _isSelecting = false;
         event->accept();
         return;
@@ -660,9 +675,6 @@ void ChatScene::handleClick(Qt::MouseButton button, const QPointF &scenePos) {
       chatItem->handleClick(chatItem->mapFromScene(scenePos), _clickMode);
     }
     _clickHandled = true;
-  } else if(button == Qt::RightButton) {
-    // TODO: context menu
-
   }
 }
 
@@ -677,14 +689,21 @@ void ChatScene::initiateDrag(QWidget *source) {
 
 /******** SELECTIONS ******************************************************************************/
 
-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
+void ChatScene::selectionToClipboard(QClipboard::Mode mode) {
+  if(!hasSelection())
+    return;
+
+  switch(mode) {
+    case QClipboard::Clipboard:
+      QApplication::clipboard()->setText(selection());
+      break;
+    case QClipboard::Selection:
+      if(QApplication::clipboard()->supportsSelection())
+        QApplication::clipboard()->setText(selection(), QClipboard::Selection);
+      break;
+    default:
+      break;
+  };
 }
 
 //!\brief Convert current selection to human-readable string.
@@ -711,6 +730,18 @@ QString ChatScene::selection() const {
   return QString();
 }
 
+bool ChatScene::hasSelection() const {
+  return hasGlobalSelection() || (selectingItem() && selectingItem()->hasSelection());
+}
+
+bool ChatScene::hasGlobalSelection() const {
+  return _selectionStart >= 0;
+}
+
+bool ChatScene::isGloballySelecting() const {
+  return _isSelecting;
+}
+
 void ChatScene::clearGlobalSelection() {
   if(hasGlobalSelection()) {
     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++)
@@ -735,7 +766,7 @@ void ChatScene::requestBacklog() {
   return;
 }
 
-ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) {
+ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) const {
   if(x < _firstColHandle->x())
     return ChatLineModel::TimestampColumn;
   if(x < _secondColHandle->x())
@@ -744,7 +775,7 @@ ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) {
   return ChatLineModel::ContentsColumn;
 }
 
-int ChatScene::rowByScenePos(qreal y) {
+int ChatScene::rowByScenePos(qreal y) const {
   // This is somewhat hacky... we look at the contents item that is at the given y position, since
   // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
   // ChatItems cover their ChatLine, so we won't get to the latter directly.