-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
185 lines (143 loc) · 5.12 KB
/
mainwindow.cpp
File metadata and controls
185 lines (143 loc) · 5.12 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QMessageBox>
#include <QDir>
#include <QListWidgetItem>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
gitProcess = new QProcess(this);
fileModel = new QFileSystemModel(this);
fileModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files);
fileModel->setRootPath("");
ui->treeView->setModel(fileModel);
ui->treeView->setColumnHidden(1, true);
ui->treeView->setColumnHidden(2, true);
ui->treeView->setColumnHidden(3, true);
connect(ui->switchButton, &QPushButton::clicked, this, &MainWindow::on_switchButton_clicked);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_path_pushButton_clicked()
{
projectPath = ui->path_lineEdit->text();
if (!QDir(projectPath).exists()) {
QMessageBox::warning(this, "Warning!", "The specified path does not exist.");
projectPath.clear();
return;
}
gitProcess->setWorkingDirectory(projectPath);
ui->treeView->setRootIndex(fileModel->setRootPath(projectPath));
loadBranchesIntoListWidget();
QMessageBox::information(this, "Info", "Working directory set to:\n" + projectPath);
}
void MainWindow::loadBranchesIntoListWidget()
{
if (projectPath.isEmpty()) {
QMessageBox::warning(this, "Warning", "Project path is not set");
return;
}
gitProcess->start("git", QStringList() << "branch");
if (!gitProcess->waitForFinished(5000)) {
QMessageBox::critical(this, "Error", "Git command timed out.");
return;
}
QString output = gitProcess->readAllStandardOutput();
QString error = gitProcess->readAllStandardError();
if (!error.isEmpty()) {
QMessageBox::critical(this, "Git Error", error);
return;
}
ui->branchListWidget->clear();
QStringList lines = output.split('\n', Qt::SkipEmptyParts);
for (const QString &line : lines) {
QString branchName = line.trimmed();
QListWidgetItem *item = nullptr;
if (branchName.startsWith('*')) {
branchName = branchName.mid(1).trimmed();
item = new QListWidgetItem(branchName);
item->setSelected(true);
QFont font = item->font();
font.setBold(true);
item->setFont(font);
} else {
item = new QListWidgetItem(branchName);
}
ui->branchListWidget->addItem(item);
}
}
void MainWindow::on_command_pushButton_clicked()
{
if (projectPath.isEmpty()) {
QMessageBox::warning(this, "Warning!", "No project path is given");
return;
}
QString commandText = ui->command_lineEdit->text().trimmed();
if (commandText.isEmpty()) {
QMessageBox::warning(this, "Warning!", "No git command entered");
return;
}
QStringList arguments = commandText.split(' ', Qt::SkipEmptyParts);
gitProcess->start("git", arguments);
if (!gitProcess->waitForFinished(5000)) {
QMessageBox::critical(this, "Error", "Git command timed out.");
return;
}
QString output = gitProcess->readAllStandardOutput();
QString error = gitProcess->readAllStandardError();
if (!error.isEmpty()) {
QMessageBox::critical(this, "Git Error", error);
return;
}
if (!arguments.isEmpty() && arguments[0] == "branch") {
loadBranchesIntoListWidget();
// Show branches info in a message box
QStringList lines = output.split('\n', Qt::SkipEmptyParts);
QString displayText = "Branches:\n";
for (QString line : lines) {
line = line.trimmed();
if (line.startsWith('*')) {
displayText += QString("• %1 (current branch)\n").arg(line.mid(1).trimmed());
} else {
displayText += QString("• %1\n").arg(line);
}
}
QMessageBox::information(this, "Git Branches", displayText);
} else {
QMessageBox::information(this, "Git Output", output.isEmpty() ? "Git command succeeded." : output);
}
}
void MainWindow::on_treeView_activated(const QModelIndex &index)
{
Q_UNUSED(index);
}
void MainWindow::on_switchButton_clicked()
{
if (projectPath.isEmpty()) {
QMessageBox::warning(this, "Warning", "Project path is not set");
return;
}
QListWidgetItem *selectedItem = ui->branchListWidget->currentItem();
if (!selectedItem) {
QMessageBox::warning(this, "Warning", "No branch selected");
return;
}
QString branchName = selectedItem->text();
gitProcess->start("git", QStringList() << "checkout" << branchName);
if (!gitProcess->waitForFinished(5000)) {
QMessageBox::critical(this, "Error", "Git command timed out.");
return;
}
QString error = gitProcess->readAllStandardError();
QString output = gitProcess->readAllStandardOutput();
if (!error.isEmpty()) {
QMessageBox::critical(this, "Git Error", error);
return;
}
QMessageBox::information(this, "Git Output", output.isEmpty() ? "Switched branch successfully." : output);
loadBranchesIntoListWidget();
}