m_hoverPixmap(),
m_textPos(),
m_text(),
- m_textBoundingRect(),
+ m_textRect(),
m_sortedVisibleRoles(),
m_expansionArea(),
m_customTextColor(),
#endif
}
-QRectF KFileItemListWidget::iconBoundingRect() const
+QRectF KFileItemListWidget::iconRect() const
{
const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
return bounds;
}
-QRectF KFileItemListWidget::textBoundingRect() const
+QRectF KFileItemListWidget::textRect() const
{
const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
- return m_textBoundingRect;
+ return m_textRect;
}
QRectF KFileItemListWidget::expansionToggleRect() const
m_text[Name].setTextWidth(maxWidth);
m_textPos[Name] = QPointF(option.margin, widgetHeight - textLinesCount * fontHeight - option.margin);
- m_textBoundingRect = QRectF(option.margin + (maxWidth - requiredWidthForName) / 2,
+ m_textRect = QRectF(option.margin + (maxWidth - requiredWidthForName) / 2,
m_textPos[Name].y(),
requiredWidthForName,
m_text[Name].size().height());
m_textPos[textId] = QPointF(option.margin, y);
m_text[textId].setTextWidth(maxWidth);
- const QRectF textBoundingRect(option.margin + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
- m_textBoundingRect |= textBoundingRect;
+ const QRectF textRect(option.margin + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
+ m_textRect |= textRect;
y += fontHeight;
}
- // Add a margin to the text bounding rectangle
+ // Add a margin to the text rectangle
const qreal margin = option.margin;
- m_textBoundingRect.adjust(-margin, -margin, margin, margin);
+ m_textRect.adjust(-margin, -margin, margin, margin);
}
void KFileItemListWidget::updateCompactLayoutTextCache()
y += fontHeight;
}
- m_textBoundingRect = QRectF(x - option.margin, 0, maximumRequiredTextWidth + 2 * option.margin, widgetHeight);
+ m_textRect = QRectF(x - option.margin, 0, maximumRequiredTextWidth + 2 * option.margin, widgetHeight);
}
void KFileItemListWidget::updateDetailsLayoutTextCache()
// +------+
// | Icon | Name role Additional role 1 Additional role 2
// +------+
- m_textBoundingRect = QRectF();
+ m_textRect = QRectF();
const KItemListStyleOption& option = styleOption();
const QHash<QByteArray, QVariant> values = data();
switch (textId) {
case Name: {
- m_textBoundingRect = QRectF(m_textPos[textId].x() - option.margin, 0,
+ m_textRect = QRectF(m_textPos[textId].x() - option.margin, 0,
requiredWidth + 2 * option.margin, size().height());
// The column after the name should always be aligned on the same x-position independent
virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
- virtual QRectF iconBoundingRect() const;
- virtual QRectF textBoundingRect() const;
+ virtual QRectF iconRect() const;
+ virtual QRectF textRect() const;
virtual QRectF expansionToggleRect() const;
/**
QPointF m_textPos[TextIdCount];
QStaticText m_text[TextIdCount];
- QRectF m_textBoundingRect;
+ QRectF m_textRect;
QList<QByteArray> m_sortedVisibleRoles;
foreach (const KItemListWidget* widget, m_view->visibleItemListWidgets()) {
const int index = widget->index();
- const QRectF widgetRect = m_view->itemBoundingRect(index);
+ const QRectF widgetRect = m_view->itemRect(index);
if (widgetRect.intersects(rubberBandRect)) {
- const QRectF iconRect = widget->iconBoundingRect().translated(widgetRect.topLeft());
- const QRectF textRect = widget->textBoundingRect().translated(widgetRect.topLeft());
+ const QRectF iconRect = widget->iconRect().translated(widgetRect.topLeft());
+ const QRectF textRect = widget->textRect().translated(widgetRect.topLeft());
if (iconRect.intersects(rubberBandRect) || textRect.intersects(rubberBandRect)) {
selectedItems.insert(index);
}
int index = increaseIndex ? m_view->lastVisibleIndex() + 1 : m_view->firstVisibleIndex() - 1;
bool selectionFinished = false;
do {
- const QRectF widgetRect = m_view->itemBoundingRect(index);
+ const QRectF widgetRect = m_view->itemRect(index);
if (widgetRect.intersects(rubberBandRect)) {
selectedItems.insert(index);
}
return QHash<QByteArray, QSizeF>();
}
-QRectF KItemListView::itemBoundingRect(int index) const
+QRectF KItemListView::itemRect(int index) const
{
- return m_layouter->itemBoundingRect(index);
+ return m_layouter->itemRect(index);
}
int KItemListView::itemsPerOffset() const
{
m_grouped = current;
if (m_grouped) {
+ // Apply the height of the header to the layouter
+ const qreal groupHeaderHeight = m_styleOption.fontMetrics.height() +
+ m_styleOption.margin * 2;
+ m_layouter->setGroupHeaderHeight(groupHeaderHeight);
+
// Assure that headers from already visible items get created
QHashIterator<int, KItemListWidget*> it(m_visibleItems);
while (it.hasNext()) {
}
const QRectF viewGeometry = geometry();
- const QRectF currentBoundingRect = itemBoundingRect(current);
+ const QRectF currentRect = itemRect(current);
- if (!viewGeometry.contains(currentBoundingRect)) {
+ if (!viewGeometry.contains(currentRect)) {
// Make sure that the new current item is fully visible in the view.
qreal newOffset = scrollOffset();
- if (currentBoundingRect.top() < viewGeometry.top()) {
+ if (currentRect.top() < viewGeometry.top()) {
Q_ASSERT(scrollOrientation() == Qt::Vertical);
- newOffset += currentBoundingRect.top() - viewGeometry.top();
- } else if ((currentBoundingRect.bottom() > viewGeometry.bottom())) {
+ newOffset += currentRect.top() - viewGeometry.top();
+ } else if ((currentRect.bottom() > viewGeometry.bottom())) {
Q_ASSERT(scrollOrientation() == Qt::Vertical);
- newOffset += currentBoundingRect.bottom() - viewGeometry.bottom();
- } else if (currentBoundingRect.left() < viewGeometry.left()) {
+ newOffset += currentRect.bottom() - viewGeometry.bottom();
+ } else if (currentRect.left() < viewGeometry.left()) {
if (scrollOrientation() == Qt::Horizontal) {
- newOffset += currentBoundingRect.left() - viewGeometry.left();
+ newOffset += currentRect.left() - viewGeometry.left();
}
- } else if ((currentBoundingRect.right() > viewGeometry.right())) {
+ } else if ((currentRect.right() > viewGeometry.right())) {
if (scrollOrientation() == Qt::Horizontal) {
- newOffset += currentBoundingRect.right() - viewGeometry.right();
+ newOffset += currentRect.right() - viewGeometry.right();
}
}
bool applyNewPos = true;
bool wasHidden = false;
- const QRectF itemBounds = m_layouter->itemBoundingRect(i);
+ const QRectF itemBounds = m_layouter->itemRect(i);
const QPointF newPos = itemBounds.topLeft();
KItemListWidget* widget = m_visibleItems.value(i);
if (!widget) {
if (animate && changedCount < 0) {
// Items have been deleted, move the created item to the
// imaginary old position.
- const QRectF itemBoundingRect = m_layouter->itemBoundingRect(i - changedCount);
- if (itemBoundingRect.isEmpty()) {
+ const QRectF itemRect = m_layouter->itemRect(i - changedCount);
+ if (itemRect.isEmpty()) {
const QPointF invisibleOldPos = (scrollOrientation() == Qt::Vertical)
? QPointF(0, size().height()) : QPointF(size().width(), 0);
widget->setPos(invisibleOldPos);
} else {
- widget->setPos(itemBoundingRect.topLeft());
+ widget->setPos(itemRect.topLeft());
}
applyNewPos = false;
}
recycleWidget(m_visibleItems.value(index));
}
+ if (m_grouped) {
+ // Update the layout of all visible group headers
+ QHashIterator<KItemListWidget*, KItemListGroupHeader*> it(m_visibleGroups);
+ while (it.hasNext()) {
+ it.next();
+ updateGroupHeaderLayout(it.key());
+ }
+ }
+
emitOffsetChanges();
}
for (int i = minFirst; i <= maxLast; ++i) {
if (!m_visibleItems.contains(i)) {
KItemListWidget* widget = createWidget(i);
- const QPointF pos = m_layouter->itemBoundingRect(i).topLeft();
+ const QPointF pos = m_layouter->itemRect(i).topLeft();
widget->setPos(pos);
}
}
}
Q_ASSERT(header->parentItem() == widget);
- // TODO:
- header->show();
- header->setPos(0, -50);
- header->resize(200, 50);
- header->setRole(model()->sortRole());
-
// Determine the shown data for the header by doing a binary
// search in the groups-list
const QList<QPair<int, QVariant> > groups = model()->groups();
max = mid - 1;
}
} while (groups.at(mid).first != index && min <= max);
+
header->setData(groups.at(mid).second);
+ header->setRole(model()->sortRole());
+
+ header->show();
+}
+
+void KItemListView::updateGroupHeaderLayout(KItemListWidget* widget)
+{
+ KItemListGroupHeader* header = m_visibleGroups.value(widget);
+ Q_ASSERT(header);
+
+ const int index = widget->index();
+ const QRectF groupHeaderRect = m_layouter->groupHeaderRect(index);
+ const QRectF itemRect = m_layouter->itemRect(index);
+
+ // The group-header is a child of the itemlist widget. Translate the
+ // group header position to the relative position.
+ const QPointF groupHeaderPos(groupHeaderRect.x() - itemRect.x(),
+ - groupHeaderRect.height());
+ header->setPos(groupHeaderPos);
+ header->resize(groupHeaderRect.size());
}
void KItemListView::recycleGroupHeaderForWidget(KItemListWidget* widget)
virtual QHash<QByteArray, QSizeF> visibleRolesSizes(const KItemRangeList& itemRanges) const;
/**
- * @return The bounding rectangle of the item relative to the top/left of
+ * @return The rectangle of the item relative to the top/left of
* the currently visible area (see KItemListView::offset()).
*/
- QRectF itemBoundingRect(int index) const;
+ QRectF itemRect(int index) const;
/**
* @return The number of items that can be shown in parallel for one offset.
*/
void updateGroupHeaderForWidget(KItemListWidget* widget);
+ /**
+ * Updates the position and size of the group-header that belongs
+ * to the itemlist widget \a widget. The given widget must represent
+ * the first item of a group.
+ */
+ void updateGroupHeaderLayout(KItemListWidget* widget);
+
/**
* Recycles the group-header from the widget.
*/
#define KITEMLISTVIEWLAYOUTER_DEBUG
-namespace {
- // TODO: Calculate dynamically
- const int GroupHeaderHeight = 50;
-};
-
KItemListViewLayouter::KItemListViewLayouter(QObject* parent) :
QObject(parent),
m_dirty(true),
m_columnWidth(0),
m_xPosInc(0),
m_columnCount(0),
- m_groups(),
- m_groupIndexes(),
- m_itemBoundingRects()
+ m_groupItemIndexes(),
+ m_groupHeaderHeight(0),
+ m_itemRects()
{
}
return m_headerHeight;
}
+void KItemListViewLayouter::setGroupHeaderHeight(qreal height)
+{
+ if (m_groupHeaderHeight != height) {
+ m_groupHeaderHeight = height;
+ m_dirty = true;
+ }
+}
+
+qreal KItemListViewLayouter::groupHeaderHeight() const
+{
+ return m_groupHeaderHeight;
+}
+
void KItemListViewLayouter::setScrollOffset(qreal offset)
{
if (m_scrollOffset != offset) {
return m_lastVisibleIndex;
}
-QRectF KItemListViewLayouter::itemBoundingRect(int index) const
+QRectF KItemListViewLayouter::itemRect(int index) const
{
const_cast<KItemListViewLayouter*>(this)->doLayout();
- if (index < 0 || index >= m_itemBoundingRects.count()) {
+ if (index < 0 || index >= m_itemRects.count()) {
return QRectF();
}
if (m_scrollOrientation == Qt::Horizontal) {
// Rotate the logical direction which is always vertical by 90°
// to get the physical horizontal direction
- const QRectF& b = m_itemBoundingRects[index];
+ const QRectF& b = m_itemRects[index];
QRectF bounds(b.y(), b.x(), b.height(), b.width());
QPointF pos = bounds.topLeft();
pos.rx() -= m_scrollOffset;
return bounds;
}
- QRectF bounds = m_itemBoundingRects[index];
+ QRectF bounds = m_itemRects[index];
bounds.moveTo(bounds.topLeft() - QPointF(m_itemOffset, m_scrollOffset));
return bounds;
}
+QRectF KItemListViewLayouter::groupHeaderRect(int index) const
+{
+ const QRectF firstItemRect = itemRect(index);
+ QPointF pos = firstItemRect.topLeft();
+ if (pos.isNull()) {
+ return QRectF();
+ }
+
+ pos.ry() -= m_groupHeaderHeight;
+
+ QSizeF size;
+ if (m_scrollOrientation == Qt::Vertical) {
+ pos.rx() = 0;
+ size = QSizeF(m_size.width(), m_groupHeaderHeight);
+ } else {
+ size = QSizeF(firstItemRect.width(), m_groupHeaderHeight);
+ }
+ return QRectF(pos, size);
+}
+
int KItemListViewLayouter::maximumVisibleItems() const
{
const_cast<KItemListViewLayouter*>(this)->doLayout();
bool KItemListViewLayouter::isFirstGroupItem(int itemIndex) const
{
- return m_groupIndexes.contains(itemIndex);
+ return m_groupItemIndexes.contains(itemIndex);
}
void KItemListViewLayouter::markAsDirty()
++rowCount;
}
- m_itemBoundingRects.reserve(itemCount);
+ m_itemRects.reserve(itemCount);
qreal y = m_headerHeight;
int rowIndex = 0;
const bool grouped = createGroupHeaders();
- int groupIndex = 0;
- int firstItemIndexOfGroup = 0;
int index = 0;
while (index < itemCount) {
if (horizontalScrolling) {
// All group headers will always be aligned on the top and not
// flipped like the other properties
- x += GroupHeaderHeight;
+ x += m_groupHeaderHeight;
}
- if (index == firstItemIndexOfGroup) {
+ if (m_groupItemIndexes.contains(index)) {
if (!horizontalScrolling) {
// The item is the first item of a group.
// Increase the y-position to provide space
// for the group header.
- y += GroupHeaderHeight;
- }
-
- // Calculate the index of the first item for
- // the next group
- ++groupIndex;
- if (groupIndex < m_groups.count()) {
- firstItemIndexOfGroup = m_groups.at(groupIndex);
- } else {
- firstItemIndexOfGroup = -1;
+ y += m_groupHeaderHeight;
}
}
}
}
const QRectF bounds(x, y, itemSize.width(), requiredItemHeight);
- if (index < m_itemBoundingRects.count()) {
- m_itemBoundingRects[index] = bounds;
+ if (index < m_itemRects.count()) {
+ m_itemRects[index] = bounds;
} else {
- m_itemBoundingRects.append(bounds);
+ m_itemRects.append(bounds);
}
maxItemHeight = qMax(maxItemHeight, requiredItemHeight);
++index;
++column;
- if (grouped && index == firstItemIndexOfGroup) {
+ if (grouped && m_groupItemIndexes.contains(index)) {
// The item represents the first index of a group
// and must aligned in the first column
break;
y += maxItemHeight;
++rowIndex;
}
- if (m_itemBoundingRects.count() > itemCount) {
- m_itemBoundingRects.erase(m_itemBoundingRects.begin() + itemCount,
- m_itemBoundingRects.end());
+ if (m_itemRects.count() > itemCount) {
+ m_itemRects.erase(m_itemRects.begin() + itemCount,
+ m_itemRects.end());
}
if (itemCount > 0) {
- m_maximumScrollOffset = m_itemBoundingRects.last().bottom();
+ m_maximumScrollOffset = m_itemRects.last().bottom();
m_maximumItemOffset = m_columnCount * m_columnWidth;
} else {
m_maximumScrollOffset = 0;
int mid = 0;
do {
mid = (min + max) / 2;
- if (m_itemBoundingRects[mid].bottom() < m_scrollOffset) {
+ if (m_itemRects[mid].bottom() < m_scrollOffset) {
min = mid + 1;
} else {
max = mid - 1;
}
} while (min <= max);
- while (mid < maxIndex && m_itemBoundingRects[mid].bottom() < m_scrollOffset) {
+ while (mid < maxIndex && m_itemRects[mid].bottom() < m_scrollOffset) {
++mid;
}
m_firstVisibleIndex = mid;
const int visibleHeight = (m_scrollOrientation == Qt::Horizontal) ? m_size.width() : m_size.height();
qreal bottom = m_scrollOffset + visibleHeight;
if (m_model->groupedSorting()) {
- bottom += GroupHeaderHeight;
+ bottom += m_groupHeaderHeight;
}
min = m_firstVisibleIndex;
max = maxIndex;
do {
mid = (min + max) / 2;
- if (m_itemBoundingRects[mid].y() <= bottom) {
+ if (m_itemRects[mid].y() <= bottom) {
min = mid + 1;
} else {
max = mid - 1;
}
} while (min <= max);
- while (mid > 0 && m_itemBoundingRects[mid].y() > bottom) {
+ while (mid > 0 && m_itemRects[mid].y() > bottom) {
--mid;
}
m_lastVisibleIndex = mid;
return false;
}
- m_groups.clear();
- m_groupIndexes.clear();
+ m_groupItemIndexes.clear();
const QList<QPair<int, QVariant> > groups = m_model->groups();
if (groups.isEmpty()) {
return false;
}
- m_groups.reserve(groups.count());
for (int i = 0; i < groups.count(); ++i) {
const int firstItemIndex = groups.at(i).first;
- m_groups.append(firstItemIndex);
- m_groupIndexes.insert(firstItemIndex);
+ m_groupItemIndexes.insert(firstItemIndex);
}
return true;
void setHeaderHeight(qreal height);
qreal headerHeight() const;
+ /**
+ * Sets the height of the group header that is used
+ * to indicate a new item group.
+ */
+ void setGroupHeaderHeight(qreal height);
+ qreal groupHeaderHeight() const;
+
// TODO: add note that offset can be < 0 or > maximumOffset!
void setScrollOffset(qreal scrollOffset);
qreal scrollOffset() const;
void setSizeHintResolver(const KItemListSizeHintResolver* sizeHintResolver);
const KItemListSizeHintResolver* sizeHintResolver() const;
-
- // TODO: mention that return value is -1 if count == 0
+ /**
+ * @return The first (at least partly) visible index. -1 is returned
+ * if the item count is 0.
+ */
int firstVisibleIndex() const;
- // TODO: mention that return value is -1 if count == 0
+ /**
+ * @return The last (at least partly) visible index. -1 is returned
+ * if the item count is 0.
+ */
int lastVisibleIndex() const;
- QRectF itemBoundingRect(int index) const;
+ /**
+ * @return Rectangle of the item with the index \a index.
+ * The top/left of the bounding rectangle is related to
+ * the top/left of the KItemListView. An empty rectangle
+ * is returned if an invalid index is given.
+ */
+ QRectF itemRect(int index) const;
+ QRectF groupHeaderRect(int index) const;
+
+ /**
+ * @return Maximum number of (at least partly) visible items for
+ * the given size.
+ */
int maximumVisibleItems() const;
+ /**
+ * @return Maximum number of items that can be shown in the same row
+ * (= vertical scrolldirection) or same column
+ * (= horizontal scrolldirection).
+ */
int itemsPerOffset() const;
/**
qreal m_xPosInc;
int m_columnCount;
- QList<int> m_groups;
-
// Stores all item indexes that are the first item of a group.
// Assures fast access for KItemListViewLayouter::isFirstGroupItem().
- QSet<int> m_groupIndexes;
+ QSet<int> m_groupItemIndexes;
+ qreal m_groupHeaderHeight;
- QList<QRectF> m_itemBoundingRects;
+ QList<QRectF> m_itemRects;
};
#endif
painter->fillRect(backgroundRect, backgroundColor);
}
- const QRect iconBounds = iconBoundingRect().toRect();
+ const QRect iconBounds = iconRect().toRect();
if (m_selected) {
QStyleOptionViewItemV4 viewItemOption;
viewItemOption.initFrom(widget);
if (isCurrent()) {
QStyleOptionViewItemV4 viewItemOption;
viewItemOption.initFrom(widget);
- viewItemOption.rect = textBoundingRect().toRect();
+ viewItemOption.rect = textRect().toRect();
viewItemOption.state = QStyle::State_Enabled | QStyle::State_Item;
viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
style()->drawPrimitive(QStyle::PE_FrameFocusRect, &viewItemOption, painter, widget);
return false;
}
- return iconBoundingRect().contains(point) ||
- textBoundingRect().contains(point) ||
+ return iconRect().contains(point) ||
+ textRect().contains(point) ||
expansionToggleRect().contains(point) ||
selectionToggleRect().contains(point);
}
{
const qreal opacity = painter->opacity();
- QRectF textBounds = textBoundingRect();
+ QRectF textBounds = textRect();
const qreal marginDiff = m_styleOption.margin / 2;
textBounds.adjust(marginDiff, marginDiff, -marginDiff, -marginDiff);
painter->setOpacity(opacity * 0.1);
QHash<QByteArray, QVariant> data() const;
/**
- * Draws the hover-bounding-rectangle if the item is hovered. Overwrite this method
+ * Draws the hover-rectangle if the item is hovered. Overwrite this method
* to show the data of the custom model provided by KItemListWidget::data().
* @reimp
*/
bool alternatingBackgroundColors() const;
/**
- * @return True if \a point is inside KItemListWidget::hoverBoundingRect(),
- * KItemListWidget::textBoundingRect(), KItemListWidget::selectionToggleRect()
+ * @return True if \a point is inside KItemListWidget::hoverRect(),
+ * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
* or KItemListWidget::expansionToggleRect().
* @reimp
*/
virtual bool contains(const QPointF& point) const;
/**
- * @return Bounding rectangle for the area that shows the icon.
+ * @return Rectangle for the area that shows the icon.
*/
- virtual QRectF iconBoundingRect() const = 0;
+ virtual QRectF iconRect() const = 0;
/**
- * @return Bounding rectangle for the area that contains the text-properties.
+ * @return Rectangle for the area that contains the text-properties.
*/
- virtual QRectF textBoundingRect() const = 0;
+ virtual QRectF textRect() const = 0;
/**
* @return Rectangle for the selection-toggle that is used to select or deselect an item.
const KFileItem item = fileItemModel()->fileItem(index);
if (GeneralSettings::showToolTips()) {
- QRectF itemRect = m_container->controller()->view()->itemBoundingRect(index);
+ QRectF itemRect = m_container->controller()->view()->itemRect(index);
const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
itemRect.moveTo(pos);