| 1 | #pragma once |
| 2 | |
| 3 | /**************************************************************************** |
| 4 | ** |
| 5 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 6 | ** Copyright (C) 2021 Francesc M. |
| 7 | ** Contact: https://www.qt.io/licensing/ |
| 8 | ** |
| 9 | ** This file is part of the examples of the Qt Toolkit. |
| 10 | ** |
| 11 | ** $QT_BEGIN_LICENSE:BSD$ |
| 12 | ** Commercial License Usage |
| 13 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 14 | ** accordance with the commercial license agreement provided with the |
| 15 | ** Software or, alternatively, in accordance with the terms contained in |
| 16 | ** a written agreement between you and The Qt Company. For licensing terms |
| 17 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 18 | ** information use the contact form at https://www.qt.io/contact-us. |
| 19 | ** |
| 20 | ** BSD License Usage |
| 21 | ** Alternatively, you may use this file under the terms of the BSD license |
| 22 | ** as follows: |
| 23 | ** |
| 24 | ** "Redistribution and use in source and binary forms, with or without |
| 25 | ** modification, are permitted provided that the following conditions are |
| 26 | ** met: |
| 27 | ** * Redistributions of source code must retain the above copyright |
| 28 | ** notice, this list of conditions and the following disclaimer. |
| 29 | ** * Redistributions in binary form must reproduce the above copyright |
| 30 | ** notice, this list of conditions and the following disclaimer in |
| 31 | ** the documentation and/or other materials provided with the |
| 32 | ** distribution. |
| 33 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 34 | ** contributors may be used to endorse or promote products derived |
| 35 | ** from this software without specific prior written permission. |
| 36 | ** |
| 37 | ** |
| 38 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 39 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 40 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 41 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 42 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 43 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 44 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 45 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 46 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 47 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 48 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 49 | ** |
| 50 | ** $QT_END_LICENSE$ |
| 51 | ** |
| 52 | ****************************************************************************/ |
| 53 | |
| 54 | #include <QPlainTextEdit> |
| 55 | #include <DiffInfo.h> |
| 56 | |
| 57 | class FileDiffHighlighter; |
| 58 | |
| 59 | class LineNumberArea; |
| 60 | |
| 61 | /*! |
| 62 | \brief The FileDiffView is an overload QPlainTextEdit class used to show the contents of a file diff between two |
| 63 | commits. |
| 64 | |
| 65 | */ |
| 66 | class FileDiffView : public QPlainTextEdit |
| 67 | { |
| 68 | Q_OBJECT |
| 69 | |
| 70 | signals: |
| 71 | /** |
| 72 | * @brief signalScrollChanged Signal emitted when the scrollbar changes its position. |
| 73 | * @param value The new scrollbar position. |
| 74 | */ |
| 75 | void signalScrollChanged(int value); |
| 76 | |
| 77 | /** |
| 78 | * @brief signalStageChunk Signal triggered when the user orders to stage a chunk. |
| 79 | * @param id The internal chunk id. |
| 80 | */ |
| 81 | void signalStageChunk(const QString &id); |
| 82 | |
| 83 | public: |
| 84 | /*! |
| 85 | \brief Default constructor. |
| 86 | |
| 87 | \param parent The parent widget if needed. |
| 88 | */ |
| 89 | explicit FileDiffView(QWidget *parent = nullptr); |
| 90 | |
| 91 | /** |
| 92 | * @brief Default constructor |
| 93 | */ |
| 94 | ~FileDiffView(); |
| 95 | |
| 96 | void addNumberArea(LineNumberArea *numberArea); |
| 97 | |
| 98 | /** |
| 99 | * @brief loadDiff Loads the text edit based on a diff text. |
| 100 | * @param text The text representing a diff |
| 101 | * @return True if correctly loaded, otherwise false. |
| 102 | */ |
| 103 | void loadDiff(const QString &text, |
| 104 | const QVector<ChunkDiffInfo::ChunkInfo> &fileDiffInfo = QVector<ChunkDiffInfo::ChunkInfo>()); |
| 105 | |
| 106 | /** |
| 107 | * @brief moveScrollBarToPos Moves the vertical scroll bar to the value defined in @p value. |
| 108 | * @param value The new scroll bar value. |
| 109 | */ |
| 110 | void moveScrollBarToPos(int value); |
| 111 | |
| 112 | /** |
| 113 | * @brief setStartingLine Makes the widget start from the line @p lineNumber. |
| 114 | * @param lineNumber The starting line number. |
| 115 | */ |
| 116 | void setStartingLine(int lineNumber) { mStartingLine = lineNumber; } |
| 117 | |
| 118 | /** |
| 119 | * @brief setUnifiedDiff Sets the diff as unified view. |
| 120 | * @param unified True if unified view must be shown. |
| 121 | */ |
| 122 | void setUnifiedDiff(bool unified) { mUnified = unified; } |
| 123 | |
| 124 | /** |
| 125 | * @brief getHeight Gets the approximated height of the widget based on the text of the QTextDocument. |
| 126 | * @return The height. |
| 127 | */ |
| 128 | int getHeight() const; |
| 129 | |
| 130 | /** |
| 131 | * @brief getLineHeigth Method that returns the height value of the rows. |
| 132 | * @return The height of a row. |
| 133 | */ |
| 134 | int getLineHeigth() const; |
| 135 | |
| 136 | protected: |
| 137 | /*! |
| 138 | \brief Overloaded method to process the resize event. Used to set an updated geometry to the line number area. |
| 139 | |
| 140 | \param event The resize event. |
| 141 | */ |
| 142 | void resizeEvent(QResizeEvent *event) override; |
| 143 | |
| 144 | /** |
| 145 | * @brief eventFilter Custom event filter to enable the mechanism of storing comments (used by Jenkins view). |
| 146 | * @param target The target object of the event. |
| 147 | * @param event The event that was triggered. |
| 148 | * @return True if filtered, otherwise false. |
| 149 | */ |
| 150 | bool eventFilter(QObject *target, QEvent *event) override; |
| 151 | |
| 152 | /** |
| 153 | * @brief showStagingMenu Shows the context menu to stage lines or chunks. |
| 154 | * @param cursorPos The current position of the mouse cursor. |
| 155 | */ |
| 156 | void (const QPoint &cursorPos); |
| 157 | |
| 158 | private: |
| 159 | /*! |
| 160 | \brief Updates the line number area width based on the number of the line. |
| 161 | |
| 162 | \param newBlockCount The number of the line. |
| 163 | */ |
| 164 | void updateLineNumberAreaWidth(int newBlockCount); |
| 165 | /*! |
| 166 | \brief Updates the line number area whenever the QPlainTextEdit emits the updateRequest signal. |
| 167 | |
| 168 | \param rect The rect area that was updated. |
| 169 | \param dy The increment. |
| 170 | */ |
| 171 | void updateLineNumberArea(const QRect &rect, int dy); |
| 172 | |
| 173 | /*! |
| 174 | \brief Returns the width of the line number area. |
| 175 | |
| 176 | \return int The width in pixels. |
| 177 | */ |
| 178 | int lineNumberAreaWidth(); |
| 179 | |
| 180 | QVector<ChunkDiffInfo::ChunkInfo> mFileDiffInfo; |
| 181 | LineNumberArea *mLineNumberArea = nullptr; |
| 182 | FileDiffHighlighter *mDiffHighlighter = nullptr; |
| 183 | int mStartingLine = 0; |
| 184 | bool mUnified = false; |
| 185 | int mRow = -1; |
| 186 | |
| 187 | friend class LineNumberArea; |
| 188 | }; |
| 189 | |