File indexing completed on 2024-04-28 05:49:04

0001 /*
0002     SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "branchesdialogmodel.h"
0007 
0008 #include <QFont>
0009 #include <QIcon>
0010 
0011 BranchesDialogModel::BranchesDialogModel(QObject *parent)
0012     : QAbstractTableModel(parent)
0013 {
0014 }
0015 
0016 int BranchesDialogModel::rowCount(const QModelIndex &parent) const
0017 {
0018     if (parent.isValid()) {
0019         return 0;
0020     }
0021     return m_modelEntries.size();
0022 }
0023 
0024 int BranchesDialogModel::columnCount(const QModelIndex &parent) const
0025 {
0026     Q_UNUSED(parent);
0027     return 1;
0028 }
0029 
0030 QVariant BranchesDialogModel::data(const QModelIndex &idx, int role) const
0031 {
0032     if (!idx.isValid()) {
0033         return {};
0034     }
0035 
0036     const Branch &branch = m_modelEntries.at(idx.row());
0037     if (role == Qt::DisplayRole) {
0038         return branch.name;
0039     } else if (role == Role::FuzzyScore) {
0040         return branch.score;
0041     } else if (role == Qt::DecorationRole) {
0042         if (branch.itemType == BranchItem) {
0043             static const auto branchIcon = QIcon::fromTheme(QStringLiteral("vcs-branch"));
0044             return branchIcon;
0045         }
0046     } else if (role == Qt::FontRole) {
0047         if (branch.itemType == CreateBranch || branch.itemType == CreateBranchFrom) {
0048             QFont font;
0049             font.setBold(true);
0050             return font;
0051         }
0052     } else if (role == Role::CheckoutName) {
0053         return branch.refType == GitUtils::RefType::Remote ? branch.name.mid(branch.remote.size() + 1) : branch.name;
0054     } else if (role == Role::RefType) {
0055         return branch.refType;
0056     } else if (role == Role::ItemTypeRole) {
0057         return branch.itemType;
0058     }
0059 
0060     return {};
0061 }
0062 
0063 void BranchesDialogModel::refresh(const QList<GitUtils::Branch> &branches, bool checkingOut)
0064 {
0065     QList<Branch> temp;
0066     if (checkingOut) {
0067         Branch create{branches.at(0).name, {}, {}, 0, ItemType::CreateBranch};
0068         Branch createFrom{branches.at(1).name, {}, {}, 0, ItemType::CreateBranchFrom};
0069         temp.push_back(create);
0070         temp.push_back(createFrom);
0071     }
0072 
0073     int i = checkingOut ? 2 : 0;
0074     for (; i < branches.size(); ++i) {
0075         temp.append({branches.at(i).name, branches.at(i).remote, branches.at(i).type, -1, ItemType::BranchItem});
0076     }
0077 
0078     beginResetModel();
0079     m_modelEntries = std::move(temp);
0080     endResetModel();
0081 }
0082 
0083 void BranchesDialogModel::clear()
0084 {
0085     beginResetModel();
0086     QList<Branch>().swap(m_modelEntries);
0087     endResetModel();
0088 }
0089 
0090 void BranchesDialogModel::clearBranchCreationItems()
0091 {
0092     beginRemoveRows(QModelIndex(), 0, 1);
0093     m_modelEntries.removeFirst();
0094     m_modelEntries.removeFirst();
0095     endRemoveRows();
0096 }
0097 
0098 #include "moc_branchesdialogmodel.cpp"