repositorymodel.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "repositorymodel.h"
  2. #include <gitrepository.h>
  3. #include <QDebug>
  4. QHash<int, QByteArray> RepositoryModel::m_roles;
  5. RepositoryModel::RepositoryModel(QObject* parent) : QAbstractListModel(parent)
  6. {
  7. if(m_roles.isEmpty()) {
  8. m_roles[RepoName] = "repoName";
  9. m_roles[RepoRoot] = "repoRoot";
  10. m_roles[RepoBranches] = "repoBranches";
  11. }
  12. }
  13. RepositoryModel::~RepositoryModel()
  14. {
  15. foreach (QPointer<GitRepository> repo, m_repositories) {
  16. delete repo.data();
  17. }
  18. m_repositories.clear();
  19. }
  20. bool RepositoryModel::addRepository(GitRepository* repo)
  21. {
  22. if(repo == nullptr)
  23. {
  24. qDebug() << "Repo is null";
  25. return false;
  26. }
  27. if(m_repositories.contains(repo->root())) {
  28. qDebug() << "Repository" << repo->root() << "already exists";
  29. return false;
  30. }
  31. beginInsertRows(QModelIndex(),0,0);
  32. m_repositories.insert(repo->root(), QPointer<GitRepository>(repo));
  33. endInsertRows();
  34. return true;
  35. }
  36. void RepositoryModel::removeRepository(GitRepository* repo)
  37. {
  38. if(repo == nullptr) {
  39. return;
  40. }
  41. if(m_repositories.contains(repo->root())) {
  42. beginRemoveRows(QModelIndex(), m_repositories.values().indexOf(repo), m_repositories.values().indexOf(repo));
  43. m_repositories.remove(repo->root());
  44. endRemoveRows();
  45. }
  46. }
  47. void RepositoryModel::removeRepository(const QString& root)
  48. {
  49. m_repositories.remove(root);
  50. }
  51. QVariant RepositoryModel::data(const QModelIndex &index, int role) const
  52. {
  53. int row = index.row();
  54. if(row < 0 || row >= m_repositories.count()) {
  55. return QVariant();
  56. }
  57. GitRepository* repo = m_repositories.values().at(row).data();
  58. switch (role) {
  59. case RepoName:
  60. return QVariant(repo->name());
  61. case RepoRoot:
  62. return QVariant(repo->root());
  63. default:
  64. break;
  65. }
  66. return QVariant();
  67. }
  68. int RepositoryModel::rowCount(const QModelIndex &parent) const
  69. {
  70. Q_UNUSED(parent)
  71. return m_repositories.count();
  72. }
  73. QHash<int, QByteArray> RepositoryModel::roleNames() const
  74. {
  75. return m_roles;
  76. }