File indexing completed on 2025-01-05 05:14:49

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 "filehistorydialog.h"
0008 #include "models/logsmodel.h"
0009 
0010 #include <entities/commit.h>
0011 #include <gitmanager.h>
0012 
0013 #include <KLocalizedString>
0014 
0015 FileHistoryDialog::FileHistoryDialog(Git::Manager *git, const QString &fileName, QWidget *parent)
0016     : AppDialog(git, parent)
0017     , mFileName(fileName)
0018 {
0019     setupUi(this);
0020 
0021     const auto hashes = git->fileLog(fileName);
0022 
0023     const auto logs = git->logsModel();
0024 
0025     for (const auto &hash : hashes) {
0026         auto log = logs->findLogByHash(hash);
0027         if (!log)
0028             continue;
0029 
0030         auto item = new QListWidgetItem(log->subject());
0031         item->setData(dataRole, log->commitHash());
0032         listWidget->addItem(item);
0033 
0034         auto treeItem = new QTreeWidgetItem{treeWidget};
0035         treeItem->setText(0, log->subject());
0036         treeItem->setData(0, dataRole, log->commitHash());
0037         treeWidget->addTopLevelItem(treeItem);
0038     }
0039     plainTextEdit->setHighlighting(fileName);
0040     setWindowTitle(i18nc("@title:window", "File log: %1", fileName));
0041 
0042     connect(listWidget, &QListWidget::itemClicked, this, &FileHistoryDialog::slotListWidgetItemClicked);
0043     connect(treeWidget, &QTreeWidget::itemClicked, this, &FileHistoryDialog::slotTreeViewItemClicked);
0044     connect(radioButtonRegularView, &QRadioButton::toggled, this, &FileHistoryDialog::slotRadioButtonRegularViewToggled);
0045     connect(radioButtonDifferentialView, &QRadioButton::toggled, this, &FileHistoryDialog::slotRadioButtonDifferentialViewToggled);
0046     widgetDiffView->showSameSize(true);
0047 
0048     treeWidget->header()->setSectionResizeMode(0, QHeaderView::Stretch);
0049     treeWidget->header()->setSectionResizeMode(1, QHeaderView::Fixed);
0050     treeWidget->header()->setSectionResizeMode(2, QHeaderView::Fixed);
0051     treeWidget->header()->resizeSection(1, 50);
0052     treeWidget->header()->resizeSection(2, 50);
0053     treeWidget->header()->setStretchLastSection(false);
0054     radioButtonRegularView->setChecked(true);
0055 }
0056 
0057 FileHistoryDialog::FileHistoryDialog(Git::Manager *git, const Git::File &file, QWidget *parent)
0058     : FileHistoryDialog(git, file.fileName(), parent)
0059 {
0060 }
0061 
0062 void FileHistoryDialog::slotListWidgetItemClicked(QListWidgetItem *item)
0063 {
0064     const auto content = mGit->fileContent(item->data(dataRole).toString(), mFileName);
0065     plainTextEdit->setPlainText(content);
0066 }
0067 
0068 void FileHistoryDialog::slotRadioButtonRegularViewToggled(bool toggle)
0069 {
0070     if (toggle) {
0071         stackedWidgetFileSelector->setCurrentIndex(0);
0072         stackedWidgetFileView->setCurrentIndex(0);
0073     }
0074 }
0075 
0076 void FileHistoryDialog::slotRadioButtonDifferentialViewToggled(bool toggle)
0077 {
0078     if (toggle) {
0079         stackedWidgetFileSelector->setCurrentIndex(1);
0080         stackedWidgetFileView->setCurrentIndex(1);
0081     }
0082 }
0083 
0084 void FileHistoryDialog::compareFiles()
0085 {
0086     if (!mLeftFile || !mRightFile)
0087         return;
0088 
0089     widgetDiffView->setOldFile(QSharedPointer<Git::File>{new Git::File{mGit, mLeftFile->data(0, dataRole).toString(), mFileName}});
0090     widgetDiffView->setNewFile(QSharedPointer<Git::File>{new Git::File{mGit, mRightFile->data(0, dataRole).toString(), mFileName}});
0091     widgetDiffView->compare();
0092 }
0093 
0094 void FileHistoryDialog::slotTreeViewItemClicked(QTreeWidgetItem *item, int column)
0095 {
0096     if (column == 1) {
0097         item->setText(1, QStringLiteral("✓"));
0098         if (mLeftFile)
0099             mLeftFile->setText(1, QLatin1String());
0100         mLeftFile = item;
0101     }
0102     if (column == 2) {
0103         item->setText(2, QStringLiteral("✓"));
0104         if (mRightFile)
0105             mRightFile->setText(2, QLatin1String());
0106         mRightFile = item;
0107     }
0108     compareFiles();
0109 }
0110 
0111 #include "moc_filehistorydialog.cpp"