]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Version control: Apply text-color if an item is versioned
authorPeter Penz <peter.penz19@gmail.com>
Thu, 8 Sep 2011 16:57:52 +0000 (18:57 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 8 Sep 2011 16:58:59 +0000 (18:58 +0200)
src/kitemviews/kfileitemlistwidget.cpp
src/kitemviews/kfileitemmodel.cpp
src/kitemviews/kfileitemmodel.h
src/views/dolphinfileitemlistwidget.cpp
src/views/dolphinfileitemlistwidget.h
src/views/versioncontrol/updateitemstatesthread.cpp
src/views/versioncontrol/versioncontrolobserver.cpp
src/views/versioncontrol/versioncontrolobserver.h

index 8cd12443788f23c4aae360edb5a7b1fe10521242..8078d0d4d7c62e055a5a81b151d9828f26dcf1f9 100644 (file)
@@ -168,12 +168,14 @@ void KFileItemListWidget::setTextColor(const QColor& color)
         } else {
             *m_customTextColor = color;
         }
-    } else {
+        updateAdditionalInfoTextColor();
+        update();
+    } else if (m_customTextColor){
         delete m_customTextColor;
         m_customTextColor = 0;
+        updateAdditionalInfoTextColor();
+        update();
     }
-    updateAdditionalInfoTextColor();
-    update();
 }
 
 QColor KFileItemListWidget::textColor() const
index 2a52de9865cf7f595e72d2044d1625f4408c792c..9de99d8de564b2087369ca692d1c4a6d8ca589d8 100644 (file)
@@ -204,13 +204,22 @@ KFileItem KFileItemModel::fileItem(int index) const
     return KFileItem();
 }
 
+KFileItem KFileItemModel::fileItem(const KUrl& url) const
+{
+    const int index = m_items.value(url, -1);
+    if (index >= 0) {
+        return m_sortedItems.at(index);
+    }
+    return KFileItem();
+}
+
 int KFileItemModel::index(const KFileItem& item) const
 {
     if (item.isNull()) {
         return -1;
     }
 
-    return m_items.value(item, -1);
+    return m_items.value(item.url(), -1);
 }
 
 KUrl KFileItemModel::rootDirectory() const
