[go: up one dir, main page]

File: listview.cpp

package info (click to toggle)
qgit 2.12-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,364 kB
  • sloc: cpp: 13,251; xml: 47; sh: 25; javascript: 16; makefile: 3
file content (1350 lines) | stat: -rw-r--r-- 36,525 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
/*
	Description: qgit revision list view

	Author: Marco Costalba (C) 2005-2007

	Copyright: See COPYING file that comes with this distribution

*/
#include <QApplication>
#include <QHeaderView>
#include <QMimeData>
#include <QMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QShortcut>
#include <QDrag>
#include <QUrl>
#include <QSettings>
#include "FileHistory.h"
#include "domain.h"
#include "mainimpl.h"
#include "git.h"
#include "listview.h"

void getTagMarkParams(QString &name, QStyleOptionViewItem& o,
                      const int type, const bool isCurrent);
uint refTypeFromName(SCRef name);

using namespace QGit;

ListView::ListView(QWidget* parent) : QTreeView(parent), d(NULL), git(NULL), fh(NULL), lp(NULL), dropInfo(NULL) {}

void ListView::setup(Domain* dm, Git* g) {

	d = dm;
	git = g;
	fh = d->model();
	st = &(d->st);
	filterNextContextMenuRequest = false;

	setFont(QGit::STD_FONT);

	// create ListViewProxy unplugged, will be plug
	// to the model only when filtering is needed
	lp = new ListViewProxy(this, d, git);
	setModel(fh);

	ListViewDelegate* lvd = new ListViewDelegate(git, lp, this);
        lvd->setLaneHeight(fontMetrics().height() + 2);
	setItemDelegate(lvd);

	setupGeometry(); // after setting delegate

	// shortcuts are activated only if widget is visible, this is good
	new QShortcut(Qt::Key_Up,   this, SLOT(on_keyUp()));
	new QShortcut(Qt::Key_Down, this, SLOT(on_keyDown()));

	connect(lvd, SIGNAL(updateView()), viewport(), SLOT(update()));

	connect(this, SIGNAL(diffTargetChanged(int)), lvd, SLOT(diffTargetChanged(int)));

	connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
	        this, SLOT(on_customContextMenuRequested(const QPoint&)));
}

ListView::~ListView() {

	git->cancelDataLoading(fh); // non blocking

	const QString settingsKey = git->isMainHistory(fh) ? QGit::REV_COLS_KEY : QGit::FILE_COLS_KEY;
	QSettings settings;
	settings.setValue(settingsKey, header()->saveState());
}

const QString ListView::sha(int row) const {

	if (!lp->sourceModel()) // unplugged
		return fh->sha(row);

	QModelIndex idx = lp->mapToSource(lp->index(row, 0));
	return fh->sha(idx.row());
}

int ListView::row(SCRef sha) const {

	if (!lp->sourceModel()) // unplugged
		return fh->row(sha);

	int row = fh->row(sha);
	QModelIndex idx = fh->index(row, 0);
	return lp->mapFromSource(idx).row();
}

void ListView::setupGeometry() {

	QHeaderView* hv = header();
	hv->setStretchLastSection(true);
#if QT_VERSION >= 0x050000
	hv->setSectionResizeMode(LOG_COL, QHeaderView::Interactive);
	hv->setSectionResizeMode(TIME_COL, QHeaderView::Interactive);
	hv->setSectionResizeMode(ANN_ID_COL, QHeaderView::ResizeToContents);
#else
	hv->setResizeMode(LOG_COL, QHeaderView::Interactive);
	hv->setResizeMode(TIME_COL, QHeaderView::Interactive);
	hv->setResizeMode(ANN_ID_COL, QHeaderView::ResizeToContents);
#endif
	hv->resizeSection(GRAPH_COL, DEF_GRAPH_COL_WIDTH);
	hv->resizeSection(LOG_COL, DEF_LOG_COL_WIDTH);
	hv->resizeSection(HASH_COL, DEF_HASH_COL_WIDTH);
	hv->resizeSection(AUTH_COL, DEF_AUTH_COL_WIDTH);
	hv->resizeSection(TIME_COL, DEF_TIME_COL_WIDTH);

	if (git->isMainHistory(fh))
		hideColumn(ANN_ID_COL);

	const QString settingsKey = git->isMainHistory(fh) ? QGit::REV_COLS_KEY : QGit::FILE_COLS_KEY;
	QSettings settings;
	QVariant v = settings.value(settingsKey);
	if (v.isValid()) hv->restoreState(v.toByteArray());
}

void ListView::scrollToNextHighlighted(int direction) {

	// Depending on the value of direction, scroll to:
	// -1 = the next highlighted item above the current one (i.e. newer in history)
	//  1 = the next highlighted item below the current one (i.e. older in history)
	//  0 = the first highlighted item from the top of the list

	QModelIndex idx = currentIndex();

	if (!direction) {
		idx = idx.sibling(0,0);
		if (lp->isHighlighted(idx.row())) {
			setCurrentIndex(idx);
			return;
		}
	}

	do {
		idx = (direction >= 0 ? indexBelow(idx) : indexAbove(idx));
		if (!idx.isValid())
			return;

	} while (!lp->isHighlighted(idx.row()));

	setCurrentIndex(idx);
}

void ListView::scrollToNext(int direction) {
	// Depending on the value of direction, scroll to:
	// -1 = the next child in history
	//  1 = the previous parent in history
	SCRef s = sha(currentIndex().row());
	const Rev* r = git->revLookup(s);
	if (!r) return;
	const QStringList& next = direction < 0 ? git->getChildren(s) : r->parents();
	if (next.size() >= 1)
		setCurrentIndex(model()->index(row(next.first()), 0));
}

