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 "historyviewwidget.h"
0008 #include "actions/commitactions.h"
0009 #include "models/logsmodel.h"
0010 
0011 #include <entities/commit.h>
0012 #include <gitmanager.h>
0013 #include <widgets/graphpainter.h>
0014 #include <windows/diffwindow.h>
0015 
0016 HistoryViewWidget::HistoryViewWidget(Git::Manager *git, AppWindow *parent)
0017     : WidgetBase(git, parent)
0018     , mActions(new CommitActions(git, this))
0019     , mHistoryModel(git->logsModel())
0020 {
0021     setupUi(this);
0022     treeViewHistory->setModel(mHistoryModel);
0023 
0024     mGraphPainter = new GraphPainter(mHistoryModel, this);
0025     treeViewHistory->setItemDelegateForColumn(0, mGraphPainter);
0026 
0027     // textBrowser->setEnableCommitsLinks(true);
0028 
0029     connect(treeViewHistory, &TreeView::itemActivated, this, &HistoryViewWidget::slotTreeViewHistoryItemActivated);
0030     connect(widgetCommit, &CommitDetails::hashClicked, this, &HistoryViewWidget::slotTextBrowserHashClicked);
0031     connect(widgetCommit, &CommitDetails::fileClicked, this, &HistoryViewWidget::slotTextBrowserFileClicked);
0032     connect(treeViewHistory, &TreeView::customContextMenuRequested, this, &HistoryViewWidget::slotTreeViewHistoryCustomContextMenuRequested);
0033 }
0034 
0035 void HistoryViewWidget::setBranch(const QString &branchName)
0036 {
0037     treeViewHistory->setItemDelegateForColumn(0, nullptr);
0038     mHistoryModel->setBranch(branchName);
0039     if (mHistoryModel->rowCount(QModelIndex()))
0040         treeViewHistory->setCurrentIndex(mHistoryModel->index(0));
0041 }
0042 
0043 void HistoryViewWidget::saveState(QSettings &settings) const
0044 {
0045     save(settings, splitter);
0046 }
0047 
0048 void HistoryViewWidget::restoreState(QSettings &settings)
0049 {
0050     restore(settings, splitter);
0051 }
0052 
0053 void HistoryViewWidget::slotTreeViewHistoryItemActivated(const QModelIndex &index)
0054 {
0055     auto log = mHistoryModel->fromIndex(index);
0056     if (!log)
0057         return;
0058 
0059     widgetCommit->setCommit(log);
0060 }
0061 
0062 void HistoryViewWidget::slotTextBrowserHashClicked(const QString &hash)
0063 {
0064     auto index = mHistoryModel->findIndexByHash(hash);
0065     if (index.isValid()) {
0066         treeViewHistory->setCurrentIndex(index);
0067         slotTreeViewHistoryItemActivated(index);
0068     }
0069 }
0070 
0071 void HistoryViewWidget::slotTextBrowserFileClicked(const QString &file)
0072 {
0073     auto log = widgetCommit->commit();
0074 
0075     if (!log || !log->parents().size())
0076         return;
0077 
0078     QSharedPointer<Git::File> oldFile{new Git::File{mGit, log->parents().first(), file}};
0079     QSharedPointer<Git::File> newFile{new Git::File{mGit, log->commitHash(), file}};
0080 
0081     auto diffWin = new DiffWindow(oldFile, newFile);
0082     diffWin->showModal();
0083 }
0084 
0085 void HistoryViewWidget::slotTreeViewHistoryCustomContextMenuRequested(const QPoint &pos)
0086 {
0087     Q_UNUSED(pos)
0088     auto commit = mHistoryModel->fromIndex(treeViewHistory->currentIndex());
0089     if (!commit)
0090         return;
0091     mActions->setCommit(commit);
0092 
0093     mActions->popup();
0094 }
0095 
0096 #include "moc_historyviewwidget.cpp"