@@ -360,7 +369,7 @@ void KFileItemModel::onSortRoleChanged(const QByteArray& current, const QByteArr
     int index = 0;
     foreach (const KFileItem& item, sortedItems) {
         m_sortedItems.append(item);
-        m_items.insert(item, index);
+        m_items.insert(item.url(), index);
         m_data.append(retrieveData(item));
 
         ++index;
@@ -504,7 +513,7 @@ void KFileItemModel::insertItems(const KFileItemList& items)
     // The indexes of all m_items must be adjusted, not only the index
     // of the new items
     for (int i = 0; i < m_sortedItems.count(); ++i) {
-        m_items.insert(m_sortedItems.at(i), i);
+        m_items.insert(m_sortedItems.at(i).url(), i);
     }
 
     itemRanges << KItemRange(insertedAtIndex, insertedCount);
@@ -566,7 +575,7 @@ void KFileItemModel::removeItems(const KFileItemList& items)
     // Delete the items
     for (int i = indexesToRemove.count() - 1; i >= 0; --i) {
         const int indexToRemove = indexesToRemove.at(i);
-        m_items.remove(m_sortedItems.at(indexToRemove));
+        m_items.remove(m_sortedItems.at(indexToRemove).url());
         m_sortedItems.removeAt(indexToRemove);
         m_data.removeAt(indexToRemove);
     }
@@ -574,7 +583,7 @@ void KFileItemModel::removeItems(const KFileItemList& items)
     // The indexes of all m_items must be adjusted, not only the index
     // of the removed items
     for (int i = 0; i < m_sortedItems.count(); ++i) {
-        m_items.insert(m_sortedItems.at(i), i);
+        m_items.insert(m_sortedItems.at(i).url(), i);
     }
 
     if (count() <= 0) {
index 189c50846965cc166ef96df0b1e54b6ab7c5fe8b..1b7c8bdca70d5fabe1fc2aacc0838e408da9145f 100644 (file)
@@ -82,6 +82,13 @@ public:
      */
     KFileItem fileItem(int index) const;
 
+    /**
+     * @return The file-item for the url \a url. If no file-item with the given
+     *         URL is found KFileItem::isNull() will be true for the returned
+     *         file-item. The runtime complexity of this call is O(1).
+     */
+    KFileItem fileItem(const KUrl& url) const;
+
     /**
      * @return The index for the file-item \a item. -1 is returned if no file-item
      *         is found or if the file-item is null. The runtime
@@ -183,7 +190,7 @@ private:
     Qt::CaseSensitivity m_caseSensitivity;
 
     KFileItemList m_sortedItems;   // Allows O(1) access for KFileItemModel::fileItem(int index)
-    QHash<KFileItem, int> m_items; // Allows O(1) access for KFileItemModel::index(const KFileItem& item)
+    QHash<KUrl, int> m_items;      // Allows O(1) access for KFileItemModel::index(const KFileItem& item)
     QList<QHash<QByteArray, QVariant> > m_data;
 
     bool m_requestRole[RolesCount];
index dd391ac9af6d1ee2b3b93ef53a92a6abd42b84ed..c3ec2b8ded80eb39d80640fb6c19b6b8b4d252d7 100644 (file)
 
 #include "dolphinfileitemlistwidget.h"
 
+#include <kversioncontrolplugin.h>
+#include <QColor>
+
+#include <KDebug>
+
 DolphinFileItemListWidget::DolphinFileItemListWidget(QGraphicsItem* parent) :
     KFileItemListWidget(parent)
 {
@@ -28,4 +33,44 @@ DolphinFileItemListWidget::~DolphinFileItemListWidget()
 {
 }
 
+void DolphinFileItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles)
+{
+    KFileItemListWidget::dataChanged(current, roles);
+
+    QColor color;
+    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);
+        }
+    }
+
+    setTextColor(color);
+}
+
 #include "dolphinfileitemlistwidget.moc"
index 39c2e454d1b88e93ae7784819961e6a85ed944f0..d94a9810e99ce720ec23b63e36835af8ab2b4dc8 100644 (file)
@@ -31,6 +31,11 @@ class LIBDOLPHINPRIVATE_EXPORT DolphinFileItemListWidget : public KFileItemListW
 public:
     DolphinFileItemListWidget(QGraphicsItem* parent);
     virtual ~DolphinFileItemListWidget();
+
+protected:
+    /** @reimp */
+    virtual void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
+
 };
 
 #endif
index e6bd761cc47ebe52899f3f8cf414c078d4051456..1fa3a6255c815f7029b9fda4a377af1eea925605 100644 (file)
@@ -55,12 +55,8 @@ void UpdateItemStatesThread::run()
     Q_ASSERT(!m_itemStates.isEmpty());
     Q_ASSERT(m_plugin);
 
-    // The items from m_itemStates may be located in different directory levels. The version
-    // plugin requires the root directory for KVersionControlPlugin::beginRetrieval(). Instead
-    // of doing an expensive search, we utilize the knowledge of the implementation of
-    // VersionControlObserver::addDirectory() to be sure that the last item contains the root.
     QMutexLocker itemLocker(&m_itemMutex);
-    const QString directory = m_itemStates.last().item.url().directory(KUrl::AppendTrailingSlash);
+    const QString directory = m_itemStates.first().item.url().directory(KUrl::AppendTrailingSlash);
     itemLocker.unlock();
 
     QMutexLocker pluginLocker(m_globalPluginMutex);
index c4824ac59cd7b78cb23f4bb0cc62840fa535c6b9..14f5e0bc924c058a4aa2fc7b1a58ca2578ff5d07 100644 (file)
@@ -165,8 +165,6 @@ void VersionControlObserver::verifyDirectory()
             // The directory is versioned. Assume that the user will further browse through
             // versioned directories and decrease the verification timer.
             m_dirVerificationTimer->setInterval(100);
-            connect(m_model, SIGNAL(itemsInserted(KItemRangeList)),
-                   this, SLOT(delayedDirectoryVerification()));
         }
         updateItemStates();
     } else if (m_versionedDirectory) {
@@ -176,8 +174,6 @@ void VersionControlObserver::verifyDirectory()
         // value, so that browsing through non-versioned directories is not slown down
         // by an immediate verification.
         m_dirVerificationTimer->setInterval(500);
-        disconnect(m_model, SIGNAL(itemsInserted(KItemRangeList)),
-                   this, SLOT(delayedDirectoryVerification()));
     }
 }
 