void ListView::scrollToCurrent(ScrollHint hint) {

	if (currentIndex().isValid())
		scrollTo(currentIndex(), hint);
}

void ListView::on_keyUp() {

	QModelIndex idx = indexAbove(currentIndex());
	if (idx.isValid())
		setCurrentIndex(idx);
}

void ListView::on_keyDown() {

	QModelIndex idx = indexBelow(currentIndex());
	if (idx.isValid())
		setCurrentIndex(idx);
}

void ListView::on_changeFont(const QFont& f) {

	setFont(f);
	ListViewDelegate* lvd = static_cast<ListViewDelegate*>(itemDelegate());
	lvd->setLaneHeight(fontMetrics().height());
	scrollToCurrent();
}

const QString ListView::currentText(int column) {

	QModelIndex idx = model()->index(currentIndex().row(), column);
	return (idx.isValid() ? idx.data().toString() : "");
}

int ListView::getLaneType(SCRef sha, int pos) const {

	const Rev* r = git->revLookup(sha, fh);
	return (r && pos < r->lanes.count() && pos >= 0 ? r->lanes.at(pos) : -1);
}

void ListView::showIdValues() {

	fh->setAnnIdValid();
	viewport()->update();
}

void ListView::getSelectedItems(QStringList& selectedItems) {

	selectedItems.clear();
	QModelIndexList ml = selectionModel()->selectedRows();
	FOREACH (QModelIndexList, it, ml)
		selectedItems.append(sha((*it).row()));

	// selectedRows() returns the items in an unspecified order,
	// so be sure rows are ordered from newest to oldest.
	selectedItems = git->sortShaListByIndex(selectedItems);
}

const QString ListView::shaFromAnnId(int id) {

	if (git->isMainHistory(fh))
		return "";

	return sha(model()->rowCount() - id);
}

int ListView::filterRows(bool isOn, bool highlight, SCRef filter, int colNum, ShaSet* set) {

	setUpdatesEnabled(false);
	int matchedNum = lp->setFilter(isOn, highlight, filter, colNum, set);
	viewport()->update();
	setUpdatesEnabled(true);
	UPDATE_DOMAIN(d);
	return matchedNum;
}

bool ListView::update() {

	int stRow = row(st->sha());
	if (stRow == -1)
		return false; // main/tree view asked us a sha not in history

	QModelIndex index = currentIndex();
	QItemSelectionModel* sel = selectionModel();

	if (index.isValid() && (index.row() == stRow)) {

		if (sel->isSelected(index) != st->selectItem())
			sel->select(index, QItemSelectionModel::Toggle);

		scrollTo(index);
	} else {
		// setCurrentIndex() does not clear previous
		// selections in a multi selection QListView
		clearSelection();

		QModelIndex newIndex = model()->index(stRow, 0);
		if (newIndex.isValid()) {

			// emits QItemSelectionModel::currentChanged()
			setCurrentIndex(newIndex);
			scrollTo(newIndex);
			if (!st->selectItem())
				sel->select(newIndex, QItemSelectionModel::Deselect);
		}
	}
	if (git->isMainHistory(fh))
		emit diffTargetChanged(row(st->diffToSha()));

	return currentIndex().isValid();
}

void ListView::currentChanged(const QModelIndex& index, const QModelIndex&) {

	SCRef selRev = sha(index.row());
	if (st->sha() != selRev) { // to avoid looping
		st->setSha(selRev);
		st->setSelectItem(true);
		UPDATE_DOMAIN(d);
	}
}

void ListView::markDiffToSha(SCRef sha) {
	if (sha != st->diffToSha()) {
		st->setDiffToSha(sha);
		emit showStatusMessage("Marked " +  sha + " for diff. (Ctrl-RightClick)");
	} else {
		st->setDiffToSha(""); // restore std view
		emit showStatusMessage("Unmarked diff reference.");
	}
	UPDATE_DOMAIN(d);
}

bool ListView::filterRightButtonPressed(QMouseEvent* e) {

	QModelIndex index = indexAt(e->pos());
	SCRef selSha = sha(index.row());
	if (selSha.isEmpty())
		return false;

	if (e->modifiers() == Qt::ControlModifier) { // check for 'diff to' function
		if (selSha != ZERO_SHA) {

			filterNextContextMenuRequest = true;
			markDiffToSha(selSha);
			return true; // filter event out
		}
	}
	// check for 'children & parents' function, i.e. if mouse is on the graph
	if (index.column() == GRAPH_COL) {

		filterNextContextMenuRequest = true;
		QStringList parents, children;
                if (getLaneParentsChildren(selSha, e->pos().x(), parents, children))
			emit lanesContextMenuRequested(parents, children);

		return true; // filter event out
	}
	return false;
}

void ListView::mousePressEvent(QMouseEvent* e) {
	lastRefName = refNameAt(e->pos());

	if (e->button() == Qt::RightButton && filterRightButtonPressed(e))
		return; // filtered out

	QTreeView::mousePressEvent(e);
}

void ListView::mouseReleaseEvent(QMouseEvent* e) {

	lastRefName = ""; // reset
	QTreeView::mouseReleaseEvent(e);
}


