1#include "SubmodulesContextMenu.h"
2
3#include <BranchDlg.h>
4#include <GitStashes.h>
5#include <GitQlientStyles.h>
6#include <AddSubmoduleDlg.h>
7#include <GitSubmodules.h>
8#include <GitBase.h>
9
10#include <QApplication>
11#include <QModelIndex>
12#include <QMessageBox>
13
14SubmodulesContextMenu::SubmodulesContextMenu(const QSharedPointer<GitBase> &git, const QModelIndex &index,
15 QWidget *parent)
16 : QMenu(parent)
17 , mGit(git)
18{
19 setAttribute(Qt::WA_DeleteOnClose);
20
21 if (!index.isValid())
22 {
23 const auto addSubmoduleAction = addAction(tr("Add submodule"));
24 connect(addSubmoduleAction, &QAction::triggered, this, [this] {
25 const auto git = QSharedPointer<GitSubmodules>::create(mGit);
26 AddSubmoduleDlg addDlg(git);
27 const auto ret = addDlg.exec();
28 if (ret == QDialog::Accepted)
29 emit infoUpdated();
30 });
31 }
32 else
33 {
34 const auto submoduleName = index.data().toString();
35 const auto updateSubmoduleAction = addAction(tr("Update"));
36 connect(updateSubmoduleAction, &QAction::triggered, this, [this, submoduleName]() {
37 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
38 QScopedPointer<GitSubmodules> git(new GitSubmodules(mGit));
39 const auto ret = git->submoduleUpdate(submoduleName);
40 QApplication::restoreOverrideCursor();
41
42 if (ret)
43 emit infoUpdated();
44 });
45
46 const auto openSubmoduleAction = addAction(tr("Open"));
47 connect(openSubmoduleAction, &QAction::triggered, this,
48 [this, submoduleName]() { emit openSubmodule(mGit->getWorkingDir().append("/").append(submoduleName)); });
49
50 const auto deleteSubmoduleAction = addAction(tr("Delete"));
51 connect(deleteSubmoduleAction, &QAction::triggered, this, [this, submoduleName]() {
52 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
53 QScopedPointer<GitSubmodules> git(new GitSubmodules(mGit));
54 const auto ret = git->submoduleRemove(submoduleName);
55 QApplication::restoreOverrideCursor();
56
57 if (ret)
58 emit infoUpdated();
59 });
60 }
61}
62
63SubmodulesContextMenu::SubmodulesContextMenu(const QSharedPointer<GitBase> &git, const QString &name, QWidget *parent)
64 : QMenu(parent)
65 , mGit(git)
66{
67 const auto submoduleName = name;
68 const auto updateSubmoduleAction = addAction(tr("Update"));
69 connect(updateSubmoduleAction, &QAction::triggered, this, [this, submoduleName]() {
70 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
71 QScopedPointer<GitSubmodules> git(new GitSubmodules(mGit));
72 const auto ret = git->submoduleUpdate(submoduleName);
73 QApplication::restoreOverrideCursor();
74
75 if (ret)
76 emit infoUpdated();
77 });
78
79 const auto openSubmoduleAction = addAction(tr("Open"));
80 connect(openSubmoduleAction, &QAction::triggered, this,
81 [this, submoduleName]() { emit openSubmodule(mGit->getWorkingDir().append("/").append(submoduleName)); });
82}
83