#include "kitemlistcontroller.h"
+#include <KGlobalSettings>
+#include <KDebug>
+
#include "kitemlistview.h"
-#include "kitemlistrubberband_p.h"
#include "kitemlistselectionmanager.h"
-#include "kitemlistkeyboardsearchmanager_p.h"
+
+#include "private/kitemlistrubberband.h"
+#include "private/kitemlistkeyboardsearchmanager.h"
#include <QApplication>
#include <QDrag>
#include <QMimeData>
#include <QTimer>
-#include <KGlobalSettings>
-#include <KDebug>
-
-KItemListController::KItemListController(QObject* parent) :
+KItemListController::KItemListController(KItemModelBase* model, KItemListView* view, QObject* parent) :
QObject(parent),
m_singleClickActivation(KGlobalSettings::singleClick()),
m_selectionTogglePressed(false),
m_clearSelectionIfItemsAreNotDragged(false),
m_selectionBehavior(NoSelection),
+ m_autoActivationBehavior(ActivationAndExpansion),
+ m_mouseDoubleClickAction(ActivateItemOnly),
m_model(0),
m_view(0),
m_selectionManager(new KItemListSelectionManager(this)),
{
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)));
m_autoActivationTimer = new QTimer(this);
m_autoActivationTimer->setSingleShot(true);
m_autoActivationTimer->setInterval(-1);
connect(m_autoActivationTimer, SIGNAL(timeout()), this, SLOT(slotAutoActivationTimeout()));
+
+ setModel(model);
+ setView(view);
}
KItemListController::~KItemListController()
{
+ setView(0);
+ Q_ASSERT(!m_view);
+
+ setModel(0);
+ Q_ASSERT(!m_model);
}
void KItemListController::setModel(KItemModelBase* model)
}
KItemModelBase* oldModel = m_model;
+ if (oldModel) {
+ oldModel->deleteLater();
+ }
+
m_model = model;
+ if (m_model) {
+ m_model->setParent(this);
+ }
if (m_view) {
m_view->setModel(m_model);
KItemListView* oldView = m_view;
if (oldView) {
disconnect(oldView, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(slotViewScrollOffsetChanged(qreal,qreal)));
+ oldView->deleteLater();
}
m_view = view;
if (m_view) {
+ 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)));
+ updateExtendedSelectionRegion();
}
emit viewChanged(m_view, oldView);
void KItemListController::setSelectionBehavior(SelectionBehavior behavior)
{
m_selectionBehavior = behavior;
+ updateExtendedSelectionRegion();
}
KItemListController::SelectionBehavior KItemListController::selectionBehavior() const
return m_selectionBehavior;
}
+void KItemListController::setAutoActivationBehavior(AutoActivationBehavior behavior)
+{
+ m_autoActivationBehavior = behavior;
+}
+
+KItemListController::AutoActivationBehavior KItemListController::autoActivationBehavior() const
+{
+ return m_autoActivationBehavior;
+}
+
+void KItemListController::setMouseDoubleClickAction(MouseDoubleClickAction action)
+{
+ m_mouseDoubleClickAction = action;
+}
+
+KItemListController::MouseDoubleClickAction KItemListController::mouseDoubleClickAction() const
+{
+ return m_mouseDoubleClickAction;
+}
+
void KItemListController::setAutoActivationDelay(int delay)
{
m_autoActivationTimer->setInterval(delay);
case Qt::Key_Left:
if (index > 0) {
- --index;
+ const int expandedParentsCount = m_model->expandedParentsCount(index);
+ if (expandedParentsCount == 0) {
+ --index;
+ } else {
+ // Go to the parent of the current item.
+ do {
+ --index;
+ } while (index > 0 && m_model->expandedParentsCount(index) == expandedParentsCount);
+ }
m_keyboardAnchorIndex = index;
m_keyboardAnchorPos = keyboardAnchorPos(index);
}
break;
}
+ case Qt::Key_Escape:
+ if (m_selectionBehavior != SingleSelection) {
+ m_selectionManager->clearSelection();
+ }
+ m_keyboardManager->cancelSearch();
+ break;
+
default:
m_keyboardManager->addKeys(event->text());
return false;
return;
}
- if (m_model->supportsDropping(index)) {
+ /* m_view->isUnderMouse() fixes a bug in the Folder-View-Panel and in the
+ * Places-Panel.
+ *
+ * Bug: When you drag a file onto a Folder-View-Item or a Places-Item and
+ * then move away before the auto-activation timeout triggers, than the
+ * item still becomes activated/expanded.
+ *
+ * See Bug 293200 and 305783
+ */
+ if (m_model->supportsDropping(index) && m_view->isUnderMouse()) {
if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
const bool expanded = m_model->isExpanded(index);
m_model->setExpanded(index, !expanded);
- } else {
+ } else if (m_autoActivationBehavior != ExpansionOnly) {
emit itemActivated(index);
}
}
break;
case MultiSelection:
- if (controlPressed) {
+ if (controlPressed && !shiftPressed) {
m_selectionManager->setSelected(m_pressedIndex, 1, KItemListSelectionManager::Toggle);
m_selectionManager->beginAnchoredSelection(m_pressedIndex);
} else if (!shiftPressed || !m_selectionManager->isAnchoredSelectionActive()) {
const QPointF pos = transform.map(event->pos());
const int index = m_view->itemAt(pos);
+ // Expand item if desired - See Bug 295573
+ if (m_mouseDoubleClickAction != ActivateItemOnly) {
+ if (m_view && m_model && m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
+ const bool expanded = m_model->isExpanded(index);
+ m_model->setExpanded(index, !expanded);
+ }
+ }
+
bool emitItemActivated = !m_singleClickActivation &&
(event->button() & Qt::LeftButton) &&
index >= 0 && index < m_model->count();
Q_UNUSED(transform);
m_view->setAutoScroll(false);
+ m_view->hideDropIndicator();
KItemListWidget* widget = hoveredWidget();
if (widget) {
bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
{
- Q_UNUSED(transform);
if (!m_model || !m_view) {
return false;
}
+ event->acceptProposedAction();
+
KItemListWidget* oldHoveredWidget = hoveredWidget();
const QPointF pos = transform.map(event->pos());
}
if (newHoveredWidget) {
+ bool droppingBetweenItems = false;
+ if (m_model->sortRole().isEmpty()) {
+ // The model supports inserting items between other items.
+ droppingBetweenItems = (m_view->showDropIndicator(pos) >= 0);
+ }
+
const int index = newHoveredWidget->index();
- if (m_model->supportsDropping(index)) {
+ if (!droppingBetweenItems && m_model->supportsDropping(index)) {
+ // Something has been dragged on an item.
+ m_view->hideDropIndicator();
newHoveredWidget->setHovered(true);
- }
- emit itemHovered(index);
+ emit itemHovered(index);
- if (m_autoActivationTimer->interval() >= 0) {
- m_autoActivationTimer->setProperty("index", index);
- m_autoActivationTimer->start();
+ if (m_autoActivationTimer->interval() >= 0) {
+ m_autoActivationTimer->setProperty("index", index);
+ m_autoActivationTimer->start();
+ }
}
}
}
bool KItemListController::dropEvent(QGraphicsSceneDragDropEvent* event, const QTransform& transform)
{
- Q_UNUSED(transform)
if (!m_view) {
return false;
}
m_view->setAutoScroll(false);
const QPointF pos = transform.map(event->pos());
- const int index = m_view->itemAt(pos);
- emit itemDropEvent(index, event);
+
+ int dropAboveIndex = -1;
+ if (m_model->sortRole().isEmpty()) {
+ // The model supports inserting of items between other items.
+ dropAboveIndex = m_view->showDropIndicator(pos);
+ }
+
+ if (dropAboveIndex >= 0) {
+ // Something has been dropped between two items.
+ m_view->hideDropIndicator();
+ emit aboveItemDropEvent(dropAboveIndex, event);
+ } else {
+ // Something has been dropped on an item or on an empty part of the view.
+ emit itemDropEvent(m_view->itemAt(pos), event);
+ }
return true;
}
const QPixmap pixmap = m_view->createDragPixmap(selectedItems);
drag->setPixmap(pixmap);
+ const QPoint hotSpot(pixmap.width() / 2, 0);
+ drag->setHotSpot(hotSpot);
+
drag->exec(Qt::MoveAction | Qt::CopyAction | Qt::LinkAction, Qt::CopyAction);
}
return 0;
}
+void KItemListController::updateExtendedSelectionRegion()
+{
+ if (m_view) {
+ const bool extend = (m_selectionBehavior != MultiSelection);
+ KItemListStyleOption option = m_view->styleOption();
+ if (option.extendedSelectionRegion != extend) {
+ option.extendedSelectionRegion = extend;
+ m_view->setStyleOption(option);
+ }
+ }
+}
+
#include "kitemlistcontroller.moc"