From: Peter Penz Date: Thu, 8 Sep 2011 18:28:41 +0000 (+0200) Subject: Version control: show pixmap overlays for the version state X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/f0debd937766045c77dea5f2f5255de89f7b2697 Version control: show pixmap overlays for the version state --- diff --git a/src/kitemviews/kfileitemlistwidget.cpp b/src/kitemviews/kfileitemlistwidget.cpp index 8078d0d4d..d162ed8a2 100644 --- a/src/kitemviews/kfileitemlistwidget.cpp +++ b/src/kitemviews/kfileitemlistwidget.cpp @@ -56,8 +56,9 @@ KFileItemListWidget::KFileItemListWidget(QGraphicsItem* parent) : m_textBoundingRect(), m_sortedVisibleRoles(), m_expansionArea(), - m_customTextColor(0), - m_additionalInfoTextColor() + m_customTextColor(), + m_additionalInfoTextColor(), + m_overlay() { for (int i = 0; i < TextIdCount; ++i) { m_text[i].setTextFormat(Qt::PlainText); @@ -67,8 +68,6 @@ KFileItemListWidget::KFileItemListWidget(QGraphicsItem* parent) : KFileItemListWidget::~KFileItemListWidget() { - delete m_customTextColor; - m_customTextColor = 0; } void KFileItemListWidget::setLayout(Layout layout) @@ -117,11 +116,7 @@ void KFileItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIte } painter->setFont(itemListStyleOption.font); - if (m_customTextColor) { - painter->setPen(*m_customTextColor); - } else { - painter->setPen(itemListStyleOption.palette.text().color()); - } + painter->setPen(textColor()); painter->drawStaticText(m_textPos[Name], m_text[Name]); painter->setPen(m_additionalInfoTextColor); @@ -162,17 +157,8 @@ QRectF KFileItemListWidget::expansionToggleRect() const void KFileItemListWidget::setTextColor(const QColor& color) { - if (color.isValid()) { - if (!m_customTextColor) { - m_customTextColor = new QColor(color); - } else { - *m_customTextColor = color; - } - updateAdditionalInfoTextColor(); - update(); - } else if (m_customTextColor){ - delete m_customTextColor; - m_customTextColor = 0; + if (color != m_customTextColor) { + m_customTextColor = color; updateAdditionalInfoTextColor(); update(); } @@ -180,7 +166,23 @@ void KFileItemListWidget::setTextColor(const QColor& color) QColor KFileItemListWidget::textColor() const { - return m_customTextColor ? *m_customTextColor : styleOption().palette.text().color(); + return m_customTextColor.isValid() ? m_customTextColor : styleOption().palette.text().color(); +} + +void KFileItemListWidget::setOverlay(const QPixmap& overlay) +{ + const bool updateContent = (overlay.isNull() && !m_overlay.isNull()) || + (!overlay.isNull() && m_overlay.isNull()); + if (updateContent) { + m_overlay = overlay; + m_dirtyContent = true; + update(); + } +} + +QPixmap KFileItemListWidget::overlay() const +{ + return m_overlay; } void KFileItemListWidget::dataChanged(const QHash& current, @@ -369,6 +371,10 @@ void KFileItemListWidget::updatePixmapCache() Q_ASSERT(m_pixmap.height() == iconHeight); } + if (!m_overlay.isNull()) { + QPainter painter(&m_pixmap); + painter.drawPixmap(0, m_pixmap.height() - m_overlay.height(), m_overlay); + } m_scaledPixmapSize = QSize(scaledIconHeight, scaledIconHeight); @@ -626,7 +632,7 @@ void KFileItemListWidget::updateAdditionalInfoTextColor() // For the color of the additional info the inactive text color // is not used as this might lead to unreadable text for some color schemes. Instead // the text color is slightly mixed with the background color. - const QColor c1 = m_customTextColor ? *m_customTextColor : styleOption().palette.text().color(); + const QColor c1 = textColor(); const QColor c2 = styleOption().palette.background().color(); const int p1 = 70; const int p2 = 100 - p1; diff --git a/src/kitemviews/kfileitemlistwidget.h b/src/kitemviews/kfileitemlistwidget.h index 0332f1159..a16e75a8e 100644 --- a/src/kitemviews/kfileitemlistwidget.h +++ b/src/kitemviews/kfileitemlistwidget.h @@ -55,6 +55,9 @@ public: protected: void setTextColor(const QColor& color); QColor textColor() const; + + void setOverlay(const QPixmap& overlay); + QPixmap overlay() const; virtual void dataChanged(const QHash& current, const QSet& roles = QSet()); virtual void visibleRolesChanged(const QHash& current, const QHash& previous); @@ -116,8 +119,11 @@ private: QList m_sortedVisibleRoles; QRectF m_expansionArea; - QColor* m_customTextColor; + + QColor m_customTextColor; QColor m_additionalInfoTextColor; + + QPixmap m_overlay; }; #endif diff --git a/src/views/dolphinfileitemlistwidget.cpp b/src/views/dolphinfileitemlistwidget.cpp index c3ec2b8de..a3fac7b2e 100644 --- a/src/views/dolphinfileitemlistwidget.cpp +++ b/src/views/dolphinfileitemlistwidget.cpp @@ -19,7 +19,8 @@ #include "dolphinfileitemlistwidget.h" -#include +#include +#include #include #include @@ -38,39 +39,97 @@ void DolphinFileItemListWidget::dataChanged(const QHash& c KFileItemListWidget::dataChanged(current, roles); QColor color; + QPixmap overlay; if (roles.contains("version")) { // The item is under version control. Apply the text color corresponding // to its version state. const KVersionControlPlugin::VersionState version = static_cast(current.value("version").toInt()); - if (version != KVersionControlPlugin::UnversionedVersion) { - const QColor textColor = styleOption().palette.text().color(); - QColor tintColor = textColor; - - // Using hardcoded colors is generally a bad idea. In this case the colors just act - // as tint colors and are mixed with the current set text color. The tint colors - // have been optimized for the base colors of the corresponding Oxygen emblems. - switch (version) { - case KVersionControlPlugin::UpdateRequiredVersion: tintColor = Qt::yellow; break; - case KVersionControlPlugin::LocallyModifiedUnstagedVersion: tintColor = Qt::darkGreen; break; - case KVersionControlPlugin::LocallyModifiedVersion: tintColor = Qt::green; break; - case KVersionControlPlugin::AddedVersion: tintColor = Qt::green; break; - case KVersionControlPlugin::RemovedVersion: tintColor = Qt::darkRed; break; - case KVersionControlPlugin::ConflictingVersion: tintColor = Qt::red; break; - case KVersionControlPlugin::UnversionedVersion: - case KVersionControlPlugin::NormalVersion: - default: - // use the default text color - return; - } - - color = QColor((tintColor.red() + textColor.red()) / 2, - (tintColor.green() + textColor.green()) / 2, - (tintColor.blue() + textColor.blue()) / 2, - (tintColor.alpha() + textColor.alpha()) / 2); + const QColor textColor = styleOption().palette.text().color(); + QColor tintColor = textColor; + + // Using hardcoded colors is generally a bad idea. In this case the colors just act + // as tint colors and are mixed with the current set text color. The tint colors + // have been optimized for the base colors of the corresponding Oxygen emblems. + switch (version) { + case KVersionControlPlugin::UpdateRequiredVersion: tintColor = Qt::yellow; break; + case KVersionControlPlugin::LocallyModifiedUnstagedVersion: tintColor = Qt::green; break; + case KVersionControlPlugin::LocallyModifiedVersion: tintColor = Qt::green; break; + case KVersionControlPlugin::AddedVersion: tintColor = Qt::green; break; + case KVersionControlPlugin::RemovedVersion: tintColor = Qt::darkRed; break; + case KVersionControlPlugin::ConflictingVersion: tintColor = Qt::red; break; + case KVersionControlPlugin::UnversionedVersion: tintColor = Qt::white; break; + case KVersionControlPlugin::NormalVersion: + default: + break; } + + color = QColor((tintColor.red() + textColor.red()) / 2, + (tintColor.green() + textColor.green()) / 2, + (tintColor.blue() + textColor.blue()) / 2, + (tintColor.alpha() + textColor.alpha()) / 2); + + overlay = overlayForState(version, styleOption().iconSize); } setTextColor(color); + setOverlay(overlay); +} + +void DolphinFileItemListWidget::styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous) +{ + KFileItemListWidget::styleOptionChanged(current, previous); + + if (!overlay().isNull() && current.iconSize != previous.iconSize) { + const KVersionControlPlugin::VersionState version = static_cast(data().value("version").toInt()); + const QPixmap newOverlay = overlayForState(version, current.iconSize); + setOverlay(newOverlay); + } +} + +QPixmap DolphinFileItemListWidget::overlayForState(KVersionControlPlugin::VersionState version, int size) +{ + int overlayHeight = KIconLoader::SizeSmall; + if (size >= KIconLoader::SizeEnormous) { + overlayHeight = KIconLoader::SizeMedium; + } else if (size >= KIconLoader::SizeLarge) { + overlayHeight = KIconLoader::SizeSmallMedium; + } else if (size >= KIconLoader::SizeMedium) { + overlayHeight = KIconLoader::SizeSmall; + } else { + overlayHeight = KIconLoader::SizeSmall / 2; + } + + QString iconName; + switch (version) { + case KVersionControlPlugin::NormalVersion: + iconName = "vcs-normal"; + break; + case KVersionControlPlugin::UpdateRequiredVersion: + iconName = "vcs-update-required"; + break; + case KVersionControlPlugin::LocallyModifiedVersion: + iconName = "vcs-locally-modified"; + break; + case KVersionControlPlugin::LocallyModifiedUnstagedVersion: + iconName = "vcs-locally-modified-unstaged"; + break; + case KVersionControlPlugin::AddedVersion: + iconName = "vcs-added"; + break; + case KVersionControlPlugin::RemovedVersion: + iconName = "vcs-removed"; + break; + case KVersionControlPlugin::ConflictingVersion: + iconName = "vcs-conflicting"; + break; + case KVersionControlPlugin::UnversionedVersion: + break; + default: + Q_ASSERT(false); + break; + } + + return KIcon(iconName).pixmap(QSize(overlayHeight, overlayHeight)); } #include "dolphinfileitemlistwidget.moc" diff --git a/src/views/dolphinfileitemlistwidget.h b/src/views/dolphinfileitemlistwidget.h index d94a9810e..b08b96428 100644 --- a/src/views/dolphinfileitemlistwidget.h +++ b/src/views/dolphinfileitemlistwidget.h @@ -23,7 +23,14 @@ #include #include - +#include + +/** + * @brief Extends KFileItemListWidget to handle the "version" role. + * + * The "version" role is set if version-control-plugins have been enabled. + * @see KVersionControlPlugin + */ class LIBDOLPHINPRIVATE_EXPORT DolphinFileItemListWidget : public KFileItemListWidget { Q_OBJECT @@ -33,8 +40,11 @@ public: virtual ~DolphinFileItemListWidget(); protected: - /** @reimp */ virtual void dataChanged(const QHash& current, const QSet& roles = QSet()); + virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous); + +private: + static QPixmap overlayForState(KVersionControlPlugin::VersionState state, int size); };