- const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
- return m_isExpandable ? m_expansionArea : QRectF();
-}
-
-QRectF KFileItemListWidget::selectionToggleRect() const
-{
- const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
-
- const int iconHeight = styleOption().iconSize;
-
- int toggleSize = KIconLoader::SizeSmall;
- if (iconHeight >= KIconLoader::SizeEnormous) {
- toggleSize = KIconLoader::SizeMedium;
- } else if (iconHeight >= KIconLoader::SizeLarge) {
- toggleSize = KIconLoader::SizeSmallMedium;
- }
-
- QPointF pos = iconRect().topLeft();
-
- // If the selection toggle has a very small distance to the
- // widget borders, the size of the selection toggle will get
- // increased to prevent an accidental clicking of the item
- // when trying to hit the toggle.
- const int widgetHeight = size().height();
- const int widgetWidth = size().width();
- const int minMargin = 2;
-
- if (toggleSize + minMargin * 2 >= widgetHeight) {
- pos.rx() -= (widgetHeight - toggleSize) / 2;
- toggleSize = widgetHeight;
- pos.setY(0);
- }
- if (toggleSize + minMargin * 2 >= widgetWidth) {
- pos.ry() -= (widgetWidth - toggleSize) / 2;
- toggleSize = widgetWidth;
- pos.setX(0);
- }
-
- return QRectF(pos, QSizeF(toggleSize, toggleSize));
-}
-
-QSizeF KFileItemListWidget::itemSizeHint(int index, const KItemListView* view)
-{
- 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;
- 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.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.height());
- 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)
-{
- qreal width = 0;
-
- const QHash<QByteArray, QVariant> values = view->model()->data(index);
- const KItemListStyleOption& option = view->styleOption();
-
- const QString text = KFileItemListWidget::roleText(role, values);
- if (!text.isEmpty()) {
- const qreal columnPadding = option.padding * 3;
- width = qMax(width, qreal(2 * columnPadding + 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();