5
5
#include < QMainWindow>
6
6
#include < QSplitter>
7
7
#include < QFileInfo>
8
+ #include < QPainter>
8
9
9
10
#include < string>
10
11
#include < iostream>
@@ -103,6 +104,126 @@ bool fileExists(QString path) {
103
104
}
104
105
105
106
107
+
108
+ class DiffSplitterHandle : public QSplitterHandle
109
+ {
110
+ public:
111
+ DiffSplitterHandle (edbee::TextEditorWidget* leftEditor, edbee::TextEditorWidget* rightEditor, QSplitter *parent = 0 ) : QSplitterHandle(Qt::Orientation::Horizontal, parent)
112
+ {
113
+ _leftEditorWidget = leftEditor;
114
+ _rightEditorWidget = rightEditor;
115
+ // _diffLookup = QVector<QVector<stringdiff::Diff>>(0);
116
+ }
117
+ void setDiffLookup (QVector<QVector<stringdiff::Diff>> diffLookup);
118
+
119
+ protected:
120
+ void DiffSplitterHandle::paintEvent (QPaintEvent *event) override ;
121
+ edbee::TextRenderer* DiffSplitterHandle::renderer () const ;
122
+ int DiffSplitterHandle::getLineStatus (int line);
123
+
124
+
125
+ private:
126
+ QVector<QVector<stringdiff::Diff>> _diffLookup;
127
+ edbee::TextEditorWidget* _leftEditorWidget;
128
+ edbee::TextEditorWidget* _rightEditorWidget;
129
+
130
+ };
131
+
132
+ void DiffSplitterHandle::setDiffLookup (QVector<QVector<stringdiff::Diff>> diffLookup)
133
+ {
134
+ _diffLookup = diffLookup;
135
+ }
136
+
137
+ edbee::TextRenderer* DiffSplitterHandle::renderer () const
138
+ {
139
+ return _rightEditorWidget->textRenderer ();
140
+ }
141
+
142
+ int DiffSplitterHandle::getLineStatus (int lineIndex) {
143
+
144
+ if (_diffLookup.isEmpty ()) return 0 ;
145
+ QVector<stringdiff::Diff> diffs = _diffLookup.at (lineIndex);
146
+
147
+ // TODO: this would be cool
148
+ // if any_of(diffs.cbegin(), diffs.cend(), [](<diff_match_patch<string>::Diff* diff){ return })
149
+ bool inserted = false ;
150
+ bool deleted = false ;
151
+ for (int i = 0 ; i < diffs.count (); ++i) {
152
+ stringdiff::Diff diff = diffs.at (i);
153
+
154
+ // qDebug() << "line" << lineIndex << "diff" << i << "op" << diff.operation;// << "txt";// << diff->text;
155
+
156
+ if (diff.operation == stringdiff::DELETE) {
157
+ deleted = true ;
158
+ }
159
+ if (diff.operation == stringdiff::INSERT) {
160
+ inserted = true ;
161
+ }
162
+
163
+ }
164
+ // qDebug() << "line" << lineIndex << " deleted and inserted are" << deleted << inserted;
165
+ if (deleted && inserted) return 3 ;
166
+ if (inserted) return 2 ;
167
+ if (deleted) return 1 ;
168
+ return 0 ;
169
+
170
+ }
171
+
172
+ void DiffSplitterHandle::paintEvent (QPaintEvent *event)
173
+ {
174
+ QPainter painter (this );
175
+ int lineHeight = renderer ()->lineHeight ();
176
+ /* QBrush brush = QBrush(QColor(0, 0, 0), Qt::BrushStyle::SolidPattern);*/
177
+ painter.fillRect (event->rect (), QBrush ());
178
+ const QSize& size = this ->size ();
179
+ QRect paintRect = event->rect ();
180
+ // QRect translatedRect( clipRect.x()+offsetX, clipRect.y()+offsetY, clipRect.width(), clipRect.height() );
181
+ // paintRect.adjust(0, top_, 0, top_);
182
+ renderer ()->renderBegin (paintRect);
183
+ int startLine = renderer ()->startLine ();
184
+ int endLine = renderer ()->endLine ();
185
+ QColor baseColor = renderer ()->theme ()->backgroundColor ();
186
+
187
+ QColor changedColor = baseColor.darker (120 );
188
+
189
+ for (int line = startLine; line <= endLine; ++line) {
190
+
191
+ int changeType = getLineStatus (line);
192
+
193
+ if (changeType > 0 ) {
194
+ painter.fillRect (0 , line*lineHeight, size.width (), lineHeight, changedColor);
195
+ }
196
+ }
197
+ renderer ()->renderEnd (paintRect);
198
+
199
+
200
+ }
201
+
202
+
203
+ class DiffSplitter : public QSplitter
204
+ {
205
+ public:
206
+ DiffSplitter (edbee::TextEditorWidget* leftEditor, edbee::TextEditorWidget* rightEditor, QWidget *parent = 0 ) : QSplitter(Qt::Orientation::Horizontal, parent)
207
+ {
208
+ _leftEditorWidget = leftEditor;
209
+ _rightEditorWidget = rightEditor;
210
+ }
211
+
212
+ protected:
213
+ QSplitterHandle *createHandle ();
214
+
215
+ private:
216
+ edbee::TextEditorWidget* _leftEditorWidget;
217
+ edbee::TextEditorWidget* _rightEditorWidget;
218
+ };
219
+
220
+ QSplitterHandle *DiffSplitter::createHandle ()
221
+ {
222
+ return new DiffSplitterHandle (_leftEditorWidget, _rightEditorWidget, this );
223
+ }
224
+
225
+
226
+
106
227
int main (int argc, char *argv[])
107
228
{
108
229
QApplication a (argc, argv);
@@ -187,7 +308,9 @@ int main(int argc, char *argv[])
187
308
188
309
189
310
190
- QSplitter *splitter = new QSplitter ();
311
+ DiffSplitter *splitter = new DiffSplitter (&left, &right);
312
+
313
+ splitter->setHandleWidth (50 );
191
314
splitter->addWidget (&left);
192
315
splitter->addWidget (&right);
193
316
@@ -199,6 +322,9 @@ int main(int argc, char *argv[])
199
322
win.setWindowTitle (QString::fromStdString (rightFile));
200
323
win.show ();
201
324
325
+ DiffSplitterHandle* handle = (DiffSplitterHandle*)splitter->handle (1 );
326
+ handle->setDiffLookup (deletionLookup);
327
+
202
328
// scroll to first change
203
329
int leftOffset = deletionLookup[0 ][0 ].text .length ();
204
330
left.controller ()->scrollOffsetVisible (leftOffset);
0 commit comments