1#include <GitServerWidget.h>
2
3#include <ServerConfigDlg.h>
4#include <GitServerCache.h>
5//#include <CreateIssueDlg.h>
6//#include <CreatePullRequestDlg.h>
7#include <GitHubRestApi.h>
8#include <GitLabRestApi.h>
9#include <IssuesList.h>
10#include <PrList.h>
11#include <IssueDetailedView.h>
12#include <Platform.h>
13#include <GitBase.h>
14
15#include <QPushButton>
16#include <QToolButton>
17#include <QHBoxLayout>
18#include <QScrollArea>
19#include <QStackedLayout>
20#include <QLabel>
21#include <QMessageBox>
22#include <QStackedLayout>
23
24using namespace GitServer;
25
26GitServerWidget::GitServerWidget(const QSharedPointer<GitCache> &cache, const QSharedPointer<GitBase> &git,
27 const QSharedPointer<GitServerCache> &gitServerCache, QWidget *parent)
28 : QFrame(parent)
29 , mCache(cache)
30 , mGit(git)
31 , mGitServerCache(gitServerCache)
32{
33}
34
35GitServerWidget::~GitServerWidget()
36{
37 delete mDetailedView;
38 delete mOldIssue;
39 delete mOldPr;
40 delete mRefresh;
41}
42
43bool GitServerWidget::configure(const GitServer::ConfigData &config)
44{
45 if (mConfigured)
46 return true;
47
48 if (config.user.isEmpty() || config.token.isEmpty())
49 {
50 const auto configDlg = new ServerConfigDlg(mGitServerCache, config, this);
51 mConfigured = configDlg->exec() == QDialog::Accepted;
52 }
53 else
54 mConfigured = true;
55
56 if (mConfigured)
57 createWidget();
58
59 return mConfigured;
60}
61
62void GitServerWidget::openPullRequest(int prNumber)
63{
64 mDetailedView->loadData(IssueDetailedView::Config::PullRequests, prNumber);
65}
66
67void GitServerWidget::createWidget()
68{
69 const auto prLabel = QString::fromUtf8(
70 mGitServerCache->getPlatform() == GitServer::Platform::GitHub ? "pull request" : "merge request");
71
72 const auto home = new QPushButton();
73 home->setIcon(QIcon(":/icons/home"));
74 home->setToolTip(tr("General view"));
75
76 const auto newIssue = new QPushButton();
77 newIssue->setIcon(QIcon(":/icons/new_issue"));
78 newIssue->setToolTip(tr("Create a new issue"));
79
80 const auto newPr = new QPushButton();
81 newPr->setIcon(QIcon(":/icons/new_pr"));
82 newPr->setToolTip(tr("Create a new %1").arg(prLabel));
83
84 const auto refresh = new QPushButton();
85 refresh->setIcon(QIcon(":/icons/refresh"));
86 refresh->setToolTip(tr("Refresh"));
87
88 const auto buttonsLayout = new QHBoxLayout();
89 buttonsLayout->setContentsMargins(QMargins());
90 buttonsLayout->setSpacing(10);
91 buttonsLayout->addWidget(home);
92 buttonsLayout->addWidget(newIssue);
93 buttonsLayout->addWidget(newPr);
94 buttonsLayout->addWidget(refresh);
95 buttonsLayout->addStretch();
96
97 mDetailedView = new IssueDetailedView(mGit, mGitServerCache);
98 connect(mDetailedView, &IssueDetailedView::openDiff, this, &GitServerWidget::openDiff);
99
100 const auto issues = new IssuesList(mGitServerCache);
101 connect(issues, &AGitServerItemList::selected, mDetailedView,
102 [this](int issueNum) { mDetailedView->loadData(IssueDetailedView::Config::Issues, issueNum); });
103
104 const auto pullRequests = new PrList(mGitServerCache);
105 connect(pullRequests, &AGitServerItemList::selected, this, &GitServerWidget::openPullRequest);
106
107 connect(refresh, &QPushButton::clicked, this, [issues, pullRequests]() {
108 issues->refreshData();
109 pullRequests->refreshData();
110 });
111
112 const auto issuesLayout = new QVBoxLayout();
113 issuesLayout->setContentsMargins(QMargins());
114 issuesLayout->setSpacing(10);
115 issuesLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
116 issuesLayout->addWidget(issues);
117 issuesLayout->addWidget(pullRequests);
118
119 const auto detailsLayout = new QVBoxLayout();
120 detailsLayout->setContentsMargins(QMargins());
121 detailsLayout->setSpacing(10);
122 detailsLayout->setAlignment(Qt::AlignTop);
123 detailsLayout->addWidget(mDetailedView);
124
125 const auto generalViewLayout = new QGridLayout();
126 generalViewLayout->setContentsMargins(10, 10, 10, 10);
127 generalViewLayout->setSpacing(10);
128 generalViewLayout->setColumnStretch(0, 1);
129 generalViewLayout->setColumnStretch(1, 3);
130 generalViewLayout->addLayout(issuesLayout, 2, 0);
131 generalViewLayout->addLayout(detailsLayout, 2, 1);
132
133 mGeneralView = new QFrame();
134 mGeneralView->setLayout(generalViewLayout);
135
136// mCreateIssueView = new CreateIssueDlg(mGitServerCache, mGit->getWorkingDir(), this);
137// mCreatePrView = new CreatePullRequestDlg(mCache, mGitServerCache, this);
138
139 mStackedLayout = new QStackedLayout();
140 mStackedLayout->addWidget(mGeneralView);
141// mStackedLayout->addWidget(mCreateIssueView);
142// mStackedLayout->addWidget(mCreatePrView);
143
144 const auto centralLayout = new QVBoxLayout();
145 centralLayout->setContentsMargins(10, 10, 10, 10);
146 centralLayout->setSpacing(10);
147 centralLayout->addLayout(buttonsLayout);
148 centralLayout->addLayout(mStackedLayout);
149
150 issues->loadData();
151 pullRequests->loadData();
152
153 connect(home, &QPushButton::clicked, this, [this]() { mStackedLayout->setCurrentIndex(0); });
154 connect(newIssue, &QPushButton::clicked, [this]() { mStackedLayout->setCurrentIndex(1); });
155 connect(newPr, &QPushButton::clicked, [this]() {
156 mStackedLayout->setCurrentIndex(2);
157// mCreatePrView->configure(mGit->getWorkingDir(), mGit->getCurrentBranch());
158 });
159
160 delete mOldIssue;
161 mOldIssue = newIssue;
162
163 delete mOldPr;
164 mOldPr = newPr;
165
166 delete mRefresh;
167 mRefresh = refresh;
168
169 delete layout();
170 setLayout(centralLayout);
171}
172