1#include "BranchesWidgetMinimal.h"
2
3#include <GitCache.h>
4#include <GitSubmodules.h>
5#include <GitStashes.h>
6
7#include <QVBoxLayout>
8#include <QLabel>
9#include <QMenu>
10#include <QToolButton>
11#include <QPushButton>
12#include <QEvent>
13
14BranchesWidgetMinimal::BranchesWidgetMinimal(const QSharedPointer<GitCache> &cache,
15 const QSharedPointer<GitBase> git, QWidget *parent)
16 : QFrame(parent)
17 , mGit(git)
18 , mCache(cache)
19 , mBack(new QPushButton())
20 , mLocal(new QToolButton())
21 , mLocalMenu(new QMenu(mLocal))
22 , mRemote(new QToolButton())
23 , mRemoteMenu(new QMenu(mRemote))
24 , mTags(new QToolButton())
25 , mTagsMenu(new QMenu(mTags))
26 , mStashes(new QToolButton())
27 , mStashesMenu(new QMenu(mStashes))
28 , mSubmodules(new QToolButton())
29 , mSubmodulesMenu(new QMenu(mSubmodules))
30{
31 mBack->setIcon(QIcon(":/icons/back"));
32 mBack->setToolTip(tr("Full view"));
33 connect(mBack, &QPushButton::clicked, this, &BranchesWidgetMinimal::showFullBranchesView);
34
35 const auto layout = new QVBoxLayout(this);
36 layout->setContentsMargins(QMargins());
37 layout->setSpacing(0);
38 layout->addWidget(mBack);
39 layout->addWidget(mLocal);
40 layout->addWidget(mRemote);
41 layout->addWidget(mTags);
42 layout->addWidget(mStashes);
43 layout->addWidget(mSubmodules);
44
45 mLocalMenu->installEventFilter(this);
46 mLocal->setMenu(mLocalMenu);
47 mLocal->setIcon(QIcon(":/icons/local"));
48 mLocal->setPopupMode(QToolButton::InstantPopup);
49 mLocal->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
50 mLocal->setText(" " + QString::number(mLocalMenu->actions().count()));
51 mLocal->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
52 mLocal->setToolTip(tr("Local branches"));
53
54 mRemoteMenu->installEventFilter(this);
55 mRemote->setMenu(mRemoteMenu);
56 mRemote->setIcon(QIcon(":/icons/server"));
57 mRemote->setPopupMode(QToolButton::InstantPopup);
58 mRemote->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
59 mRemote->setText(" " + QString::number(mRemoteMenu->actions().count()));
60 mRemote->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
61 mRemote->setToolTip(tr("Remote branches"));
62
63 mTagsMenu->installEventFilter(this);
64 mTags->setMenu(mTagsMenu);
65 mTags->setIcon(QIcon(":/icons/tags"));
66 mTags->setPopupMode(QToolButton::InstantPopup);
67 mTags->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
68 mTags->setText(" " + QString::number(mTagsMenu->actions().count()));
69 mTags->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
70 mTags->setToolTip(tr("Tags"));
71
72 mStashesMenu->installEventFilter(this);
73 mStashes->setMenu(mStashesMenu);
74 mStashes->setIcon(QIcon(":/icons/stashes"));
75 mStashes->setPopupMode(QToolButton::InstantPopup);
76 mStashes->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
77 mStashes->setText(" " + QString::number(mStashesMenu->actions().count()));
78 mStashes->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
79 mStashes->setToolTip(tr("Stashes"));
80
81 mSubmodulesMenu->installEventFilter(this);
82 mSubmodules->setMenu(mSubmodulesMenu);
83 mSubmodules->setIcon(QIcon(":/icons/submodules"));
84 mSubmodules->setPopupMode(QToolButton::InstantPopup);
85 mSubmodules->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
86 mSubmodules->setText(" " + QString::number(mSubmodulesMenu->actions().count()));
87 mSubmodules->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
88 mSubmodules->setToolTip(tr("Submodules"));
89}
90
91bool BranchesWidgetMinimal::eventFilter(QObject *obj, QEvent *event)
92{
93
94 if (const auto menu = qobject_cast<QMenu *>(obj); menu && event->type() == QEvent::Show)
95 {
96 auto localPos = menu->parentWidget()->pos();
97 localPos.setX(localPos.x());
98 auto pos = mapToGlobal(localPos);
99 menu->show();
100 pos.setX(pos.x() - menu->width());
101 menu->move(pos);
102 return true;
103 }
104
105 return false;
106}
107
108void BranchesWidgetMinimal::addActionToMenu(const QString &sha, const QString &name, QMenu *menu)
109{
110 const auto action = new QAction(name, menu);
111 action->setData(sha);
112 connect(action, &QAction::triggered, this, [this, sha] { emit commitSelected(sha); });
113
114 menu->addAction(action);
115}
116
117void BranchesWidgetMinimal::configureLocalMenu(const QString &sha, const QString &branch)
118{
119 addActionToMenu(sha, branch, mLocalMenu);
120 mLocal->setText(" " + QString::number(mLocalMenu->actions().count()));
121}
122
123void BranchesWidgetMinimal::configureRemoteMenu(const QString &sha, const QString &branch)
124{
125 addActionToMenu(sha, branch, mRemoteMenu);
126 mRemote->setText(" " + QString::number(mRemoteMenu->actions().count()));
127}
128
129void BranchesWidgetMinimal::configureTagsMenu(const QString &sha, const QString &tag)
130{
131 addActionToMenu(sha, tag, mTagsMenu);
132 mTags->setText(" " + QString::number(mTagsMenu->actions().count()));
133}
134
135void BranchesWidgetMinimal::configureStashesMenu(const QString &stashId, const QString &name)
136{
137 const auto action = new QAction(name);
138 action->setData(stashId);
139 connect(action, &QAction::triggered, this, [this, stashId] { emit stashSelected(stashId); });
140
141 mStashesMenu->addAction(action);
142 mStashes->setText(" " + QString::number(mStashesMenu->actions().count()));
143}
144
145void BranchesWidgetMinimal::configureSubmodulesMenu(const QString &name)
146{
147 const auto action = new QAction(name);
148 action->setData(name);
149 mSubmodulesMenu->addAction(action);
150 mSubmodules->setText(" " + QString::number(mSubmodulesMenu->actions().count()));
151}
152
153void BranchesWidgetMinimal::clearActions()
154{
155 mLocalMenu->clear();
156 mRemoteMenu->clear();
157 mTagsMenu->clear();
158 mStashesMenu->clear();
159 mSubmodulesMenu->clear();
160}
161