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 <QFrame>
27#include <QDateTime>
28
29class GitBase;
30class QScrollArea;
31class ButtonLink;
32class QLabel;
33class GitCache;
34
35/*!
36 \brief The FileBalmeWidget class is the widget that creates the view for the blame of a file. It is formed by two
37 parts: in the right side it is shown the code of the file. In the right and for every different line, it is shown the
38 information of the commit with a color guide. The bright color indicates the more recent changes whereas the darkest
39 color indicates the oldest.
40
41*/
42class FileBlameWidget : public QFrame
43{
44 Q_OBJECT
45
46signals:
47 /*!
48 \brief Signal triggered when the user selects a revision in the blame.
49
50 \param sha
51 */
52 void signalCommitSelected(const QString &sha);
53
54public:
55 /*!
56 \brief Default constructor.
57
58 \param cache The internal repository cache.
59 \param git The git object to perform Git operations.
60 \param parent The parent widget if needed.
61 */
62 explicit FileBlameWidget(const QSharedPointer<GitCache> &cache, const QSharedPointer<GitBase> &git,
63 QWidget *parent = nullptr);
64
65 /*!
66 \brief Sets up the widget by providing the file to blame and the last commit SHA where the file was modified. The
67 previous sha is passed for general information.
68
69 \param fileName The file name to blame.
70 \param currentSha The last commit SHA where the file was modified.
71 \param previousSha The previous commit SHA where the file was modified.
72 */
73 void setup(const QString &fileName, const QString &currentSha, const QString &previousSha);
74 /*!
75 \brief Reloads the current file blame information based on the new \p currentSha. The
76 previous sha is passed for general information.
77
78 \param currentSha A commit SHA where the file was modified.
79 \param previousSha The previous commit SHA where the file was modified.
80 */
81 void reload(const QString &currentSha, const QString &previousSha);
82 /*!
83 \brief Retrieves the current used SHA.
84
85 \return QString The current SHA.
86 */
87 QString getCurrentSha() const;
88 /*!
89 \brief Retrieves the file displayed.
90
91 \return QString The file being displayed.
92 */
93 QString getCurrentFile() const { return mCurrentFile; }
94
95private:
96 QSharedPointer<GitCache> mCache;
97 QSharedPointer<GitBase> mGit;
98 QFrame *mAnotation = nullptr;
99 QLabel *mCurrentSha = nullptr;
100 QLabel *mPreviousSha = nullptr;
101 QScrollArea *mScrollArea = nullptr;
102 QFont mInfoFont;
103 QFont mCodeFont;
104 QString mCurrentFile;
105
106 /*!
107 \brief Private class that stores data of a annotation. An annotation is the informatio regarding when a line was
108 last modified.
109
110 */
111 struct Annotation
112 {
113 QString sha;
114 QString author;
115 QDateTime dateTime;
116 int line = 0;
117 QString content;
118 };
119
120 /*!
121 \brief Processes a blame converting the git output into a vector of annotations per each line.
122
123 \param blame The git blame output.
124 \return QVector<Annotation> Vector of the annotations for every line.
125 */
126 QVector<Annotation> processBlame(const QString &blame);
127 /*!
128 \brief Process all the \p annotations and creates the view of the file with that information.
129
130 \param annotations The annotations to process.
131 */
132 void formatAnnotatedFile(const QVector<Annotation> &annotations);
133 /*!
134 \brief Factory method that creates a label with the date and time based on an annotation.
135
136 \param annotation The annotation to process.
137 \param isFirst Indicates if it's the first item in the blame.
138 \return QLabel Returns a newly created QLabel.
139 */
140 QLabel *createDateLabel(const Annotation &annotation, bool isFirst);
141 /*!
142 \brief Factory method that creates a label with the author information based on an annotation.
143
144 \param author The author to be shown.
145 \param isFirst Indicates if it's the first item in the blame.
146 \return QLabel Returns a newly created QLabel.
147 */
148 QLabel *createAuthorLabel(const QString &author, bool isFirst);
149 /*!
150 \brief Factory method that creates a Clickable frame with the informatio of the commit (SHA and title).
151
152 \param sha The sha to show.
153 \param isFirst Indicates if it's the first item in the blame.
154 \return Returns a newly created ButtonLink.
155 */
156 ButtonLink *createMessageLabel(const QString &sha, bool isFirst);
157 /*!
158 \brief Factory method that creates a label with the number of line. Uses the \p annotation parameter to display a
159 visual help about when the change was done.
160
161 \param annotation The annotation to process.
162 \param row The row to display.
163 \return QLabel Returns a newly created QLabel.
164 */
165 QLabel *createNumLabel(const Annotation &annotation, int row);
166 /*!
167 \brief Factory method that creates a label with the code line to be displayed.
168
169 \param content The code line to display.
170 \return QLabel Returns a newly created QLable.
171 */
172 QLabel *createCodeLabel(const QString &content);
173};
174