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 "changedfilesmodel.h"
0008 
0009 #include "gitmanager.h"
0010 
0011 #include <KommitSettings.h>
0012 #include <QIcon>
0013 #include <QPainter>
0014 #include <QPixmap>
0015 
0016 QIcon ChangedFilesModel::createIcon(Git::ChangeStatus status)
0017 {
0018     if (mIcons.contains(status))
0019         return mIcons[status];
0020 
0021     const QIcon icon = Git::statusIcon(status);
0022     mIcons.insert(status, icon);
0023     return icon;
0024 }
0025 
0026 bool ChangedFilesModel::setData(const QModelIndex &index, const QVariant &value, int role)
0027 {
0028     if (mCheckable && role == Qt::CheckStateRole && index.isValid()) {
0029         bool shouldEmit = mData[index.row()].checked != value.toBool();
0030         mData[index.row()].checked = value.toBool();
0031         if (shouldEmit)
0032             Q_EMIT checkedCountChanged();
0033         Q_EMIT dataChanged(index, index);
0034     }
0035     return QAbstractListModel::setData(index, value, role);
0036 }
0037 
0038 Qt::ItemFlags ChangedFilesModel::flags(const QModelIndex &index) const
0039 {
0040     Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
0041     if (index.isValid()) {
0042         return defaultFlags | Qt::ItemIsUserCheckable;
0043     }
0044     return defaultFlags;
0045 }
0046 
0047 ChangedFilesModel::ChangedFilesModel(Git::Manager *git, bool checkable, QObject *parent)
0048     : QAbstractListModel(parent)
0049     , mGit(git)
0050     , mCheckable(checkable)
0051 {
0052 }
0053 
0054 void ChangedFilesModel::reload()
0055 {
0056     beginResetModel();
0057 
0058     mData.clear();
0059 
0060     auto files = mGit->changedFiles();
0061     for (auto i = files.begin(); i != files.end(); ++i) {
0062         if (i.value() == Git::ChangeStatus::Ignored)
0063             continue;
0064 
0065         qDebug() << i.key() << (int)i.value();
0066         Row d;
0067         d.filePath = i.key();
0068         d.status = i.value();
0069         d.checked = true;
0070         createIcon(d.status);
0071         mData << d;
0072     }
0073 
0074     endResetModel();
0075 }
0076 
0077 int ChangedFilesModel::rowCount(const QModelIndex &parent) const
0078 {
0079     Q_UNUSED(parent)
0080     return mData.size();
0081 }
0082 
0083 int ChangedFilesModel::columnCount(const QModelIndex &parent) const
0084 {
0085     Q_UNUSED(parent)
0086     return 1;
0087 }
0088 
0089 QVariant ChangedFilesModel::data(const QModelIndex &index, int role) const
0090 {
0091     if (index.row() < 0 || index.row() >= mData.size())
0092         return {};
0093     auto row = mData[index.row()];
0094 
0095     switch (role) {
0096     case Qt::DecorationRole:
0097         return mIcons.value(row.status);
0098 
0099     case Qt::CheckStateRole: {
0100         if (mCheckable)
0101             return row.checked ? Qt::Checked : Qt::Unchecked;
0102         else
0103             return {};
0104     }
0105     }
0106 
0107     if (role == Qt::DisplayRole) {
0108         if (index.column() == 1)
0109             return row.filePath;
0110         return row.filePath;
0111     }
0112     return {};
0113 }
0114 
0115 QString ChangedFilesModel::filePath(int index) const
0116 {
0117     if (index < 0 || index >= mData.size())
0118         return {};
0119     return mData[index].filePath;
0120 }
0121 
0122 int ChangedFilesModel::size() const
0123 {
0124     return mData.size();
0125 }
0126 
0127 QStringList ChangedFilesModel::checkedFiles() const
0128 {
0129     QStringList list;
0130     for (auto &row : mData)
0131         if (row.checked) {
0132             list << row.filePath;
0133             if (!row.oldFilePath.isEmpty())
0134                 list << row.oldFilePath;
0135         }
0136     return list;
0137 }
0138 
0139 void ChangedFilesModel::checkByStatus(Git::ChangeStatus status)
0140 {
0141     for (auto &row : mData)
0142         row.checked = row.status == status;
0143     Q_EMIT dataChanged(index(0), index(mData.size() - 1));
0144 }
0145 
0146 void ChangedFilesModel::checkByStatus(const QList<Git::ChangeStatus> &statuses)
0147 {
0148     for (auto &row : mData)
0149         row.checked = statuses.contains(row.status);
0150     Q_EMIT dataChanged(index(0), index(mData.size() - 1));
0151 }
0152 
0153 void ChangedFilesModel::toggleCheckAll(bool checked)
0154 {
0155     for (auto &row : mData)
0156         row.checked = checked;
0157     Q_EMIT dataChanged(index(0), index(mData.size() - 1));
0158 }
0159 
0160 const QList<ChangedFilesModel::Row> &ChangedFilesModel::data() const
0161 {
0162     return mData;
0163 }
0164 
0165 const ChangedFilesModel::Row *ChangedFilesModel::data(int index) const
0166 {
0167     return &mData.at(index);
0168 }
0169 
0170 int ChangedFilesModel::checkedCount() const
0171 {
0172     int ret{0};
0173     for (const auto &row : mData)
0174         if (row.checked)
0175             ret++;
0176     return ret;
0177 }
0178 
0179 #include "moc_changedfilesmodel.cpp"