KompareDiff2

difference.cpp
1 /*
2 SPDX-FileCopyrightText: 2001-2004,2009 Otto Bruggeman <[email protected]>
3 SPDX-FileCopyrightText: 2001-2003 John Firebaugh <[email protected]>
4 
5 SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "difference.h"
9 #include "differencestringpair.h"
10 #include "levenshteintable.h"
11 
12 using namespace Diff2;
13 
14 Difference::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 
26 Difference::~Difference()
27 {
28  qDeleteAll(m_sourceLines);
29  qDeleteAll(m_destinationLines);
30 }
31 
32 void Difference::addSourceLine(QString line)
33 {
34  m_sourceLines.append(new DifferenceString(line));
35 }
36 
37 void Difference::addDestinationLine(QString line)
38 {
39  m_destinationLines.append(new DifferenceString(line));
40 }
41 
42 int Difference::sourceLineCount() const
43 {
44  return m_sourceLines.count();
45 }
46 
47 int Difference::destinationLineCount() const
48 {
49  return m_destinationLines.count();
50 }
51 
52 int Difference::sourceLineEnd() const
53 {
54  return m_sourceLineNo + m_sourceLines.count();
55 }
56 
57 int Difference::destinationLineEnd() const
58 {
59  return m_destinationLineNo + m_destinationLines.count();
60 }
61 
62 int Difference::trackingDestinationLineEnd() const
63 {
64  return m_trackingDestinationLineNo + m_destinationLines.count();
65 }
66 
67 void 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 
77 void Difference::applyQuietly(bool apply)
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 
113 QString 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 
Q_EMITQ_EMIT
Type type(const QSqlDatabase &db)
QVector::iterator begin()
Diff2 namespace.
Definition: cvsdiffparser.h:12
void determineInlineDifferences()
This method will calculate the differences between the individual strings and store them as Markers.
Definition: difference.cpp:86
void append(const T &value)
void applyQuietly(bool apply)
Apply without emitting any signals.
Definition: difference.cpp:77
QVector::iterator end()
A difference string.
Definition: difference.h:27
unsigned int createTable(SequencePair *sequences)
This calculates the levenshtein distance of 2 sequences.
int count(const T &value) const const
KGuiItem apply()
Computes the Levenshtein distance between two sequences.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Nov 27 2022 03:52:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.