KompareDiff2

difference.cpp
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#include "difference.h"
9#include "differencestringpair.h"
10#include "levenshteintable.h"
11
12using namespace Diff2;
13
14Difference::Difference(int sourceLineNo, int destinationLineNo, int type) :
15 QObject(),
16 m_type(type),
17 m_sourceLineNo(sourceLineNo),
18 m_destinationLineNo(destinationLineNo),
19 m_trackingDestinationLineNo(sourceLineNo), // The whole patch starts as unapplied
20 m_applied(false),
21 m_conflicts(false),
22 m_unsaved(false)
23{
24}
25
26Difference::~Difference()
27{
28 qDeleteAll(m_sourceLines);
29 qDeleteAll(m_destinationLines);
30}
31
32void Difference::addSourceLine(QString line)
33{
34 m_sourceLines.append(new DifferenceString(line));
35}
36
37void Difference::addDestinationLine(QString line)
38{
39 m_destinationLines.append(new DifferenceString(line));
40}
41
42int Difference::sourceLineCount() const
43{
44 return m_sourceLines.count();
45}
46
47int Difference::destinationLineCount() const
48{
49 return m_destinationLines.count();
50}
51
52int Difference::sourceLineEnd() const
53{
54 return m_sourceLineNo + m_sourceLines.count();
55}
56
57int Difference::destinationLineEnd() const
58{
59 return m_destinationLineNo + m_destinationLines.count();
60}
61
62int Difference::trackingDestinationLineEnd() const
63{
64 return m_trackingDestinationLineNo + m_destinationLines.count();
65}
66
67void Difference::apply(bool apply)
68{
69 if (apply != m_applied)
70 {
71 m_applied = apply;
72 m_unsaved = !m_unsaved;
73 Q_EMIT differenceApplied(this);
74 }
75}
76
78{
79 if (m_applied != apply)
80 {
81 m_unsaved = !m_unsaved;
82 m_applied = apply;
83 }
84}
85
87{
88 if (m_type != Difference::Change)
89 return;
90
91 // Do nothing for now when the slc != dlc
92 // One could try to find the closest matching destination string for any
93 // of the source strings but this is compute intensive
94 int slc = sourceLineCount();
95
96 if (slc != destinationLineCount())
97 return;
98
100
101 for (int i = 0; i < slc; ++i)
102 {
103 DifferenceString* sl = sourceLineAt(i);
104 DifferenceString* dl = destinationLineAt(i);
105 DifferenceStringPair* pair = new DifferenceStringPair(sl, dl);
106
107 // return value 0 means something went wrong creating the table so don't bother finding markers
108 if (table.createTable(pair) != 0)
109 table.createListsOfMarkers();
110 }
111}
112
113QString Difference::recreateDifference() const
114{
115 QString difference;
116
117 // source
118 DifferenceStringListConstIterator stringIt = m_sourceLines.begin();
119 DifferenceStringListConstIterator sEnd = m_sourceLines.end();
120
121 for (; stringIt != sEnd; ++stringIt)
122 {
123 switch (m_type)
124 {
125 case Change:
126 case Delete:
127 difference += QLatin1Char('-');
128 break;
129 default:
130 // Insert but this is not possible in source
131 // Unchanged will be handled in destination
132 // since they are the same
133// qCDebug(LIBKOMPAREDIFF2) << "Go away, nothing to do for you in source...";
134 continue;
135 }
136 difference += (*stringIt)->string();
137 }
138
139 //destination
140 stringIt = m_destinationLines.begin();
141 sEnd = m_destinationLines.end();
142
143 for (; stringIt != sEnd; ++stringIt)
144 {
145 switch (m_type)
146 {
147 case Change:
148 case Insert:
149 difference += QLatin1Char('+');
150 break;
151 case Unchanged:
152 difference += QLatin1Char(' ');
153 break;
154 default: // Delete but this is not possible in destination
155// qCDebug(LIBKOMPAREDIFF2) << "Go away, nothing to do for you in destination...";
156 continue;
157 }
158 difference += (*stringIt)->string();
159 }
160
161 return difference;
162}
163
164#include "moc_difference.cpp"
A difference string.
Definition difference.h:30
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.
Computes the Levenshtein distance between two sequences.
unsigned int createTable(SequencePair *sequences)
This calculates the levenshtein distance of 2 sequences.
Type type(const QSqlDatabase &db)
Diff2 namespace.
KGuiItem apply()
Q_EMITQ_EMIT
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.