X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/994095076cccdb703b2bb285d4118bd9c654fd93..93ce7f40e2d067eba48b317ef4028c2c75eebebd:/src/dolphinfileitemdelegate.cpp diff --git a/src/dolphinfileitemdelegate.cpp b/src/dolphinfileitemdelegate.cpp index 53c1d781e..7232c38f5 100644 --- a/src/dolphinfileitemdelegate.cpp +++ b/src/dolphinfileitemdelegate.cpp @@ -19,12 +19,17 @@ #include "dolphinfileitemdelegate.h" +#include +#include +#include +#include + #include #include #include - -#include -#include +#include +#include +#include DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) : KFileItemDelegate(parent), @@ -40,27 +45,85 @@ void DolphinFileItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { - if (m_hasMinimizedNameColumn && (index.column() == KDirModel::Name)) { + const QAbstractProxyModel* proxyModel = static_cast(index.model()); + const DolphinModel* dolphinModel = static_cast(proxyModel->sourceModel()); + const bool isNameColumn = (index.column() == KDirModel::Name); + + if (m_hasMinimizedNameColumn && isNameColumn) { QStyleOptionViewItemV4 opt(option); - - const QAbstractProxyModel* proxyModel = static_cast(index.model()); - const KDirModel* dirModel = static_cast(proxyModel->sourceModel()); - const QModelIndex dirIndex = proxyModel->mapToSource(index); - const KFileItem item = dirModel->itemForIndex(dirIndex); - if (!item.isNull()) { - const int width = nameColumnWidth(item.name(), opt); - opt.rect.setWidth(width); - } + adjustOptionWidth(opt, proxyModel, dolphinModel, index); KFileItemDelegate::paint(painter, opt, index); } else { KFileItemDelegate::paint(painter, option, index); } + + if (dolphinModel->hasRevisionData() && isNameColumn) { + // The currently shown items are under revision control. Show the current revision + // state by adding an emblem. + const QModelIndex dirIndex = proxyModel->mapToSource(index); + const QModelIndex revisionIndex = dolphinModel->index(dirIndex.row(), DolphinModel::Revision); + const QVariant data = dolphinModel->data(revisionIndex, Qt::DecorationRole); + const DolphinModel::RevisionState state = static_cast(data.toInt()); + + if (state != DolphinModel::LocalRevision) { + // TODO: extend KFileItemDelegate to be able to get the icon boundaries + const QRect iconRect(option.rect.x(), option.rect.y(), + KIconLoader::SizeSmall, KIconLoader::SizeSmall); + const QPixmap emblem = emblemForState(state, iconRect.size()); + painter->drawPixmap(iconRect.x(), iconRect.y(), emblem); + } + } } int DolphinFileItemDelegate::nameColumnWidth(const QString& name, const QStyleOptionViewItem& option) { QFontMetrics fontMetrics(option.font); - return option.decorationSize.width() + fontMetrics.width(name) + 16; + int width = option.decorationSize.width() + fontMetrics.width(name) + 16; + + const int defaultWidth = option.rect.width(); + if ((defaultWidth > 0) && (defaultWidth < width)) { + width = defaultWidth; + } + return width; +} + +void DolphinFileItemDelegate::adjustOptionWidth(QStyleOptionViewItemV4& option, + const QAbstractProxyModel* proxyModel, + const DolphinModel* dolphinModel, + const QModelIndex& index) +{ + const QModelIndex dirIndex = proxyModel->mapToSource(index); + const KFileItem item = dolphinModel->itemForIndex(dirIndex); + if (!item.isNull()) { + // symbolic links are displayed in an italic font + if (item.isLink()) { + option.font.setItalic(true); + } + + const int width = nameColumnWidth(item.text(), option); + option.rect.setWidth(width); + } +} + +QPixmap DolphinFileItemDelegate::emblemForState(DolphinModel::RevisionState state, const QSize& size) +{ + // TODO #1: all icons that are use here will be replaced by revision control emblems provided by the + // Oxygen team before KDE 4.4 + // TODO #2: cache the icons + switch (state) { + case DolphinModel::LatestRevision: + return KIcon("dialog-ok-apply").pixmap(size); + break; + + case DolphinModel::ConflictingRevision: + return KIcon("emblem-important").pixmap(size); + break; + + // ... + + default: + break; + } + return QPixmap(); } -#include "dolphinfileitemdelegate.moc"