From: Peter Penz Date: Thu, 8 Sep 2011 16:57:52 +0000 (+0200) Subject: Version control: Apply text-color if an item is versioned X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/be629fe8501941138da849cf1179ad67cd5570cb Version control: Apply text-color if an item is versioned --- diff --git a/src/kitemviews/kfileitemlistwidget.cpp b/src/kitemviews/kfileitemlistwidget.cpp index 8cd124437..8078d0d4d 100644 --- a/src/kitemviews/kfileitemlistwidget.cpp +++ b/src/kitemviews/kfileitemlistwidget.cpp @@ -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 diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index 2a52de986..9de99d8de 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -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) { diff --git a/src/kitemviews/kfileitemmodel.h b/src/kitemviews/kfileitemmodel.h index 189c50846..1b7c8bdca 100644 --- a/src/kitemviews/kfileitemmodel.h +++ b/src/kitemviews/kfileitemmodel.h @@ -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 m_items; // Allows O(1) access for KFileItemModel::index(const KFileItem& item) + QHash m_items; // Allows O(1) access for KFileItemModel::index(const KFileItem& item) QList > m_data; bool m_requestRole[RolesCount]; diff --git a/src/views/dolphinfileitemlistwidget.cpp b/src/views/dolphinfileitemlistwidget.cpp index dd391ac9a..c3ec2b8de 100644 --- a/src/views/dolphinfileitemlistwidget.cpp +++ b/src/views/dolphinfileitemlistwidget.cpp @@ -19,6 +19,11 @@ #include "dolphinfileitemlistwidget.h" +#include +#include + +#include + DolphinFileItemListWidget::DolphinFileItemListWidget(QGraphicsItem* parent) : KFileItemListWidget(parent) { @@ -28,4 +33,44 @@ DolphinFileItemListWidget::~DolphinFileItemListWidget() { } +void DolphinFileItemListWidget::dataChanged(const QHash& current, const QSet& 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(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" diff --git a/src/views/dolphinfileitemlistwidget.h b/src/views/dolphinfileitemlistwidget.h index 39c2e454d..d94a9810e 100644 --- a/src/views/dolphinfileitemlistwidget.h +++ b/src/views/dolphinfileitemlistwidget.h @@ -31,6 +31,11 @@ class LIBDOLPHINPRIVATE_EXPORT DolphinFileItemListWidget : public KFileItemListW public: DolphinFileItemListWidget(QGraphicsItem* parent); virtual ~DolphinFileItemListWidget(); + +protected: + /** @reimp */ + virtual void dataChanged(const QHash& current, const QSet& roles = QSet()); + }; #endif diff --git a/src/views/versioncontrol/updateitemstatesthread.cpp b/src/views/versioncontrol/updateitemstatesthread.cpp index e6bd761cc..1fa3a6255 100644 --- a/src/views/versioncontrol/updateitemstatesthread.cpp +++ b/src/views/versioncontrol/updateitemstatesthread.cpp @@ -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); diff --git a/src/views/versioncontrol/versioncontrolobserver.cpp b/src/views/versioncontrol/versioncontrolobserver.cpp index c4824ac59..14f5e0bc9 100644 --- a/src/views/versioncontrol/versioncontrolobserver.cpp +++ b/src/views/versioncontrol/versioncontrolobserver.cpp @@ -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 itemStates = m_updateItemStatesThread->itemStates(); foreach (const ItemState& itemState, itemStates) { QHash values; - values.insert("version", QVariant(static_cast(itemState.version))); + values.insert("version", QVariant(itemState.version)); m_model->setData(itemState.index, values); } @@ -229,7 +225,18 @@ void VersionControlObserver::updateItemStates() } QList 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& 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; diff --git a/src/views/versioncontrol/versioncontrolobserver.h b/src/views/versioncontrol/versioncontrolobserver.h index 88c764baf..e160008d7 100644 --- a/src/views/versioncontrol/versioncontrolobserver.h +++ b/src/views/versioncontrol/versioncontrolobserver.h @@ -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& itemStates); - /** * Returns a matching plugin for the given directory. * 0 is returned, if no matching plugin has been found.