#include "kitemlistcontroller.h"
#include "kitemlistgroupheader.h"
+#include "kitemlistheader_p.h"
+#include "kitemlistrubberband_p.h"
#include "kitemlistselectionmanager.h"
#include "kitemlistsizehintresolver_p.h"
#include "kitemlistviewlayouter_p.h"
#include <KDebug>
+#include <QCursor>
#include <QGraphicsSceneMouseEvent>
+#include <QPainter>
#include <QPropertyAnimation>
#include <QStyle>
+#include <QStyleOptionRubberBand>
#include <QTimer>
+namespace {
+ // Time in ms until reaching the autoscroll margin triggers
+ // an initial autoscrolling
+ const int InitialAutoScrollDelay = 700;
+
+ // Delay in ms for triggering the next autoscroll
+ const int RepeatingAutoScrollDelay = 1000 / 60;
+}
+
KItemListView::KItemListView(QGraphicsWidget* parent) :
QGraphicsWidget(parent),
m_grouped(false),
m_animation(0),
m_layoutTimer(0),
m_oldOffset(0),
- m_oldMaximumOffset(0)
+ m_oldMaximumOffset(0),
+ m_skipAutoScrollForRubberBand(false),
+ m_rubberBand(0),
+ m_mousePos(),
+ m_autoScrollIncrement(0),
+ m_autoScrollTimer(0),
+ m_header(0),
+ m_useHeaderWidths(false)
{
setAcceptHoverEvents(true);
m_layoutTimer->setInterval(300);
m_layoutTimer->setSingleShot(true);
connect(m_layoutTimer, SIGNAL(timeout()), this, SLOT(slotLayoutTimerFinished()));
+
+ m_rubberBand = new KItemListRubberBand(this);
+ connect(m_rubberBand, SIGNAL(activationChanged(bool)), this, SLOT(slotRubberBandActivationChanged(bool)));
}
KItemListView::~KItemListView()
return m_layouter->maximumOffset();
}
-void KItemListView::setVisibleRoles(const QHash<QByteArray, int>& roles)
+void KItemListView::setVisibleRoles(const QList<QByteArray>& roles)
{
- const QHash<QByteArray, int> previousRoles = m_visibleRoles;
+ const QList<QByteArray> previousRoles = m_visibleRoles;
m_visibleRoles = roles;
QHashIterator<int, KItemListWidget*> it(m_visibleItems);
markVisibleRolesSizesAsDirty();
updateLayout();
+
+ if (m_header) {
+ m_header->setVisibleRoles(roles);
+ m_header->setVisibleRolesWidths(headerRolesWidths());
+ m_useHeaderWidths = false;
+ }
}
-QHash<QByteArray, int> KItemListView::visibleRoles() const
+QList<QByteArray> KItemListView::visibleRoles() const
{
return m_visibleRoles;
}
+void KItemListView::setAutoScroll(bool enabled)
+{
+ if (enabled && !m_autoScrollTimer) {
+ m_autoScrollTimer = new QTimer(this);
+ m_autoScrollTimer->setSingleShot(false);
+ connect(m_autoScrollTimer, SIGNAL(timeout()), this, SLOT(triggerAutoScrolling()));
+ m_autoScrollTimer->start(InitialAutoScrollDelay);
+ } else if (!enabled && m_autoScrollTimer) {
+ delete m_autoScrollTimer;
+ m_autoScrollTimer = 0;
+ }
+
+}
+
+bool KItemListView::autoScroll() const
+{
+ return m_autoScrollTimer != 0;
+}
+
+void KItemListView::setHeaderShown(bool show)
+{
+ if (show && !m_header) {
+ m_header = new KItemListHeader(this);
+ m_header->setPos(0, 0);
+ m_header->setModel(m_model);
+ m_header->setVisibleRoles(m_visibleRoles);
+ m_header->setVisibleRolesWidths(headerRolesWidths());
+ m_header->setZValue(1);
+
+ m_useHeaderWidths = false;
+ updateHeaderWidth();
+
+ connect(m_header, SIGNAL(visibleRoleWidthChanged(QByteArray,qreal,qreal)),
+ this, SLOT(slotVisibleRoleWidthChanged(QByteArray,qreal,qreal)));
+
+ m_layouter->setHeaderHeight(m_header->size().height());
+ } else if (!show && m_header) {
+ delete m_header;
+ m_header = 0;
+ m_useHeaderWidths = false;
+ m_layouter->setHeaderHeight(0);
+ }
+}
+
+bool KItemListView::isHeaderShown() const
+{
+ return m_header != 0;
+}
+
KItemListController* KItemListView::controller() const
{
return m_controller;
m_layouter->setSize(rect.size());
}
- m_layoutTimer->start();
+ if (!m_layoutTimer->isActive()) {
+ m_layoutTimer->start();
+ }
}
int KItemListView::itemAt(const QPointF& pos) const
return m_activeTransactions > 0;
}
+QPixmap KItemListView::createDragPixmap(const QSet<int>& indexes) const
+{
+ Q_UNUSED(indexes);
+ return QPixmap();
+}
+
+void KItemListView::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
+{
+ QGraphicsWidget::paint(painter, option, widget);
+
+ if (m_rubberBand->isActive()) {
+ QRectF rubberBandRect = QRectF(m_rubberBand->startPosition(),
+ m_rubberBand->endPosition()).normalized();
+
+ const QPointF topLeft = rubberBandRect.topLeft();
+ if (scrollOrientation() == Qt::Vertical) {
+ rubberBandRect.moveTo(topLeft.x(), topLeft.y() - offset());
+ } else {
+ rubberBandRect.moveTo(topLeft.x() - offset(), topLeft.y());
+ }
+
+ QStyleOptionRubberBand opt;
+ opt.initFrom(widget);
+ opt.shape = QRubberBand::Rectangle;
+ opt.opaque = false;
+ opt.rect = rubberBandRect.toRect();
+ style()->drawControl(QStyle::CE_RubberBand, &opt, painter);
+ }
+}
+
void KItemListView::initializeItemListWidget(KItemListWidget* item)
{
Q_UNUSED(item);
}
+bool KItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
+{
+ Q_UNUSED(changedRoles);
+ return true;
+}
+
void KItemListView::onControllerChanged(KItemListController* current, KItemListController* previous)
{
Q_UNUSED(current);
Q_UNUSED(previous);
}
-void KItemListView::onVisibleRolesChanged(const QHash<QByteArray, int>& current, const QHash<QByteArray, int>& previous)
+void KItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
{
Q_UNUSED(current);
Q_UNUSED(previous);
void KItemListView::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
+ m_mousePos = transform().map(event->pos());
event->accept();
}
+void KItemListView::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
+{
+ QGraphicsWidget::mouseMoveEvent(event);
+
+ m_mousePos = transform().map(event->pos());
+ if (m_autoScrollTimer && !m_autoScrollTimer->isActive()) {
+ m_autoScrollTimer->start(InitialAutoScrollDelay);
+ }
+}
+
+void KItemListView::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
+{
+ event->setAccepted(true);
+ setAutoScroll(true);
+}
+
+void KItemListView::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
+{
+ QGraphicsWidget::dragMoveEvent(event);
+
+ m_mousePos = transform().map(event->pos());
+ if (m_autoScrollTimer && !m_autoScrollTimer->isActive()) {
+ m_autoScrollTimer->start(InitialAutoScrollDelay);
+ }
+}
+
+void KItemListView::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
+{
+ QGraphicsWidget::dragLeaveEvent(event);
+ setAutoScroll(false);
+}
+
+void KItemListView::dropEvent(QGraphicsSceneDragDropEvent* event)
+{
+ QGraphicsWidget::dropEvent(event);
+ setAutoScroll(false);
+}
+
QList<KItemListWidget*> KItemListView::visibleItemListWidgets() const
{
return m_visibleItems.values();
}
+void KItemListView::resizeEvent(QGraphicsSceneResizeEvent* event)
+{
+ QGraphicsWidget::resizeEvent(event);
+ updateHeaderWidth();
+}
+
+bool KItemListView::markVisibleRolesSizesAsDirty()
+{
+ const bool dirty = m_itemSize.isEmpty();
+ if (dirty && !m_useHeaderWidths) {
+ m_visibleRolesSizes.clear();
+ m_layouter->setItemSize(QSizeF());
+ }
+ return dirty;
+}
+
void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
{
markVisibleRolesSizesAsDirty();
foreach (const KItemRange& range, itemRanges) {
// range.index is related to the model before anything has been inserted.
// As in each loop the current item-range gets inserted the index must
- // be increased by the already previoulsy inserted items.
+ // be increased by the already previously inserted items.
const int index = range.index + previouslyInsertedCount;
const int count = range.count;
if (index < 0 || count <= 0) {
void KItemListView::slotItemsChanged(const KItemRangeList& itemRanges,
const QSet<QByteArray>& roles)
{
+ const bool updateSizeHints = itemSizeHintUpdateRequired(roles);
+ if (updateSizeHints) {
+ markVisibleRolesSizesAsDirty();
+ }
+
foreach (const KItemRange& itemRange, itemRanges) {
const int index = itemRange.index;
const int count = itemRange.count;
- m_sizeHintResolver->itemsChanged(index, count, roles);
+ if (updateSizeHints) {
+ m_sizeHintResolver->itemsChanged(index, count, roles);
+ m_layouter->markAsDirty();
+ if (!m_layoutTimer->isActive()) {
+ m_layoutTimer->start();
+ }
+ }
+ // Apply the changed roles to the visible item-widgets
const int lastIndex = index + count - 1;
for (int i = index; i <= lastIndex; ++i) {
KItemListWidget* widget = m_visibleItems.value(i);
widget->setData(m_model->data(i), roles);
}
}
+
}
}
if (currentBoundingRect.top() < viewGeometry.top()) {
Q_ASSERT(scrollOrientation() == Qt::Vertical);
newOffset += currentBoundingRect.top() - viewGeometry.top();
- }
- else if ((currentBoundingRect.bottom() > viewGeometry.bottom())) {
+ } else if ((currentBoundingRect.bottom() > viewGeometry.bottom())) {
Q_ASSERT(scrollOrientation() == Qt::Vertical);
newOffset += currentBoundingRect.bottom() - viewGeometry.bottom();
- }
- else if (currentBoundingRect.left() < viewGeometry.left()) {
+ } else if (currentBoundingRect.left() < viewGeometry.left()) {
if (scrollOrientation() == Qt::Horizontal) {
newOffset += currentBoundingRect.left() - viewGeometry.left();
}
- }
- else if ((currentBoundingRect.right() > viewGeometry.right())) {
+ } else if ((currentBoundingRect.right() > viewGeometry.right())) {
if (scrollOrientation() == Qt::Horizontal) {
newOffset += currentBoundingRect.right() - viewGeometry.right();
}
}
- emit scrollTo(newOffset);
+ if (newOffset != offset()) {
+ emit scrollTo(newOffset);
+ }
}
}
doLayout(Animation, 0, 0);
}
+void KItemListView::slotRubberBandPosChanged()
+{
+ update();
+}
+
+void KItemListView::slotRubberBandActivationChanged(bool active)
+{
+ if (active) {
+ connect(m_rubberBand, SIGNAL(startPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
+ connect(m_rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
+ m_skipAutoScrollForRubberBand = true;
+ } else {
+ disconnect(m_rubberBand, SIGNAL(startPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
+ disconnect(m_rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
+ m_skipAutoScrollForRubberBand = false;
+ }
+
+ update();
+}
+
+void KItemListView::slotVisibleRoleWidthChanged(const QByteArray& role,
+ qreal currentWidth,
+ qreal previousWidth)
+{
+ Q_UNUSED(previousWidth);
+
+ m_useHeaderWidths = true;
+
+ if (m_visibleRolesSizes.contains(role)) {
+ QSizeF roleSize = m_visibleRolesSizes.value(role);
+ roleSize.setWidth(currentWidth);
+ m_visibleRolesSizes.insert(role, roleSize);
+ }
+
+ m_layouter->setItemSize(QSizeF()); // Forces an update in applyDynamicItemSize()
+ updateLayout();
+}
+
+void KItemListView::triggerAutoScrolling()
+{
+ if (!m_autoScrollTimer) {
+ return;
+ }
+
+ int pos = 0;
+ int visibleSize = 0;
+ if (scrollOrientation() == Qt::Vertical) {
+ pos = m_mousePos.y();
+ visibleSize = size().height();
+ } else {
+ pos = m_mousePos.x();
+ visibleSize = size().width();
+ }
+
+ if (m_autoScrollTimer->interval() == InitialAutoScrollDelay) {
+ m_autoScrollIncrement = 0;
+ }
+
+ m_autoScrollIncrement = calculateAutoScrollingIncrement(pos, visibleSize, m_autoScrollIncrement);
+ if (m_autoScrollIncrement == 0) {
+ // The mouse position is not above an autoscroll margin (the autoscroll timer
+ // will be restarted in mouseMoveEvent())
+ m_autoScrollTimer->stop();
+ return;
+ }
+
+ if (m_rubberBand->isActive() && m_skipAutoScrollForRubberBand) {
+ // If a rubberband selection is ongoing the autoscrolling may only get triggered
+ // if the direction of the rubberband is similar to the autoscroll direction. This
+ // prevents that starting to create a rubberband within the autoscroll margins starts
+ // an autoscrolling.
+
+ const qreal minDiff = 4; // Ignore any autoscrolling if the rubberband is very small
+ const qreal diff = (scrollOrientation() == Qt::Vertical)
+ ? m_rubberBand->endPosition().y() - m_rubberBand->startPosition().y()
+ : m_rubberBand->endPosition().x() - m_rubberBand->startPosition().x();
+ if (qAbs(diff) < minDiff || (m_autoScrollIncrement < 0 && diff > 0) || (m_autoScrollIncrement > 0 && diff < 0)) {
+ // The rubberband direction is different from the scroll direction (e.g. the rubberband has
+ // been moved up although the autoscroll direction might be down)
+ m_autoScrollTimer->stop();
+ return;
+ }
+ }
+
+ // As soon as the autoscrolling has been triggered at least once despite having an active rubberband,
+ // the autoscrolling may not get skipped anymore until a new rubberband is created
+ m_skipAutoScrollForRubberBand = false;
+
+ setOffset(offset() + m_autoScrollIncrement);
+
+ // Trigger the autoscroll timer which will periodically call
+ // triggerAutoScrolling()
+ m_autoScrollTimer->start(RepeatingAutoScrollDelay);
+}
+
void KItemListView::setController(KItemListController* controller)
{
if (m_controller != controller) {
onModelChanged(model, previous);
}
+KItemListRubberBand* KItemListView::rubberBand() const
+{
+ return m_rubberBand;
+}
+
void KItemListView::updateLayout()
{
doLayout(Animation, 0, 0);
void KItemListView::emitOffsetChanges()
{
- const int newOffset = m_layouter->offset();
+ const qreal newOffset = m_layouter->offset();
if (m_oldOffset != newOffset) {
emit offsetChanged(newOffset, m_oldOffset);
m_oldOffset = newOffset;
}
- const int newMaximumOffset = m_layouter->maximumOffset();
+ const qreal newMaximumOffset = m_layouter->maximumOffset();
if (m_oldMaximumOffset != newMaximumOffset) {
emit maximumOffsetChanged(newMaximumOffset, m_oldMaximumOffset);
m_oldMaximumOffset = newMaximumOffset;
}
}
-bool KItemListView::markVisibleRolesSizesAsDirty()
-{
- const bool dirty = m_itemSize.isEmpty();
- if (dirty) {
- m_visibleRolesSizes.clear();
- m_layouter->setItemSize(QSizeF());
- }
- return dirty;
-}
-
void KItemListView::applyDynamicItemSize()
{
if (!m_itemSize.isEmpty()) {
if (m_visibleRolesSizes.isEmpty()) {
m_visibleRolesSizes = visibleRoleSizes();
- foreach (KItemListWidget* widget, visibleItemListWidgets()) {
- widget->setVisibleRolesSizes(m_visibleRolesSizes);
+ if (m_header) {
+ m_header->setVisibleRolesWidths(headerRolesWidths());
}
}
if (m_layouter->itemSize().isEmpty()) {
+ // Calculate the maximum size of an item by considering the
+ // visible role sizes and apply them to the layouter.
qreal requiredWidth = 0;
qreal requiredHeight = 0;
}
m_layouter->setItemSize(dynamicItemSize);
+
+ // Update the role sizes for all visible widgets
+ foreach (KItemListWidget* widget, visibleItemListWidgets()) {
+ widget->setVisibleRolesSizes(m_visibleRolesSizes);
+ }
}
}
widget->setData(m_model->data(index));
}
+void KItemListView::updateHeaderWidth()
+{
+ if (!m_header) {
+ return;
+ }
+
+ // TODO 1: Use the required width of all roles
+ m_header->resize(size().width(), m_header->size().height());
+}
+
+QHash<QByteArray, qreal> KItemListView::headerRolesWidths() const
+{
+ QHash<QByteArray, qreal> rolesWidths;
+
+ QHashIterator<QByteArray, QSizeF> it(m_visibleRolesSizes);
+ while (it.hasNext()) {
+ it.next();
+ rolesWidths.insert(it.key(), it.value().width());
+ }
+
+ return rolesWidths;
+}
+
+int KItemListView::calculateAutoScrollingIncrement(int pos, int range, int oldInc)
+{
+ int inc = 0;
+
+ const int minSpeed = 4;
+ const int maxSpeed = 128;
+ const int speedLimiter = 96;
+ const int autoScrollBorder = 64;
+
+ // Limit the increment that is allowed to be added in comparison to 'oldInc'.
+ // This assures that the autoscrolling speed grows gradually.
+ const int incLimiter = 1;
+
+ if (pos < autoScrollBorder) {
+ inc = -minSpeed + qAbs(pos - autoScrollBorder) * (pos - autoScrollBorder) / speedLimiter;
+ inc = qMax(inc, -maxSpeed);
+ inc = qMax(inc, oldInc - incLimiter);
+ } else if (pos > range - autoScrollBorder) {
+ inc = minSpeed + qAbs(pos - range + autoScrollBorder) * (pos - range + autoScrollBorder) / speedLimiter;
+ inc = qMin(inc, maxSpeed);
+ inc = qMin(inc, oldInc + incLimiter);
+ }
+
+ return inc;
+}
+
+
KItemListCreatorBase::~KItemListCreatorBase()
{
qDeleteAll(m_recycleableWidgets);