QPixmap ListView::pixmapFromSelection(const QStringList &revs, const QString &ref) const {
#if QT_VERSION >= 0x060000
	const qsizetype maxRows = 10;
#else
	const int maxRows = 10;
#endif
	const int dotdotRow = 5;
	QStyleOptionViewItem opt; opt.initFrom(this);
//	ListViewDelegate *lvd = dynamic_cast<ListViewDelegate*>(itemDelegate());

	QFontMetrics fm(opt.font);
	int height = fm.height()+2;
	int row = 0, rows = std::min(revs.count() + (ref.isEmpty() ? 0 : 1), maxRows);
	int spacing = 4;
	QPixmap pixmap (columnWidth(LOG_COL), rows*height);
	pixmap.fill(Qt::transparent);

	QPainter painter(&pixmap);
	// render selected ref name
	if (!ref.isEmpty()) {
		QStyleOptionViewItem o(opt);
		QString dummy;
		getTagMarkParams(dummy, o, refTypeFromName(ref), false);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
		painter.fillRect(0, 0, fm.horizontalAdvance(ref)+2*spacing, height, o.palette.window());
#else
		painter.fillRect(0, 0, fm.width(ref)+2*spacing, height, o.palette.window());
#endif
		painter.drawText(spacing, fm.ascent()+1, ref);
		row = 1;
	}

	painter.fillRect(0, row*height, pixmap.width(), (rows-row)*height, opt.palette.window());
	for (QStringList::const_iterator it = revs.begin(), end = revs.end(); it != end; ++it) {
		const Rev* r = git->revLookup(it->section(" ", 0, 0));
		if (!r) continue; // should not happen
		painter.drawText(spacing, row*height + fm.ascent()+1, r->shortLog()); ++row;
		// jump to last dotdotRows-1 items if necessary
		if (rows-row == dotdotRow && end-it > dotdotRow+1) {
			++row; // leave one line empty
			it += end-it - dotdotRow;
		}
	}
	painter.end();
	return pixmap;
}

void ListView::startDragging(QMouseEvent* /*e*/) {

	QStringList selRevs;
	getSelectedItems(selRevs);
	selRevs.removeAll(ZERO_SHA);

	QDrag* drag = new QDrag(this);
	QMimeData* mimeData = new QMimeData;
	if (!selRevs.empty()) {
		Qt::DropActions actions = Qt::CopyAction; // patch
		Qt::DropAction default_action = Qt::CopyAction;

		// compose mime data
		bool contiguous = git->isContiguous(selRevs);

		// standard text for range description
		if (contiguous) {
			QString text;
			if (selRevs.size() > 1) text += selRevs.back() + "..";
			text += lastRefName.isEmpty() ? selRevs.front() : lastRefName;
			mimeData->setText(text);
		}

		// revision range for patch/merge/rebase operations
		QString mime = QString("%1@%2\n").arg(contiguous ? "RANGE" : "LIST").arg(d->m()->currentDir());
		if (contiguous && !lastRefName.isEmpty())
			selRevs.front() += " " + lastRefName; // append ref name
		mime.append(selRevs.join("\n"));
		mimeData->setData("application/x-qgit-revs", mime.toUtf8());

		drag->setMimeData(mimeData);
		drag->setPixmap(pixmapFromSelection(selRevs, lastRefName));

		if (contiguous) { // rebase enabled
			actions |= Qt::MoveAction; // rebase (local branch) or push (remote branch)
		}
		// merging (of several shas) is also enabled
		actions |= Qt::LinkAction;

		drag->exec(actions, default_action);
	}
}

void ListView::mouseMoveEvent(QMouseEvent* e) {

	if (e->buttons() == Qt::LeftButton && QGit::testFlag(QGit::ENABLE_DRAGNDROP_F)) {
		startDragging(e);
		return;
	}

	QTreeView::mouseMoveEvent(e);
}

struct ListView::DropInfo {
	DropInfo (uint f) : flags(f) {}

	enum Flags {
		PATCHES  = 1 << 0,
		REV_LIST = 1 << 1,
		REV_RANGE = 1 << 2,
		SAME_REPO     = 1 << 3
	};
	enum Action {
		PatchAction = Qt::CopyAction,
		RebaseAction = Qt::MoveAction,
		MoveRefAction = (Qt::LinkAction << 1) | Qt::MoveAction,
		MergeAction = Qt::LinkAction
	};

	QString sourceRepo;
	QString sourceRef;
	uint    sourceRefType;
	Action  action;
	uint flags;
	QStringList shas;

	static QString actionName (ListView::DropInfo::Action a) {
		switch (a) {
		case PatchAction: return "patching";
		case RebaseAction: return "rebasing";
		case MoveRefAction: return "moving";
		case MergeAction: return "merging";
		default: return "This should not happen.";
		}
	}
};

uint refTypeFromName(SCRef name) {
	if (name.startsWith("tags/")) return Git::TAG;
	if (name.startsWith("remotes/")) return Git::RMT_BRANCH;
	if (!name.isEmpty()) return Git::BRANCH;
	return 0;
}

