1#pragma once
2
3/****************************************************************************************
4 ** GitQlient is an application to manage and operate one or several Git repositories. With
5 ** GitQlient you will be able to add commits, branches and manage all the options Git provides.
6 ** Copyright (C) 2021 Francesc Martinez
7 **
8 ** LinkedIn: www.linkedin.com/in/cescmm/
9 ** Web: www.francescmm.com
10 **
11 ** This program is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU Lesser General Public
13 ** License as published by the Free Software Foundation; either
14 ** version 2 of the License, or (at your option) any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** Lesser General Public License for more details.
20 **
21 ** You should have received a copy of the GNU Lesser General Public
22 ** License along with this library; if not, write to the Free Software
23 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 ***************************************************************************************/
25
26#include <QAbstractItemModel>
27#include <QSharedPointer>
28
29class GitCache;
30class GitBase;
31class CommitInfo;
32class GitServerCache;
33enum class CommitHistoryColumns;
34
35/**
36 * @brief The CommitHistoryModel contains the data model (is the Model in the MVC pattern) that will be displayed by the
37 * view.
38 *
39 * @class CommitHistoryModel CommitHistoryModel.h "CommitHistoryModel.h"
40 */
41class CommitHistoryModel : public QAbstractItemModel
42{
43 Q_OBJECT
44public:
45 /**
46 * @brief The default constructor.
47 *
48 * @param cache The internal cache of the current repository.
49 * @param git The git object to execute Git operations.
50 * @param parent The parent widget if needed.
51 */
52 explicit CommitHistoryModel(const QSharedPointer<GitCache> &cache, const QSharedPointer<GitBase> &git,
53 const QSharedPointer<GitServerCache> &gitServerCache, QObject *parent = nullptr);
54
55 /**
56 * @brief Clears the contents without deleting the cache.
57 */
58 void clear();
59 /**
60 * @brief Method used to retrieve the sha of a specific row. The view access this method instead of the internal
61 * cache because the row could change when a proxy filter is used between the View and the Model data.
62 *
63 * @param row The row to get the SHA from.
64 * @return QString The SHA.
65 */
66 QString sha(int row) const;
67
68 /**
69 * @brief Returns the data stored under the given \p role for the item referred to by the \p index
70 *
71 * @param index The index to get the data from.
72 * @param role The role from where to extract the data.
73 * @return QVariant The data value.
74 */
75 QVariant data(const QModelIndex &index, int role) const override;
76 /**
77 * @brief Returns the header data for a specific column.
78 *
79 * @param s The column.
80 * @param o The orientation. Refers to what header to extract the data: Horizontal or Vertical.
81 * @param role The role from where to extract the data.
82 * @return QVariant The data.
83 */
84 QVariant headerData(int s, Qt::Orientation o, int role = Qt::DisplayRole) const override;
85 /**
86 * @brief Returns the index of the item in the model specified by the given row, column and parent index
87 *
88 * @param r The row number.
89 * @param c The column number.
90 * @param par The parent index.
91 * @return QModelIndex The index.
92 */
93 QModelIndex index(int r, int c, const QModelIndex &par = QModelIndex()) const override;
94 /**
95 * @brief Returns the parent of the model item with the given index. If the item has no parent, an invalid
96 * QModelIndex is returned.
97 *
98 * @param index The index.
99 * @return QModelIndex The parent of the given \p index.
100 */
101 QModelIndex parent(const QModelIndex &index) const override;
102 /**
103 * @brief Returns the number of rows of an index.
104 *
105 * @param par The index to retrieve the rows.
106 * @return int The number of rows.
107 */
108 int rowCount(const QModelIndex &par = QModelIndex()) const override;
109 /**
110 * @brief Returns if an index contains children.
111 *
112 * @param par The index.
113 * @return bool True if has children, otherwise false.
114 */
115 bool hasChildren(const QModelIndex &par = QModelIndex()) const override;
116 /**
117 * @brief Returns the number of columns of the model.
118 *
119 * @return int The number of columns.
120 */
121 int columnCount(const QModelIndex &) const override { return mColumns.count(); }
122 /**
123 * @brief Resets the model when new revisions are available.
124 *
125 * @param totalCommits The total of new revisions.
126 */
127 void onNewRevisions(int totalCommits);
128 /*!
129 * \brief Gets the number of columns in the model.
130 * \return The number of columns.
131 */
132 int columnCount() const { return mColumns.count(); }
133
134private:
135 QSharedPointer<GitCache> mCache;
136 QSharedPointer<GitBase> mGit;
137 QSharedPointer<GitServerCache> mGitServerCache;
138 QMap<CommitHistoryColumns, QString> mColumns;
139
140 /**
141 * @brief Returns the tool tip data.
142 *
143 * @param r The commit to generate the tooltip data.
144 * @return QVariant The tool tip data.
145 */
146 QVariant getToolTipData(const CommitInfo &r) const;
147 /**
148 * @brief Returns the data that will be display for every \p column.
149 *
150 * @param rev The commit info to retrieve the data that will be displayed.
151 * @param column The column where the data will be shown.
152 * @return QVariant The data to be shown.
153 */
154 QVariant getDisplayData(const CommitInfo &rev, int column) const;
155};
156