]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Version control: show pixmap overlays for the version state
authorPeter Penz <peter.penz19@gmail.com>
Thu, 8 Sep 2011 18:28:41 +0000 (20:28 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 8 Sep 2011 18:29:27 +0000 (20:29 +0200)
src/kitemviews/kfileitemlistwidget.cpp
src/kitemviews/kfileitemlistwidget.h
src/views/dolphinfileitemlistwidget.cpp
src/views/dolphinfileitemlistwidget.h

index 8078d0d4d7c62e055a5a81b151d9828f26dcf1f9..d162ed8a2c13ef8036f8c0a13905342d91ce9eae 100644 (file)
@@ -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<QByteArray, QVariant>& 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;
index 0332f1159d56c49d4d03db255e2a970bb1e39334..a16e75a8e0b5dafe4724d6868fe918767acb4fa0 100644 (file)
@@ -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<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
     virtual void visibleRolesChanged(const QHash<QByteArray, int>& current, const QHash<QByteArray, int>& previous);
@@ -116,8 +119,11 @@ private:
     QList<QByteArray> m_sortedVisibleRoles;
 
     QRectF m_expansionArea;
-    QColor* m_customTextColor;
+
+    QColor m_customTextColor;
     QColor m_additionalInfoTextColor;
+
+    QPixmap m_overlay;
 };
 
 #endif
index c3ec2b8ded80eb39d80640fb6c19b6b8b4d252d7..a3fac7b2e46aa0f3af35d727effd84f002f82ada 100644 (file)
@@ -19,7 +19,8 @@
 
 #include "dolphinfileitemlistwidget.h"
 
-#include <kversioncontrolplugin.h>
+#include <KIcon>
+#include <KIconLoader>
 #include <QColor>
 
 #include <KDebug>
@@ -38,39 +39,97 @@ void DolphinFileItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& 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<KVersionControlPlugin::VersionState>(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<KVersionControlPlugin::VersionState>(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"
index d94a9810e99ce720ec23b63e36835af8ab2b4dc8..b08b96428f4f262bccd192c9f5f41fec8c6bc2d4 100644 (file)
 #include <libdolphin_export.h>
 
 #include <kitemviews/kfileitemlistwidget.h>
-
+#include <kversioncontrolplugin.h>
+
+/**
+ * @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<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
+    virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
+
+private:
+    static QPixmap overlayForState(KVersionControlPlugin::VersionState state, int size);
 
 };