void ListView::dragEnterEvent(QDragEnterEvent* e) {

	if (dropInfo) delete dropInfo;
	dropInfo = NULL;
	bool bCleanWorkDir = git->isNothingToCommit();

	// accept local file urls for patching
	bool valid=true;
	const QList<QUrl>& urls = e->mimeData()->urls();
	for(QList<QUrl>::const_iterator it=urls.begin(), end=urls.end();
	    valid && it!=end; ++it)
		valid &= it->isLocalFile();
	if (urls.size() > 0 && valid && bCleanWorkDir) {
		dropInfo = new DropInfo(DropInfo::PATCHES);
		e->accept();
	}

	// parse internal mime format
	if (!e->mimeData()->hasFormat("application/x-qgit-revs"))
		return;

	dropInfo = new DropInfo(DropInfo::REV_LIST);

	SCRef revsText(e->mimeData()->data("application/x-qgit-revs"));
	QString header = revsText.section("\n", 0, 0);
	dropInfo->shas = revsText.section("\n", 1).split('\n', QGIT_SPLITBEHAVIOR(SkipEmptyParts));
	// extract refname and sha from first entry again
	dropInfo->sourceRef = dropInfo->shas.front().section(" ", 1);
	dropInfo->shas.front() = dropInfo->shas.front().section(" ", 0, 0);
	dropInfo->sourceRefType = refTypeFromName(dropInfo->sourceRef);

	// check source repo
	dropInfo->sourceRepo = header.section("@", 1);
	if (dropInfo->sourceRepo != d->m()->currentDir()) {
		if (!QDir().exists(dropInfo->sourceRepo)) {
			emit showStatusMessage("Remote repository missing: " + dropInfo->sourceRepo, 10000);
			e->ignore();
			return;
		}
	} else
		dropInfo->flags |= DropInfo::SAME_REPO;


	if (!bCleanWorkDir && // dirty work dir doesn't allow merging/rebasing
	    // only exception is moving ref names within same repo
	    !(dropInfo->sourceRefType != 0 && dropInfo->shas.count() == 1 && (dropInfo->flags & DropInfo::SAME_REPO))) {
		emit showStatusMessage("Drag-n-drop rejected: First clean your working dir!", 10000);
		e->ignore();
		return;
	}
	e->accept();

	if (header.startsWith("RANGE")) dropInfo->flags |= DropInfo::REV_RANGE;
}