@@ -196,7 +192,7 @@ void VersionControlObserver::slotThreadFinished()
     const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
     foreach (const ItemState& itemState, itemStates) {
         QHash<QByteArray, QVariant> values;
-        values.insert("version", QVariant(static_cast<int>(itemState.version)));
+        values.insert("version", QVariant(itemState.version));
         m_model->setData(itemState.index, values);
     }
 
@@ -229,7 +225,18 @@ void VersionControlObserver::updateItemStates()
     }
 
     QList<ItemState> itemStates;
-    //addDirectory(QModelIndex(), itemStates);
+    const int itemCount = m_model->count();
+    itemStates.reserve(itemCount);
+
+    for (int i = 0; i < itemCount; ++i) {
+        ItemState itemState;
+        itemState.index = i;
+        itemState.item = m_model->fileItem(i);
+        itemState.version = KVersionControlPlugin::UnversionedVersion;
+
+        itemStates.append(itemState);
+    }
+
     if (!itemStates.isEmpty()) {
         if (!m_silentUpdate) {
             emit infoMessage(i18nc("@info:status", "Updating version information..."));
@@ -239,24 +246,6 @@ void VersionControlObserver::updateItemStates()
     }
 }
 
-/*void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList<ItemState>& itemStates)
-{
-    Q_UNUSED(parentIndex);
-    Q_UNUSED(itemStates);
-    const int rowCount = m_dolphinModel->rowCount(parentIndex);
-    for (int row = 0; row < rowCount; ++row) {
-        const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex);
-        addDirectory(index, itemStates);
-
-        ItemState itemState;
-        itemState.index = index;
-        itemState.item = m_dolphinModel->itemForIndex(index);
-        itemState.version = KVersionControlPlugin::UnversionedVersion;
-
-        itemStates.append(itemState);
-    }
-}*/
-
 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
 {
     static bool pluginsAvailable = true;
@@ -293,11 +282,8 @@ KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& director
     Q_UNUSED(directory);
     foreach (KVersionControlPlugin* plugin, plugins) {
         // Use the KDirLister cache to check for .svn, .git, ... files
-        KUrl dirUrl(directory);
-        KUrl fileUrl = dirUrl;
-        fileUrl.addPath(plugin->fileName());
-        const KFileItem item; // = m_dirLister->findByUrl(fileUrl);
-        if (!item.isNull()) {
+        const QString fileName = directory.path(KUrl::AddTrailingSlash) + plugin->fileName();
+        if (QFile::exists(fileName)) {
             return plugin;
         }
 
@@ -308,11 +294,11 @@ KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& director
         // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
         // must be shown at least once.
         if (m_versionedDirectory) {
+            KUrl dirUrl(directory);
             KUrl upUrl = dirUrl.upUrl();
             while (upUrl != dirUrl) {
-                const QString filePath = dirUrl.pathOrUrl(KUrl::AddTrailingSlash) + plugin->fileName();
-                QFileInfo file(filePath);
-                if (file.exists()) {
+                const QString fileName = dirUrl.path(KUrl::AddTrailingSlash) + plugin->fileName();
+                if (QFile::exists(fileName)) {
                     return plugin;
                 }
                 dirUrl = upUrl;
index 88c764baf7d7feca89b42a2cd2b5b83e304e900b..e160008d7b7652cca27906405bb0aea8ee5e14c4 100644 (file)
@@ -110,12 +110,6 @@ private:
 
     void updateItemStates();
 
-    /**
-     * Adds recursively all items from the directory \p parentIndex into
-     * the list \p itemStates.
-     */
-    //void addDirectory(const QModelIndex& parentIndex, QList<ItemState>& itemStates);
-
     /**
      * Returns a matching plugin for the given directory.
      * 0 is returned, if no matching plugin has been found.