File indexing completed on 2025-01-05 05:14:40
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "commitswidget.h" 0008 0009 #include "actions/branchactions.h" 0010 #include "actions/commitactions.h" 0011 #include "core/commitsfiltermodel.h" 0012 0013 #include <entities/branch.h> 0014 #include <entities/commit.h> 0015 #include <gitmanager.h> 0016 #include <models/logsmodel.h> 0017 #include <models/treemodel.h> 0018 #include <windows/diffwindow.h> 0019 0020 #include <KommitSettings.h> 0021 0022 CommitsWidget::CommitsWidget(Git::Manager *git, AppWindow *parent) 0023 : WidgetBase(git, parent) 0024 { 0025 setupUi(this); 0026 init(); 0027 } 0028 0029 void CommitsWidget::reload() 0030 { 0031 mRepoModel->clear(); 0032 QStringList branchNames; 0033 auto branches = git()->branches(Git::Manager::BranchType::AllBranches); 0034 for (auto &branch : branches) { 0035 branchNames << branch->name(); 0036 mBranchesMap.insert(branch->name(), branch); 0037 } 0038 mRepoModel->addData(branchNames); 0039 0040 if (branchNames.contains(QStringLiteral("master"))) 0041 mMainBranch = QStringLiteral("master"); 0042 else if (branchNames.contains(QStringLiteral("main"))) 0043 mMainBranch = QStringLiteral("main"); 0044 0045 setBranch(QString()); 0046 } 0047 0048 void CommitsWidget::saveState(QSettings &settings) const 0049 { 0050 save(settings, splitter); 0051 save(settings, splitter_2); 0052 save(settings, treeViewRepo); 0053 save(settings, treeViewHistory); 0054 } 0055 0056 void CommitsWidget::restoreState(QSettings &settings) 0057 { 0058 restore(settings, splitter); 0059 restore(settings, splitter_2); 0060 restore(settings, treeViewRepo); 0061 restore(settings, treeViewHistory); 0062 } 0063 0064 void CommitsWidget::settingsUpdated() 0065 { 0066 mHistoryModel->setCalendarType(KommitSettings::calendarType()); 0067 } 0068 0069 void CommitsWidget::slotTreeViewRepoItemActivated(const QModelIndex &index) 0070 { 0071 auto key = mRepoModel->key(index); 0072 if (!key.isEmpty()) 0073 setBranch(key); 0074 } 0075 0076 void CommitsWidget::slotTreeViewRepoCustomContextMenuRequested(const QPoint &pos) 0077 { 0078 Q_UNUSED(pos) 0079 auto branchName = mRepoModel->fullPath(treeViewRepo->currentIndex()); 0080 mActions->setBranchName(mBranchesMap.value(branchName)); 0081 mActions->popup(); 0082 } 0083 0084 void CommitsWidget::init() 0085 { 0086 mHistoryModel = new Git::LogsModel(mGit, nullptr, this); 0087 mHistoryModel->setCalendarType(KommitSettings::calendarType()); 0088 mHistoryModel->setFullDetails(true); 0089 mFilterModel = new CommitsFilterModel(mHistoryModel, this); 0090 0091 treeViewHistory->setModel(mFilterModel); 0092 0093 mRepoModel = new TreeModel(this); 0094 treeViewRepo->setModel(mRepoModel); 0095 0096 mActions = new BranchActions(mGit, this); 0097 mCommitActions = new CommitActions(mGit, this); 0098 0099 connect(treeViewRepo, &TreeView::itemActivated, this, &CommitsWidget::slotTreeViewRepoItemActivated); 0100 connect(treeViewRepo, &QTreeView::customContextMenuRequested, this, &CommitsWidget::slotTreeViewRepoCustomContextMenuRequested); 0101 connect(treeViewHistory, &TreeView::itemActivated, this, &CommitsWidget::slotTreeViewHistoryItemActivated); 0102 connect(treeViewHistory, &TreeView::itemActivated, this, &CommitsWidget::slotTreeViewHistoryItemActivated); 0103 connect(widgetCommitDetails, &CommitDetails::hashClicked, this, &CommitsWidget::slotTextBrowserHashClicked); 0104 connect(widgetCommitDetails, &CommitDetails::fileClicked, this, &CommitsWidget::slotTextBrowserFileClicked); 0105 connect(treeViewHistory, &QTreeView::customContextMenuRequested, this, &CommitsWidget::slotTreeViewHistoryCustomContextMenuRequested); 0106 connect(lineEditFilter, &QLineEdit::textChanged, this, &CommitsWidget::slotLineEditFilterTextChanged); 0107 } 0108 0109 void CommitsWidget::slotTreeViewHistoryItemActivated(const QModelIndex &index) 0110 { 0111 auto commit = mHistoryModel->fromIndex(mFilterModel->mapToSource(index)); 0112 if (!commit) 0113 return; 0114 0115 widgetCommitDetails->setCommit(commit); 0116 } 0117 0118 void CommitsWidget::slotTextBrowserHashClicked(const QString &hash) 0119 { 0120 const auto index = mHistoryModel->findIndexByHash(hash); 0121 if (index.isValid()) { 0122 treeViewHistory->setCurrentIndex(index); 0123 slotTreeViewHistoryItemActivated(index); 0124 } 0125 } 0126 0127 void CommitsWidget::slotTextBrowserFileClicked(const QString &file) 0128 { 0129 auto commit = widgetCommitDetails->commit(); 0130 0131 if (!commit || !commit->parents().size()) 0132 return; 0133 0134 QSharedPointer<Git::File> oldFile{new Git::File{mGit, commit->parents().first(), file}}; 0135 QSharedPointer<Git::File> newFile{new Git::File{mGit, commit->commitHash(), file}}; 0136 0137 auto diffWin = new DiffWindow(oldFile, newFile); 0138 diffWin->showModal(); 0139 } 0140 0141 void CommitsWidget::slotTreeViewHistoryCustomContextMenuRequested(const QPoint &pos) 0142 { 0143 Q_UNUSED(pos) 0144 auto log = mHistoryModel->fromIndex(treeViewHistory->currentIndex()); 0145 if (!log) 0146 return; 0147 mCommitActions->setCommit(log); 0148 0149 mCommitActions->popup(); 0150 } 0151 0152 void CommitsWidget::setBranch(const QString &branchName) 0153 { 0154 treeViewHistory->setItemDelegateForColumn(0, nullptr); 0155 mHistoryModel->setBranch(branchName); 0156 if (mHistoryModel->rowCount(QModelIndex())) 0157 treeViewHistory->setCurrentIndex(mHistoryModel->index(0)); 0158 } 0159 0160 void CommitsWidget::slotLineEditFilterTextChanged(const QString &text) 0161 { 0162 mFilterModel->setFilterTerm(text); 0163 } 0164 0165 #include "moc_commitswidget.cpp"