File indexing completed on 2025-01-05 05:14:39
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "difftreemodel.h" 0008 0009 #include <KommitSettings.h> 0010 0011 DiffTreeModel::DiffTreeModel(QObject *parent) 0012 : TreeModel(parent) 0013 { 0014 setLastPartAsData(true); 0015 } 0016 0017 void DiffTreeModel::addFile(const Git::FileStatus &file) 0018 { 0019 addFile(file.name(), toDiffType(file.status())); 0020 } 0021 0022 void DiffTreeModel::addFile(const QString &file, Diff::DiffType type) 0023 { 0024 const auto parts = file.split(separator()); 0025 TreeModel::Node *node; 0026 0027 if (showRoot()) 0028 node = createPath(QStringList() << separator() << parts, type); 0029 else 0030 node = createPath(parts, type); 0031 node->key = file; 0032 node->metaData = type; 0033 } 0034 0035 TreeModel::Node *DiffTreeModel::createPath(const QStringList &path, Diff::DiffType status) 0036 { 0037 Node *parent = mRootNode; 0038 for (auto &p : path) { 0039 auto child = parent->find(p); 0040 if (!child) { 0041 child = parent->createChild(); 0042 child->title = p; 0043 child->metaData = Diff::DiffType::Unchanged; 0044 } else { 0045 if (status != Diff::DiffType::Unchanged && child->metaData != status) 0046 child->metaData = Diff::DiffType::Modified; 0047 } 0048 parent = child; 0049 } 0050 return parent; 0051 } 0052 0053 QString icon(Diff::DiffType status) 0054 { 0055 switch (status) { 0056 case Diff::DiffType::Added: 0057 return QStringLiteral("git-status-added"); 0058 case Diff::DiffType::Modified: 0059 return QStringLiteral("git-status-modified"); 0060 case Diff::DiffType::Removed: 0061 return QStringLiteral("git-status-removed"); 0062 case Diff::DiffType::Unchanged: 0063 return QStringLiteral("git-status-update"); 0064 } 0065 return QStringLiteral("git-status-update"); 0066 } 0067 0068 QColor textColor(Diff::DiffType status) 0069 { 0070 switch (status) { 0071 case Diff::DiffType::Added: 0072 return KommitSettings::self()->diffAddedColor(); 0073 case Diff::DiffType::Modified: 0074 return KommitSettings::self()->diffModifiedColor(); 0075 case Diff::DiffType::Removed: 0076 return KommitSettings::self()->diffRemovedColor(); 0077 case Diff::DiffType::Unchanged: 0078 return {}; 0079 } 0080 return {}; 0081 } 0082 0083 QColor DiffTreeModel::statusColor(Diff::DiffType status) const 0084 { 0085 switch (status) { 0086 case Diff::DiffType::Unchanged: 0087 return Qt::black; 0088 case Diff::DiffType::Added: 0089 return KommitSettings::diffAddedColor(); 0090 case Diff::DiffType::Removed: 0091 return KommitSettings::diffRemovedColor(); 0092 case Diff::DiffType::Modified: 0093 return KommitSettings::diffModifiedColor(); 0094 } 0095 return {}; 0096 } 0097 0098 Diff::DiffType DiffTreeModel::toDiffType(Git::FileStatus::Status status) const 0099 { 0100 switch (status) { 0101 case Git::FileStatus::NoGit: 0102 case Git::FileStatus::Unknown: 0103 case Git::FileStatus::Unmodified: 0104 return Diff::DiffType::Unchanged; 0105 case Git::FileStatus::Added: 0106 return Diff::DiffType::Added; 0107 case Git::FileStatus::Removed: 0108 return Diff::DiffType::Removed; 0109 case Git::FileStatus::Modified: 0110 case Git::FileStatus::Renamed: 0111 case Git::FileStatus::Copied: 0112 return Diff::DiffType::Modified; 0113 case Git::FileStatus::UpdatedButInmerged: 0114 case Git::FileStatus::Ignored: 0115 case Git::FileStatus::Untracked: 0116 return Diff::DiffType::Unchanged; 0117 } 0118 return Diff::DiffType::Unchanged; 0119 } 0120 0121 QVariant DiffTreeModel::data(const QModelIndex &index, int role) const 0122 { 0123 if (!index.isValid()) 0124 return {}; 0125 0126 if (role == Qt::DisplayRole) { 0127 Node *item = static_cast<Node *>(index.internalPointer()); 0128 0129 switch (index.column()) { 0130 case 0: 0131 return item->title; 0132 case 1: 0133 return (int)item->metaData; 0134 } 0135 } else if (role == Qt::DecorationRole) { 0136 Node *item = static_cast<Node *>(index.internalPointer()); 0137 0138 // return statusColor(item->metaData); 0139 0140 // switch (item->metaData) { 0141 // case Diff::DiffType::Added: 0142 // return QColor(Qt::green); 0143 // case Diff::DiffType::Removed: 0144 // return QColor(Qt::red); 0145 // case Diff::DiffType::Modified: 0146 // return QColor(Qt::blue); 0147 // case Diff::DiffType::Unchanged: 0148 // return QColor(Qt::black); 0149 // } 0150 0151 return QIcon::fromTheme(icon(item->metaData)); 0152 } else if (role == Qt::ForegroundRole) { 0153 // Node *item = static_cast<Node *>(index.internalPointer()); 0154 // return textColor(item->metaData); 0155 } 0156 0157 return {}; 0158 } 0159 0160 void DiffTreeModel::emitAll() 0161 { 0162 beginResetModel(); 0163 endResetModel(); 0164 } 0165 0166 #include "moc_difftreemodel.cpp"