KompareDiff2

difference.cpp
1/*
2 SPDX-FileCopyrightText: 2001-2004,2009 Otto Bruggeman <bruggie@gmail.com>
3 SPDX-FileCopyrightText: 2001-2003 John Firebaugh <jfirebaugh@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "difference.h"
9#include "difference_p.h"
10
11// lib
12#include "differencestringpair.h"
13#include "levenshteintable.h"
14
15using namespace KompareDiff2;
16
17Difference::Difference(int sourceLineNo, int destinationLineNo, int type)
18 : d_ptr(new DifferencePrivate(sourceLineNo, destinationLineNo, type))
19{
20}
21
22Difference::~Difference() = default;
23
24int Difference::type() const
25{
26 Q_D(const Difference);
27
28 return d->type;
29};
30
31int Difference::sourceLineNumber() const
32{
33 Q_D(const Difference);
34
35 return d->sourceLineNo;
36}
37
38int Difference::destinationLineNumber() const
39{
40 Q_D(const Difference);
41
42 return d->destinationLineNo;
43}
44
46{
47 Q_D(const Difference);
48
49 return d->trackingDestinationLineNo;
50}
51
52void Difference::setTrackingDestinationLineNumber(int i)
53{
55
56 d->trackingDestinationLineNo = i;
57}
58
59DifferenceString *Difference::sourceLineAt(int i) const
60{
61 Q_D(const Difference);
62
63 return d->sourceLines[i];
64}
65
66DifferenceString *Difference::destinationLineAt(int i) const
67{
68 Q_D(const Difference);
69
70 return d->destinationLines[i];
71}
72
73DifferenceStringList Difference::sourceLines() const
74{
75 Q_D(const Difference);
76
77 return d->sourceLines;
78}
79
80DifferenceStringList Difference::destinationLines() const
81{
82 Q_D(const Difference);
83
84 return d->destinationLines;
85}
86
87bool Difference::hasConflict() const
88{
89 Q_D(const Difference);
90
91 return d->conflicts;
92}
93
94void Difference::setConflict(bool conflicts)
95{
97
98 d->conflicts = conflicts;
99}
100
101bool Difference::isUnsaved() const
102{
103 Q_D(const Difference);
104
105 return d->unsaved;
106}
107
108void Difference::setUnsaved(bool unsaved)
109{
111
112 d->unsaved = unsaved;
113}
114
115bool Difference::applied() const
116{
117 Q_D(const Difference);
118
119 return d->applied;
120}
121
122void Difference::setType(int type)
123{
125
126 d->type = type;
127}
128
129void Difference::addSourceLine(const QString &line)
130{
132
133 d->sourceLines.append(new DifferenceString(line));
134}
135
136void Difference::addDestinationLine(const QString &line)
137{
139
140 d->destinationLines.append(new DifferenceString(line));
141}
142
143int Difference::sourceLineCount() const
144{
145 Q_D(const Difference);
146
147 return d->sourceLines.count();
148}
149
150int Difference::destinationLineCount() const
151{
152 Q_D(const Difference);
153
154 return d->destinationLines.count();
155}
156
157int Difference::sourceLineEnd() const
158{
159 Q_D(const Difference);
160
161 return d->sourceLineNo + d->sourceLines.count();
162}
163
164int Difference::destinationLineEnd() const
165{
166 Q_D(const Difference);
167
168 return d->destinationLineNo + d->destinationLines.count();
169}
170
171int Difference::trackingDestinationLineEnd() const
172{
173 Q_D(const Difference);
174
175 return d->trackingDestinationLineNo + d->destinationLines.count();
176}
177
178void Difference::apply(bool apply)
179{
181
182 if (apply == d->applied) {
183 return;
184 }
185
186 d->applied = apply;
187 d->unsaved = !d->unsaved;
188 Q_EMIT differenceApplied(this);
189}
190
192{
194
195 if (d->applied == apply) {
196 return;
197 }
198
199 d->unsaved = !d->unsaved;
200 d->applied = apply;
201}
202
204{
206
207 if (d->type != Difference::Change)
208 return;
209
210 // Do nothing for now when the slc != dlc
211 // One could try to find the closest matching destination string for any
212 // of the source strings but this is compute intensive
213 int slc = sourceLineCount();
214
215 if (slc != destinationLineCount())
216 return;
217
219
220 for (int i = 0; i < slc; ++i) {
221 DifferenceString *sl = sourceLineAt(i);
222 DifferenceString *dl = destinationLineAt(i);
223 DifferenceStringPair *pair = new DifferenceStringPair(sl, dl);
224
225 // return value 0 means something went wrong creating the table so don't bother finding markers
226 if (table.createTable(pair) != 0)
227 table.createListsOfMarkers();
228 }
229}
230
231QString Difference::recreateDifference() const
232{
233 Q_D(const Difference);
234
235 QString difference;
236
237 // source
238 for (const DifferenceString *diffString : d->sourceLines) {
239 switch (d->type) {
240 case Change:
241 case Delete:
242 difference += QLatin1Char('-');
243 break;
244 default:
245 // Insert but this is not possible in source
246 // Unchanged will be handled in destination
247 // since they are the same
248// qCDebug(KOMPAREDIFF2_LOG) << "Go away, nothing to do for you in source...";
249 continue;
250 }
251 difference += diffString->string();
252 }
253
254 // destination
255 for (const DifferenceString *diffString : d->destinationLines) {
256 switch (d->type) {
257 case Change:
258 case Insert:
259 difference += QLatin1Char('+');
260 break;
261 case Unchanged:
262 difference += QLatin1Char(' ');
263 break;
264 default: // Delete but this is not possible in destination
265// qCDebug(KOMPAREDIFF2_LOG) << "Go away, nothing to do for you in destination...";
266 continue;
267 }
268 difference += diffString->string();
269 }
270
271 return difference;
272}
273
274#include "moc_difference.cpp"
void determineInlineDifferences()
This method will calculate the differences between the individual strings and store them as Markers.
void applyQuietly(bool apply)
Apply without emitting any signals.
int trackingDestinationLineNumber() const
Destination line number that tracks applying/unapplying of other differences Essentially a line numbe...
Computes the Levenshtein distance between two sequences.
unsigned int createTable(SequencePair *sequences)
This calculates the levenshtein distance of 2 sequences.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
KGuiItem apply()
KompareDiff2 namespace.
Q_EMITQ_EMIT
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:54:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.