#include "kfileitemlistview.h"
-#include "kitemlistgroupheader.h"
+#include "kfileitemlistgroupheader.h"
#include "kfileitemmodelrolesupdater.h"
#include "kfileitemlistwidget.h"
#include "kfileitemmodel.h"
+#include "kpixmapmodifier_p.h"
#include <KLocale>
#include <KStringHandler>
#include <KDebug>
#include <KIcon>
+#include <QPainter>
#include <QTextLine>
#include <QTimer>
-#define KFILEITEMLISTVIEW_DEBUG
+// #define KFILEITEMLISTVIEW_DEBUG
namespace {
const int ShortInterval = 50;
m_itemLayout(IconsLayout),
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<KItemListGroupHeader>());
+ setGroupHeaderCreator(new KItemListGroupHeaderCreator<KFileItemListGroupHeader>());
m_updateVisibleIndexRangeTimer = new QTimer(this);
m_updateVisibleIndexRangeTimer->setSingleShot(true);
m_updateIconSizeTimer->setInterval(ShortInterval);
connect(m_updateIconSizeTimer, SIGNAL(timeout()), this, SLOT(updateIconSize()));
- updateMinimumRolesWidths();
+ setVisibleRoles(QList<QByteArray>() << "name");
}
KFileItemListView::~KFileItemListView()
{
- delete widgetCreator();
+ // 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);
+ m_modelRolesUpdater->setPreviewsShown(show);
}
}
bool KFileItemListView::previewsShown() const
{
- return m_modelRolesUpdater->isPreviewShown();
+ return m_modelRolesUpdater ? m_modelRolesUpdater->previewsShown() : false;
+}
+
+void KFileItemListView::setEnlargeSmallPreviews(bool enlarge)
+{
+ if (m_modelRolesUpdater) {
+ m_modelRolesUpdater->setEnlargeSmallPreviews(enlarge);
+ }
+}
+
+bool KFileItemListView::enlargeSmallPreviews() const
+{
+ return m_modelRolesUpdater ? m_modelRolesUpdater->enlargeSmallPreviews() : false;
}
void KFileItemListView::setItemLayout(Layout layout)
{
if (m_itemLayout != layout) {
+ const bool updateRoles = (m_itemLayout == DetailsLayout || layout == DetailsLayout);
m_itemLayout = layout;
+ if (updateRoles) {
+ // The details-layout requires some invisible roles that
+ // must be added to the model if the new layout is "details".
+ // If the old layout was "details" the roles will get removed.
+ applyRolesToModel();
+ }
updateLayoutOfVisibleItems();
}
}
return m_itemLayout;
}
-QSizeF KFileItemListView::itemSizeHint(int index) const
+void KFileItemListView::setEnabledPlugins(const QStringList& list)
{
- 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);
+ if (m_modelRolesUpdater) {
+ m_modelRolesUpdater->setEnabledPlugins(list);
}
+}
- 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);
- }
+QStringList KFileItemListView::enabledPlugins() const
+{
+ return m_modelRolesUpdater ? m_modelRolesUpdater->enabledPlugins() : QStringList();
+}
- 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);
+QPixmap KFileItemListView::createDragPixmap(const QSet<int>& indexes) const
+{
+ if (!model()) {
+ return QPixmap();
}
- 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);
+ const int itemCount = indexes.count();
+ Q_ASSERT(itemCount > 0);
+
+ // 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.
+ int xCount;
+ int size;
+ if (itemCount > 16) {
+ xCount = 5;
+ size = KIconLoader::SizeSmall;
+ } else if (itemCount > 9) {
+ xCount = 4;
+ size = KIconLoader::SizeSmallMedium;
+ } else {
+ xCount = 3;
+ size = KIconLoader::SizeMedium;
}
- default:
- Q_ASSERT(false);
- break;
+ if (itemCount < xCount) {
+ xCount = itemCount;
}
- 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;
- }
- }
- if (maxTimeExceeded) {
- break;
- }
- ++calculatedItemCount;
+ int yCount = itemCount / xCount;
+ if (itemCount % xCount != 0) {
+ ++yCount;
+ }
+ if (yCount > xCount) {
+ yCount = xCount;
}
- // Stretch the width of the first role so that the full visible view-width
- // is used to show all roles.
- const qreal availableWidth = size().width();
+ // Draw the selected items into the grid cells.
+ QPixmap dragPixmap(xCount * size + xCount, yCount * size + yCount);
+ dragPixmap.fill(Qt::transparent);
- qreal usedWidth = 0;
- QHashIterator<QByteArray, QSizeF> it(sizes);
+ QPainter painter(&dragPixmap);
+ int x = 0;
+ int y = 0;
+ QSetIterator<int> it(indexes);
while (it.hasNext()) {
- it.next();
- usedWidth += it.value().width();
- }
+ const int index = it.next();
+
+ QPixmap pixmap = model()->data(index).value("iconPixmap").value<QPixmap>();
+ if (pixmap.isNull()) {
+ KIcon icon(model()->data(index).value("iconName").toString());
+ pixmap = icon.pixmap(size, size);
+ } else {
+ KPixmapModifier::scale(pixmap, QSize(size, size));
+ }
- if (usedWidth < availableWidth) {
- const QByteArray role = visibleRoles().first();
- QSizeF firstRoleSize = sizes.value(role);
- firstRoleSize.rwidth() += availableWidth - usedWidth;
- sizes.insert(role, firstRoleSize);
- }
+ painter.drawPixmap(x, y, pixmap);
-#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;
-}
+ x += size + 1;
+ if (x >= dragPixmap.width()) {
+ x = 0;
+ y += size + 1;
+ }
-QPixmap KFileItemListView::createDragPixmap(const QSet<int>& indexes) const
-{
- QPixmap pixmap;
-
- if (model()) {
- QSetIterator<int> it(indexes);
- while (it.hasNext()) {
- const int index = it.next();
- // TODO: Only one item is considered currently
- pixmap = model()->data(index).value("iconPixmap").value<QPixmap>();
- if (pixmap.isNull()) {
- KIcon icon(model()->data(index).value("iconName").toString());
- pixmap = icon.pixmap(itemSize().toSize());
- }
+ if (y >= dragPixmap.height()) {
+ break;
}
}
- return pixmap;
+ return dragPixmap;
}
void KFileItemListView::initializeItemListWidget(KItemListWidget* item)
case DetailsLayout: fileItemListWidget->setLayout(KFileItemListWidget::DetailsLayout); break;
default: Q_ASSERT(false); break;
}
+
+ fileItemListWidget->setSupportsItemExpanding(supportsItemExpanding());
}
bool KFileItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
Q_UNUSED(previous);
Q_ASSERT(qobject_cast<KFileItemModel*>(current));
- if (m_modelRolesUpdater) {
- delete m_modelRolesUpdater;
- }
-
+ 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->setIconSize(availableIconSize());
+
+ applyRolesToModel();
}
void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
void KFileItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
{
+ Q_UNUSED(current);
Q_UNUSED(previous);
-
- Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
- KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
-
- // KFileItemModel does not distinct between "visible" and "invisible" roles.
- // Add all roles that are mandatory for having a working KFileItemListView:
- QSet<QByteArray> keys = current.toSet();
- QSet<QByteArray> roles = keys;
- roles.insert("iconPixmap");
- roles.insert("iconName");
- roles.insert("name"); // TODO: just don't allow to disable it
- roles.insert("isDir");
- if (m_itemLayout == DetailsLayout) {
- roles.insert("isExpanded");
- roles.insert("expansionLevel");
- }
-
- fileItemModel->setRoles(roles);
-
- m_modelRolesUpdater->setRoles(keys);
+ applyRolesToModel();
}
void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
triggerIconSizeUpdate();
}
+void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
+{
+ Q_UNUSED(supportsExpanding);
+ applyRolesToModel();
+ updateLayoutOfVisibleItems();
+}
+
void KFileItemListView::onTransactionBegin()
{
m_modelRolesUpdater->setPaused(true);
updateTimersInterval();
}
+void KFileItemListView::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
+{
+ const QByteArray sortRole = model()->sortRole();
+ if (!visibleRoles().contains(sortRole)) {
+ applyRolesToModel();
+ }
+
+ KItemListView::slotSortRoleChanged(current, previous);
+}
+
void KFileItemListView::triggerVisibleIndexRangeUpdate()
{
+ if (!model()) {
+ return;
+ }
m_modelRolesUpdater->setPaused(true);
m_updateVisibleIndexRangeTimer->start();
}
// 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->setIconSize(availableIconSize());
}
m_modelRolesUpdater->setPaused(isTransactionActive());
void KFileItemListView::triggerIconSizeUpdate()
{
+ if (!model()) {
+ return;
+ }
m_modelRolesUpdater->setPaused(true);
m_updateIconSizeTimer->start();
}
return;
}
- const KItemListStyleOption& option = styleOption();
- m_modelRolesUpdater->setIconSize(QSize(option.iconSize, option.iconSize));
+ m_modelRolesUpdater->setIconSize(availableIconSize());
if (m_updateVisibleIndexRangeTimer->isActive()) {
// If the visibility-index-range update is pending do an immediate update
updateTimersInterval();
}
-QSizeF KFileItemListView::visibleRoleSizeHint(int index, const QByteArray& role) const
+void KFileItemListView::updateLayoutOfVisibleItems()
{
- 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;
-
- // Increase the width by the required space for the icon
- width += option.margin * 2 + option.iconSize;
+ if (!model()) {
+ return;
}
- return QSizeF(width, height);
-}
-
-void KFileItemListView::updateLayoutOfVisibleItems()
-{
foreach (KItemListWidget* widget, visibleItemListWidgets()) {
initializeItemListWidget(widget);
}
m_updateIconSizeTimer->setInterval(interval);
}
-void KFileItemListView::updateMinimumRolesWidths()
+void KFileItemListView::applyRolesToModel()
{
- m_minimumRolesWidths.clear();
+ if (!model()) {
+ return;
+ }
+ Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
+ KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
+
+ // KFileItemModel does not distinct between "visible" and "invisible" roles.
+ // Add all roles that are mandatory for having a working KFileItemListView:
+ QSet<QByteArray> roles = visibleRoles().toSet();
+ roles.insert("iconPixmap");
+ roles.insert("iconName");
+ roles.insert("name");
+ roles.insert("isDir");
+ if (supportsItemExpanding()) {
+ roles.insert("isExpanded");
+ roles.insert("isExpandable");
+ roles.insert("expandedParentsCount");
+ }
+
+ // Assure that the role that is used for sorting will be determined
+ roles.insert(fileItemModel->sortRole());
+
+ fileItemModel->setRoles(roles);
+ m_modelRolesUpdater->setRoles(roles);
+}
+
+QSize KFileItemListView::availableIconSize() const
+{
const KItemListStyleOption& option = styleOption();
- const QString sizeText = QLatin1String("888888") + i18nc("@item:intable", "items");
- m_minimumRolesWidths.insert("size", option.fontMetrics.width(sizeText));
+ const int iconSize = option.iconSize;
+ if (m_itemLayout == IconsLayout) {
+ const int maxIconWidth = itemSize().width() - 2 * option.padding;
+ return QSize(maxIconWidth, iconSize);
+ }
+
+ return QSize(iconSize, iconSize);
}
#include "kfileitemlistview.moc"