File indexing completed on 2024-04-28 05:45:23

0001 /*
0002  * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "dolphinfileitemlistwidget.h"
0008 #include "../kitemviews/private/kitemviewsutils.h"
0009 
0010 #include "dolphindebug.h"
0011 
0012 #include <KIconLoader>
0013 
0014 DolphinFileItemListWidget::DolphinFileItemListWidget(KItemListWidgetInformant *informant, QGraphicsItem *parent)
0015     : KFileItemListWidget(informant, parent)
0016 {
0017 }
0018 
0019 DolphinFileItemListWidget::~DolphinFileItemListWidget()
0020 {
0021 }
0022 
0023 void DolphinFileItemListWidget::refreshCache()
0024 {
0025     QColor color;
0026     const QHash<QByteArray, QVariant> values = data();
0027     if (values.contains("version")) {
0028         // The item is under version control. Apply the text color corresponding
0029         // to its version state.
0030         const KVersionControlPlugin::ItemVersion version = static_cast<KVersionControlPlugin::ItemVersion>(values.value("version").toInt());
0031         const QColor textColor = styleOption().palette.text().color();
0032         QColor tintColor = textColor;
0033 
0034         // Using hardcoded colors is generally a bad idea. In this case the colors just act
0035         // as tint colors and are mixed with the current set text color. The tint colors
0036         // have been optimized for the base colors of the corresponding Oxygen emblems.
0037         switch (version) {
0038         case KVersionControlPlugin::UpdateRequiredVersion:
0039             tintColor = Qt::yellow;
0040             break;
0041         case KVersionControlPlugin::LocallyModifiedUnstagedVersion:
0042             tintColor = Qt::green;
0043             break;
0044         case KVersionControlPlugin::LocallyModifiedVersion:
0045             tintColor = Qt::green;
0046             break;
0047         case KVersionControlPlugin::AddedVersion:
0048             tintColor = Qt::green;
0049             break;
0050         case KVersionControlPlugin::RemovedVersion:
0051             tintColor = Qt::darkRed;
0052             break;
0053         case KVersionControlPlugin::ConflictingVersion:
0054             tintColor = Qt::red;
0055             break;
0056         case KVersionControlPlugin::IgnoredVersion:
0057             tintColor = Qt::white;
0058             break;
0059         case KVersionControlPlugin::MissingVersion:
0060             tintColor = Qt::red;
0061             break;
0062         case KVersionControlPlugin::NormalVersion:
0063         case KVersionControlPlugin::UnversionedVersion:
0064         default:
0065             break;
0066         }
0067 
0068         color = QColor((tintColor.red() + textColor.red()) / 2,
0069                        (tintColor.green() + textColor.green()) / 2,
0070                        (tintColor.blue() + textColor.blue()) / 2,
0071                        (tintColor.alpha() + textColor.alpha()) / 2);
0072 
0073         setOverlay(overlayForState(version, styleOption().iconSize));
0074     } else if (!overlay().isNull()) {
0075         setOverlay(QPixmap());
0076     }
0077 
0078     setTextColor(color);
0079 }
0080 
0081 QPixmap DolphinFileItemListWidget::overlayForState(KVersionControlPlugin::ItemVersion version, int size) const
0082 {
0083     int overlayHeight = KIconLoader::SizeSmall;
0084     if (size >= KIconLoader::SizeEnormous) {
0085         overlayHeight = KIconLoader::SizeMedium;
0086     } else if (size >= KIconLoader::SizeLarge) {
0087         overlayHeight = KIconLoader::SizeSmallMedium;
0088     } else if (size >= KIconLoader::SizeMedium) {
0089         overlayHeight = KIconLoader::SizeSmall;
0090     } else {
0091         overlayHeight = KIconLoader::SizeSmall / 2;
0092     }
0093 
0094     QString iconName;
0095     switch (version) {
0096     case KVersionControlPlugin::NormalVersion:
0097         iconName = QStringLiteral("vcs-normal");
0098         break;
0099     case KVersionControlPlugin::UpdateRequiredVersion:
0100         iconName = QStringLiteral("vcs-update-required");
0101         break;
0102     case KVersionControlPlugin::LocallyModifiedVersion:
0103         iconName = QStringLiteral("vcs-locally-modified");
0104         break;
0105     case KVersionControlPlugin::LocallyModifiedUnstagedVersion:
0106         iconName = QStringLiteral("vcs-locally-modified-unstaged");
0107         break;
0108     case KVersionControlPlugin::AddedVersion:
0109         iconName = QStringLiteral("vcs-added");
0110         break;
0111     case KVersionControlPlugin::RemovedVersion:
0112         iconName = QStringLiteral("vcs-removed");
0113         break;
0114     case KVersionControlPlugin::ConflictingVersion:
0115         iconName = QStringLiteral("vcs-conflicting");
0116         break;
0117     case KVersionControlPlugin::UnversionedVersion:
0118     case KVersionControlPlugin::IgnoredVersion:
0119     case KVersionControlPlugin::MissingVersion:
0120         break;
0121     default:
0122         Q_ASSERT(false);
0123         break;
0124     }
0125 
0126     const qreal dpr = KItemViewsUtils::devicePixelRatio(this);
0127     return QIcon::fromTheme(iconName).pixmap(QSize(overlayHeight, overlayHeight), dpr);
0128 }
0129 
0130 #include "moc_dolphinfileitemlistwidget.cpp"