KompareDiff2

difference.h
1/*
2SPDX-FileCopyrightText: 2001-2004,2009 Otto Bruggeman <bruggie@gmail.com>
3SPDX-FileCopyrightText: 2001-2003 John Firebaugh <jfirebaugh@kde.org>
4
5SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#ifndef KOMPAREDIFF2_DIFFERENCE_H
9#define KOMPAREDIFF2_DIFFERENCE_H
10
11#include <QVector>
12#include <QObject>
13
14#include "komparediff2_export.h"
15#include "marker.h"
16
17// #include <komparediffdebug.h>
18
19class QString;
20
21namespace Diff2
22{
23
24/**
25 * @class DifferenceString difference.h <KompareDiff2/Difference>
26 *
27 * A difference string.
28 */
29class KOMPAREDIFF2_EXPORT DifferenceString
30{
31public:
33 {
34// qCDebug(LIBKOMPAREDIFF2) << "DifferenceString::DifferenceString()";
35 }
36 explicit DifferenceString(const QString& string, const MarkerList& markerList = MarkerList()) :
37 m_string(string),
38 m_markerList(markerList)
39 {
40// qCDebug(LIBKOMPAREDIFF2) << "DifferenceString::DifferenceString( " << string << ", " << markerList << " )";
41 calculateHash();
42 }
44 m_string(ds.m_string),
45 m_conflict(ds.m_conflict),
46 m_hash(ds.m_hash),
47 m_markerList(ds.m_markerList)
48 {
49// qCDebug(LIBKOMPAREDIFF2) << "DifferenceString::DifferenceString( const DifferenceString& " << ds << " )";
50 }
52 {
53 qDeleteAll(m_markerList);
54 }
55
56public:
57 const QString& string() const
58 {
59 return m_string;
60 }
61 const QString& conflictString() const
62 {
63 return m_conflict;
64 }
65 const MarkerList& markerList()
66 {
67 return m_markerList;
68 }
69 void setString(const QString& string)
70 {
71 m_string = string;
72 calculateHash();
73 }
74 void setConflictString(const QString& conflict)
75 {
76 m_conflict = conflict;
77 }
78 void setMarkerList(const MarkerList& markerList)
79 {
80 m_markerList = markerList;
81 }
82 void prepend(Marker* marker)
83 {
84 m_markerList.prepend(marker);
85 }
86 bool operator==(const DifferenceString& ks)
87 {
88 if (m_hash != ks.m_hash)
89 return false;
90 return m_string == ks.m_string;
91 }
92
93protected:
94 void calculateHash()
95 {
96 unsigned short const* str = reinterpret_cast<unsigned short const*>(m_string.unicode());
97 const unsigned int len = m_string.length();
98
99 m_hash = 1315423911;
100
101 for (unsigned int i = 0; i < len; ++i)
102 {
103 m_hash ^= (m_hash << 5) + str[i] + (m_hash >> 2);
104 }
105 }
106
107private:
108 QString m_string;
109 QString m_conflict;
110 unsigned int m_hash;
111 MarkerList m_markerList;
112};
113
115using DifferenceStringListIterator = QVector<DifferenceString*>::iterator;
116using DifferenceStringListConstIterator = QVector<DifferenceString*>::const_iterator;
117
118/**
119 * @class Difference difference.h <KompareDiff2/Difference>
120 *
121 * A difference.
122 */
123class KOMPAREDIFF2_EXPORT Difference : public QObject
124{
125 Q_OBJECT
126public:
127 enum Type { Change, Insert, Delete, Unchanged };
128
129public:
130 Difference(int sourceLineNo, int destinationLineNo, int type = Difference::Unchanged);
131 ~Difference() override;
132
133public:
134 int type() const { return m_type; };
135
136 int sourceLineNumber() const { return m_sourceLineNo; }
137 int destinationLineNumber() const { return m_destinationLineNo; }
138
139 int sourceLineCount() const;
140 int destinationLineCount() const;
141
142 int sourceLineEnd() const;
143 int destinationLineEnd() const;
144
145 /// Destination line number that tracks applying/unapplying of other differences
146 /// Essentially a line number in a patch consisting of applied diffs only
147 int trackingDestinationLineNumber() const { return m_trackingDestinationLineNo; }
148 int trackingDestinationLineEnd() const;
149 void setTrackingDestinationLineNumber(int i) { m_trackingDestinationLineNo = i; }
150
151 DifferenceString* sourceLineAt(int i) const { return m_sourceLines[i]; }
152 DifferenceString* destinationLineAt(int i) const { return m_destinationLines[i]; }
153
154 const DifferenceStringList sourceLines() const { return m_sourceLines; }
155 const DifferenceStringList destinationLines() const { return m_destinationLines; }
156
157 bool hasConflict() const
158 {
159 return m_conflicts;
160 }
161 void setConflict(bool conflicts)
162 {
163 m_conflicts = conflicts;
164 }
165
166 bool isUnsaved() const
167 {
168 return m_unsaved;
169 }
170 void setUnsaved(bool unsaved)
171 {
172 m_unsaved = unsaved;
173 }
174
175 void apply(bool apply);
176 /// Apply without emitting any signals
177 void applyQuietly(bool apply);
178 bool applied() const { return m_applied; }
179
180 void setType(int type) { m_type = type; }
181
182 void addSourceLine(QString line);
183 void addDestinationLine(QString line);
184
185 /** This method will calculate the differences between the individual strings and store them as Markers */
186 void determineInlineDifferences();
187
188 QString recreateDifference() const;
189
190Q_SIGNALS:
191 void differenceApplied(Difference*);
192
193private:
194 int m_type;
195
196 int m_sourceLineNo;
197 int m_destinationLineNo;
198 int m_trackingDestinationLineNo;
199
200 DifferenceStringList m_sourceLines;
201 DifferenceStringList m_destinationLines;
202
203 bool m_applied;
204 bool m_conflicts;
205 bool m_unsaved;
206};
207
208using DifferenceList = QList<Difference*>;
209using DifferenceListIterator = QList<Difference*>::iterator;
210using DifferenceListConstIterator = QList<Difference*>::const_iterator;
211
212} // End of namespace Diff2
213
214#endif
215
A difference string.
Definition difference.h:30
A difference.
Definition difference.h:124
int trackingDestinationLineNumber() const
Destination line number that tracks applying/unapplying of other differences Essentially a line numbe...
Definition difference.h:147
A Marker.
Definition marker.h:21
Type type(const QSqlDatabase &db)
Diff2 namespace.
KGuiItem apply()
void prepend(parameter_type value)
qsizetype length() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:10:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.