Quark  0.1
DiffModel.h++
Go to the documentation of this file.
1 #ifndef __libquark_diff_DiffModel_hxx
2 #define __libquark_diff_DiffModel_hxx
3 
4 #include <QObject>
5 #include <QScopedPointer>
6 #include <QTextStream>
7 #include <QVector>
8 
9 #include <quark/DiffChunk.h++>
10 #include <quark/DiffResult.h++>
11 #include <quark/DiffTypes.h++>
12 #include <quark/MutableObject.h++>
13 
14 namespace quark {
15 namespace diff {
16 
24  Q_OBJECT;
25 
26  public:
27 
28  DiffModel();
29  ~DiffModel();
30 
31  // Perform a 2-way diff. Takes as input the "base" (old) text and the "yours"
32  // (new) text. Emits the dataChanged() signal once the diff has been
33  // computed.
34  void diff2(const QString& baseText, const QString& yoursText);
35 
36  // Perform a 3-way diff. Takes as input the "base", "theirs", and "yours"
37  // texts. Emits the dataChanged() signal once the diff has been
38  // computed.
39  void diff3(const QString& theirsText, const QString& baseText,
40  const QString& yoursText);
41 
42  // Set the conflict marker that introduces the "theirs" elements. For line-
43  // based diffs, this string should include a trailing newline.
44  inline void setTheirsConflictMarker(const QString& marker)
45  { _theirsConflictMarker = marker; }
46 
47  // Set the conflict marker that introduces the "base" elements. For line-
48  // based diffs, this string should include a trailing newline.
49  inline void setBaseConflictMarker(const QString& marker)
50  { _baseConflictMarker = marker; }
51 
52  // Set the conflict marker that introduces the "yours" elements. For line-
53  // based diffs, this string should include a trailing newline.
54  inline void setYoursConflictMarker(const QString& marker)
55  { _yoursConflictMarker = marker; }
56 
57  // Set the conflict end marker. For line-based diffs, this string should
58  // include a trailing newline.
59  inline void setEndConflictMarker(const QString& marker)
60  { _endConflictMarker = marker; }
61 
62  // Return the sequence length for the given role.
63  int sequenceLength(DiffRole role) const;
64 
65  // Test if this is a two-way diff.
66  bool isTwoWay() const;
67 
68  // Test if this is a three-way diff.
69  bool isThreeWay() const;
70 
71  // Test if the model is empty.
72  bool isEmpty() const;
73 
74  // Clear the model.
75  void clear();
76 
77  const QVector<DiffChunk*>* diffChunks() const;
78 
79  // Return a pointer to the DiffChunk that contains the given line, or
80  // NULL if there is no such chunk. line is 1-based.
81  const DiffChunk* chunkForIndex(DiffRole role, int index) const;
82 
83  // Output a merge to the supplied stream. If the current model represents
84  // a 2-way diff, this simply outputs the "yours" (new) file. If it is a
85  // 3-way diff, it performs a 3-way merge, inserting conflict markers when
86  // appropriate.
87  //
88  // If 'counts' is not null, it is updated to reflect the number of diff
89  // edits of each type that were contributed to the resulting merge.
90  bool outputMerge(QTextStream& stream, DiffCounts* counts) const;
91 
92  // Output an 'ed' script (like "diff -e"), that describes the edits from
93  // the source role to the target role, to the given stream.
94  void outputEdScript(DiffRole source, DiffRole destination,
95  QTextStream& stream) const;
96 
97  inline void setRecursiveDiff(bool recursiveDiff)
98  { _recursiveDiff = recursiveDiff; }
99 
100  inline void setAggressiveMerge(bool aggressiveMerge)
101  { _aggressiveMerge = aggressiveMerge; }
102 
103  // Dump a textual representation of the model to the debug log (for
104  // debugging purposes).
105  void dump(DiffDirection diffDir) const;
106 
107  private:
108 
109  void dump(DiffDirection diffDir, const DiffResult* diffResult,
110  int level) const;
111  void diff2(DiffResult* diffResult);
112  void diff3(DiffResult* diffResult);
113 
114  // Writes the element 'range' from 'data' to the given 'stream'.
115  void outputRange(const DiffRange& range, const DiffSequence& sequence,
116  QTextStream& stream) const;
117 
118  QScopedPointer<DiffResult> _diffResult;
119  QString _theirsText;
120  QString _baseText;
121  QString _yoursText;
122  QString _theirsConflictMarker;
123  QString _baseConflictMarker;
124  QString _yoursConflictMarker;
125  QString _endConflictMarker;
126  bool _recursiveDiff;
127  bool _aggressiveMerge;
128 
129  Q_DISABLE_COPY(DiffModel);
130 };
131 
132 } // namespace diff
133 } // namespace quark
134 
135 #endif // __libquark_diff_DiffModel_hxx
void setEndConflictMarker(const QString &marker)
Definition: DiffModel.h++:59
TODO: rewrite comment A range of elements in a DiffSequence.
Definition: DiffRange.h++:17
A base class for objects that can emit a signal when their internal state changes in some way...
Definition: MutableObject.h++:17
int sequenceLength(DiffRole role) const
void outputEdScript(DiffRole source, DiffRole destination, QTextStream &stream) const
bool outputMerge(QTextStream &stream, DiffCounts *counts) const
void dump(DiffDirection diffDir) const
Definition: DiffTypes.h++:32
const DiffChunk * chunkForIndex(DiffRole role, int index) const
void setBaseConflictMarker(const QString &marker)
Definition: DiffModel.h++:49
void diff2(const QString &baseText, const QString &yoursText)
Definition: BarChartView.h++:6
TODO: rewrite comment Abstract base class for sequences that can be diffed.
Definition: DiffSequence.h++:20
void setRecursiveDiff(bool recursiveDiff)
Definition: DiffModel.h++:97
void diff3(const QString &theirsText, const QString &baseText, const QString &yoursText)
TODO: rewrite doc A data model representing a 2-way or 3-way diff.
Definition: DiffModel.h++:23
DiffDirection
Definition: DiffTypes.h++:29
bool isThreeWay() const
DiffRole
Definition: DiffTypes.h++:11
void setTheirsConflictMarker(const QString &marker)
Definition: DiffModel.h++:44
const QVector< DiffChunk * > * diffChunks() const
void setYoursConflictMarker(const QString &marker)
Definition: DiffModel.h++:54
void setAggressiveMerge(bool aggressiveMerge)
Definition: DiffModel.h++:100
TODO: rewrite comment A DiffChunk maps a DiffRange in one DiffSequence (the "base") to a correspondin...
Definition: DiffChunk.h++:46
Definition: DiffResult.h++:13