| 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 <QStyledItemDelegate> | 
| 27 | #include <QDateTime> | 
| 28 |  | 
| 29 | class CommitHistoryView; | 
| 30 | class GitCache; | 
| 31 | class GitBase; | 
| 32 | class Lane; | 
| 33 | class CommitInfo; | 
| 34 | class GitServerCache; | 
| 35 |  | 
| 36 | namespace GitServer | 
| 37 | { | 
| 38 | struct PullRequest; | 
| 39 | } | 
| 40 |  | 
| 41 | const int ROW_HEIGHT = 25; | 
| 42 | const int LANE_WIDTH = 3 * ROW_HEIGHT / 4; | 
| 43 |  | 
| 44 | /** | 
| 45 |  * @brief The RepositoryViewDelegate class is the delegate overloads the paint functionality in the RepositoryView. This | 
| 46 |  * class is the responsible of painting the graph of the repository. In addition to paint all the columns, implements | 
| 47 |  * special functionality to paint the tags, the branch names and how they are represented (local or remote) | 
| 48 |  * | 
| 49 |  */ | 
| 50 | class RepositoryViewDelegate : public QStyledItemDelegate | 
| 51 | { | 
| 52 |    Q_OBJECT | 
| 53 |  | 
| 54 | public: | 
| 55 |    /** | 
| 56 |     * @brief Default constructor. | 
| 57 |     * | 
| 58 |     * @param cache The cache for the current repository. | 
| 59 |     * @param git The git object to execute git commands. | 
| 60 |     * @param view The view that uses the delegate. | 
| 61 |     */ | 
| 62 |    RepositoryViewDelegate(const QSharedPointer<GitCache> &cache, const QSharedPointer<GitBase> &git, | 
| 63 |                           const QSharedPointer<GitServerCache> &gitServerCache, CommitHistoryView *view); | 
| 64 |  | 
| 65 |    /** | 
| 66 |     * @brief Overridden method to paint the different columns and rows in the view. | 
| 67 |     * | 
| 68 |     * @param p The painter device. | 
| 69 |     * @param o The style options of the item. | 
| 70 |     * @param i The index with the item data. | 
| 71 |     */ | 
| 72 |    void paint(QPainter *p, const QStyleOptionViewItem &o, const QModelIndex &i) const override; | 
| 73 |    /** | 
| 74 |     * @brief The size hint returns the width and height for rendering purposes. | 
| 75 |     * | 
| 76 |     * @return QSize returns the size of a row. | 
| 77 |     */ | 
| 78 |    QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const override; | 
| 79 |  | 
| 80 | protected: | 
| 81 |    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, | 
| 82 |                     const QModelIndex &index) override; | 
| 83 |  | 
| 84 | private: | 
| 85 |    QSharedPointer<GitCache> mCache; | 
| 86 |    QSharedPointer<GitBase> mGit; | 
| 87 |    QSharedPointer<GitServerCache> mGitServerCache; | 
| 88 |    CommitHistoryView *mView = nullptr; | 
| 89 |    int diffTargetRow = -1; | 
| 90 |    int mColumnPressed = -1; | 
| 91 |  | 
| 92 |    /** | 
| 93 |     * @brief Paints the log column. This method is in charge of painting the commit message as well as tags or | 
| 94 |     * branches. | 
| 95 |     * | 
| 96 |     * @param p The painter device. | 
| 97 |     * @param o The style options of the item. | 
| 98 |     * @param i The index with the item data. | 
| 99 |     */ | 
| 100 |    void paintLog(QPainter *p, const QStyleOptionViewItem &o, const CommitInfo &commit, const QString &text) const; | 
| 101 |    /** | 
| 102 |     * @brief Method that sets up the configuration to paint the lane for the commit graph representation. | 
| 103 |     * | 
| 104 |     * @param p The painter device. | 
| 105 |     * @param o The style options of the item. | 
| 106 |     * @param index The index with the item data. | 
| 107 |     */ | 
| 108 |    void paintGraph(QPainter *p, const QStyleOptionViewItem &o, const CommitInfo &commit) const; | 
| 109 |  | 
| 110 |    /** | 
| 111 |     * @brief Specialization method called by @ref paintGrapth that does the actual lane painting. | 
| 112 |     * | 
| 113 |     * @param p The painter device. | 
| 114 |     * @param type The type of lane to paint. | 
| 115 |     * @param laneHeadPresent Tells the method if the lane contains a head. | 
| 116 |     * @param x1 X coordinate where the painting starts | 
| 117 |     * @param x2 X coordinate where the painting ends | 
| 118 |     * @param col Color of the lane | 
| 119 |     * @param activeCol Color of the active lane | 
| 120 |     * @param mergeColor Color of the lane where the merge comes from in case the commit is a end-merge point. | 
| 121 |     * @param isWip Tells the method if it's the WIP commit so it's painted differently. | 
| 122 |     */ | 
| 123 |    void paintGraphLane(QPainter *p, const Lane &type, bool laneHeadPresent, int x1, int x2, const QColor &col, | 
| 124 |                        const QColor &activeCol, const QColor &mergeColor, bool isWip = false, | 
| 125 |                        bool hasChilds = true) const; | 
| 126 |  | 
| 127 |    /** | 
| 128 |     * @brief Specialized method that paints a tag in the commit message column. | 
| 129 |     * | 
| 130 |     * @param painter The painter device. | 
| 131 |     * @param opt The style options of the item. | 
| 132 |     * @param startPoint The starting X coordinate for the tag. | 
| 133 |     * @param sha The SHA reference to paint. It can be local branch, remote branch, tag or it could be detached. | 
| 134 |     */ | 
| 135 |    void paintTagBranch(QPainter *painter, QStyleOptionViewItem opt, int &startPoint, const QString &sha) const; | 
| 136 |  | 
| 137 |    /** | 
| 138 |     * @brief Specialized method that paints a tag in the commit message column. | 
| 139 |     * | 
| 140 |     * @param painter The painter device. | 
| 141 |     * @param opt The style options of the item. | 
| 142 |     * @param startPoint The starting X coordinate for the tag. | 
| 143 |     * @param pr The PullRequest status. | 
| 144 |     */ | 
| 145 |    void paintPrStatus(QPainter *painter, QStyleOptionViewItem opt, int &startPoint, | 
| 146 |                       const GitServer::PullRequest &pr) const; | 
| 147 |  | 
| 148 |    /** | 
| 149 |     * @brief getMergeColor Returns the color to be used for painting the external circle of the node. This methods | 
| 150 |     * searches the origin of the merge and uses the same lane color. | 
| 151 |     * @param currentLane The current lane type. | 
| 152 |     * @param commit The current commit. | 
| 153 |     * @param currentLaneIndex The current index of the lane. | 
| 154 |     * @param defaultColor The default color in case it's not a merge. | 
| 155 |     * @param isSet Boolean used as a shortcut. If the current iteration is a merge it will change the value for the | 
| 156 |     * following lanes. | 
| 157 |     * @return Returns the color of the lane that merges into the current node, otherwise it returns @p defaultColor. | 
| 158 |     */ | 
| 159 |    QColor getMergeColor(const Lane ¤tLane, const CommitInfo &commit, int currentLaneIndex, | 
| 160 |                         const QColor &defaultColor, bool &isSet) const; | 
| 161 | }; | 
| 162 |  |