void ListView::dragMoveEvent(QDragMoveEvent* e) {
	// When getting here, dragEnterEvent already accepted the drag in general

	SCRef targetRef = refNameAt(e->
#if QT_VERSION >= 0x060000
		position().toPoint()
#else
		pos()
#endif
	);
	uint targetRefType = refTypeFromName(targetRef);
	QModelIndex idx = indexAt(e->
#if QT_VERSION >= 0x060000
		position().toPoint()
#else
		pos()
#endif
	);
	SCRef targetSHA = sha(idx.row());
	uint   accepted_actions = DropInfo::PatchAction; // applying patches is always allowed
	DropInfo::Action  action, default_action = DropInfo::PatchAction;

	// extended drop actions (merging, rebasing, pushing) are only allowed
	if (dropInfo->flags & DropInfo::SAME_REPO && // on same repo
	    dropInfo->flags & DropInfo::REV_LIST && // qgit drags
	    idx.column() == LOG_COL) { // and when dropping onto LOG_COL
		// only accept drop when target has different sha than source shas
		if (dropInfo->shas.contains(targetSHA)) {
			e->ignore();
			emit showStatusMessage("Cannot drop onto current selection.");
			return;
		}
		// decide the preferred drop action based on context and

		// rebasing is allowed onto any sha (and workdir),
		// but only if source sha list is contiguous range
		if (dropInfo->flags & DropInfo::REV_RANGE) {
			accepted_actions |= DropInfo::RebaseAction;
			default_action = DropInfo::RebaseAction;
		}

		// merging is allowed onto any local branch
		if (targetRefType == Git::BRANCH) {
			accepted_actions |= DropInfo::MergeAction;
			default_action = DropInfo::MergeAction;
		}

		// pushing is allowed when sha list has 1 item and sourceRef is remote branch
		if (dropInfo->shas.count() == 1 &&
		    dropInfo->sourceRefType != 0 && targetSHA != ZERO_SHA) {
			accepted_actions |= DropInfo::MoveRefAction;
			default_action = DropInfo::MoveRefAction;
		}
	} else if (e->source() == this && idx.row() == currentIndex().row()) {
		// move at least by one line before enabling drag
		e->ignore();
		showStatusMessage("");
		return;
	}

	e->accept();
	// check whether modifier keys enforce an action
#if QT_VERSION >= 0x060000
	switch (e->modifiers()) {
#else
	switch (e->keyboardModifiers()) {
#endif
	case Qt::ControlModifier: action = DropInfo::PatchAction; break;
	case Qt::ShiftModifier: action = DropInfo::RebaseAction; break;
	case Qt::AltModifier: action = DropInfo::MergeAction; break;
	default: action = default_action; break;
	}
	QString statusMsg;
	if ((action & accepted_actions) == 0) {
		statusMsg = DropInfo::actionName(action) + " not allowed. ";
		statusMsg[0] = statusMsg[0].toUpper();
		action = default_action;
	}

	// inform user about the to-be-performed action in statusBar
	switch (action) {
	case DropInfo::PatchAction:
		statusMsg += "Applying patches";
		break;
	case DropInfo::RebaseAction:
		statusMsg += QString("Rebasing %1 onto %2")
		        .arg((dropInfo->sourceRefType == Git::BRANCH &&
		              dropInfo->shas.count() == 1) ? dropInfo->sourceRef : "selection")
		        .arg(targetRefType == Git::BRANCH ? targetRef : targetSHA);
		break;
	case DropInfo::MoveRefAction:
		statusMsg += "Moving " + dropInfo->sourceRef;
		break;
	case DropInfo::MergeAction:
		statusMsg += "Merging selected branches into " + targetRef;
		break;
	}
	emit showStatusMessage(statusMsg);
	dropInfo->action = action;
	e->setDropAction(static_cast<Qt::DropAction>(action & 0x7));
}

void ListView::dragLeaveEvent(QDragLeaveEvent* /*e*/)
{
    if (dropInfo) delete dropInfo;
    dropInfo = NULL;
	showStatusMessage("");
}

void ListView::dropEvent(QDropEvent *e) {

	if (dropInfo->flags & DropInfo::PATCHES) {
		QStringList files;
		QList<QUrl> urls = e->mimeData()->urls();
		FOREACH(QList<QUrl>, it, urls)
		    files << it->toLocalFile();

		emit applyPatches(files);
		return;
	}

	SCRef targetRef = refNameAt(e->
#if QT_VERSION >= 0x060000
		position().toPoint()
#else
		pos()
#endif
	);
//	uint  targetRefType = refTypeFromName(targetRef);
	SCRef targetSHA = sha(indexAt(e->
#if QT_VERSION >= 0x060000
		position().toPoint()
#else
		pos()
#endif
	).row());
	switch(dropInfo->action) {
	case DropInfo::PatchAction:
		emit applyRevisions(dropInfo->shas, dropInfo->sourceRepo);
		break;
	case DropInfo::RebaseAction:
		if (dropInfo->sourceRefType == Git::BRANCH && dropInfo->shas.count() == 1)
			emit rebase("", dropInfo->sourceRef, targetSHA);
		else
			emit rebase(dropInfo->shas.last(),
		                dropInfo->sourceRef.isEmpty() ? dropInfo->shas.first()
		                                              : dropInfo->sourceRef,
		                targetSHA);
		break;
	case DropInfo::MergeAction:
		emit merge(dropInfo->shas, targetRef);
		break;
	case DropInfo::MoveRefAction:
		emit moveRef(dropInfo->sourceRef, targetSHA);
		break;
	}
}

void ListView::on_customContextMenuRequested(const QPoint& pos) {

	QModelIndex index = indexAt(pos);
	if (!index.isValid())
		return;

	if (filterNextContextMenuRequest) {
		// event filter does not work on them
		filterNextContextMenuRequest = false;
		return;
	}
	emit contextMenu(sha(index.row()), POPUP_LIST_EV);
}

bool ListView::getLaneParentsChildren(SCRef sha, int x, SList p, SList c) {

	ListViewDelegate* lvd = static_cast<ListViewDelegate*>(itemDelegate());
	uint lane = x / lvd->laneWidth();
	int t = getLaneType(sha, lane);
	if (t == EMPTY || t == -1)
		return false;

	// first find the parents
	p.clear();
	QString root;
	if (!isFreeLane(t)) {
		p = git->revLookup(sha, fh)->parents(); // pointer cannot be NULL
		root = sha;
	} else {
		SCRef par(git->getLaneParent(sha, lane));
		if (par.isEmpty()) {
                        dbs("ASSERT getLaneParentsChildren: parent not found");
			return false;
		}
		p.append(par);
		root = p.first();
	}
	// then find children
        c = git->getChildren(root);
	return true;
}

/** Iterator over all refnames of a given sha.
 *  References are traversed in following order:
 *  detached (name empty), local branches, remote branches, tags, other refs
 */
class RefNameIterator {
	Git* git;
	const QString sha;
	uint ref_types; // all reference types associated with sha
	int  cur_state; // state indicating the currently processed ref type
	QStringList ref_names; // ref_names of current type
	QStringList::const_iterator cur_name;
	QString cur_branch;

public:
	RefNameIterator(const QString &sha, Git* git);
	bool valid() const {return cur_state != -1;}
	QString name() const {return *cur_name;}
	int type() {return cur_state;}
	bool isCurrentBranch() {return *cur_name == cur_branch;}

	void next();
};

RefNameIterator::RefNameIterator(const QString &sha, Git *git)
    : git(git), sha(sha), cur_state(0), cur_branch(git->getCurrentBranchName())
{
	ref_types = git->checkRef(sha);
	if (ref_types == 0) {
		cur_state = -1; // indicates end
		return;
	}

	// initialize dummy string list
	ref_names << "";
	cur_name = ref_names.begin();

	// detached ?
	if ((ref_types & Git::CUR_BRANCH) && cur_branch.isEmpty()) {
		// indicate detached state with type() == 0 and empty ref name
		cur_branch = *cur_name;
	} else { // advance to first real ref name
		next();
	}
}

void RefNameIterator::next()
{
	++cur_name;

	// switch to next ref type if required
	while (valid() && cur_name == ref_names.end()) {
		switch (cur_state) {
		case 0: cur_state = Git::BRANCH; break;
		case Git::BRANCH: cur_state = Git::RMT_BRANCH; break;
		case Git::RMT_BRANCH: cur_state = Git::TAG; break;
		case Git::TAG: cur_state = Git::REF; break;
		default: cur_state = -1; // indicate end
		}
		ref_names = git->getRefNames(sha, (Git::RefType)cur_state);
		cur_name = ref_names.begin();
	}
}

// *****************************************************************************

ListViewDelegate::ListViewDelegate(Git* g, ListViewProxy* px, QObject* p) : QItemDelegate(p) {

	git = g;
	lp = px;
	laneHeight = 0;
	diffTargetRow = -1;
}

QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const {

	return QSize(laneWidth(), laneHeight);
}

void ListViewDelegate::diffTargetChanged(int row) {

	if (diffTargetRow != row) {
		diffTargetRow = row;
		emit updateView();
	}
}

const Rev* ListViewDelegate::revLookup(int row, FileHistory** fhPtr) const {

	ListView* lv = static_cast<ListView*>(parent());
	FileHistory* fh = static_cast<FileHistory*>(lv->model());

	if (lp->sourceModel())
		fh = static_cast<FileHistory*>(lp->sourceModel());

	if (fhPtr)
		*fhPtr = fh;

	return git->revLookup(lv->sha(row), fh);
}

static QColor blend(const QColor& col1, const QColor& col2, int amount = 128) {

	// Returns ((256 - amount)*col1 + amount*col2) / 256;
	return QColor(((256 - amount)*col1.red()   + amount*col2.red()  ) / 256,
	              ((256 - amount)*col1.green() + amount*col2.green()) / 256,
	              ((256 - amount)*col1.blue()  + amount*col2.blue() ) / 256);
}

void ListViewDelegate::paintGraphLane(QPainter* p, int type, int x1, int x2,
                                      const QColor& col, const QColor& activeCol, const QBrush& back) const {

        const int padding = 2;
        x1 += padding;
        x2 += padding;

	int h = laneHeight / 2;
	int m = (x1 + x2) / 2;
        int r = (x2 - x1) * 1 / 3;
	int d =  2 * r;

	#define P_CENTER m , h
	#define P_0      x2, h      // >
	#define P_90     m , 0      // ^
	#define P_180    x1, h      // <
	#define P_270    m , 2 * h  // v
	#define DELTA_UR 2*(x1 - m), 2*h ,   0*16, 90*16  // -,
	#define DELTA_DR 2*(x1 - m), 2*-h, 270*16, 90*16  // -'
	#define DELTA_UL 2*(x2 - m), 2*h ,  90*16, 90*16  //  ,-
	#define DELTA_DL 2*(x2 - m), 2*-h, 180*16, 90*16  //  '-
	#define CENTER_UR x1, 2*h, 225
	#define CENTER_DR x1, 0  , 135
	#define CENTER_UL x2, 2*h, 315
	#define CENTER_DL x2, 0  ,  45
	#define R_CENTER m - r, h - r, d, d

	static QPen lanePen(QPalette().color(QPalette::WindowText), 2); // fast path here

	// arc
	switch (type) {
	case JOIN:
	case JOIN_R:
	case HEAD:
	case HEAD_R: {
		QConicalGradient gradient(CENTER_UR);
		gradient.setColorAt(0.375, col);
		gradient.setColorAt(0.625, activeCol);
                lanePen.setBrush(gradient);
                p->setPen(lanePen);
		p->drawArc(P_CENTER, DELTA_UR);
		break;
	}
	case JOIN_L: {
		QConicalGradient gradient(CENTER_UL);
		gradient.setColorAt(0.375, activeCol);
		gradient.setColorAt(0.625, col);
                lanePen.setBrush(gradient);
                p->setPen(lanePen);
		p->drawArc(P_CENTER, DELTA_UL);
		break;
	}
	case TAIL:
	case TAIL_R: {
		QConicalGradient gradient(CENTER_DR);
		gradient.setColorAt(0.375, activeCol);
		gradient.setColorAt(0.625, col);
                lanePen.setBrush(gradient);
                p->setPen(lanePen);
		p->drawArc(P_CENTER, DELTA_DR);
		break;
	}
	default:
		break;
	}

        lanePen.setColor(col);
        p->setPen(lanePen);

	// vertical line
	switch (type) {
	case ACTIVE:
	case NOT_ACTIVE:
	case MERGE_FORK:
	case MERGE_FORK_R:
	case MERGE_FORK_L:
	case JOIN:
	case JOIN_R:
	case JOIN_L:
	case CROSS:
		p->drawLine(P_90, P_270);
		break;
	case HEAD_L:
	case BRANCH:
		p->drawLine(P_CENTER, P_270);
		break;
	case TAIL_L:
	case INITIAL:
	case BOUNDARY:
	case BOUNDARY_C:
	case BOUNDARY_R:
	case BOUNDARY_L:
		p->drawLine(P_90, P_CENTER);
		break;
	default:
		break;
	}

        lanePen.setColor(activeCol);
        p->setPen(lanePen);

	// horizontal line
	switch (type) {
	case MERGE_FORK:
	case JOIN:
	case HEAD:
	case TAIL:
	case CROSS:
	case CROSS_EMPTY:
	case BOUNDARY_C:
		p->drawLine(P_180, P_0);
		break;
	case MERGE_FORK_R:
	case BOUNDARY_R:
		p->drawLine(P_180, P_CENTER);
		break;
	case MERGE_FORK_L:
	case HEAD_L:
	case TAIL_L:
	case BOUNDARY_L:
		p->drawLine(P_CENTER, P_0);
		break;
	default:
		break;
	}

	// center symbol, e.g. rect or ellipse
	switch (type) {
	case ACTIVE:
	case INITIAL:
	case BRANCH:
		p->setPen(Qt::black);
		p->setBrush(col);
		p->drawEllipse(R_CENTER);
		break;
	case MERGE_FORK:
	case MERGE_FORK_R:
	case MERGE_FORK_L:
		p->setPen(Qt::black);
		p->setBrush(col);
		p->drawRect(R_CENTER);
		break;
	case UNAPPLIED:
		// Red minus sign
		p->setPen(Qt::NoPen);
		p->setBrush(Qt::red);
		p->drawRect(m - r, h - 1, d, 2);
		break;
	case APPLIED:
		// Green plus sign
		p->setPen(Qt::NoPen);
		p->setBrush(DARK_GREEN);
		p->drawRect(m - r, h - 1, d, 2);
		p->drawRect(m - 1, h - r, 2, d);
		break;
	case BOUNDARY:
		p->setPen(Qt::black);
		p->setBrush(back);
		p->drawEllipse(R_CENTER);
		break;
	case BOUNDARY_C:
	case BOUNDARY_R:
	case BOUNDARY_L:
		p->setPen(Qt::black);
		p->setBrush(back);
		p->drawRect(R_CENTER);
		break;
	default:
		break;
	}
	#undef P_CENTER
	#undef P_0
	#undef P_90
	#undef P_180
	#undef P_270
	#undef DELTA_UR
	#undef DELTA_DR
	#undef DELTA_UL
	#undef DELTA_DL
	#undef CENTER_UR
	#undef CENTER_DR
	#undef CENTER_UL
	#undef CENTER_DL
	#undef R_CENTER
}

void ListViewDelegate::paintGraph(QPainter* p, const QStyleOptionViewItem& opt,
                                  const QModelIndex& i) const {
	// static const QColor & baseColor = QPalette().color(QPalette::WindowText);
	static const QColor colors[COLORS_NUM] = {
		QPalette().color(QPalette::WindowText),
		Qt::red, DARK_GREEN,
		Qt::blue, Qt::darkGray, BROWN,
		Qt::magenta, ORANGE
	};
	if (opt.state & QStyle::State_Selected)
		p->fillRect(opt.rect, opt.palette.highlight());
	else if (i.row() & 1)
		p->fillRect(opt.rect, opt.palette.alternateBase());
	else
		p->fillRect(opt.rect, opt.palette.base());

	FileHistory* fh;
	const Rev* r = revLookup(i.row(), &fh);
	if (!r)
		return;

	p->save();
	p->setClipRect(opt.rect, Qt::IntersectClip);
	p->translate(opt.rect.topLeft());

	// calculate lanes
	if (r->lanes.count() == 0)
		git->setLane(r->sha(), fh);

	QBrush back = opt.palette.base();
	const QVector<int>& lanes(r->lanes);
	uint laneNum = lanes.count();
	uint activeLane = 0;
	for (uint i = 0; i < laneNum; i++)
		if (isActive(lanes[i])) {
			activeLane = i;
			break;
		}

	int x1 = 0, x2 = 0;
	int maxWidth = opt.rect.width();
	int lw = laneWidth();
	QColor activeColor = colors[activeLane % COLORS_NUM];
	if (opt.state & QStyle::State_Selected)
		activeColor = blend(activeColor, opt.palette.highlightedText().color(), 208);
	for (uint i = 0; i < laneNum && x2 < maxWidth; i++) {

		x1 = x2;
		x2 += lw;

		int ln = lanes[i];
		if (ln == EMPTY)
			continue;

		QColor color = i == activeLane ? activeColor : colors[i % COLORS_NUM];
		paintGraphLane(p, ln, x1, x2, color, activeColor, back);
	}
	p->restore();
}

void ListViewDelegate::paintLog(QPainter* p, const QStyleOptionViewItem& opt,
                                const QModelIndex& index) const {

	int row = index.row();
	const Rev* r = revLookup(row);
	if (!r)
		return;

	if (r->isDiffCache)
		p->fillRect(opt.rect, changedFiles(ZERO_SHA) ? ORANGE : DARK_ORANGE);

	if (diffTargetRow == row)
		p->fillRect(opt.rect, LIGHT_BLUE);

	bool isHighlighted = lp->isHighlighted(row);
	QPixmap* pm = getTagMarks(r->sha(), opt);

	if (!pm && !isHighlighted) { // fast path in common case
		QItemDelegate::paint(p, opt, index);
		return;
	}
	QStyleOptionViewItem newOpt(opt); // we need a copy
	if (pm) {
		p->drawPixmap(newOpt.rect.x(), newOpt.rect.y() + 1, *pm); // +1 means leave a pixel spacing above the pixmap
		newOpt.rect.adjust(static_cast<int>(pm->width() / dpr()), 0, 0, 0);
		delete pm;
	}
	if (isHighlighted)
                newOpt.font.setBold(true);

	QItemDelegate::paint(p, newOpt, index);
}

void ListViewDelegate::paint(QPainter* p, const QStyleOptionViewItem& opt,
                             const QModelIndex& index) const {

  p->setRenderHints(QPainter::Antialiasing);

	if (index.column() == GRAPH_COL)
		return paintGraph(p, opt, index);

	if (index.column() == LOG_COL)
		return paintLog(p, opt, index);

	return QItemDelegate::paint(p, opt, index);
}

bool ListViewDelegate::changedFiles(SCRef sha) const {

	const RevFile* f = git->getFiles(sha);
	if (f)
		for (int i = 0; i < f->count(); i++)
			if (!f->statusCmp(i, RevFile::UNKNOWN))
				return true;
	return false;
}

// adapt style and name based on type
void getTagMarkParams(QString &name, QStyleOptionViewItem& o,
                      const int type, const bool isCurrent) {
	QColor clr;

	switch (type) {
	case 0: name = "detached"; clr = Qt::red; break;
	case Git::BRANCH: clr = isCurrent ? Qt::green : DARK_GREEN; break;
	case Git::RMT_BRANCH: clr = LIGHT_ORANGE; break;
	case Git::TAG: clr = Qt::yellow; break;
	case Git::REF: clr = PURPLE; break;
	}

	o.palette.setColor(QPalette::Window, clr);
	o.palette.setColor(QPalette::WindowText, QColor(Qt::black));
	o.font.setBold(isCurrent);
}

QPixmap* ListViewDelegate::getTagMarks(SCRef sha, const QStyleOptionViewItem& opt) const {

	uint rt = git->checkRef(sha);
	if (rt == 0)
		return NULL; // common case: no refs at all

	QPixmap* pm = new QPixmap(); // must be deleted by caller

	for (RefNameIterator it(sha, git); it.valid(); it.next()) {
		QStyleOptionViewItem o(opt);
		QString name = it.name();
		getTagMarkParams(name, o, it.type(), it.isCurrentBranch());
		addTextPixmap(&pm, name, o);
	}

	return pm;
}

QString ListView::refNameAt(const QPoint &pos)
{
	QModelIndex index = indexAt(pos);
	if (index.column() != LOG_COL) return QString();

	int spacing = 4; // inner spacing within pixmaps (cf. addTextPixmap)
	int ofs = visualRect(index).left();
	for (RefNameIterator it(sha(index.row()), git); it.valid(); it.next()) {
		QStyleOptionViewItem o;
		QString name = it.name();
		getTagMarkParams(name, o, it.type(), it.isCurrentBranch());

		QFontMetrics fm(o.font);
		ofs += fm.boundingRect(name).width() + 2*spacing;
		if (pos.x() <= ofs) {
			// name found: return fully-qualified ref name (cf. Git::getRefs() for names)
			switch (it.type()) {
			   case Git::BRANCH: return it.name(); break;
			   case Git::TAG: return "tags/" + it.name(); break;
			   case Git::RMT_BRANCH: return "remotes/" + it.name(); break;
			   case Git::REF: return "bases/" + it.name(); break;
			   default: return QString(); break;
			}
		}
		ofs += 2; // distance between pixmaps (cf. addTextPixmap)
	}
	return QString();
}

/*
 * Return the device pixel ratio
 */
qreal ListViewDelegate::dpr(void) const {
#if QT_VERSION >= QT_VERSION_CHECK(5,6,0)
    return qApp->devicePixelRatio();
#else
    return 1.0;
#endif
}

void ListViewDelegate::addTextPixmap(QPixmap** pp, SCRef txt, const QStyleOptionViewItem& opt) const {

	QPixmap* pm = *pp;

	const unsigned int mark_spacing = 2; // Space between markers in pixels
	unsigned int offset = pm->isNull() ? 0 : static_cast<unsigned int>(pm->width() / dpr()) + mark_spacing; // Marker's offset in the base pixmap

	QFontMetrics fm(opt.font);
	const unsigned int text_spacing = 4;
	unsigned int text_width = fm.boundingRect(txt).width() + 2 * text_spacing;
	unsigned int text_height = fm.height();

	// Define size of the new Pixmap
	QSize pixmapSize(static_cast<int>((offset + text_width) * dpr()),
			 static_cast<int>(text_height * dpr()));

	QPixmap* newPm = new QPixmap(pixmapSize);
#if QT_VERSION >= QT_VERSION_CHECK(5,6,0)
    newPm->setDevicePixelRatio(dpr());
#endif

	QPainter p;
	p.begin(newPm);

	if (!pm->isNull()) {
		newPm->fill(opt.palette.base().color());
		p.drawPixmap(0, 0, *pm);
	}

	p.setPen(opt.palette.color(QPalette::WindowText));
	p.setBrush(opt.palette.color(QPalette::Window));
	p.setFont(opt.font);

	p.drawRect(offset, 0, text_width - 1, text_height - 1);
	p.drawText(offset + text_spacing, fm.ascent(), txt);
	p.end();

	delete pm;
	*pp = newPm;
}

// *****************************************************************************

ListViewProxy::ListViewProxy(QObject* p, Domain * dm, Git * g) : QSortFilterProxyModel(p) {

	d = dm;
	git = g;
	colNum = 0;
	isHighLight = false;
	setDynamicSortFilter(false);
}

bool ListViewProxy::isMatch(SCRef sha) const {

	if (colNum == SHA_MAP_COL)
		// in this case shaMap contains all good sha to search for
		return shaSet.contains(sha);

	const Rev* r = git->revLookup(sha);
	if (!r) {
		dbp("ASSERT in ListViewFilter::isMatch, sha <%1> not found", sha);
		return false;
	}
	QString target;
	if (colNum == LOG_COL)
		target = r->shortLog();
	else if (colNum == AUTH_COL)
		target = r->author();
	else if (colNum == LOG_MSG_COL)
		target = r->longLog();
	else if (colNum == COMMIT_COL)
		target = sha;

	// wildcard search, case insensitive
	return (target.contains(filter));
}

bool ListViewProxy::isMatch(int source_row) const {

	FileHistory* fh = d->model();
	if (fh->rowCount() <= source_row) // FIXME required to avoid an ASSERT in d->isMatch()
		return false;

	bool extFilter = (colNum == -1);
	return ((!extFilter && isMatch(fh->sha(source_row)))
	      ||( extFilter && d->isMatch(fh->sha(source_row))));
}

bool ListViewProxy::isHighlighted(int row) const {

	// FIXME row == source_row only because when
	// higlights the rows are not hidden
	return (isHighLight && isMatch(row));
}

bool ListViewProxy::filterAcceptsRow(int source_row, const QModelIndex&) const {

	return (isHighLight || isMatch(source_row));
}

int ListViewProxy::setFilter(bool isOn, bool h, SCRef fl, int cn, ShaSet* s) {

#if QT_VERSION >= 0x060000
	filter = QRegularExpression::fromWildcard(fl, Qt::CaseInsensitive);
#else
	filter = QRegExp(fl, Qt::CaseInsensitive, QRegExp::Wildcard);
#endif
	colNum = cn;
	if (s)
		shaSet = *s;

	// isHighlighted() is called also when filter is off,
	// so reset 'isHighLight' flag in that case
	isHighLight = h && isOn;

	ListView* lv = static_cast<ListView*>(parent());
	FileHistory* fh = d->model();
	SCRef cur = lv->sha(lv->currentIndex().row());

	if (!isOn && sourceModel()){
		lv->setModel(fh);
		setSourceModel(NULL);

	} else if (isOn && !isHighLight) {
		setSourceModel(fh); // trigger a rows scanning
		lv->setModel(this);
	}
	lv->setCurrentIndex(lv->model()->index(lv->row(cur), 0));
	return (sourceModel() ? rowCount() : 0);
}