]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistcontroller.cpp
Introduce "isExpandable" role
[dolphin.git] / src / kitemviews / kitemlistcontroller.cpp
index e77de292ae48c2edb4df098fc816059b50a95d8d..8577bf8630ae0eb033be5ca748c457f6c4e3a14c 100644 (file)
@@ -30,7 +30,9 @@
 #include <QApplication>
 #include <QDrag>
 #include <QEvent>
+#include <QGraphicsScene>
 #include <QGraphicsSceneEvent>
+#include <QGraphicsView>
 #include <QMimeData>
 #include <QTimer>
 
@@ -186,6 +188,16 @@ bool KItemListController::keyPressEvent(QKeyEvent* event)
         default:            break;
         }
     }
+    
+    const bool selectSingleItem = itemCount == 1 &&
+                                  (key == Qt::Key_Home || key == Qt::Key_End  ||
+                                   key == Qt::Key_Up   || key == Qt::Key_Down ||
+                                   key == Qt::Key_Left || key == Qt::Key_Right);
+    if (selectSingleItem) {
+        const int current = m_selectionManager->currentItem();
+        m_selectionManager->setSelected(current);
+        return true;
+    }
 
     switch (key) {
     case Qt::Key_Home:
@@ -218,8 +230,7 @@ bool KItemListController::keyPressEvent(QKeyEvent* event)
         if (index + itemsPerRow < itemCount) {
             // We are not in the last row yet.
             index += itemsPerRow;
-        }
-        else {
+        } else {
             // We are either in the last row already, or we are in the second-last row,
             // and there is no item below the current item.
             // In the latter case, we jump to the very last item.
@@ -250,8 +261,34 @@ bool KItemListController::keyPressEvent(QKeyEvent* event)
             m_selectionManager->endAnchoredSelection();
             m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
             m_selectionManager->beginAnchoredSelection(index);
-            break;
+        } else {
+            const int current = m_selectionManager->currentItem();
+            m_selectionManager->setSelected(current);
+        }
+        break;
+
+    case Qt::Key_Menu: {
+        // Emit the signal itemContextMenuRequested() in case if at least one
+        // item is selected. Otherwise the signal viewContextMenuRequested() will be emitted.
+        const QSet<int> selectedItems = m_selectionManager->selectedItems();
+        int index = -1;
+        if (selectedItems.count() >= 2) {
+            const int currentItemIndex = m_selectionManager->currentItem();
+            index = selectedItems.contains(currentItemIndex)
+                    ? currentItemIndex : selectedItems.toList().first();
+        } else if (selectedItems.count() == 1) {
+            index = selectedItems.toList().first();
+        }
+
+        if (index >= 0) {
+            const QRectF contextRect = m_view->itemContextRect(index);
+            const QPointF pos(m_view->scene()->views().first()->mapToGlobal(contextRect.bottomRight().toPoint()));
+            emit itemContextMenuRequested(index, pos);
+        } else {
+            emit viewContextMenuRequested(QCursor::pos());
         }
+        break;
+    }
 
     default:
         m_keyboardManager->addKeys(event->text());
@@ -288,8 +325,7 @@ void KItemListController::slotChangeCurrentItem(const QString& text, bool search
     int index;
     if (searchFromNextItem) {
         index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count());
-    }
-    else {
+    } else {
         index = m_model->indexForKeyboardSearch(text, currentIndex);
     }
     if (index >= 0) {
@@ -297,6 +333,7 @@ void KItemListController::slotChangeCurrentItem(const QString& text, bool search
         m_selectionManager->clearSelection();
         m_selectionManager->setSelected(index, 1);
         m_selectionManager->beginAnchoredSelection(index);
+        m_view->scrollToItem(index);
     }
 }
 
@@ -335,6 +372,9 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const
 
     m_pressedMousePos = transform.map(event->pos());
     m_pressedIndex = m_view->itemAt(m_pressedMousePos);
+    if (m_pressedIndex >= 0) {
+        emit itemPressed(m_pressedIndex, event->button());
+    }
 
     if (m_view->isAboveExpansionToggle(m_pressedIndex, m_pressedMousePos)) {
         m_selectionManager->setCurrentItem(m_pressedIndex);
@@ -344,7 +384,11 @@ bool KItemListController::mousePressEvent(QGraphicsSceneMouseEvent* event, const
     m_selectionTogglePressed = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos);
     if (m_selectionTogglePressed) {
         m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
+        // The previous anchored selection has been finished already in
+        // KItemListSelectionManager::setSelected(). We can safely change
+        // the current item and start a new anchored selection now.
         m_selectionManager->setCurrentItem(m_pressedIndex);
+        m_selectionManager->beginAnchoredSelection(m_pressedIndex);
         return true;
     }
 
@@ -497,6 +541,10 @@ bool KItemListController::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, con
         return false;
     }
 
+    if (m_pressedIndex >= 0) {
+        emit itemReleased(m_pressedIndex, event->button());
+    }
+
     const bool isAboveSelectionToggle = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos);
     if (isAboveSelectionToggle) {
         m_selectionTogglePressed = false;