#include "kitemlistcontroller.h"
-#include <KGlobalSettings>
-#include <KDebug>
-
-#include "kitemlistview.h"
#include "kitemlistselectionmanager.h"
-
-#include "private/kitemlistrubberband.h"
+#include "kitemlistview.h"
#include "private/kitemlistkeyboardsearchmanager.h"
+#include "private/kitemlistrubberband.h"
+#include "views/draganddrophelper.h"
+#include <QAccessible>
#include <QApplication>
#include <QDrag>
-#include <QEvent>
#include <QGraphicsScene>
#include <QGraphicsSceneEvent>
#include <QGraphicsView>
#include <QMimeData>
#include <QTimer>
-#include <QAccessible>
KItemListController::KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent) :
QObject(parent),
m_selectionBehavior(NoSelection),
m_autoActivationBehavior(ActivationAndExpansion),
m_mouseDoubleClickAction(ActivateItemOnly),
- m_model(0),
- m_view(0),
+ m_model(nullptr),
+ m_view(nullptr),
m_selectionManager(new KItemListSelectionManager(this)),
m_keyboardManager(new KItemListKeyboardSearchManager(this)),
m_pressedIndex(-1),
m_pressedMousePos(),
- m_autoActivationTimer(0),
+ m_autoActivationTimer(nullptr),
m_oldSelection(),
m_keyboardAnchorIndex(-1),
m_keyboardAnchorPos(0)
{
- connect(m_keyboardManager, SIGNAL(changeCurrentItem(QString,bool)),
- this, SLOT(slotChangeCurrentItem(QString,bool)));
- connect(m_selectionManager, SIGNAL(currentChanged(int,int)),
- m_keyboardManager, SLOT(slotCurrentChanged(int,int)));
+ connect(m_keyboardManager, &KItemListKeyboardSearchManager::changeCurrentItem,
+ this, &KItemListController::slotChangeCurrentItem);
+ connect(m_selectionManager, &KItemListSelectionManager::currentChanged,
+ m_keyboardManager, &KItemListKeyboardSearchManager::slotCurrentChanged);
+ connect(m_selectionManager, &KItemListSelectionManager::selectionChanged,
+ m_keyboardManager, &KItemListKeyboardSearchManager::slotSelectionChanged);
m_autoActivationTimer = new QTimer(this);
m_autoActivationTimer->setSingleShot(true);
m_autoActivationTimer->setInterval(-1);
- connect(m_autoActivationTimer, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
+ connect(m_autoActivationTimer, &QTimer::timeout, this, &KItemListController::slotAutoActivationTimeout);
setModel(model);
setView(view);
KItemListController::~KItemListController()
{
- setView(0);
+ setView(nullptr);
Q_ASSERT(!m_view);
- setModel(0);
+ setModel(nullptr);
Q_ASSERT(!m_model);
}
KItemListView* oldView = m_view;
if (oldView) {
- disconnect(oldView, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(slotViewScrollOffsetChanged(qreal,qreal)));
+ disconnect(oldView, &KItemListView::scrollOffsetChanged, this, &KItemListController::slotViewScrollOffsetChanged);
oldView->deleteLater();
}
m_view->setParent(this);
m_view->setController(this);
m_view->setModel(m_model);
- connect(m_view, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(slotViewScrollOffsetChanged(qreal,qreal)));
+ connect(m_view, &KItemListView::scrollOffsetChanged, this, &KItemListController::slotViewScrollOffsetChanged);
updateExtendedSelectionRegion();
}
return m_mouseDoubleClickAction;
}
+int KItemListController::indexCloseToMousePressedPosition() const
+{
+ QHashIterator<KItemListWidget*, KItemListGroupHeader*> it(m_view->m_visibleGroups);
+ while (it.hasNext()) {
+ it.next();
+ KItemListGroupHeader *groupHeader = it.value();
+ const QPointF mappedToGroup = groupHeader->mapFromItem(nullptr, m_pressedMousePos);
+ if (groupHeader->contains(mappedToGroup)) {
+ return it.key()->index();
+ }
+ }
+ return -1;
+}
+
void KItemListController::setAutoActivationDelay(int delay)
{
m_autoActivationTimer->setInterval(delay);
return m_singleClickActivationEnforced;
}
-bool KItemListController::showEvent(QShowEvent* event)
-{
- Q_UNUSED(event);
- return false;
-}
-
-bool KItemListController::hideEvent(QHideEvent* event)
-{
- Q_UNUSED(event);
- return false;
-}
-
bool KItemListController::keyPressEvent(QKeyEvent* event)
{
int index = m_selectionManager->currentItem();
const bool shiftPressed = event->modifiers() & Qt::ShiftModifier;
const bool controlPressed = event->modifiers() & Qt::ControlModifier;
const bool shiftOrControlPressed = shiftPressed || controlPressed;
+ const bool navigationPressed = key == Qt::Key_Home || key == Qt::Key_End ||
+ key == Qt::Key_PageUp || key == Qt::Key_PageDown ||
+ key == Qt::Key_Up || key == Qt::Key_Down ||
+ key == Qt::Key_Left || key == Qt::Key_Right;
const int itemCount = m_model->count();
}
}
- const bool selectSingleItem = m_selectionBehavior != NoSelection &&
- 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);
+ const bool selectSingleItem = m_selectionBehavior != NoSelection && itemCount == 1 && navigationPressed;
+
if (selectSingleItem) {
const int current = m_selectionManager->currentItem();
m_selectionManager->setSelected(current);
}
}
}
- // Fall through to the default case and add the Space to the current search string.
-
+ Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
default:
m_keyboardManager->addKeys(event->text());
// Make sure unconsumed events get propagated up the chain. #302329
}
break;
}
+ }
+ if (navigationPressed) {
m_view->scrollToItem(index);
}
return true;
if (!m_model || m_model->count() == 0) {
return;
}
- const int currentIndex = m_selectionManager->currentItem();
int index;
if (searchFromNextItem) {
+ const int currentIndex = m_selectionManager->currentItem();
index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count());
} else {
- index = m_model->indexForKeyboardSearch(text, currentIndex);
+ index = m_model->indexForKeyboardSearch(text, 0);
}
if (index >= 0) {
m_selectionManager->setCurrentItem(index);
if (m_selectionBehavior != NoSelection) {
- m_selectionManager->clearSelection();
- m_selectionManager->setSelected(index, 1);
+ m_selectionManager->replaceSelection(index);
m_selectionManager->beginAnchoredSelection(index);
}
bool KItemListController::inputMethodEvent(QInputMethodEvent* event)
{
- Q_UNUSED(event);
+ Q_UNUSED(event)
return false;
}
m_pressedIndex = m_view->itemAt(m_pressedMousePos);
emit mouseButtonPressed(m_pressedIndex, event->buttons());
- if ((event->buttons() & (Qt::XButton1 | Qt::XButton2)) && m_pressedIndex < 0) {
- // Do not select items when clicking the empty part of the view with
- // the back/forward buttons, see https://bugs.kde.org/show_bug.cgi?id=327412.
- // Note that clicking an item with these buttons selects it, see comment in
- // DolphinView::slotMouseButtonPressed(int, Qt::MouseButtons).
+ if (event->buttons() & (Qt::BackButton | Qt::ForwardButton)) {
+ // Do not select items when clicking the back/forward buttons, see
+ // https://bugs.kde.org/show_bug.cgi?id=327412.
return true;
}
// -> remember that the user pressed an item which had been selected already and
// clear the selection in mouseReleaseEvent(), unless the items are dragged.
m_clearSelectionIfItemsAreNotDragged = true;
+
+ if (m_selectionManager->selectedItems().count() == 1 && m_view->isAboveText(m_pressedIndex, m_pressedMousePos)) {
+ emit selectedItemTextPressed(m_pressedIndex);
+ }
}
if (!shiftPressed) {
m_selectionManager->endAnchoredSelection();
}
+ if (event->buttons() & Qt::RightButton) {
+ // Stop rubber band from persisting after right-clicks
+ KItemListRubberBand* rubberBand = m_view->rubberBand();
+ if (rubberBand->isActive()) {
+ disconnect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
+ rubberBand->setActive(false);
+ m_view->setAutoScroll(false);
+ }
+ }
+
if (m_pressedIndex >= 0) {
m_selectionManager->setCurrentItem(m_pressedIndex);
rubberBand->setStartPosition(startPos);
rubberBand->setEndPosition(startPos);
rubberBand->setActive(true);
- connect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged()));
+ connect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
m_view->setAutoScroll(true);
}
KItemListRubberBand* rubberBand = m_view->rubberBand();
if (rubberBand->isActive()) {
- disconnect(rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandChanged()));
+ disconnect(rubberBand, &KItemListRubberBand::endPositionChanged, this, &KItemListController::slotRubberBandChanged);
rubberBand->setActive(false);
m_oldSelection.clear();
m_view->setAutoScroll(false);
} else if (shiftOrControlPressed) {
// The mouse click should only update the selection, not trigger the item
emitItemActivated = false;
- } else if (!(KGlobalSettings::singleClick() || m_singleClickActivationEnforced)) {
+ } else if (!(m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced)) {
emitItemActivated = false;
}
if (emitItemActivated) {
}
}
- bool emitItemActivated = !(KGlobalSettings::singleClick() || m_singleClickActivationEnforced) &&
+ if (event->button() & Qt::RightButton) {
+ m_selectionManager->clearSelection();
+ if (index >= 0) {
+ m_selectionManager->setSelected(index);
+ emit itemContextMenuRequested(index, event->screenPos());
+ } else {
+ const QRectF headerBounds = m_view->headerBoundaries();
+ if (headerBounds.contains(event->pos())) {
+ emit headerContextMenuRequested(event->screenPos());
+ } else {
+ emit viewContextMenuRequested(event->screenPos());
+ }
+ }
+ return true;
+ }
+
+ bool emitItemActivated = !(m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) || m_singleClickActivationEnforced) &&
(event->button() & Qt::LeftButton) &&
index >= 0 && index < m_model->count();
if (emitItemActivated) {
bool KItemListController::dragEnterEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
{
- Q_UNUSED(event);
- Q_UNUSED(transform);
+ Q_UNUSED(event)
+ Q_UNUSED(transform)
+
+ DragAndDropHelper::clearUrlListMatchesUrlCache();
+
return false;
}
bool KItemListController::dragLeaveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
{
- Q_UNUSED(event);
- Q_UNUSED(transform);
+ Q_UNUSED(event)
+ Q_UNUSED(transform)
+ m_autoActivationTimer->stop();
m_view->setAutoScroll(false);
m_view->hideDropIndicator();
return false;
}
- event->acceptProposedAction();
+ QUrl hoveredDir = m_model->directory();
KItemListWidget* oldHoveredWidget = hoveredWidget();
const QPointF pos = transform.map(event->pos());
}
const int index = newHoveredWidget->index();
+
+ if (m_model->isDir(index)) {
+ hoveredDir = m_model->url(index);
+ }
+
if (!droppingBetweenItems) {
if (m_model->supportsDropping(index)) {
// Something has been dragged on an item.
m_view->hideDropIndicator();
}
+ event->setAccepted(!DragAndDropHelper::urlListMatchesUrl(event->mimeData()->urls(), hoveredDir));
+
return false;
}
// Something has been dropped between two items.
m_view->hideDropIndicator();
emit aboveItemDropEvent(dropAboveIndex, event);
- } else {
+ } else if (!event->mimeData()->hasFormat(m_model->blacklistItemDropEventMimeType())) {
// Something has been dropped on an item or on an empty part of the view.
emit itemDropEvent(m_view->itemAt(pos), event);
}
- QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropEnd);
+ QAccessibleEvent accessibilityEvent(view(), QAccessible::DragDropEnd);
+ QAccessible::updateAccessibility(&accessibilityEvent);
return true;
}
bool KItemListController::hoverEnterEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
{
- Q_UNUSED(event);
- Q_UNUSED(transform);
+ Q_UNUSED(event)
+ Q_UNUSED(transform)
return false;
}
bool KItemListController::hoverMoveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
{
- Q_UNUSED(transform);
+ Q_UNUSED(transform)
if (!m_model || !m_view) {
return false;
}
if (newHoveredWidget) {
newHoveredWidget->setHovered(true);
+ const QPointF mappedPos = newHoveredWidget->mapFromItem(m_view, pos);
+ newHoveredWidget->setHoverPosition(mappedPos);
emit itemHovered(newHoveredWidget->index());
}
+ } else if (oldHoveredWidget) {
+ const QPointF mappedPos = oldHoveredWidget->mapFromItem(m_view, pos);
+ oldHoveredWidget->setHoverPosition(mappedPos);
}
return false;
bool KItemListController::hoverLeaveEvent(QGraphicsSceneHoverEvent* event, const QTransform& transform)
{
- Q_UNUSED(event);
- Q_UNUSED(transform);
+ Q_UNUSED(event)
+ Q_UNUSED(transform)
if (!m_model || !m_view) {
return false;
bool KItemListController::wheelEvent(QGraphicsSceneWheelEvent* event, const QTransform& transform)
{
- Q_UNUSED(event);
- Q_UNUSED(transform);
+ Q_UNUSED(event)
+ Q_UNUSED(transform)
return false;
}
bool KItemListController::resizeEvent(QGraphicsSceneResizeEvent* event, const QTransform& transform)
{
- Q_UNUSED(event);
- Q_UNUSED(transform);
+ Q_UNUSED(event)
+ Q_UNUSED(transform)
return false;
}
const QPixmap pixmap = m_view->createDragPixmap(selectedItems);
drag->setPixmap(pixmap);
- const QPoint hotSpot(pixmap.width() / 2, 0);
+ const QPoint hotSpot((pixmap.width() / pixmap.devicePixelRatio()) / 2, 0);
drag->setHotSpot(hotSpot);
drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::CopyAction);
- QAccessible::updateAccessibility(view(), 0, QAccessible::DragDropStart);
+
+ QAccessibleEvent accessibilityEvent(view(), QAccessible::DragDropStart);
+ QAccessible::updateAccessibility(&accessibilityEvent);
}
KItemListWidget* KItemListController::hoveredWidget() const
}
}
- return 0;
+ return nullptr;
}
KItemListWidget* KItemListController::widgetForPos(const QPointF& pos) const
}
}
- return 0;
+ return nullptr;
}
void KItemListController::updateKeyboardAnchor()
}
}
-#include "kitemlistcontroller.moc"