#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),
{
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
default: break;
}
}
-
- const bool selectSingleItem = itemCount == 1 &&
+
+ 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);
switch (key) {
case Qt::Key_Home:
index = 0;
+ m_keyboardAnchorIndex = index;
+ m_keyboardAnchorPos = keyboardAnchorPos(index);
break;
case Qt::Key_End:
index = itemCount - 1;
+ m_keyboardAnchorIndex = index;
+ m_keyboardAnchorPos = keyboardAnchorPos(index);
break;
case Qt::Key_Left:
}
case Qt::Key_Space:
- if (controlPressed) {
- m_selectionManager->endAnchoredSelection();
- m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
- m_selectionManager->beginAnchoredSelection(index);
- } else {
- const int current = m_selectionManager->currentItem();
- m_selectionManager->setSelected(current);
+ if (m_selectionBehavior == MultiSelection) {
+ if (controlPressed) {
+ m_selectionManager->endAnchoredSelection();
+ m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
+ m_selectionManager->beginAnchoredSelection(index);
+ } else {
+ const int current = m_selectionManager->currentItem();
+ m_selectionManager->setSelected(current);
+ }
}
break;
break;
}
+ case Qt::Key_Escape:
+ if (m_selectionBehavior != SingleSelection) {
+ m_selectionManager->clearSelection();
+ }
+ m_keyboardManager->cancelSearch();
+ break;
+
default:
m_keyboardManager->addKeys(event->text());
return false;
}
if (m_selectionManager->currentItem() != index) {
- if (controlPressed) {
- m_selectionManager->endAnchoredSelection();
- }
-
- m_selectionManager->setCurrentItem(index);
+ switch (m_selectionBehavior) {
+ case NoSelection:
+ m_selectionManager->setCurrentItem(index);
+ break;
- if (!shiftOrControlPressed || m_selectionBehavior == SingleSelection) {
+ case SingleSelection:
+ m_selectionManager->setCurrentItem(index);
m_selectionManager->clearSelection();
m_selectionManager->setSelected(index, 1);
- }
+ break;
- if (!shiftPressed) {
- m_selectionManager->beginAnchoredSelection(index);
+ case MultiSelection:
+ if (controlPressed) {
+ m_selectionManager->endAnchoredSelection();
+ }
+
+ m_selectionManager->setCurrentItem(index);
+
+ if (!shiftOrControlPressed) {
+ m_selectionManager->clearSelection();
+ m_selectionManager->setSelected(index, 1);
+ }
+
+ if (!shiftPressed) {
+ m_selectionManager->beginAnchoredSelection(index);
+ }
+ break;
}
m_view->scrollToItem(index);
m_pressedMousePos = transform.map(event->pos());
m_pressedIndex = m_view->itemAt(m_pressedMousePos);
- if (m_pressedIndex >= 0) {
- emit itemPressed(m_pressedIndex, event->button());
- }
+ emit mouseButtonPressed(m_pressedIndex, event->buttons());
if (m_view->isAboveExpansionToggle(m_pressedIndex, m_pressedMousePos)) {
m_selectionManager->endAnchoredSelection();
return false;
}
- if (m_pressedIndex >= 0) {
- emit itemReleased(m_pressedIndex, event->button());
- }
+ emit mouseButtonReleased(m_pressedIndex, event->buttons());
const bool isAboveSelectionToggle = m_view->isAboveSelectionToggle(m_pressedIndex, m_pressedMousePos);
if (isAboveSelectionToggle) {
{
Q_UNUSED(event);
Q_UNUSED(transform);
+
+ m_view->setAutoScroll(false);
+
+ KItemListWidget* widget = hoveredWidget();
+ if (widget) {
+ widget->setHovered(false);
+ emit itemUnhovered(widget->index());
+ }
return false;
}
return false;
}
+ event->acceptProposedAction();
+
KItemListWidget* oldHoveredWidget = hoveredWidget();
const QPointF pos = transform.map(event->pos());
}
m_autoActivationTimer->stop();
+ m_view->setAutoScroll(false);
const QPointF pos = transform.map(event->pos());
const int index = m_view->itemAt(pos);
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"