-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmainimpl.cpp
More file actions
1888 lines (1525 loc) · 51.6 KB
/
mainimpl.cpp
File metadata and controls
1888 lines (1525 loc) · 51.6 KB
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
/*
Description: qgit main view
Author: Marco Costalba (C) 2005-2007
Copyright: See COPYING file that comes with this distribution
*/
#include <QCloseEvent>
#include <QDrag>
#include <QEvent>
#include <QFileDialog>
#include <QInputDialog>
#include <QMenu>
#include <QMessageBox>
#include <QProgressBar>
#include <QScrollBar>
#include <QSettings>
#include <QShortcut>
#include <QStatusBar>
#include <QTimer>
#include <QWheelEvent>
#include "config.h" // defines PACKAGE_VERSION
#include "consoleimpl.h"
#include "commitimpl.h"
#include "common.h"
#include "customactionimpl.h"
#include "fileview.h"
#include "git.h"
#include "help.h"
#include "listview.h"
#include "mainimpl.h"
#include "patchview.h"
#include "rangeselectimpl.h"
#include "revdesc.h"
#include "revsview.h"
#include "settingsimpl.h"
#include "treeview.h"
#include "ui_help.h"
#include "ui_revsview.h"
#include "ui_fileview.h"
#include "ui_patchview.h"
using namespace QGit;
MainImpl::MainImpl(SCRef cd, QWidget* p) : QMainWindow(p) {
EM_INIT(exExiting, "Exiting");
setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);
// manual setup widgets not buildable with Qt designer
lineEditSHA = new QLineEdit(NULL);
lineEditFilter = new QLineEdit(NULL);
cmbSearch = new QComboBox(NULL);
QString list("Short log,Log msg,Author,SHA1,File,Patch,Patch (regExp)");
cmbSearch->addItems(list.split(","));
toolBar->addWidget(lineEditSHA);
QAction* act = toolBar->insertWidget(ActSearchAndFilter, lineEditFilter);
toolBar->insertWidget(act, cmbSearch);
connect(lineEditSHA, SIGNAL(returnPressed()), this, SLOT(lineEditSHA_returnPressed()));
connect(lineEditFilter, SIGNAL(returnPressed()), this, SLOT(lineEditFilter_returnPressed()));
// create light and dark colors for alternate background
ODD_LINE_COL = palette().color(QPalette::Base);
EVEN_LINE_COL = ODD_LINE_COL.dark(103);
// our interface to git world
git = new Git(this);
setupShortcuts();
qApp->installEventFilter(this);
// init native types
setRepositoryBusy = false;
// init filter match highlighters
shortLogRE.setMinimal(true);
shortLogRE.setCaseSensitivity(Qt::CaseInsensitive);
longLogRE.setMinimal(true);
longLogRE.setCaseSensitivity(Qt::CaseInsensitive);
// set-up standard revisions and files list font
QSettings settings;
QString font(settings.value(STD_FNT_KEY).toString());
if (font.isEmpty())
font = QApplication::font().toString();
QGit::STD_FONT.fromString(font);
// set-up typewriter (fixed width) font
font = settings.value(TYPWRT_FNT_KEY).toString();
if (font.isEmpty()) { // choose a sensible default
QFont fnt = QApplication::font();
fnt.setStyleHint(QFont::TypeWriter, QFont::PreferDefault);
fnt.setFixedPitch(true);
fnt.setFamily(fnt.defaultFamily()); // the family corresponding
font = fnt.toString(); // to current style hint
}
QGit::TYPE_WRITER_FONT.fromString(font);
// set-up tab view
delete tabWdg->currentWidget(); // cannot be done in Qt Designer
rv = new RevsView(this, git, true); // set has main domain
tabWdg->addTab(rv->tabPage(), "&Rev list");
// set-up tab corner widget ('close tab' button)
QToolButton* ct = new QToolButton(tabWdg);
ct->setIcon(QIcon(QString::fromUtf8(":/icons/resources/tab_remove.png")));
ct->setToolTip("Close tab");
ct->setEnabled(false);
tabWdg->setCornerWidget(ct);
connect(ct, SIGNAL(clicked()), this, SLOT(pushButtonCloseTab_clicked()));
connect(this, SIGNAL(closeTabButtonEnabled(bool)), ct, SLOT(setEnabled(bool)));
// set-up file names loading progress bar
pbFileNamesLoading = new QProgressBar(statusBar());
pbFileNamesLoading->setTextVisible(false);
pbFileNamesLoading->setToolTip("Background file names loading");
pbFileNamesLoading->hide();
statusBar()->addPermanentWidget(pbFileNamesLoading);
QVector<QSplitter*> v(1, treeSplitter);
QGit::restoreGeometrySetting(QGit::MAIN_GEOM_KEY, this, &v);
treeView->hide();
// set-up menu for recent visited repositories
connect(File, SIGNAL(triggered(QAction*)), this, SLOT(openRecent_triggered(QAction*)));
doUpdateRecentRepoMenu("");
// set-up menu for custom actions
connect(Actions, SIGNAL(triggered(QAction*)), this, SLOT(customAction_triggered(QAction*)));
doUpdateCustomActionMenu(settings.value(ACT_LIST_KEY).toStringList());
// manual adjust lineEditSHA width
QString tmp(41, '8');
int wd = lineEditSHA->fontMetrics().boundingRect(tmp).width();
lineEditSHA->setMinimumWidth(wd);
// disable all actions
updateGlobalActions(false);
connect(git, SIGNAL(fileNamesLoad(int, int)), this, SLOT(fileNamesLoad(int, int)));
connect(git, SIGNAL(newRevsAdded(const FileHistory*, const QVector<ShaString>&)),
this, SLOT(newRevsAdded(const FileHistory*, const QVector<ShaString>&)));
connect(this, SIGNAL(typeWriterFontChanged()), this, SIGNAL(updateRevDesc()));
connect(this, SIGNAL(changeFont(const QFont&)), git, SIGNAL(changeFont(const QFont&)));
// connect cross-domain update signals
connect(rv->tab()->listViewLog, SIGNAL(doubleClicked(const QModelIndex&)),
this, SLOT(listViewLog_doubleClicked(const QModelIndex&)));
connect(rv->tab()->fileList, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(fileList_itemDoubleClicked(QListWidgetItem*)));
connect(treeView, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),
this, SLOT(treeView_doubleClicked(QTreeWidgetItem*, int)));
// use most recent repo as startup dir if it exists and user opted to do so
QStringList recents(settings.value(REC_REP_KEY).toStringList());
QDir checkRepo;
if ( recents.size() >= 1
&& testFlag(REOPEN_REPO_F, FLAGS_KEY)
&& checkRepo.exists(recents.at(0)))
{
startUpDir = recents.at(0);
}
else {
startUpDir = (cd.isEmpty() ? QDir::current().absolutePath() : cd);
}
// MainImpl c'tor is called before to enter event loop,
// but some stuff requires event loop to init properly
QTimer::singleShot(10, this, SLOT(initWithEventLoopActive()));
}
void MainImpl::initWithEventLoopActive() {
git->checkEnvironment();
setRepository(startUpDir);
startUpDir = ""; // one shot
}
void MainImpl::saveCurrentGeometry() {
QVector<QSplitter*> v(1, treeSplitter);
QGit::saveGeometrySetting(QGit::MAIN_GEOM_KEY, this, &v);
}
void MainImpl::highlightAbbrevSha(SCRef abbrevSha) {
// reset any previous highlight
if (ActSearchAndHighlight->isChecked())
ActSearchAndHighlight->toggle();
// set to highlight on SHA matching
cmbSearch->setCurrentIndex(CS_SHA1);
// set substring to search for
lineEditFilter->setText(abbrevSha);
// go with highlighting
ActSearchAndHighlight->toggle();
}
void MainImpl::lineEditSHA_returnPressed() {
QString sha = git->getRefSha(lineEditSHA->text());
if (!sha.isEmpty()) // good, we can resolve to an unique sha
{
rv->st.setSha(sha);
UPDATE_DOMAIN(rv);
} else { // try a multiple match search
highlightAbbrevSha(lineEditSHA->text());
goMatch(0);
}
}
void MainImpl::ActBack_activated() {
lineEditSHA->undo(); // first for insert(text)
if (lineEditSHA->text().isEmpty())
lineEditSHA->undo(); // double undo, see RevsView::updateLineEditSHA()
lineEditSHA_returnPressed();
}
void MainImpl::ActForward_activated() {
lineEditSHA->redo();
if (lineEditSHA->text().isEmpty())
lineEditSHA->redo();
lineEditSHA_returnPressed();
}
// *************************** ExternalDiffViewer ***************************
void MainImpl::ActExternalDiff_activated() {
QStringList args;
QStringList filenames;
getExternalDiffArgs(&args, &filenames);
ExternalDiffProc* externalDiff = new ExternalDiffProc(filenames, this);
externalDiff->setWorkingDirectory(curDir);
if (!QGit::startProcess(externalDiff, args)) {
QString text("Cannot start external viewer: ");
text.append(args[0]);
QMessageBox::warning(this, "Error - QGit", text);
delete externalDiff;
}
}
void MainImpl::getExternalDiffArgs(QStringList* args, QStringList* filenames) {
// save files to diff in working directory,
// will be removed by ExternalDiffProc on exit
QFileInfo f(rv->st.fileName());
QString prevRevSha(rv->st.diffToSha());
if (prevRevSha.isEmpty()) { // default to first parent
const Rev* r = git->revLookup(rv->st.sha());
prevRevSha = (r && r->parentsCount() > 0 ? r->parent(0) : rv->st.sha());
}
QFileInfo fi(f);
QString fName1(curDir + "/" + rv->st.sha().left(6) + "_" + fi.fileName());
QString fName2(curDir + "/" + prevRevSha.left(6) + "_" + fi.fileName());
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
QByteArray fileContent;
QString fileSha(git->getFileSha(rv->st.fileName(), rv->st.sha()));
git->getFile(fileSha, NULL, &fileContent, rv->st.fileName());
if (!writeToFile(fName1, QString(fileContent)))
statusBar()->showMessage("Unable to save " + fName1);
fileSha = git->getFileSha(rv->st.fileName(), prevRevSha);
git->getFile(fileSha, NULL, &fileContent, rv->st.fileName());
if (!writeToFile(fName2, QString(fileContent)))
statusBar()->showMessage("Unable to save " + fName2);
// get external diff viewer command
QSettings settings;
QString extDiff(settings.value(EXT_DIFF_KEY, EXT_DIFF_DEF).toString());
QApplication::restoreOverrideCursor();
// if command doesn't have %1 and %2 to denote filenames, add them to end
if (!extDiff.contains("%1")) {
extDiff.append(" %1");
}
if (!extDiff.contains("%2")) {
extDiff.append(" %2");
}
// set process arguments
QStringList extDiffArgs = extDiff.split(' ');
QString curArg;
for (int i = 0; i < extDiffArgs.count(); i++) {
curArg = extDiffArgs.value(i);
// perform any filename replacements that are necessary
// (done inside the loop to handle whitespace in paths properly)
curArg.replace("%1", fName2);
curArg.replace("%2", fName1);
args->append(curArg);
}
// set filenames so that they can be deleted when the process completes
filenames->append(fName1);
filenames->append(fName2);
}
// ********************** Repository open or changed *************************
void MainImpl::setRepository(SCRef newDir, bool refresh, bool keepSelection,
const QStringList* passedArgs, bool overwriteArgs) {
/*
Because Git::init calls processEvents(), if setRepository() is called in
a tight loop (as example keeping pressed F5 refresh button) then a lot
of pending init() calls would be stacked.
On returning from processEvents() an exception is trown and init is exited,
so we end up with a long list of 'exception thrown' messages.
But the worst thing is that we have to wait for _all_ the init call to exit
and this could take a long time as example in case of working directory refreshing
'git update-index' of a big tree.
So we use a guard flag to guarantee we have only one init() call 'in flight'
*/
if (setRepositoryBusy)
return;
setRepositoryBusy = true;
// check for a refresh or open of a new repository while in filtered view
if (ActFilterTree->isChecked() && passedArgs == NULL)
// toggle() triggers a refresh and a following setRepository()
// call that is filtered out by setRepositoryBusy guard flag
ActFilterTree->toggle(); // triggers ActFilterTree_toggled()
try {
EM_REGISTER(exExiting);
bool archiveChanged;
git->getBaseDir(newDir, curDir, archiveChanged);
git->stop(archiveChanged); // stop all pending processes, non blocking
if (archiveChanged && refresh)
dbs("ASSERT in setRepository: different dir with no range select");
// now we can clear all our data
setWindowTitle(curDir + " - QGit");
bool complete = !refresh || !keepSelection;
rv->clear(complete);
if (archiveChanged)
emit closeAllTabs();
// disable all actions
updateGlobalActions(false);
updateContextActions("", "", false, false);
ActCommit_setEnabled(false);
if (ActFilterTree->isChecked())
setWindowTitle(windowTitle() + " - FILTER ON < " +
passedArgs->join(" ") + " >");
// tree name should be set before init because in case of
// StGIT archives the first revs are sent before init returns
QString n(curDir);
treeView->setTreeName(n.prepend('/').section('/', -1, -1));
bool quit;
bool ok = git->init(curDir, !refresh, passedArgs, overwriteArgs, &quit); // blocking call
if (quit)
goto exit;
updateCommitMenu(ok && git->isStGITStack());
ActCheckWorkDir->setChecked(testFlag(DIFF_INDEX_F)); // could be changed in Git::init()
if (ok) {
updateGlobalActions(true);
if (archiveChanged)
updateRecentRepoMenu(curDir);
} else
statusBar()->showMessage("Not a git archive");
exit:
setRepositoryBusy = false;
EM_REMOVE(exExiting);
if (quit && !startUpDir.isEmpty())
close();
} catch (int i) {
EM_REMOVE(exExiting);
if (EM_MATCH(i, exExiting, "loading repository")) {
EM_THROW_PENDING;
return;
}
const QString info("Exception \'" + EM_DESC(i) + "\' not "
"handled in setRepository...re-throw");
dbs(info);
throw;
}
}
void MainImpl::updateGlobalActions(bool b) {
ActRefresh->setEnabled(b);
ActCheckWorkDir->setEnabled(b);
ActViewRev->setEnabled(b);
ActViewDiff->setEnabled(b);
ActViewDiffNewTab->setEnabled(b && firstTab<PatchView>());
ActShowTree->setEnabled(b);
ActMailApplyPatch->setEnabled(b);
ActMailFormatPatch->setEnabled(b);
rv->setEnabled(b);
}
void MainImpl::updateContextActions(SCRef newRevSha, SCRef newFileName,
bool isDir, bool found) {
bool pathActionsEnabled = !newFileName.isEmpty();
bool fileActionsEnabled = (pathActionsEnabled && !isDir);
ActViewFile->setEnabled(fileActionsEnabled);
ActViewFileNewTab->setEnabled(fileActionsEnabled && firstTab<FileView>());
ActExternalDiff->setEnabled(fileActionsEnabled);
ActSaveFile->setEnabled(fileActionsEnabled);
ActFilterTree->setEnabled(pathActionsEnabled || ActFilterTree->isChecked());
bool isTag, isUnApplied, isApplied;
isTag = isUnApplied = isApplied = false;
if (found) {
const Rev* r = git->revLookup(newRevSha);
isTag = git->checkRef(newRevSha, Git::TAG);
isUnApplied = r->isUnApplied;
isApplied = r->isApplied;
}
ActBranch->setEnabled(found && (newRevSha != ZERO_SHA) && !isUnApplied);
ActTag->setEnabled(found && (newRevSha != ZERO_SHA) && !isUnApplied);
ActTagDelete->setEnabled(found && isTag && (newRevSha != ZERO_SHA) && !isUnApplied);
ActPush->setEnabled(found && isUnApplied && git->isNothingToCommit());
ActPop->setEnabled(found && isApplied && git->isNothingToCommit());
}
// ************************* cross-domain update Actions ***************************
void MainImpl::listViewLog_doubleClicked(const QModelIndex& index) {
if (index.isValid() && ActViewDiff->isEnabled())
ActViewDiff->activate(QAction::Trigger);
}
void MainImpl::histListView_doubleClicked(const QModelIndex& index) {
if (index.isValid() && ActViewRev->isEnabled())
ActViewRev->activate(QAction::Trigger);
}
void MainImpl::fileList_itemDoubleClicked(QListWidgetItem* item) {
bool isFirst = (item && item->listWidget()->item(0) == item);
if (isFirst && rv->st.isMerge())
return;
bool isMainView = (item && item->listWidget() == rv->tab()->fileList);
if (isMainView && ActViewDiff->isEnabled())
ActViewDiff->activate(QAction::Trigger);
if (item && !isMainView && ActViewFile->isEnabled())
ActViewFile->activate(QAction::Trigger);
}
void MainImpl::treeView_doubleClicked(QTreeWidgetItem* item, int) {
if (item && ActViewFile->isEnabled())
ActViewFile->activate(QAction::Trigger);
}
void MainImpl::pushButtonCloseTab_clicked() {
Domain* t;
switch (currentTabType(&t)) {
case TAB_REV:
break;
case TAB_PATCH:
t->deleteWhenDone();
ActViewDiffNewTab->setEnabled(ActViewDiff->isEnabled() && firstTab<PatchView>());
break;
case TAB_FILE:
t->deleteWhenDone();
ActViewFileNewTab->setEnabled(ActViewFile->isEnabled() && firstTab<FileView>());
break;
default:
dbs("ASSERT in pushButtonCloseTab_clicked: unknown current page");
break;
}
}
void MainImpl::ActRangeDlg_activated() {
QString args;
RangeSelectImpl rs(this, &args, false, git);
bool quit = (rs.exec() == QDialog::Rejected); // modal execution
if (!quit) {
const QStringList l(args.split(" "));
setRepository(curDir, true, true, &l, true);
}
}
void MainImpl::ActViewRev_activated() {
Domain* t;
if (currentTabType(&t) == TAB_FILE) {
rv->st = t->st;
UPDATE_DOMAIN(rv);
}
tabWdg->setCurrentWidget(rv->tabPage());
}
void MainImpl::ActViewFile_activated() {
openFileTab(firstTab<FileView>());
}
void MainImpl::ActViewFileNewTab_activated() {
openFileTab();
}
void MainImpl::openFileTab(FileView* fv) {
if (!fv) {
fv = new FileView(this, git);
tabWdg->addTab(fv->tabPage(), "File");
connect(fv->tab()->histListView, SIGNAL(doubleClicked(const QModelIndex&)),
this, SLOT(histListView_doubleClicked(const QModelIndex&)));
connect(this, SIGNAL(closeAllTabs()), fv, SLOT(on_closeAllTabs()));
ActViewFileNewTab->setEnabled(ActViewFile->isEnabled());
}
tabWdg->setCurrentWidget(fv->tabPage());
fv->st = rv->st;
UPDATE_DOMAIN(fv);
}
void MainImpl::ActViewDiff_activated() {
Domain* t;
if (currentTabType(&t) == TAB_FILE) {
rv->st = t->st;
UPDATE_DOMAIN(rv);
}
rv->viewPatch(false);
ActViewDiffNewTab->setEnabled(true);
if (ActSearchAndFilter->isChecked() || ActSearchAndHighlight->isChecked()) {
bool isRegExp = (cmbSearch->currentIndex() == CS_PATCH_REGEXP);
emit highlightPatch(lineEditFilter->text(), isRegExp);
}
}
void MainImpl::ActViewDiffNewTab_activated() {
rv->viewPatch(true);
}
bool MainImpl::eventFilter(QObject* obj, QEvent* ev) {
if (ev->type() == QEvent::Wheel) {
QWheelEvent* e = static_cast<QWheelEvent*>(ev);
if (e->modifiers() == Qt::AltModifier) {
int idx = tabWdg->currentIndex();
if (e->delta() < 0)
idx = (++idx == tabWdg->count() ? 0 : idx);
else
idx = (--idx < 0 ? tabWdg->count() - 1 : idx);
tabWdg->setCurrentIndex(idx);
return true;
}
}
return QWidget::eventFilter(obj, ev);
}
void MainImpl::revisionsDragged(SCList selRevs) {
const QString h(QString::fromLatin1("@") + curDir + '\n');
const QString dragRevs = selRevs.join(h).append(h).trimmed();
QDrag* drag = new QDrag(this);
QMimeData* mimeData = new QMimeData;
mimeData->setText(dragRevs);
drag->setMimeData(mimeData);
drag->start(); // blocking until drop event
}
void MainImpl::revisionsDropped(SCList remoteRevs) {
// remoteRevs is already sanity checked to contain some possible valid data
if (rv->isDropping()) // avoid reentrancy
return;
QDir dr(curDir + QGit::PATCHES_DIR);
if (dr.exists()) {
const QString tmp("Please remove stale import directory " + dr.absolutePath());
statusBar()->showMessage(tmp);
return;
}
bool workDirOnly, fold;
if (!askApplyPatchParameters(&workDirOnly, &fold))
return;
// ok, let's go
rv->setDropping(true);
dr.setFilter(QDir::Files);
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
raise();
EM_PROCESS_EVENTS;
uint revNum = 0;
QStringList::const_iterator it(remoteRevs.constEnd());
do {
--it;
QString tmp("Importing revision %1 of %2");
statusBar()->showMessage(tmp.arg(++revNum).arg(remoteRevs.count()));
SCRef sha((*it).section('@', 0, 0));
SCRef remoteRepo((*it).section('@', 1));
if (!dr.exists(remoteRepo))
break;
// we create patches one by one
if (!git->formatPatch(QStringList(sha), dr.absolutePath(), remoteRepo))
break;
dr.refresh();
if (dr.count() != 1) {
qDebug("ASSERT in on_droppedRevisions: found %i files "
"in %s", dr.count(), QGit::PATCHES_DIR.toLatin1().constData());
break;
}
SCRef fn(dr.absoluteFilePath(dr[0]));
bool is_applied = git->applyPatchFile(fn, fold, Git::optDragDrop);
dr.remove(fn);
if (!is_applied)
break;
} while (it != remoteRevs.constBegin());
if (it == remoteRevs.constBegin())
statusBar()->clearMessage();
else
statusBar()->showMessage("Failed to import revision " + QString::number(revNum--));
if (workDirOnly && (revNum > 0))
git->resetCommits(revNum);
dr.rmdir(dr.absolutePath()); // 'dr' must be already empty
QApplication::restoreOverrideCursor();
rv->setDropping(false);
refreshRepo();
}
// ******************************* Filter ******************************
void MainImpl::newRevsAdded(const FileHistory* fh, const QVector<ShaString>&) {
if (!git->isMainHistory(fh))
return;
if (ActSearchAndFilter->isChecked())
ActSearchAndFilter_toggled(true); // filter again on new arrived data
if (ActSearchAndHighlight->isChecked())
ActSearchAndHighlight_toggled(true); // filter again on new arrived data
// first rev could be a StGIT unapplied patch so check more then once
if ( !ActCommit->isEnabled()
&& (!git->isNothingToCommit() || git->isUnknownFiles())
&& !git->isCommittingMerge())
ActCommit_setEnabled(true);
}
void MainImpl::lineEditFilter_returnPressed() {
ActSearchAndFilter->setChecked(true);
}
void MainImpl::ActSearchAndFilter_toggled(bool isOn) {
ActSearchAndHighlight->setEnabled(!isOn);
ActSearchAndFilter->setEnabled(false);
filterList(isOn, false); // blocking call
ActSearchAndFilter->setEnabled(true);
}
void MainImpl::ActSearchAndHighlight_toggled(bool isOn) {
ActSearchAndFilter->setEnabled(!isOn);
ActSearchAndHighlight->setEnabled(false);
filterList(isOn, true); // blocking call
ActSearchAndHighlight->setEnabled(true);
}
void MainImpl::filterList(bool isOn, bool onlyHighlight) {
lineEditFilter->setEnabled(!isOn);
cmbSearch->setEnabled(!isOn);
SCRef filter(lineEditFilter->text());
if (filter.isEmpty())
return;
ShaSet shaSet;
bool patchNeedsUpdate, isRegExp;
patchNeedsUpdate = isRegExp = false;
int idx = cmbSearch->currentIndex(), colNum = 0;
if (isOn) {
switch (idx) {
case CS_SHORT_LOG:
colNum = LOG_COL;
shortLogRE.setPattern(filter);
break;
case CS_LOG_MSG:
colNum = LOG_MSG_COL;
longLogRE.setPattern(filter);
break;
case CS_AUTHOR:
colNum = AUTH_COL;
break;
case CS_SHA1:
colNum = COMMIT_COL;
break;
case CS_FILE:
case CS_PATCH:
case CS_PATCH_REGEXP:
colNum = SHA_MAP_COL;
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
EM_PROCESS_EVENTS; // to paint wait cursor
if (idx == CS_FILE)
git->getFileFilter(filter, shaSet);
else {
isRegExp = (idx == CS_PATCH_REGEXP);
if (!git->getPatchFilter(filter, isRegExp, shaSet)) {
QApplication::restoreOverrideCursor();
ActSearchAndFilter->toggle();
return;
}
patchNeedsUpdate = (shaSet.count() > 0);
}
QApplication::restoreOverrideCursor();
break;
}
} else {
patchNeedsUpdate = (idx == CS_PATCH || idx == CS_PATCH_REGEXP);
shortLogRE.setPattern("");
longLogRE.setPattern("");
}
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
ListView* lv = rv->tab()->listViewLog;
int matchedCnt = lv->filterRows(isOn, onlyHighlight, filter, colNum, &shaSet);
QApplication::restoreOverrideCursor();
emit updateRevDesc(); // could be highlighted
if (patchNeedsUpdate)
emit highlightPatch(isOn ? filter : "", isRegExp);
QString msg;
if (isOn && !onlyHighlight)
msg = QString("Found %1 matches. Toggle filter/highlight "
"button to remove the filter").arg(matchedCnt);
QApplication::postEvent(rv, new MessageEvent(msg)); // deferred message, after update
}
bool MainImpl::event(QEvent* e) {
BaseEvent* de = dynamic_cast<BaseEvent*>(e);
if (!de)
return QWidget::event(e);
SCRef data = de->myData();
bool ret = true;
switch ((EventType)e->type()) {
case ERROR_EV: {
QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor));
EM_PROCESS_EVENTS;
MainExecErrorEvent* me = (MainExecErrorEvent*)e;
QString text("An error occurred while executing command:\n\n");
text.append(me->command() + "\n\nGit says: \n\n" + me->report());
QMessageBox::warning(this, "Error - QGit", text);
QApplication::restoreOverrideCursor(); }
break;
case MSG_EV:
statusBar()->showMessage(data);
break;
case POPUP_LIST_EV:
doContexPopup(data);
break;
case POPUP_FILE_EV:
case POPUP_TREE_EV:
doFileContexPopup(data, e->type());
break;
default:
dbp("ASSERT in MainImpl::event unhandled event %1", e->type());
ret = false;
break;
}
return ret;
}
int MainImpl::currentTabType(Domain** t) {
*t = NULL;
QWidget* curPage = tabWdg->currentWidget();
if (curPage == rv->tabPage()) {
*t = rv;
return TAB_REV;
}
QList<PatchView*>* l = getTabs<PatchView>(curPage);
if (l->count() > 0) {
*t = l->first();
delete l;
return TAB_PATCH;
}
delete l;
QList<FileView*>* l2 = getTabs<FileView>(curPage);
if (l2->count() > 0) {
*t = l2->first();
delete l2;
return TAB_FILE;
}
if (l2->count() > 0)
dbs("ASSERT in tabType file not found");
delete l2;
return -1;
}
template<class X> QList<X*>* MainImpl::getTabs(QWidget* tabPage) {
QList<X*> l = this->findChildren<X*>();
QList<X*>* ret = new QList<X*>;
for (int i = 0; i < l.size(); ++i) {
if (!tabPage || l.at(i)->tabPage() == tabPage)
ret->append(l.at(i));
}
return ret; // 'ret' must be deleted by caller
}
template<class X> X* MainImpl::firstTab(QWidget* startPage) {
int minVal = 99, firstVal = 99;
int startPos = tabWdg->indexOf(startPage);
X* min = NULL;
X* first = NULL;
QList<X*>* l = getTabs<X>();
for (int i = 0; i < l->size(); ++i) {
X* d = l->at(i);
int idx = tabWdg->indexOf(d->tabPage());
if (idx < minVal) {
minVal = idx;
min = d;
}
if (idx < firstVal && idx > startPos) {
firstVal = idx;
first = d;
}
}
delete l;
return (first ? first : min);
}
void MainImpl::tabWdg_currentChanged(int w) {
if (w == -1)
return;
// set correct focus for keyboard browsing
Domain* t;
switch (currentTabType(&t)) {
case TAB_REV:
static_cast<RevsView*>(t)->tab()->listViewLog->setFocus();
emit closeTabButtonEnabled(false);
break;
case TAB_PATCH:
static_cast<PatchView*>(t)->tab()->textEditDiff->setFocus();
emit closeTabButtonEnabled(true);
break;
case TAB_FILE:
static_cast<FileView*>(t)->tab()->histListView->setFocus();
emit closeTabButtonEnabled(true);
break;
default:
dbs("ASSERT in tabWdg_currentChanged: unknown current page");
break;
}
}
void MainImpl::setupShortcuts() {
new QShortcut(Qt::Key_I, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_K, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_N, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_Left, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_Right, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_Delete, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_Backspace, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_Space, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_B, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_D, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_F, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_P, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_R, this, SLOT(shortCutActivated()));
new QShortcut(Qt::Key_U, this, SLOT(shortCutActivated()));
new QShortcut(Qt::SHIFT | Qt::Key_Up, this, SLOT(shortCutActivated()));
new QShortcut(Qt::SHIFT | Qt::Key_Down, this, SLOT(shortCutActivated()));
new QShortcut(Qt::CTRL | Qt::Key_Plus, this, SLOT(shortCutActivated()));
new QShortcut(Qt::CTRL | Qt::Key_Minus, this, SLOT(shortCutActivated()));
}
void MainImpl::shortCutActivated() {
QShortcut* se = dynamic_cast<QShortcut*>(sender());
if (!se)
return;
bool isKey_P = false;
switch (se->key()) {
case Qt::Key_I:
rv->tab()->listViewLog->on_keyUp();
break;
case Qt::Key_K:
case Qt::Key_N:
rv->tab()->listViewLog->on_keyDown();
break;
case Qt::SHIFT | Qt::Key_Up:
goMatch(-1);
break;
case Qt::SHIFT | Qt::Key_Down:
goMatch(1);
break;
case Qt::Key_Left:
ActBack_activated();
break;
case Qt::Key_Right:
ActForward_activated();
break;
case Qt::CTRL | Qt::Key_Plus:
adjustFontSize(1);
break;
case Qt::CTRL | Qt::Key_Minus:
adjustFontSize(-1);
break;
case Qt::Key_U:
scrollTextEdit(-18);
break;
case Qt::Key_D:
scrollTextEdit(18);
break;
case Qt::Key_Delete:
case Qt::Key_B:
case Qt::Key_Backspace:
scrollTextEdit(-1);
break;
case Qt::Key_Space:
scrollTextEdit(1);
break;
case Qt::Key_R:
tabWdg->setCurrentWidget(rv->tabPage());
break;
case Qt::Key_P:
isKey_P = true;
case Qt::Key_F: {
QWidget* cp = tabWdg->currentWidget();
Domain* d = isKey_P ? static_cast<Domain*>(firstTab<PatchView>(cp)) :
static_cast<Domain*>(firstTab<FileView>(cp));
if (d)