| 1 | #include "CommitHistoryModel.h" |
| 2 | |
| 3 | #include <CommitHistoryColumns.h> |
| 4 | #include <CommitInfo.h> |
| 5 | #include <GitBase.h> |
| 6 | #include <GitCache.h> |
| 7 | #include <GitServerCache.h> |
| 8 | |
| 9 | #include <QDateTime> |
| 10 | #include <QLocale> |
| 11 | |
| 12 | CommitHistoryModel::CommitHistoryModel(const QSharedPointer<GitCache> &cache, const QSharedPointer<GitBase> &git, |
| 13 | const QSharedPointer<GitServerCache> &gitServerCache, QObject *p) |
| 14 | : QAbstractItemModel(p) |
| 15 | , mCache(cache) |
| 16 | , mGit(git) |
| 17 | , mGitServerCache(gitServerCache) |
| 18 | { |
| 19 | mColumns.insert(CommitHistoryColumns::TreeViewIcon, "" ); |
| 20 | mColumns.insert(CommitHistoryColumns::Graph, "" ); |
| 21 | mColumns.insert(CommitHistoryColumns::Sha, "Sha" ); |
| 22 | mColumns.insert(CommitHistoryColumns::Log, "History" ); |
| 23 | mColumns.insert(CommitHistoryColumns::Author, "Author" ); |
| 24 | mColumns.insert(CommitHistoryColumns::Date, "Date" ); |
| 25 | } |
| 26 | |
| 27 | int CommitHistoryModel::rowCount(const QModelIndex &parent) const |
| 28 | { |
| 29 | return !parent.isValid() ? mCache->commitCount() : 0; |
| 30 | } |
| 31 | |
| 32 | bool CommitHistoryModel::hasChildren(const QModelIndex &parent) const |
| 33 | { |
| 34 | return !parent.isValid(); |
| 35 | } |
| 36 | |
| 37 | QString CommitHistoryModel::sha(int row) const |
| 38 | { |
| 39 | return index(row, static_cast<int>(CommitHistoryColumns::Sha)).data().toString(); |
| 40 | } |
| 41 | |
| 42 | void CommitHistoryModel::clear() |
| 43 | { |
| 44 | beginResetModel(); |
| 45 | endResetModel(); |
| 46 | emit headerDataChanged(Qt::Horizontal, 0, 5); |
| 47 | } |
| 48 | |
| 49 | void CommitHistoryModel::onNewRevisions(int totalCommits) |
| 50 | { |
| 51 | beginResetModel(); |
| 52 | endResetModel(); |
| 53 | |
| 54 | beginInsertRows(QModelIndex(), 0, totalCommits - 2); |
| 55 | endInsertRows(); |
| 56 | } |
| 57 | |
| 58 | QVariant CommitHistoryModel::(int section, Qt::Orientation orientation, int role) const |
| 59 | { |
| 60 | if (orientation == Qt::Horizontal && role == Qt::DisplayRole) |
| 61 | return mColumns.value(static_cast<CommitHistoryColumns>(section)); |
| 62 | |
| 63 | return QVariant(); |
| 64 | } |
| 65 | |
| 66 | QModelIndex CommitHistoryModel::index(int row, int column, const QModelIndex &) const |
| 67 | { |
| 68 | return row >= 0 && row < mCache->commitCount() ? createIndex(row, column, nullptr) : QModelIndex(); |
| 69 | } |
| 70 | |
| 71 | QModelIndex CommitHistoryModel::parent(const QModelIndex &) const |
| 72 | { |
| 73 | return QModelIndex(); |
| 74 | } |
| 75 | |
| 76 | QVariant CommitHistoryModel::getToolTipData(const CommitInfo &r) const |
| 77 | { |
| 78 | QString auxMessage; |
| 79 | const auto sha = r.sha; |
| 80 | |
| 81 | if (mGit->getCurrentBranch().isEmpty()) |
| 82 | auxMessage.append(tr("<p>Status: <b>detached</b></p>" )); |
| 83 | |
| 84 | const auto localBranches = mCache->getReferences(sha, References::Type::LocalBranch); |
| 85 | |
| 86 | if (!localBranches.isEmpty()) |
| 87 | auxMessage.append(tr("<p><b>Local: </b>%1</p>" ).arg(localBranches.join("," ))); |
| 88 | |
| 89 | const auto remoteBranches = mCache->getReferences(sha, References::Type::RemoteBranches); |
| 90 | |
| 91 | if (!remoteBranches.isEmpty()) |
| 92 | auxMessage.append(tr("<p><b>Remote: </b>%1</p>" ).arg(remoteBranches.join("," ))); |
| 93 | |
| 94 | const auto tags = mCache->getReferences(sha, References::Type::LocalTag); |
| 95 | |
| 96 | if (!tags.isEmpty()) |
| 97 | auxMessage.append(tr("<p><b>Tags: </b>%1</p>" ).arg(tags.join("," ))); |
| 98 | |
| 99 | QDateTime d; |
| 100 | d.setSecsSinceEpoch(r.dateSinceEpoch.count()); |
| 101 | |
| 102 | QLocale locale; |
| 103 | |
| 104 | auto tooltip = sha == CommitInfo::ZERO_SHA |
| 105 | ? QString() |
| 106 | : QString("<p>%1 - %2</p><p>%3</p>%4%5" ) |
| 107 | .arg(r.author.split("<" ).first(), d.toString(locale.dateTimeFormat(QLocale::ShortFormat)), sha, |
| 108 | !auxMessage.isEmpty() ? QString("<p>%1</p>" ).arg(auxMessage) : "" , |
| 109 | r.isSigned() |
| 110 | ? tr("<p> GPG key (%1): %2</p>" ) |
| 111 | .arg(QString::fromUtf8(r.verifiedSignature() ? "verified" : "not verified" ), r.gpgKey) |
| 112 | : "" ); |
| 113 | |
| 114 | if (mGitServerCache) |
| 115 | { |
| 116 | if (const auto pr = mGitServerCache->getPullRequest(sha); pr.isValid()) |
| 117 | tooltip.append(tr("<p><b>PR state: </b>%1.</p>" ).arg(pr.state.state)); |
| 118 | } |
| 119 | |
| 120 | return tooltip; |
| 121 | } |
| 122 | |
| 123 | QVariant CommitHistoryModel::getDisplayData(const CommitInfo &rev, int column) const |
| 124 | { |
| 125 | switch (static_cast<CommitHistoryColumns>(column)) |
| 126 | { |
| 127 | case CommitHistoryColumns::Sha: { |
| 128 | const auto sha = rev.sha; |
| 129 | return sha; |
| 130 | } |
| 131 | case CommitHistoryColumns::Log: |
| 132 | return rev.shortLog; |
| 133 | case CommitHistoryColumns::Author: { |
| 134 | const auto author = rev.author.split("<" ).first(); |
| 135 | return author; |
| 136 | } |
| 137 | case CommitHistoryColumns::Date: { |
| 138 | return QDateTime::fromSecsSinceEpoch(rev.dateSinceEpoch.count()).toString("dd MMM yyyy hh:mm" ); |
| 139 | } |
| 140 | default: |
| 141 | return QVariant(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | QVariant CommitHistoryModel::data(const QModelIndex &index, int role) const |
| 146 | { |
| 147 | if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::ToolTipRole)) |
| 148 | return QVariant(); |
| 149 | |
| 150 | const auto r = mCache->commitInfo(index.row()); |
| 151 | |
| 152 | if (role == Qt::ToolTipRole) |
| 153 | return getToolTipData(r); |
| 154 | |
| 155 | if (role == Qt::DisplayRole) |
| 156 | return getDisplayData(r, index.column()); |
| 157 | |
| 158 | return QVariant(); |
| 159 | } |
| 160 | |