-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathravenlhsview.cpp
More file actions
147 lines (122 loc) · 5.81 KB
/
ravenlhsview.cpp
File metadata and controls
147 lines (122 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "ravenlhsview.h"
#include <QLabel>
#include <QMessageBox>
#include <QVBoxLayout>
RavenLHSView::RavenLHSView(QWidget *parent)
: QWidget(parent), m_treeView(new RavenTree(this)), m_maxStatusFileCountWarningLabel(new QLabel(this)) {
// Widget config
auto layout = new QVBoxLayout(this);
layout->addWidget(m_treeView);
// Increases QTreeView height against commit message UI
layout->setStretch(0, 1);
// Commit message UI
QLabel *commitMessageHeading = new QLabel(this);
commitMessageHeading->setText("Commit Message");
layout->addWidget(commitMessageHeading);
// Commit message text box
m_commitMessageTextBox = new QPlainTextEdit(this);
m_commitMessageTextBox->setTabChangesFocus(true);
layout->addWidget(m_commitMessageTextBox);
// Commit button & "ammend commit" checkbox
auto commitOrAmendWidget = new QWidget(this);
auto commitOrAmmendLayout = new QHBoxLayout(commitOrAmendWidget);
commitOrAmmendLayout->setContentsMargins(0, 0, 0, 0);
m_amendCommitCheckbox = new QCheckBox(commitOrAmendWidget);
m_amendCommitCheckbox->setText("Amend commit");
m_commitMessageButton = new QPushButton(commitOrAmendWidget);
m_commitMessageButton->setText("Commit");
commitOrAmmendLayout->addWidget(m_amendCommitCheckbox);
commitOrAmmendLayout->addWidget(m_commitMessageButton);
layout->addWidget(commitOrAmendWidget);
// connect listeners
connect(m_treeView->model(), &RavenTreeModel::dataChanged, this, &RavenLHSView::updateCommitMessageUI);
connect(m_commitMessageButton, &QPushButton::clicked, this, &RavenLHSView::commit);
// Max status file size banner
connect(this, &RavenLHSView::signalMaxStatusFileCountReached, this, &RavenLHSView::slotMaxStatusFileCountReached);
}
RavenLHSView::~RavenLHSView() = default;
void RavenLHSView::updateCommitMessageUI() {
// qDebug() << "RavenLHSView::updateCommitMessageUI called";
auto model = m_treeView->model();
auto stagingEmpty = model->isStagingEmpty();
updateCommitMessageUIState(!stagingEmpty);
}
void RavenLHSView::slotMaxStatusFileCountReached(bool reached) {
qDebug() << "RavenLHSView::slotMaxStatusFileCountReached called with reached=" << reached;
// Update layout if required
// qDebug() << "BEFORE isMaxStatusFileCountWarningVisible" << this->isMaxStatusFileCountWarningVisible;
this->isMaxStatusFileCountWarningVisible = layout()->children().contains(this->m_maxStatusFileCountWarningLabel);
// qDebug() << "AFTER isMaxStatusFileCountWarningVisible" << this->isMaxStatusFileCountWarningVisible;
if (!this->isMaxStatusFileCountWarningVisible && reached)
this->buildMaxStatusFileCountWarningUI();
}
void RavenLHSView::buildMaxStatusFileCountWarningUI() const {
QString msg;
// FIXME: Use `QIcon` for warning icon.
msg.append("⚠️ Large number of files detected, showing first ");
msg.append(QString::number(m_treeView->getMaxStatusFilesCount()));
msg.append(" items.");
this->m_maxStatusFileCountWarningLabel->setText(msg);
this->m_maxStatusFileCountWarningLabel->setStyleSheet("background-color:yellow;color:black;padding:5px");
this->m_maxStatusFileCountWarningLabel->setWordWrap(true);
dynamic_cast<QVBoxLayout *>(layout())->insertWidget(0, m_maxStatusFileCountWarningLabel);
}
void RavenLHSView::getAllStagingItemAbsPathsAsync() {
qDebug() << "RavenLHSView::getAllStagingItemAbsPathsAsync() called";
const auto gitManager = qApp->findChild<GitManager *>();
connect(
gitManager, &GitManager::statusChanged, this,
[this](const GitManager::status_data &statusResponse) {
QList<QString> paths;
foreach (auto item, statusResponse.statusItems) {
if (item.category == RavenTreeItem::BOTH || item.category == RavenTreeItem::STAGING) {
qDebug() << "staging item=" << item.path;
paths.append(item.path);
}
}
emit signalStagingItemAbsPathsReady(paths);
},
Qt::SingleShotConnection);
gitManager->statusAsync();
}
void RavenLHSView::updateCommitMessageUIState(bool state) const {
qDebug() << "RavenLHSView::updateCommitMessageUIState with state=" << state;
m_commitMessageTextBox->setEnabled(state);
m_commitMessageButton->setEnabled(state);
m_amendCommitCheckbox->setEnabled(state);
}
void RavenLHSView::commit() {
qDebug() << "RavenLHSView::commit() called";
auto gitManager = qApp->findChild<GitManager *>();
// Extract absPaths from all children of staging root node.
// Handle response
connect(
this, &RavenLHSView::signalStagingItemAbsPathsReady, this,
[this, gitManager](QList<QString> paths) {
QString msg = m_commitMessageTextBox->toPlainText();
// Check amend condition
bool amend = m_amendCommitCheckbox->isChecked();
int result = gitManager->commit(paths, msg, amend);
qDebug() << "GitManager::commit result=" << result;
if (result == 0) {
qDebug() << "GitManager::commit success, updating UI";
// update UI
resetCommitMessageUI();
gitManager->statusAsync();
} else {
qDebug() << "GitManager::commit failed.";
// Failed to commit changes
QMessageBox msg(QMessageBox::Critical, "Error",
"Failed to commit staged changes. Please check the logs for more information.",
QMessageBox::Ok, this);
msg.exec();
}
},
Qt::SingleShotConnection);
// Trigger request
getAllStagingItemAbsPathsAsync();
}
void RavenLHSView::resetCommitMessageUI() const {
m_commitMessageTextBox->setPlainText("");
m_amendCommitCheckbox->setChecked(false);
}