#include "kfileitemlistview.h"
-#include "kfileitemlistgroupheader.h"
#include "kfileitemmodelrolesupdater.h"
#include "kfileitemlistwidget.h"
#include "kfileitemmodel.h"
-#include "kpixmapmodifier_p.h"
-#include <KLocale>
-#include <KStringHandler>
+#include "private/kpixmapmodifier.h"
-#include <KDebug>
-#include <KIcon>
+#include <QIcon>
+#include <KIconLoader>
#include <QPainter>
-#include <QTextLine>
#include <QTimer>
// #define KFILEITEMLISTVIEW_DEBUG
namespace {
+ // If the visible index range changes, KFileItemModelRolesUpdater is not
+ // informed immediatetly, but with a short delay. This ensures that scrolling
+ // always feels smooth and is not interrupted by icon loading (which can be
+ // quite expensive if a disk access is required to determine the final icon).
const int ShortInterval = 50;
+
+ // If the icon size changes, a longer delay is used. This prevents that
+ // the expensive re-generation of all previews is triggered repeatedly when
+ // changing the zoom level.
const int LongInterval = 300;
}
KFileItemListView::KFileItemListView(QGraphicsWidget* parent) :
- KItemListView(parent),
- m_itemLayout(IconsLayout),
+ KStandardItemListView(parent),
m_modelRolesUpdater(0),
m_updateVisibleIndexRangeTimer(0),
- m_updateIconSizeTimer(0),
- m_minimumRolesWidths()
+ m_updateIconSizeTimer(0)
{
setAcceptDrops(true);
setScrollOrientation(Qt::Vertical);
- setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
- setGroupHeaderCreator(new KItemListGroupHeaderCreator<KFileItemListGroupHeader>());
m_updateVisibleIndexRangeTimer = new QTimer(this);
m_updateVisibleIndexRangeTimer->setSingleShot(true);
m_updateVisibleIndexRangeTimer->setInterval(ShortInterval);
- connect(m_updateVisibleIndexRangeTimer, SIGNAL(timeout()), this, SLOT(updateVisibleIndexRange()));
+ connect(m_updateVisibleIndexRangeTimer, &QTimer::timeout, this, &KFileItemListView::updateVisibleIndexRange);
m_updateIconSizeTimer = new QTimer(this);
m_updateIconSizeTimer->setSingleShot(true);
- m_updateIconSizeTimer->setInterval(ShortInterval);
- connect(m_updateIconSizeTimer, SIGNAL(timeout()), this, SLOT(updateIconSize()));
-
- setVisibleRoles(QList<QByteArray>() << "name");
+ m_updateIconSizeTimer->setInterval(LongInterval);
+ connect(m_updateIconSizeTimer, &QTimer::timeout, this, &KFileItemListView::updateIconSize);
- updateMinimumRolesWidths();
+ setVisibleRoles({"text"});
}
KFileItemListView::~KFileItemListView()
{
- // The group headers are children of the widgets created by
- // widgetCreator(). So it is mandatory to delete the group headers
- // first.
- delete groupHeaderCreator();
- delete widgetCreator();
-
- delete m_modelRolesUpdater;
- m_modelRolesUpdater = 0;
}
void KFileItemListView::setPreviewsShown(bool show)
{
- if (m_modelRolesUpdater) {
- m_modelRolesUpdater->setPreviewShown(show);
+ if (!m_modelRolesUpdater) {
+ return;
+ }
+
+ if (m_modelRolesUpdater->previewsShown() != show) {
+ beginTransaction();
+ m_modelRolesUpdater->setPreviewsShown(show);
+ onPreviewsShownChanged(show);
+ endTransaction();
}
}
bool KFileItemListView::previewsShown() const
{
- return m_modelRolesUpdater->isPreviewShown();
+ return m_modelRolesUpdater ? m_modelRolesUpdater->previewsShown() : false;
}
-void KFileItemListView::setItemLayout(Layout layout)
+void KFileItemListView::setEnlargeSmallPreviews(bool enlarge)
{
- if (m_itemLayout != layout) {
- m_itemLayout = layout;
- updateLayoutOfVisibleItems();
+ if (m_modelRolesUpdater) {
+ m_modelRolesUpdater->setEnlargeSmallPreviews(enlarge);
}
}
-KFileItemListView::Layout KFileItemListView::itemLayout() const
+bool KFileItemListView::enlargeSmallPreviews() const
{
- return m_itemLayout;
+ return m_modelRolesUpdater ? m_modelRolesUpdater->enlargeSmallPreviews() : false;
}
void KFileItemListView::setEnabledPlugins(const QStringList& list)
return m_modelRolesUpdater ? m_modelRolesUpdater->enabledPlugins() : QStringList();
}
-QSizeF KFileItemListView::itemSizeHint(int index) const
-{
- const QHash<QByteArray, QVariant> values = model()->data(index);
- const KItemListStyleOption& option = styleOption();
- const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
-
- switch (m_itemLayout) {
- case IconsLayout: {
- const QString text = KStringHandler::preProcessWrap(values["name"].toString());
-
- const qreal maxWidth = itemSize().width() - 2 * option.margin;
- int textLinesCount = 0;
- QTextLine line;
-
- // Calculate the number of lines required for wrapping the name
- QTextOption textOption(Qt::AlignHCenter);
- textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
-
- QTextLayout layout(text, option.font);
- layout.setTextOption(textOption);
- layout.beginLayout();
- while ((line = layout.createLine()).isValid()) {
- line.setLineWidth(maxWidth);
- line.naturalTextWidth();
- ++textLinesCount;
- }
- layout.endLayout();
-
- // Add one line for each additional information
- textLinesCount += additionalRolesCount;
-
- const qreal height = textLinesCount * option.fontMetrics.height() +
- option.iconSize +
- option.margin * 4;
- return QSizeF(itemSize().width(), height);
- }
-
- case CompactLayout: {
- // For each row exactly one role is shown. Calculate the maximum required width that is necessary
- // to show all roles without horizontal clipping.
- qreal maximumRequiredWidth = 0.0;
-
- foreach (const QByteArray& role, visibleRoles()) {
- const QString text = KFileItemListWidget::roleText(role, values);
- const qreal requiredWidth = option.fontMetrics.width(text);
- maximumRequiredWidth = qMax(maximumRequiredWidth, requiredWidth);
- }
-
- const qreal width = option.margin * 4 + option.iconSize + maximumRequiredWidth;
- const qreal height = option.margin * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.height());
- return QSizeF(width, height);
- }
-
- case DetailsLayout: {
- // The width will be determined dynamically by KFileItemListView::visibleRoleSizes()
- const qreal height = option.margin * 2 + qMax(option.iconSize, option.fontMetrics.height());
- return QSizeF(-1, height);
- }
-
- default:
- Q_ASSERT(false);
- break;
- }
-
- return QSize();
-}
-
-QHash<QByteArray, QSizeF> KFileItemListView::visibleRolesSizes(const KItemRangeList& itemRanges) const
-{
- QElapsedTimer timer;
- timer.start();
-
- QHash<QByteArray, QSizeF> sizes;
-
- int calculatedItemCount = 0;
- bool maxTimeExceeded = false;
- foreach (const KItemRange& itemRange, itemRanges) {
- const int startIndex = itemRange.index;
- const int endIndex = startIndex + itemRange.count - 1;
-
- for (int i = startIndex; i <= endIndex; ++i) {
- foreach (const QByteArray& visibleRole, visibleRoles()) {
- QSizeF maxSize = sizes.value(visibleRole, QSizeF(0, 0));
- const QSizeF itemSize = visibleRoleSizeHint(i, visibleRole);
- maxSize = maxSize.expandedTo(itemSize);
- sizes.insert(visibleRole, maxSize);
- }
-
- if (calculatedItemCount > 100 && timer.elapsed() > 200) {
- // When having several thousands of items calculating the sizes can get
- // very expensive. We accept a possibly too small role-size in favour
- // of having no blocking user interface.
- #ifdef KFILEITEMLISTVIEW_DEBUG
- kDebug() << "Timer exceeded, stopped after" << calculatedItemCount << "items";
- #endif
- maxTimeExceeded = true;
- break;
- }
- ++calculatedItemCount;
- }
- if (maxTimeExceeded) {
- break;
- }
- }
-
-#ifdef KFILEITEMLISTVIEW_DEBUG
- int rangesItemCount = 0;
- foreach (const KItemRange& itemRange, itemRanges) {
- rangesItemCount += itemRange.count;
- }
- kDebug() << "[TIME] Calculated dynamic item size for " << rangesItemCount << "items:" << timer.elapsed();
-#endif
- return sizes;
-}
-
-bool KFileItemListView::supportsItemExpanding() const
-{
- return m_itemLayout == DetailsLayout;
-}
-
-QPixmap KFileItemListView::createDragPixmap(const QSet<int>& indexes) const
+QPixmap KFileItemListView::createDragPixmap(const KItemSet& indexes) const
{
if (!model()) {
return QPixmap();
const int itemCount = indexes.count();
Q_ASSERT(itemCount > 0);
+ if (itemCount == 1) {
+ return KItemListView::createDragPixmap(indexes);
+ }
// If more than one item is dragged, align the items inside a
// rectangular grid. The maximum grid size is limited to 5 x 5 items.
QPainter painter(&dragPixmap);
int x = 0;
int y = 0;
- QSetIterator<int> it(indexes);
- while (it.hasNext()) {
- const int index = it.next();
+ foreach (int index, indexes) {
QPixmap pixmap = model()->data(index).value("iconPixmap").value<QPixmap>();
if (pixmap.isNull()) {
- KIcon icon(model()->data(index).value("iconName").toString());
+ QIcon icon = QIcon::fromTheme(model()->data(index).value("iconName").toString());
pixmap = icon.pixmap(size, size);
} else {
KPixmapModifier::scale(pixmap, QSize(size, size));
return dragPixmap;
}
+KItemListWidgetCreatorBase* KFileItemListView::defaultWidgetCreator() const
+{
+ return new KItemListWidgetCreator<KFileItemListWidget>();
+}
+
void KFileItemListView::initializeItemListWidget(KItemListWidget* item)
{
- KFileItemListWidget* fileItemListWidget = static_cast<KFileItemListWidget*>(item);
+ KStandardItemListView::initializeItemListWidget(item);
- switch (m_itemLayout) {
- case IconsLayout: fileItemListWidget->setLayout(KFileItemListWidget::IconsLayout); break;
- case CompactLayout: fileItemListWidget->setLayout(KFileItemListWidget::CompactLayout); break;
- case DetailsLayout: fileItemListWidget->setLayout(KFileItemListWidget::DetailsLayout); break;
- default: Q_ASSERT(false); break;
- }
+ // Make sure that the item has an icon.
+ QHash<QByteArray, QVariant> data = item->data();
+ if (!data.contains("iconName") && data["iconPixmap"].value<QPixmap>().isNull()) {
+ Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
+ KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
- fileItemListWidget->setAlternatingBackgroundColors(m_itemLayout == DetailsLayout &&
- visibleRoles().count() > 1);
+ const KFileItem fileItem = fileItemModel->fileItem(item->index());
+ data.insert("iconName", fileItem.iconName());
+ item->setData(data, {"iconName"});
+ }
}
-bool KFileItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
+void KFileItemListView::onPreviewsShownChanged(bool shown)
{
- // Even if the icons have a different size they are always aligned within
- // the area defined by KItemStyleOption.iconSize and hence result in no
- // change of the item-size.
- const bool containsIconName = changedRoles.contains("iconName");
- const bool containsIconPixmap = changedRoles.contains("iconPixmap");
- const int count = changedRoles.count();
+ Q_UNUSED(shown);
+}
- const bool iconChanged = (containsIconName && containsIconPixmap && count == 2) ||
- (containsIconName && count == 1) ||
- (containsIconPixmap && count == 1);
- return !iconChanged;
+void KFileItemListView::onItemLayoutChanged(ItemLayout current, ItemLayout previous)
+{
+ KStandardItemListView::onItemLayoutChanged(current, previous);
+ triggerVisibleIndexRangeUpdate();
}
void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
{
- Q_UNUSED(previous);
Q_ASSERT(qobject_cast<KFileItemModel*>(current));
+ KStandardItemListView::onModelChanged(current, previous);
delete m_modelRolesUpdater;
- m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
- const int size = styleOption().iconSize;
- m_modelRolesUpdater->setIconSize(QSize(size, size));
+ m_modelRolesUpdater = 0;
- applyRolesToModel();
+ if (current) {
+ m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
+ m_modelRolesUpdater->setIconSize(availableIconSize());
+
+ applyRolesToModel();
+ }
}
void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
- updateLayoutOfVisibleItems();
+ KStandardItemListView::onScrollOrientationChanged(current, previous);
+ triggerVisibleIndexRangeUpdate();
}
void KFileItemListView::onItemSizeChanged(const QSizeF& current, const QSizeF& previous)
void KFileItemListView::onScrollOffsetChanged(qreal current, qreal previous)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ KStandardItemListView::onScrollOffsetChanged(current, previous);
triggerVisibleIndexRangeUpdate();
}
void KFileItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ KStandardItemListView::onVisibleRolesChanged(current, previous);
applyRolesToModel();
-
- if (m_itemLayout == DetailsLayout) {
- // Only enable the alternating background colors if more than one role
- // is visible
- const int previousCount = previous.count();
- const int currentCount = current.count();
- if ((previousCount <= 1 && currentCount > 1) || (previousCount > 1 && currentCount <= 1)) {
- const bool enabled = (currentCount > 1);
- foreach (KItemListWidget* widget, visibleItemListWidgets()) {
- widget->setAlternatingBackgroundColors(enabled);
- }
- }
- }
}
void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ KStandardItemListView::onStyleOptionChanged(current, previous);
triggerIconSizeUpdate();
}
+void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
+{
+ applyRolesToModel();
+ KStandardItemListView::onSupportsItemExpandingChanged(supportsExpanding);
+ triggerVisibleIndexRangeUpdate();
+}
+
void KFileItemListView::onTransactionBegin()
{
- m_modelRolesUpdater->setPaused(true);
+ if (m_modelRolesUpdater) {
+ m_modelRolesUpdater->setPaused(true);
+ }
}
void KFileItemListView::onTransactionEnd()
{
+ if (!m_modelRolesUpdater) {
+ return;
+ }
+
// Only unpause the model-roles-updater if no timer is active. If one
// timer is still active the model-roles-updater will be unpaused later as
// soon as the timer has been exceeded.
void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent* event)
{
- KItemListView::resizeEvent(event);
+ KStandardItemListView::resizeEvent(event);
triggerVisibleIndexRangeUpdate();
}
void KFileItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
{
- KItemListView::slotItemsRemoved(itemRanges);
- updateTimersInterval();
+ KStandardItemListView::slotItemsRemoved(itemRanges);
}
void KFileItemListView::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
applyRolesToModel();
}
- KItemListView::slotSortRoleChanged(current, previous);
+ KStandardItemListView::slotSortRoleChanged(current, previous);
}
void KFileItemListView::triggerVisibleIndexRangeUpdate()
return;
}
m_modelRolesUpdater->setPaused(true);
- m_updateVisibleIndexRangeTimer->start();
+
+ // If the icon size has been changed recently, wait until
+ // m_updateIconSizeTimer expires.
+ if (!m_updateIconSizeTimer->isActive()) {
+ m_updateVisibleIndexRangeTimer->start();
+ }
}
void KFileItemListView::updateVisibleIndexRange()
const int index = firstVisibleIndex();
const int count = lastVisibleIndex() - index + 1;
+ m_modelRolesUpdater->setMaximumVisibleItems(maximumVisibleItems());
m_modelRolesUpdater->setVisibleIndexRange(index, count);
-
- if (m_updateIconSizeTimer->isActive()) {
- // If the icon-size update is pending do an immediate update
- // of the icon-size before unpausing m_modelRolesUpdater. This prevents
- // an unnecessary expensive recreation of all previews afterwards.
- m_updateIconSizeTimer->stop();
- const KItemListStyleOption& option = styleOption();
- m_modelRolesUpdater->setIconSize(QSize(option.iconSize, option.iconSize));
- }
-
m_modelRolesUpdater->setPaused(isTransactionActive());
- updateTimersInterval();
}
void KFileItemListView::triggerIconSizeUpdate()
}
m_modelRolesUpdater->setPaused(true);
m_updateIconSizeTimer->start();
+
+ // The visible index range will be updated when m_updateIconSizeTimer expires.
+ // Stop m_updateVisibleIndexRangeTimer to prevent an expensive re-generation
+ // of all previews (note that the user might change the icon size again soon).
+ m_updateVisibleIndexRangeTimer->stop();
}
void KFileItemListView::updateIconSize()
return;
}
- const KItemListStyleOption& option = styleOption();
- m_modelRolesUpdater->setIconSize(QSize(option.iconSize, option.iconSize));
-
- if (m_updateVisibleIndexRangeTimer->isActive()) {
- // If the visibility-index-range update is pending do an immediate update
- // of the range before unpausing m_modelRolesUpdater. This prevents
- // an unnecessary expensive recreation of all previews afterwards.
- m_updateVisibleIndexRangeTimer->stop();
- const int index = firstVisibleIndex();
- const int count = lastVisibleIndex() - index + 1;
- m_modelRolesUpdater->setVisibleIndexRange(index, count);
- }
-
- m_modelRolesUpdater->setPaused(isTransactionActive());
- updateTimersInterval();
-}
-
-QSizeF KFileItemListView::visibleRoleSizeHint(int index, const QByteArray& role) const
-{
- const KItemListStyleOption& option = styleOption();
-
- qreal width = m_minimumRolesWidths.value(role, 0);
- const qreal height = option.margin * 2 + option.fontMetrics.height();
-
- const QHash<QByteArray, QVariant> values = model()->data(index);
- const QString text = KFileItemListWidget::roleText(role, values);
- if (!text.isEmpty()) {
- const qreal columnMargin = option.margin * 3;
- width = qMax(width, qreal(2 * columnMargin + option.fontMetrics.width(text)));
- }
-
- if (role == "name") {
- // Increase the width by the expansion-toggle and the current expansion level
- const int expansionLevel = values.value("expansionLevel", 0).toInt();
- width += option.margin + expansionLevel * itemSize().height() + KIconLoader::SizeSmall;
+ m_modelRolesUpdater->setIconSize(availableIconSize());
- // Increase the width by the required space for the icon
- width += option.margin * 2 + option.iconSize;
- }
-
- return QSizeF(width, height);
-}
-
-void KFileItemListView::updateLayoutOfVisibleItems()
-{
- if (!model()) {
- return;
- }
-
- foreach (KItemListWidget* widget, visibleItemListWidgets()) {
- initializeItemListWidget(widget);
- }
- triggerVisibleIndexRangeUpdate();
-}
-
-void KFileItemListView::updateTimersInterval()
-{
- if (!model()) {
- return;
- }
-
- // The ShortInterval is used for cases like switching the directory: If the
- // model is empty and filled later the creation of the previews should be done
- // as soon as possible. The LongInterval is used when the model already contains
- // items and assures that operations like zooming don't result in too many temporary
- // recreations of the previews.
-
- const int interval = (model()->count() <= 0) ? ShortInterval : LongInterval;
- m_updateVisibleIndexRangeTimer->setInterval(interval);
- m_updateIconSizeTimer->setInterval(interval);
-}
-
-void KFileItemListView::updateMinimumRolesWidths()
-{
- m_minimumRolesWidths.clear();
+ // Update the visible index range (which has most likely changed after the
+ // icon size change) before unpausing m_modelRolesUpdater.
+ const int index = firstVisibleIndex();
+ const int count = lastVisibleIndex() - index + 1;
+ m_modelRolesUpdater->setVisibleIndexRange(index, count);
- const KItemListStyleOption& option = styleOption();
- const QString sizeText = QLatin1String("888888") + i18nc("@item:intable", "items");
- m_minimumRolesWidths.insert("size", option.fontMetrics.width(sizeText));
+ m_modelRolesUpdater->setPaused(isTransactionActive());
}
void KFileItemListView::applyRolesToModel()
QSet<QByteArray> roles = visibleRoles().toSet();
roles.insert("iconPixmap");
roles.insert("iconName");
- roles.insert("name");
+ roles.insert("text");
roles.insert("isDir");
- if (m_itemLayout == DetailsLayout) {
+ roles.insert("isLink");
+ if (supportsItemExpanding()) {
roles.insert("isExpanded");
- roles.insert("expansionLevel");
+ roles.insert("isExpandable");
+ roles.insert("expandedParentsCount");
}
// Assure that the role that is used for sorting will be determined
m_modelRolesUpdater->setRoles(roles);
}
-#include "kfileitemlistview.moc"
+QSize KFileItemListView::availableIconSize() const
+{
+ const KItemListStyleOption& option = styleOption();
+ const int iconSize = option.iconSize;
+ if (itemLayout() == IconsLayout) {
+ const int maxIconWidth = itemSize().width() - 2 * option.padding;
+ return QSize(maxIconWidth, iconSize);
+ }
+
+ return QSize(iconSize, iconSize);
+}
+