- const QHash<QByteArray, QVariant> values = view->model()->data(index);
- const KItemListStyleOption& option = view->styleOption();
- const int additionalRolesCount = qMax(view->visibleRoles().count() - 1, 0);
-
- switch (static_cast<const KFileItemListView*>(view)->itemLayout()) {
- case IconsLayout: {
- const QString text = KStringHandler::preProcessWrap(values["name"].toString());
-
- const qreal maxWidth = view->itemSize().width() - 2 * option.padding;
- QTextLine line;
-
- // Calculate the number of lines required for wrapping the name
- QTextOption textOption(Qt::AlignHCenter);
- textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
-
- qreal textHeight = 0;
- QTextLayout layout(text, option.font);
- layout.setTextOption(textOption);
- layout.beginLayout();
- while ((line = layout.createLine()).isValid()) {
- line.setLineWidth(maxWidth);
- line.naturalTextWidth();
- textHeight += line.height();
- }
- layout.endLayout();
-
- // Add one line for each additional information
- const qreal height = textHeight +
- additionalRolesCount * option.fontMetrics.lineSpacing() +
- option.iconSize +
- option.padding * 3;
- return QSizeF(view->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, view->visibleRoles()) {
- const QString text = KFileItemListWidget::roleText(role, values);
- const qreal requiredWidth = option.fontMetrics.width(text);
- maximumRequiredWidth = qMax(maximumRequiredWidth, requiredWidth);
- }
-
- const qreal width = option.padding * 4 + option.iconSize + maximumRequiredWidth;
- const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.lineSpacing());
- return QSizeF(width, height);
- }
-
- case DetailsLayout: {
- const qreal height = option.padding * 2 + qMax(option.iconSize, option.fontMetrics.height());
- return QSizeF(-1, height);
- }
-
- default:
- Q_ASSERT(false);
- break;
- }
-
- return QSize();
-}
-
-qreal KFileItemListWidget::preferredRoleColumnWidth(const QByteArray& role,
- int index,
- const KItemListView* view)
-{
-
- const QHash<QByteArray, QVariant> values = view->model()->data(index);
- const KItemListStyleOption& option = view->styleOption();
-
- const QString text = KFileItemListWidget::roleText(role, values);
- qreal width = columnPadding(option);
-
- if (role == "rating") {
- width += preferredRatingSize(option).width();
- } else {
- width += option.fontMetrics.width(text);
-
- if (role == "name") {
- // Increase the width by the expansion-toggle and the current expansion level
- const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
- width += option.padding + (expandedParentsCount + 1) * view->itemSize().height() + KIconLoader::SizeSmall;
-
- // Increase the width by the required space for the icon
- width += option.padding * 2 + option.iconSize;
- }
- }
-
- return width;
-}
-
-void KFileItemListWidget::invalidateCache()
-{
- m_dirtyLayout = true;
- m_dirtyContent = true;
-}
-
-void KFileItemListWidget::refreshCache()
-{
-}
-
-void KFileItemListWidget::setTextColor(const QColor& color)
-{
- if (color != m_customTextColor) {
- m_customTextColor = color;
- updateAdditionalInfoTextColor();
- update();
- }
-}
-
-QColor KFileItemListWidget::textColor() const
-{
- if (m_customTextColor.isValid() && !isSelected()) {
- return m_customTextColor;
- }
-
- const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
- const QPalette::ColorRole role = isSelected() ? QPalette::HighlightedText : QPalette::Text;
- return styleOption().palette.brush(group, role).color();
-}
-
-void KFileItemListWidget::setOverlay(const QPixmap& overlay)
-{
- m_overlay = overlay;
- m_dirtyContent = true;
- update();
-}
-
-QPixmap KFileItemListWidget::overlay() const
-{
- return m_overlay;
-}
-
-void KFileItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
- const QSet<QByteArray>& roles)
-{
- Q_UNUSED(current);
-
- m_dirtyContent = true;
-
- QSet<QByteArray> dirtyRoles;
- if (roles.isEmpty()) {
- dirtyRoles = visibleRoles().toSet();
- dirtyRoles.insert("iconPixmap");
- dirtyRoles.insert("iconName");
- } else {
- dirtyRoles = roles;
- }
-
- QSetIterator<QByteArray> it(dirtyRoles);
- while (it.hasNext()) {
- const QByteArray& role = it.next();
- m_dirtyContentRoles.insert(role);
- }
-}
-
-void KFileItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
- const QList<QByteArray>& previous)
-{
- Q_UNUSED(previous);
- m_sortedVisibleRoles = current;
- m_dirtyLayout = true;
-}
-
-void KFileItemListWidget::columnWidthChanged(const QByteArray& role,
- qreal current,
- qreal previous)
-{
- Q_UNUSED(role);
- Q_UNUSED(current);
- Q_UNUSED(previous);
- m_dirtyLayout = true;
-}
-
-void KFileItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
- const KItemListStyleOption& previous)
-{
- Q_UNUSED(current);
- Q_UNUSED(previous);
- updateAdditionalInfoTextColor();
- m_dirtyLayout = true;
-}
-
-void KFileItemListWidget::hoveredChanged(bool hovered)
-{
- Q_UNUSED(hovered);
- m_dirtyLayout = true;
-}
-
-void KFileItemListWidget::selectedChanged(bool selected)
-{
- Q_UNUSED(selected);
- updateAdditionalInfoTextColor();
-}
-
-void KFileItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
-{
- Q_UNUSED(current);
- Q_UNUSED(previous);
- m_dirtyLayout = true;
-}
-
-
-void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
-{
- KItemListWidget::resizeEvent(event);
- m_dirtyLayout = true;
-}
-
-void KFileItemListWidget::showEvent(QShowEvent* event)
-{
- KItemListWidget::showEvent(event);
-
- // Listen to changes of the clipboard to mark the item as cut/uncut
- KFileItemClipboard* clipboard = KFileItemClipboard::instance();
-
- const KUrl itemUrl = data().value("url").value<KUrl>();
- m_isCut = clipboard->isCut(itemUrl);
-
- connect(clipboard, SIGNAL(cutItemsChanged()),
- this, SLOT(slotCutItemsChanged()));
-}
-
-void KFileItemListWidget::hideEvent(QHideEvent* event)
-{
- disconnect(KFileItemClipboard::instance(), SIGNAL(cutItemsChanged()),
- this, SLOT(slotCutItemsChanged()));
-
- KItemListWidget::hideEvent(event);
-}
-
-void KFileItemListWidget::slotCutItemsChanged()
-{
- const KUrl itemUrl = data().value("url").value<KUrl>();
- const bool isCut = KFileItemClipboard::instance()->isCut(itemUrl);
- if (m_isCut != isCut) {
- m_isCut = isCut;
- m_pixmap = QPixmap();
- m_dirtyContent = true;
- update();
- }
-}
-
-void KFileItemListWidget::triggerCacheRefreshing()
-{
- if ((!m_dirtyContent && !m_dirtyLayout) || index() < 0) {
- return;
- }
-
- refreshCache();
-
- const QHash<QByteArray, QVariant> values = data();
- m_isExpandable = m_supportsItemExpanding && values["isExpandable"].toBool();
- m_isHidden = values["name"].toString().startsWith(QLatin1Char('.'));
-
- updateExpansionArea();
- updateTextsCache();
- updatePixmapCache();
-
- m_dirtyLayout = false;
- m_dirtyContent = false;
- m_dirtyContentRoles.clear();
-}
-
-void KFileItemListWidget::updateExpansionArea()
-{
- if (m_supportsItemExpanding) {
- const QHash<QByteArray, QVariant> values = data();
- Q_ASSERT(values.contains("expandedParentsCount"));
- const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
- if (expandedParentsCount >= 0) {
- const qreal widgetHeight = size().height();
- const qreal inc = (widgetHeight - KIconLoader::SizeSmall) / 2;
- const qreal x = expandedParentsCount * widgetHeight + inc;
- const qreal y = inc;
- m_expansionArea = QRectF(x, y, KIconLoader::SizeSmall, KIconLoader::SizeSmall);
- return;
- }
- }
-
- m_expansionArea = QRectF();
-}
-
-void KFileItemListWidget::updatePixmapCache()
-{
- // Precondition: Requires already updated m_textPos values to calculate
- // the remaining height when the alignment is vertical.
-
- const QSizeF widgetSize = size();
- const bool iconOnTop = (m_layout == IconsLayout);
- const KItemListStyleOption& option = styleOption();
- const qreal padding = option.padding;
-
- const int maxIconWidth = iconOnTop ? widgetSize.width() - 2 * padding : option.iconSize;
- const int maxIconHeight = option.iconSize;
-
- const QHash<QByteArray, QVariant> values = data();
-
- bool updatePixmap = (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight);
- if (!updatePixmap && m_dirtyContent) {
- updatePixmap = m_dirtyContentRoles.isEmpty()
- || m_dirtyContentRoles.contains("iconPixmap")
- || m_dirtyContentRoles.contains("iconName")
- || m_dirtyContentRoles.contains("iconOverlays");
- }
-
- if (updatePixmap) {
- m_pixmap = values["iconPixmap"].value<QPixmap>();
- if (m_pixmap.isNull()) {
- // Use the icon that fits to the MIME-type
- QString iconName = values["iconName"].toString();
- if (iconName.isEmpty()) {
- // The icon-name has not been not resolved by KFileItemModelRolesUpdater,
- // use a generic icon as fallback
- iconName = QLatin1String("unknown");
- }
- m_pixmap = pixmapForIcon(iconName, maxIconHeight);
- } else if (m_pixmap.width() != maxIconWidth || m_pixmap.height() != maxIconHeight) {
- // A custom pixmap has been applied. Assure that the pixmap
- // is scaled to the maximum available size.
- KPixmapModifier::scale(m_pixmap, QSize(maxIconWidth, maxIconHeight));
- }