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 "branchesstatuswidget.h" 0008 0009 #include "actions/branchactions.h" 0010 #include "core/kmessageboxhelper.h" 0011 #include "models/branchesmodel.h" 0012 0013 #include <entities/branch.h> 0014 #include <gitmanager.h> 0015 0016 #include <KLocalizedString> 0017 #include <KMessageBox> 0018 #include <QDebug> 0019 0020 BranchesStatusWidget::BranchesStatusWidget(Git::Manager *git, AppWindow *parent) 0021 : WidgetBase(git, parent) 0022 0023 { 0024 setupUi(this); 0025 init(git); 0026 } 0027 0028 void BranchesStatusWidget::init(Git::Manager *git) 0029 { 0030 mActions = new BranchActions(git, this); 0031 mModel = git->branchesModel(); 0032 treeView->setModel(mModel); 0033 0034 comboBoxReferenceBranch->setModel(mModel); 0035 0036 pushButtonNew->setAction(mActions->actionCreate()); 0037 pushButtonBrowse->setAction(mActions->actionBrowse()); 0038 pushButtonCheckout->setAction(mActions->actionCheckout()); 0039 pushButtonDiff->setAction(mActions->actionDiff()); 0040 pushButtonRemoveSelected->setAction(mActions->actionRemove()); 0041 0042 mActions->setOtherBranch(mModel->findByName(comboBoxReferenceBranch->currentText())); 0043 0044 connect(comboBoxReferenceBranch, &QComboBox::currentIndexChanged, this, &BranchesStatusWidget::slotComboBoxReferenceBranchCurrentIndexChanged); 0045 connect(pushButtonRemoveSelected, &QPushButton::clicked, this, &BranchesStatusWidget::slotPushButtonRemoveSelectedClicked); 0046 connect(treeView, &QTreeView::customContextMenuRequested, this, &BranchesStatusWidget::slotTreeViewCustomContextMenuRequested); 0047 } 0048 0049 void BranchesStatusWidget::saveState(QSettings &settings) const 0050 { 0051 save(settings, treeView); 0052 } 0053 0054 void BranchesStatusWidget::restoreState(QSettings &settings) 0055 { 0056 restore(settings, treeView); 0057 } 0058 0059 void BranchesStatusWidget::slotComboBoxReferenceBranchCurrentIndexChanged(int) 0060 { 0061 auto selectedBranch = comboBoxReferenceBranch->currentText(); 0062 mModel->setReferenceBranch(selectedBranch); 0063 mActions->setOtherBranch(mModel->findByName(selectedBranch)); 0064 } 0065 0066 void BranchesStatusWidget::slotPushButtonRemoveSelectedClicked() 0067 { 0068 if (!treeView->currentIndex().isValid()) 0069 return; 0070 0071 if (KMessageBoxHelper::removeQuestion(this, i18n("Are you sure to remove the selected branch?"))) { 0072 auto branchData = mGit->branchesModel()->fromIndex(treeView->currentIndex()); 0073 if (branchData) { 0074 if (!mGit->removeBranch(branchData->name())) { 0075 KMessageBox::information(this, i18n("Unable to remove the selected branch")); 0076 return; 0077 } 0078 mGit->branchesModel()->load(); 0079 } 0080 } 0081 } 0082 0083 void BranchesStatusWidget::slotTreeViewCustomContextMenuRequested(const QPoint &pos) 0084 { 0085 Q_UNUSED(pos) 0086 auto b = mModel->fromIndex(treeView->currentIndex()); 0087 if (!b) 0088 return; 0089 mActions->setBranchName(b); 0090 mActions->popup(); 0091 } 0092 0093 #include "moc_branchesstatuswidget.cpp"