RedPanda-CPP/RedPandaIDE/vcs/gitremotedialog.cpp

190 lines
5.2 KiB
C++
Raw Normal View History

2022-02-24 07:22:44 +08:00
#include "gitremotedialog.h"
#include "ui_gitremotedialog.h"
#include "gitmanager.h"
#include "../iconsmanager.h"
#include "../widgets/infomessagebox.h"
GitRemoteDialog::GitRemoteDialog(const QString& folder, QWidget *parent) :
QDialog(parent),
ui(new Ui::GitRemoteDialog),
mFolder(folder),
mChooseMode(false)
2022-02-24 07:22:44 +08:00
{
ui->setupUi(this);
GitManager manager;
2022-02-24 11:07:37 +08:00
mRemotes = manager.listRemotes(folder);
ui->lstRemotes->addItems(mRemotes);
2022-02-24 07:22:44 +08:00
connect(pIconsManager, &IconsManager::actionIconsUpdated,
this, &GitRemoteDialog::onUpdateIcons);
2022-02-24 07:22:44 +08:00
ui->btnRemove->setEnabled(false);
ui->pnlProcess->setVisible(false);
ui->grpDetail->setEnabled(false);
connect(ui->lstRemotes->selectionModel(),
&QItemSelectionModel::selectionChanged,
this,
&GitRemoteDialog::onRemotesSelectionChanged);
}
GitRemoteDialog::~GitRemoteDialog()
{
delete ui;
}
QString GitRemoteDialog::chooseRemote()
{
mChooseMode = true;
ui->btnClose->setText(tr("Ok"));
if (exec()==QDialog::Accepted) {
if (ui->lstRemotes->selectedItems().count()>0)
return ui->lstRemotes->selectedItems()[0]->text();
}
return "";
}
void GitRemoteDialog::onUpdateIcons()
2022-02-24 07:22:44 +08:00
{
ui->btnAdd->setIcon(pIconsManager->getIcon(IconsManager::ACTION_MISC_ADD));
ui->btnRemove->setIcon(pIconsManager->getIcon(IconsManager::ACTION_MISC_REMOVE));
}
void GitRemoteDialog::onRemotesSelectionChanged()
{
bool enabled=(ui->lstRemotes->selectedItems().count()>0);
ui->btnRemove->setEnabled(enabled);
ui->pnlProcess->setVisible(enabled);
ui->grpDetail->setEnabled(enabled);
if (enabled) {
QString remoteName = ui->lstRemotes->selectedItems()[0]->text();
GitManager manager;
QString remoteURL = manager.getRemoteURL(mFolder,remoteName);
ui->txtName->setText(remoteName);
ui->txtURL->setText(remoteURL);
ui->btnProcess->setText(tr("Update"));
}
}
2022-02-24 11:07:37 +08:00
void GitRemoteDialog::checkDetails()
{
if (ui->txtURL->text().isEmpty()) {
ui->btnProcess->setEnabled(false);
return;
}
if (ui->txtName->text().isEmpty()) {
ui->btnProcess->setEnabled(false);
return;
}
if (ui->btnProcess->text() == tr("Add")) {
ui->btnProcess->setEnabled(!mRemotes.contains(ui->txtName->text()));
} else {
if (ui->lstRemotes->selectedItems().count()>0) {
QString remoteName = ui->lstRemotes->selectedItems()[0]->text();
ui->btnProcess->setEnabled(ui->txtName->text()==remoteName
|| !mRemotes.contains(ui->txtName->text()) );
} else
ui->btnProcess->setEnabled(false);
}
}
2022-02-24 07:22:44 +08:00
void GitRemoteDialog::on_btnAdd_clicked()
{
ui->grpDetail->setEnabled(true);
ui->pnlProcess->setVisible(true);
ui->btnProcess->setText(tr("Add"));
ui->btnRemove->setEnabled(false);
if (ui->lstRemotes->count()==0) {
ui->txtName->setText("origin");
ui->txtURL->setFocus();
} else
ui->txtName->setFocus();
2022-02-24 07:22:44 +08:00
}
void GitRemoteDialog::on_btnRemove_clicked()
{
if (ui->lstRemotes->selectedItems().count()>0) {
QString remoteName = ui->lstRemotes->selectedItems()[0]->text();
GitManager manager;
QString output;
if (!manager.removeRemote(mFolder,remoteName,output)) {
InfoMessageBox infoBox;
infoBox.showMessage(output);
} else {
refresh();
}
}
}
void GitRemoteDialog::refresh()
{
2022-02-24 11:07:37 +08:00
ui->txtName->setText("");
ui->txtURL->setText("");
2022-02-24 07:22:44 +08:00
ui->lstRemotes->clear();
GitManager manager;
2022-02-24 11:07:37 +08:00
mRemotes = manager.listRemotes(mFolder);
ui->lstRemotes->addItems(mRemotes);
2022-02-24 07:22:44 +08:00
ui->btnRemove->setEnabled(false);
ui->pnlProcess->setVisible(false);
ui->grpDetail->setEnabled(false);
}
2022-02-24 11:07:37 +08:00
void GitRemoteDialog::on_btnProcess_clicked()
{
if (ui->btnProcess->text() == tr("Add")) {
// add remote
QString remoteName = ui->txtName->text();
QString remoteURL = ui->txtURL->text();
GitManager manager;
QString output;
if (!manager.addRemote(mFolder, remoteName, remoteURL, output)) {
InfoMessageBox infoBox;
infoBox.showMessage(output);
} else {
refresh();
}
} else {
// update remote
if (ui->lstRemotes->selectedItems().count()<=0)
return;
QString oldName = ui->lstRemotes->selectedItems()[0]->text();
QString newName = ui->txtName->text();
QString url = ui->txtURL->text();
GitManager manager;
QString output;
if (!manager.setRemoteURL(mFolder,oldName,url,output)) {
InfoMessageBox infoBox;
infoBox.showMessage(output);
return;
}
if (oldName != newName) {
if (!manager.setRemoteURL(mFolder,oldName,url,output)) {
InfoMessageBox infoBox;
infoBox.showMessage(output);
return;
}
}
refresh();
}
}
void GitRemoteDialog::on_txtName_textChanged(const QString &/*arg1*/)
{
checkDetails();
}
void GitRemoteDialog::on_txtURL_textChanged(const QString & /*arg1*/)
{
checkDetails();
}
void GitRemoteDialog::on_btnClose_clicked()
{
accept();
}