File indexing completed on 2025-01-19 05:11:26

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 "mainwidget.h"
0008 #include "buttonsgroup.h"
0009 #include "dialogs/taginfodialog.h"
0010 #include "diffwindow.h"
0011 #include "models/treemodel.h"
0012 
0013 #include "kommit_appdebug.h"
0014 #include <QMenu>
0015 #include <QToolButton>
0016 
0017 #include "dialogs/filestreedialog.h"
0018 #include "git/gitmanager.h"
0019 #include "git/gitremote.h"
0020 
0021 Git::Manager *MainWidget::git() const
0022 {
0023     return _git;
0024 }
0025 
0026 void MainWidget::setGit(Git::Manager *newGit)
0027 {
0028     _git = newGit;
0029 }
0030 
0031 void MainWidget::reload()
0032 {
0033     _branches = _git->branches();
0034 
0035     _repoModel->clear();
0036     _repoModel->addData(_git->branches());
0037     treeViewRepo->setModel(_repoModel);
0038 
0039     listWidgetTags->clear();
0040     listWidgetTags->addItems(_git->tags());
0041 }
0042 
0043 MainWidget::MainWidget(QWidget *parent)
0044     : QWidget(parent)
0045 {
0046     auto actionBrowseBranch = new QAction(this);
0047     auto actionDiffBranch = new QAction(this);
0048     Q_SET_OBJECT_NAME(actionBrowseBranch);
0049     Q_SET_OBJECT_NAME(actionDiffBranch);
0050 
0051     setupUi(this);
0052     _repoModel = new TreeModel(this);
0053 
0054     _git = Git::Manager::instance();
0055     connect(_git, &Git::Manager::pathChanged, this, &MainWidget::reload);
0056 
0057     auto styleSheet = QString(R"CSS(
0058         #scrollAreaWidgetContents {
0059             background-color: #%1;
0060         }
0061         QToolButton {
0062             background-color: #%1;
0063             border: none;
0064             padding-top: 10px;
0065             padding-bottom: 10px;
0066             height: 48px;
0067         }
0068 
0069         QToolButton:hover {
0070             background-color: #%2;
0071         }
0072 
0073         QToolButton:checked {
0074             background-color: #%3;
0075             color: white;
0076         }
0077 
0078 )CSS")
0079                           .arg(palette().color(QPalette::Base).rgba(), 0, 16)
0080                           .arg(palette().color(QPalette::Highlight).lighter().rgba(), 0, 16)
0081                           .arg(palette().color(QPalette::Highlight).rgba(), 0, 16);
0082 
0083     scrollAreaWidgetContents->setStyleSheet(styleSheet);
0084 
0085     _pagesButtonsGroup = new ButtonsGroup(this);
0086     _pagesButtonsGroup->addButton(toolButtonOverview);
0087     _pagesButtonsGroup->addButton(toolButtonBranches);
0088     _pagesButtonsGroup->addButton(toolButtonCommits);
0089     _pagesButtonsGroup->addButton(toolButtonStashes);
0090     _pagesButtonsGroup->addButton(toolButtonRemotes);
0091     _pagesButtonsGroup->addButton(toolButtonTags);
0092     _pagesButtonsGroup->addButton(toolButtonSubmodules);
0093     connect(_pagesButtonsGroup, &ButtonsGroup::clicked, this, &MainWidget::pageButtons_clicked);
0094     _pagesButtonsGroup->simulateClickOn(0);
0095 
0096     _branchesMenu = new QMenu(this);
0097     _branchesMenu->addAction(actionBrowseBranch);
0098     _branchesMenu->addAction(actionDiffBranch);
0099 
0100     actionBrowseBranch->setText(i18n("Browse..."));
0101     actionDiffBranch->setText(i18n("Diff with master..."));
0102 
0103     connect(treeViewRepo, &QTreeView::activated, this, &::slotTreeViewRepoActivated);
0104     connect(treeViewRepo, &QTreeViewRepo::customContextMenuRequested, this, &::slotTreeViewRepoCustomContextMenuRequested);
0105     connect(actionBrowseBranch, &QAction::triggered, this, &::slotActionBrowseBranchTriggered);
0106     connect(actionDiffBranch, &QAction::triggered, this, &::slotActionDiffBranchTriggered);
0107     connect(pushButtonAddTag, &QPushButton::clicked, this, &::slotPushButtonAddTagClicked);
0108 }
0109 
0110 MainWidget::~MainWidget()
0111 {
0112 }
0113 
0114 void MainWidget::listButton_clicked()
0115 {
0116     auto btn = qobject_cast<QToolButton *>(sender());
0117     if (!btn)
0118         return;
0119 
0120     int index = btn->property("listIndex").toInt();
0121     qCDebug(KOMMIT_LOG) << index << "clicked";
0122     btn->setChecked(true);
0123     stackedWidget->setCurrentIndex(index);
0124 }
0125 
0126 void MainWidget::pageButtons_clicked(int index)
0127 {
0128     auto btn = _pagesButtonsGroup->at(index);
0129     labelTitle->setText(btn->text().replace("&", ""));
0130     labelPageIcon->setPixmap(btn->icon().pixmap({32, 32}));
0131     stackedWidget->setCurrentIndex(index);
0132     return;
0133 }
0134 
0135 void MainWidget::slotTreeViewRepoActivated(const QModelIndex &index)
0136 {
0137     auto key = _repoModel->key(index);
0138     if (!key.isEmpty())
0139         widgetCommitsView->setBranch(key);
0140 }
0141 
0142 void MainWidget::slotTreeViewRepoCustomContextMenuRequested(const QPoint &pos)
0143 {
0144     _branchesMenu->popup(treeViewRepo->mapToGlobal(pos));
0145 }
0146 
0147 void MainWidget::slotActionBrowseBranchTriggered()
0148 {
0149     auto branchName = _repoModel->fullPath(treeViewRepo->currentIndex());
0150     FilesTreeDialog d(branchName, this);
0151     d.exec();
0152 }
0153 
0154 void MainWidget::slotActionDiffBranchTriggered()
0155 {
0156     auto branchName = _repoModel->fullPath(treeViewRepo->currentIndex());
0157 
0158     auto diffWin = new DiffWindow(branchName, "master");
0159     diffWin->showModal();
0160 }
0161 
0162 void MainWidget::slotPushButtonAddTagClicked()
0163 {
0164     TagInfoDialog d(this);
0165     d.setWindowTitle(i18n("New tag"));
0166     if (d.exec() == QDialog::Accepted) {
0167         _git->createTag(d.tagName(), d.message());
0168     }
0169 }
0170 
0171 #include "moc_mainwidget.cpp"