#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_model(0),
m_visibleRoles(),
m_visibleRolesSizes(),
+ m_stretchedVisibleRolesSizes(),
m_widgetCreator(0),
m_groupHeaderCreator(0),
m_styleOption(),
m_layouter(0),
m_animation(0),
m_layoutTimer(0),
- m_oldOffset(0),
- m_oldMaximumOffset(0)
+ m_oldScrollOffset(0),
+ m_oldMaximumScrollOffset(0),
+ m_oldItemOffset(0),
+ m_oldMaximumItemOffset(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()
m_animation->setScrollOrientation(orientation);
m_sizeHintResolver->clearCache();
updateLayout();
+
onScrollOrientationChanged(orientation, previousOrientation);
+ emit scrollOrientationChanged(orientation, previousOrientation);
}
Qt::Orientation KItemListView::scrollOrientation() const
m_itemSize = itemSize;
- if (!markVisibleRolesSizesAsDirty()) {
+ const bool emptySize = itemSize.isEmpty();
+ if (emptySize) {
+ updateVisibleRolesSizes();
+ } else {
if (itemSize.width() < previousSize.width() || itemSize.height() < previousSize.height()) {
prepareLayoutForIncreasedItemCount(itemSize, ItemSize);
} else {
return m_itemSize;
}
-void KItemListView::setOffset(qreal offset)
+void KItemListView::setScrollOffset(qreal offset)
{
if (offset < 0) {
offset = 0;
}
- const qreal previousOffset = m_layouter->offset();
+ const qreal previousOffset = m_layouter->scrollOffset();
if (offset == previousOffset) {
return;
}
- m_layouter->setOffset(offset);
- m_animation->setOffset(offset);
+ m_layouter->setScrollOffset(offset);
+ m_animation->setScrollOffset(offset);
+ if (!m_layoutTimer->isActive()) {
+ doLayout(NoAnimation, 0, 0);
+ update();
+ }
+ onScrollOffsetChanged(offset, previousOffset);
+}
+
+qreal KItemListView::scrollOffset() const
+{
+ return m_layouter->scrollOffset();
+}
+
+qreal KItemListView::maximumScrollOffset() const
+{
+ return m_layouter->maximumScrollOffset();
+}
+
+void KItemListView::setItemOffset(qreal offset)
+{
+ m_layouter->setItemOffset(offset);
+ if (m_header) {
+ m_header->setPos(-offset, 0);
+ }
if (!m_layoutTimer->isActive()) {
doLayout(NoAnimation, 0, 0);
update();
}
- onOffsetChanged(offset, previousOffset);
}
-qreal KItemListView::offset() const
+qreal KItemListView::itemOffset() const
{
- return m_layouter->offset();
+ return m_layouter->itemOffset();
}
-qreal KItemListView::maximumOffset() const
+qreal KItemListView::maximumItemOffset() const
{
- return m_layouter->maximumOffset();
+ return m_layouter->maximumItemOffset();
}
-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);
it.next();
KItemListWidget* widget = it.value();
widget->setVisibleRoles(roles);
- widget->setVisibleRolesSizes(m_visibleRolesSizes);
+ widget->setVisibleRolesSizes(m_stretchedVisibleRolesSizes);
}
m_sizeHintResolver->clearCache();
m_layouter->markAsDirty();
- onVisibleRolesChanged(roles, previousRoles);
- markVisibleRolesSizesAsDirty();
+ if (m_header) {
+ m_header->setVisibleRoles(roles);
+ m_header->setVisibleRolesWidths(headerRolesWidths());
+ m_useHeaderWidths = false;
+ }
+
+ updateVisibleRolesSizes();
updateLayout();
+
+ onVisibleRolesChanged(roles, previousRoles);
}
-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;
+}
+
KItemListController* KItemListView::controller() const
{
return m_controller;
void KItemListView::setGeometry(const QRectF& rect)
{
QGraphicsWidget::setGeometry(rect);
+
if (!m_model) {
return;
}
- if (m_itemSize.isEmpty()) {
- m_layouter->setItemSize(QSizeF());
- }
-
if (m_model->count() > 0) {
prepareLayoutForIncreasedItemCount(rect.size(), LayouterSize);
} else {
m_layouter->setSize(rect.size());
}
- m_layoutTimer->start();
+ if (!m_layoutTimer->isActive()) {
+ m_layoutTimer->start();
+ }
+
+ // Changing the geometry does not require to do an expensive
+ // update of the visible-roles sizes, only the stretched sizes
+ // need to be adjusted to the new size.
+ updateStretchedVisibleRolesSizes();
}
int KItemListView::itemAt(const QPointF& pos) const
return itemSize();
}
-QHash<QByteArray, QSizeF> KItemListView::visibleRoleSizes() const
+QHash<QByteArray, QSizeF> KItemListView::visibleRolesSizes(const KItemRangeList& itemRanges) const
{
+ Q_UNUSED(itemRanges);
return QHash<QByteArray, QSizeF>();
}
return m_layouter->itemBoundingRect(index);
}
+int KItemListView::itemsPerOffset() const
+{
+ return m_layouter->itemsPerOffset();
+}
+
void KItemListView::beginTransaction()
{
++m_activeTransactions;
return m_activeTransactions > 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;
+
+ 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;
+}
+
+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() - scrollOffset());
+ } else {
+ rubberBandRect.moveTo(topLeft.x() - scrollOffset(), 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::onOffsetChanged(qreal current, qreal previous)
+void KItemListView::onScrollOffsetChanged(qreal current, qreal 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);
+ if (m_itemSize.isEmpty() && m_useHeaderWidths) {
+ QSizeF dynamicItemSize = m_layouter->itemSize();
+ const QSizeF newSize = event->newSize();
+
+ if (m_itemSize.width() < 0) {
+ const qreal requiredWidth = visibleRolesSizesWidthSum();
+ if (newSize.width() > requiredWidth) {
+ dynamicItemSize.setWidth(newSize.width());
+ }
+ const qreal headerWidth = qMax(newSize.width(), requiredWidth);
+ m_header->resize(headerWidth, m_header->size().height());
+ }
+
+ if (m_itemSize.height() < 0) {
+ const qreal requiredHeight = visibleRolesSizesHeightSum();
+ if (newSize.height() > requiredHeight) {
+ dynamicItemSize.setHeight(newSize.height());
+ }
+ // TODO: KItemListHeader is not prepared for vertical alignment
+ }
+
+ m_layouter->setItemSize(dynamicItemSize);
+ }
+}
+
void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
{
- markVisibleRolesSizesAsDirty();
+ updateVisibleRolesSizes(itemRanges);
const bool hasMultipleRanges = (itemRanges.count() > 1);
if (hasMultipleRanges) {
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) {
}
m_layouter->markAsDirty();
- if (m_model->count() == count && maximumOffset() > size().height()) {
+ if (m_model->count() == count && maximumScrollOffset() > size().height()) {
kDebug() << "Scrollbar required, skipping layout";
const int scrollBarExtent = style()->pixelMetric(QStyle::PM_ScrollBarExtent);
QSizeF layouterSize = m_layouter->size();
void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
{
- markVisibleRolesSizesAsDirty();
+ updateVisibleRolesSizes();
const bool hasMultipleRanges = (itemRanges.count() > 1);
if (hasMultipleRanges) {
}
}
+void KItemListView::slotItemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes)
+{
+ const int firstVisibleMovedIndex = qMax(firstVisibleIndex(), itemRange.index);
+ const int lastVisibleMovedIndex = qMin(lastVisibleIndex(), itemRange.index + itemRange.count - 1);
+
+ for (int index = firstVisibleMovedIndex; index <= lastVisibleMovedIndex; ++index) {
+ KItemListWidget* widget = m_visibleItems.value(index);
+ if (widget) {
+ updateWidgetProperties(widget, index);
+ }
+ }
+
+ if (m_controller) {
+ m_controller->selectionManager()->itemsMoved(itemRange, movedToIndexes);
+ }
+}
+
void KItemListView::slotItemsChanged(const KItemRangeList& itemRanges,
const QSet<QByteArray>& roles)
{
+ const bool updateSizeHints = itemSizeHintUpdateRequired(roles);
+ if (updateSizeHints) {
+ updateVisibleRolesSizes(itemRanges);
+ }
+
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);
}
}
+
}
}
+void KItemListView::slotGroupedSortingChanged(bool current)
+{
+ m_grouped = current;
+}
+
void KItemListView::slotCurrentChanged(int current, int previous)
{
Q_UNUSED(previous);
Q_ASSERT(!currentWidget->isCurrent());
currentWidget->setCurrent(true);
}
+
+ const QRectF viewGeometry = geometry();
+ const QRectF currentBoundingRect = itemBoundingRect(current);
+
+ if (!viewGeometry.contains(currentBoundingRect)) {
+ // Make sure that the new current item is fully visible in the view.
+ qreal newOffset = scrollOffset();
+ if (currentBoundingRect.top() < viewGeometry.top()) {
+ Q_ASSERT(scrollOrientation() == Qt::Vertical);
+ newOffset += currentBoundingRect.top() - viewGeometry.top();
+ } else if ((currentBoundingRect.bottom() > viewGeometry.bottom())) {
+ Q_ASSERT(scrollOrientation() == Qt::Vertical);
+ newOffset += currentBoundingRect.bottom() - viewGeometry.bottom();
+ } else if (currentBoundingRect.left() < viewGeometry.left()) {
+ if (scrollOrientation() == Qt::Horizontal) {
+ newOffset += currentBoundingRect.left() - viewGeometry.left();
+ }
+ } else if ((currentBoundingRect.right() > viewGeometry.right())) {
+ if (scrollOrientation() == Qt::Horizontal) {
+ newOffset += currentBoundingRect.right() - viewGeometry.right();
+ }
+ }
+
+ if (newOffset != scrollOffset()) {
+ emit scrollTo(newOffset);
+ }
+ }
}
void KItemListView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
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_stretchedVisibleRolesSizes.insert(role, roleSize);
+
+ // Apply the new size to the layouter
+ QSizeF dynamicItemSize = m_itemSize;
+ if (dynamicItemSize.width() < 0) {
+ const qreal requiredWidth = visibleRolesSizesWidthSum();
+ dynamicItemSize.setWidth(qMax(size().width(), requiredWidth));
+ }
+ if (dynamicItemSize.height() < 0) {
+ const qreal requiredHeight = visibleRolesSizesHeightSum();
+ dynamicItemSize.setHeight(qMax(size().height(), requiredHeight));
+ }
+
+ m_layouter->setItemSize(dynamicItemSize);
+
+ // Update the role sizes for all visible widgets
+ foreach (KItemListWidget* widget, visibleItemListWidgets()) {
+ widget->setVisibleRolesSizes(m_stretchedVisibleRolesSizes);
+ }
+
+ 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;
+
+ setScrollOffset(scrollOffset() + m_autoScrollIncrement);
+
+ // Trigger the autoscroll timer which will periodically call
+ // triggerAutoScrolling()
+ m_autoScrollTimer->start(RepeatingAutoScrollDelay);
+}
+
void KItemListView::setController(KItemListController* controller)
{
if (m_controller != controller) {
this, SLOT(slotItemsInserted(KItemRangeList)));
disconnect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
this, SLOT(slotItemsRemoved(KItemRangeList)));
+ disconnect(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)),
+ this, SLOT(slotItemsMoved(KItemRange,QList<int>)));
+ disconnect(m_model, SIGNAL(groupedSortingChanged(bool)),
+ this, SLOT(slotGroupedSortingChanged(bool)));
}
m_model = model;
m_layouter->setModel(model);
- m_grouped = !model->groupRole().isEmpty();
+ m_grouped = model->groupedSorting();
if (m_model) {
connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
this, SLOT(slotItemsInserted(KItemRangeList)));
connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
this, SLOT(slotItemsRemoved(KItemRangeList)));
+ connect(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)),
+ this, SLOT(slotItemsMoved(KItemRange,QList<int>)));
+ connect(m_model, SIGNAL(groupedSortingChanged(bool)),
+ this, SLOT(slotGroupedSortingChanged(bool)));
}
onModelChanged(model, previous);
}
+KItemListRubberBand* KItemListView::rubberBand() const
+{
+ return m_rubberBand;
+}
+
void KItemListView::updateLayout()
{
doLayout(Animation, 0, 0);
return;
}
- applyDynamicItemSize();
+ //markVisibleRolesSizesAsDirty();
const int firstVisibleIndex = m_layouter->firstVisibleIndex();
const int lastVisibleIndex = m_layouter->lastVisibleIndex();
return;
}
- // Do a sanity check of the offset-property: When properties of the itemlist-view have been changed
+ // Do a sanity check of the scroll-offset property: When properties of the itemlist-view have been changed
// it might be possible that the maximum offset got changed too. Assure that the full visible range
// is still shown if the maximum offset got decreased.
const qreal visibleOffsetRange = (scrollOrientation() == Qt::Horizontal) ? size().width() : size().height();
- const qreal maxOffsetToShowFullRange = maximumOffset() - visibleOffsetRange;
- if (offset() > maxOffsetToShowFullRange) {
- m_layouter->setOffset(qMax(qreal(0), maxOffsetToShowFullRange));
+ const qreal maxOffsetToShowFullRange = maximumScrollOffset() - visibleOffsetRange;
+ if (scrollOffset() > maxOffsetToShowFullRange) {
+ m_layouter->setScrollOffset(qMax(qreal(0), maxOffsetToShowFullRange));
}
// Determine all items that are completely invisible and might be
void KItemListView::emitOffsetChanges()
{
- const int newOffset = m_layouter->offset();
- if (m_oldOffset != newOffset) {
- emit offsetChanged(newOffset, m_oldOffset);
- m_oldOffset = newOffset;
+ const qreal newScrollOffset = m_layouter->scrollOffset();
+ if (m_oldScrollOffset != newScrollOffset) {
+ emit scrollOffsetChanged(newScrollOffset, m_oldScrollOffset);
+ m_oldScrollOffset = newScrollOffset;
+ }
+
+ const qreal newMaximumScrollOffset = m_layouter->maximumScrollOffset();
+ if (m_oldMaximumScrollOffset != newMaximumScrollOffset) {
+ emit maximumScrollOffsetChanged(newMaximumScrollOffset, m_oldMaximumScrollOffset);
+ m_oldMaximumScrollOffset = newMaximumScrollOffset;
+ }
+
+ const qreal newItemOffset = m_layouter->itemOffset();
+ if (m_oldItemOffset != newItemOffset) {
+ emit itemOffsetChanged(newItemOffset, m_oldItemOffset);
+ m_oldItemOffset = newItemOffset;
}
- const int newMaximumOffset = m_layouter->maximumOffset();
- if (m_oldMaximumOffset != newMaximumOffset) {
- emit maximumOffsetChanged(newMaximumOffset, m_oldMaximumOffset);
- m_oldMaximumOffset = newMaximumOffset;
+ const qreal newMaximumItemOffset = m_layouter->maximumItemOffset();
+ if (m_oldMaximumItemOffset != newMaximumItemOffset) {
+ emit maximumItemOffsetChanged(newMaximumItemOffset, m_oldMaximumItemOffset);
+ m_oldMaximumItemOffset = newMaximumItemOffset;
}
}
KItemListWidget* KItemListView::createWidget(int index)
{
KItemListWidget* widget = m_widgetCreator->create(this);
+ widget->setFlag(QGraphicsItem::ItemStacksBehindParent);
+
updateWidgetProperties(widget, index);
m_visibleItems.insert(index, widget);
}
}
-bool KItemListView::markVisibleRolesSizesAsDirty()
+void KItemListView::updateWidgetProperties(KItemListWidget* widget, int index)
+{
+ widget->setVisibleRoles(m_visibleRoles);
+ widget->setVisibleRolesSizes(m_stretchedVisibleRolesSizes);
+ widget->setStyleOption(m_styleOption);
+
+ const KItemListSelectionManager* selectionManager = m_controller->selectionManager();
+ widget->setCurrent(index == selectionManager->currentItem());
+ widget->setSelected(selectionManager->isSelected(index));
+ widget->setHovered(false);
+ widget->setAlternatingBackgroundColors(false);
+ widget->setIndex(index);
+ widget->setData(m_model->data(index));
+}
+
+QHash<QByteArray, qreal> KItemListView::headerRolesWidths() const
{
- const bool dirty = m_itemSize.isEmpty();
- if (dirty) {
- m_visibleRolesSizes.clear();
- m_layouter->setItemSize(QSizeF());
+ QHash<QByteArray, qreal> rolesWidths;
+
+ QHashIterator<QByteArray, QSizeF> it(m_stretchedVisibleRolesSizes);
+ while (it.hasNext()) {
+ it.next();
+ rolesWidths.insert(it.key(), it.value().width());
}
- return dirty;
+
+ return rolesWidths;
}
-void KItemListView::applyDynamicItemSize()
+void KItemListView::updateVisibleRolesSizes(const KItemRangeList& itemRanges)
{
- if (!m_itemSize.isEmpty()) {
+ if (!m_itemSize.isEmpty() || m_useHeaderWidths) {
return;
}
- if (m_visibleRolesSizes.isEmpty()) {
- m_visibleRolesSizes = visibleRoleSizes();
- foreach (KItemListWidget* widget, visibleItemListWidgets()) {
- widget->setVisibleRolesSizes(m_visibleRolesSizes);
- }
+ const int itemCount = m_model->count();
+ int rangesItemCount = 0;
+ foreach (const KItemRange& range, itemRanges) {
+ rangesItemCount += range.count;
}
- if (m_layouter->itemSize().isEmpty()) {
- qreal requiredWidth = 0;
- qreal requiredHeight = 0;
-
- QHashIterator<QByteArray, QSizeF> it(m_visibleRolesSizes);
+ if (itemCount == rangesItemCount) {
+ m_visibleRolesSizes = visibleRolesSizes(itemRanges);
+ if (m_header) {
+ // Assure the the sizes are not smaller than the minimum defined by the header
+ // TODO: Currently only implemented for a top-aligned header
+ const qreal minHeaderRoleWidth = m_header->minimumRoleWidth();
+ QMutableHashIterator<QByteArray, QSizeF> it (m_visibleRolesSizes);
+ while (it.hasNext()) {
+ it.next();
+ const QSizeF& size = it.value();
+ if (size.width() < minHeaderRoleWidth) {
+ const QSizeF newSize(minHeaderRoleWidth, size.height());
+ m_visibleRolesSizes.insert(it.key(), newSize);
+ }
+ }
+ }
+ } else {
+ // Only a sub range of the roles need to be determined.
+ // The chances are good that the sizes of the sub ranges
+ // already fit into the available sizes and hence no
+ // expensive update might be required.
+ bool updateRequired = false;
+
+ const QHash<QByteArray, QSizeF> updatedSizes = visibleRolesSizes(itemRanges);
+ QHashIterator<QByteArray, QSizeF> it(updatedSizes);
while (it.hasNext()) {
it.next();
- const QSizeF& visibleRoleSize = it.value();
- requiredWidth += visibleRoleSize.width();
- requiredHeight += visibleRoleSize.height();
+ const QByteArray& role = it.key();
+ const QSizeF& updatedSize = it.value();
+ const QSizeF currentSize = m_visibleRolesSizes.value(role);
+ if (updatedSize.width() > currentSize.width() || updatedSize.height() > currentSize.height()) {
+ m_visibleRolesSizes.insert(role, updatedSize);
+ updateRequired = true;
+ }
}
- QSizeF dynamicItemSize = m_itemSize;
- if (dynamicItemSize.width() <= 0) {
- dynamicItemSize.setWidth(qMax(requiredWidth, size().width()));
- }
- if (dynamicItemSize.height() <= 0) {
- dynamicItemSize.setHeight(qMax(requiredHeight, size().height()));
+ if (!updateRequired) {
+ // All the updated sizes are smaller than the current sizes and no change
+ // of the stretched roles-widths is required
+ return;
}
+ }
- m_layouter->setItemSize(dynamicItemSize);
+ updateStretchedVisibleRolesSizes();
+}
+
+void KItemListView::updateVisibleRolesSizes()
+{
+ const int itemCount = m_model->count();
+ if (itemCount > 0) {
+ updateVisibleRolesSizes(KItemRangeList() << KItemRange(0, itemCount));
}
}
-void KItemListView::updateWidgetProperties(KItemListWidget* widget, int index)
+void KItemListView::updateStretchedVisibleRolesSizes()
{
- widget->setVisibleRoles(m_visibleRoles);
- widget->setVisibleRolesSizes(m_visibleRolesSizes);
- widget->setStyleOption(m_styleOption);
+ if (!m_itemSize.isEmpty() || m_useHeaderWidths) {
+ return;
+ }
- const KItemListSelectionManager* selectionManager = m_controller->selectionManager();
- widget->setCurrent(index == selectionManager->currentItem());
+ // Calculate the maximum size of an item by considering the
+ // visible role sizes and apply them to the layouter. If the
+ // size does not use the available view-size it the size of the
+ // first role will get stretched.
+ m_stretchedVisibleRolesSizes = m_visibleRolesSizes;
+ const QByteArray role = visibleRoles().first();
+ QSizeF firstRoleSize = m_stretchedVisibleRolesSizes.value(role);
+
+ QSizeF dynamicItemSize = m_itemSize;
+
+ if (dynamicItemSize.width() <= 0) {
+ const qreal requiredWidth = visibleRolesSizesWidthSum();
+ const qreal availableWidth = size().width();
+ if (requiredWidth < availableWidth) {
+ // Stretch the first role to use the whole width for the item
+ firstRoleSize.rwidth() += availableWidth - requiredWidth;
+ m_stretchedVisibleRolesSizes.insert(role, firstRoleSize);
+ }
+ dynamicItemSize.setWidth(qMax(requiredWidth, availableWidth));
+ }
- if (selectionManager->hasSelection()) {
- const QSet<int> selectedItems = selectionManager->selectedItems();
- widget->setSelected(selectedItems.contains(index));
- } else {
- widget->setSelected(false);
+ if (dynamicItemSize.height() <= 0) {
+ const qreal requiredHeight = visibleRolesSizesHeightSum();
+ const qreal availableHeight = size().height();
+ if (requiredHeight < availableHeight) {
+ // Stretch the first role to use the whole height for the item
+ firstRoleSize.rheight() += availableHeight - requiredHeight;
+ m_stretchedVisibleRolesSizes.insert(role, firstRoleSize);
+ }
+ dynamicItemSize.setHeight(qMax(requiredHeight, availableHeight));
}
- widget->setHovered(false);
+ m_layouter->setItemSize(dynamicItemSize);
- widget->setIndex(index);
- widget->setData(m_model->data(index));
+ if (m_header) {
+ m_header->setVisibleRolesWidths(headerRolesWidths());
+ m_header->resize(dynamicItemSize.width(), m_header->size().height());
+ }
+
+ // Update the role sizes for all visible widgets
+ foreach (KItemListWidget* widget, visibleItemListWidgets()) {
+ widget->setVisibleRolesSizes(m_stretchedVisibleRolesSizes);
+ }
+}
+
+qreal KItemListView::visibleRolesSizesWidthSum() const
+{
+ qreal widthSum = 0;
+ QHashIterator<QByteArray, QSizeF> it(m_visibleRolesSizes);
+ while (it.hasNext()) {
+ it.next();
+ widthSum += it.value().width();
+ }
+ return widthSum;
}
+qreal KItemListView::visibleRolesSizesHeightSum() const
+{
+ qreal heightSum = 0;
+ QHashIterator<QByteArray, QSizeF> it(m_visibleRolesSizes);
+ while (it.hasNext()) {
+ it.next();
+ heightSum += it.value().height();
+ }
+ return heightSum;
+}
+
+QRectF KItemListView::headerBoundaries() const
+{
+ return m_header ? m_header->geometry() : QRectF();
+}
+
+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);