work save: commit

This commit is contained in:
Roy Qu 2022-02-15 17:22:44 +08:00
parent 463779aa0d
commit b5acf15467
9 changed files with 101 additions and 21 deletions

View File

@ -226,7 +226,7 @@ void Editor::saveFile(QString filename) {
this->lines()->saveToFile(file,mEncodingOption, this->lines()->saveToFile(file,mEncodingOption,
pSettings->editor().useUTF8ByDefault()? ENCODING_UTF8 : QTextCodec::codecForLocale()->name(), pSettings->editor().useUTF8ByDefault()? ENCODING_UTF8 : QTextCodec::codecForLocale()->name(),
mFileEncoding); mFileEncoding);
pMainWindow->updateForEncodingInfo(); emit fileSaved(filename, mInProject);
} }
void Editor::convertToEncoding(const QByteArray &encoding) void Editor::convertToEncoding(const QByteArray &encoding)

View File

@ -225,6 +225,7 @@ public:
void tab() override; void tab() override;
signals: signals:
void renamed(const QString& oldName, const QString& newName, bool firstSave); void renamed(const QString& oldName, const QString& newName, bool firstSave);
void fileSaved(const QString& filename, bool inProject);
private slots: private slots:
void onStatusChanged(SynStatusChanges changes); void onStatusChanged(SynStatusChanges changes);
void onGutterClicked(Qt::MouseButton button, int x, int y, int line); void onGutterClicked(Qt::MouseButton button, int x, int y, int line);

View File

@ -62,6 +62,8 @@ Editor* EditorList::newEditor(const QString& filename, const QByteArray& encodin
e->setInProject(true); e->setInProject(true);
} }
} }
connect(e,&Editor::fileSaved,
pMainWindow, &MainWindow::onFileSaved);
return e; return e;
} }

View File

@ -40,6 +40,7 @@
#include "widgets/newclassdialog.h" #include "widgets/newclassdialog.h"
#include "widgets/newheaderdialog.h" #include "widgets/newheaderdialog.h"
#include "vcs/gitmanager.h" #include "vcs/gitmanager.h"
#include "vcs/gitrepository.h"
#include <QCloseEvent> #include <QCloseEvent>
#include <QComboBox> #include <QComboBox>
@ -744,6 +745,22 @@ void MainWindow::updateDPI()
applySettings(); applySettings();
} }
void MainWindow::onFileSaved(const QString &path, bool inProject)
{
qDebug()<<path<<inProject<<mFileSystemModel.rootPath();
if (inProject && mProject) {
mProject->model()->beginUpdate();
mProject->model()->endUpdate();
}
QModelIndex index = mFileSystemModel.index(path);
if (index.isValid()) {
mFileSystemModelIconProvider.update();
mFileSystemModel.setIconProvider(&mFileSystemModelIconProvider);
ui->treeFiles->update(index);
}
pMainWindow->updateForEncodingInfo();
}
void MainWindow::updateAppTitle() void MainWindow::updateAppTitle()
{ {
QString appName=tr("Red Panda C++"); QString appName=tr("Red Panda C++");
@ -5591,7 +5608,7 @@ void MainWindow::updateVCSActions()
{ {
bool hasRepository = false; bool hasRepository = false;
bool shouldEnable = false; bool shouldEnable = false;
if (ui->projectView->isVisible()) { if (ui->projectView->isVisible() && mProject) {
GitManager vcsManager; GitManager vcsManager;
QString branch; QString branch;
hasRepository = vcsManager.hasRepository(mProject->folder(),branch); hasRepository = vcsManager.hasRepository(mProject->folder(),branch);
@ -5747,6 +5764,7 @@ void MainWindow::setFilesViewRoot(const QString &path)
mFileSystemModel.setRootPath(path); mFileSystemModel.setRootPath(path);
ui->treeFiles->setRootIndex(mFileSystemModel.index(path)); ui->treeFiles->setRootIndex(mFileSystemModel.index(path));
pSettings->environment().setCurrentFolder(path); pSettings->environment().setCurrentFolder(path);
QDir::setCurrent(path);
int pos = ui->cbFilesPath->findText(path); int pos = ui->cbFilesPath->findText(path);
if (pos<0) { if (pos<0) {
ui->cbFilesPath->addItem(mFileSystemModel.iconProvider()->icon(QFileIconProvider::Folder),path); ui->cbFilesPath->addItem(mFileSystemModel.iconProvider()->icon(QFileIconProvider::Folder),path);
@ -6541,9 +6559,11 @@ void MainWindow::on_actionGit_Create_Repository_triggered()
if (pos>=0) { if (pos>=0) {
ui->cbFilesPath->setItemIcon(pos, pIconsManager->getIcon(IconsManager::FILESYSTEM_GIT)); ui->cbFilesPath->setItemIcon(pos, pIconsManager->getIcon(IconsManager::FILESYSTEM_GIT));
} }
} else if (ui->projectView->isVisible()) { } else if (ui->projectView->isVisible() && mProject) {
GitManager vcsManager; GitManager vcsManager;
vcsManager.createRepository(mProject->folder()); vcsManager.createRepository(mProject->folder());
}
if (mProject) {
mProject->model()->beginUpdate(); mProject->model()->beginUpdate();
mProject->model()->endUpdate(); mProject->model()->endUpdate();
} }
@ -6562,7 +6582,7 @@ void MainWindow::on_actionGit_Add_Files_triggered()
//update icons in files view //update icons in files view
mFileSystemModelIconProvider.update(); mFileSystemModelIconProvider.update();
mFileSystemModel.setIconProvider(&mFileSystemModelIconProvider); mFileSystemModel.setIconProvider(&mFileSystemModelIconProvider);
} else if (ui->projectView->isVisible()) { } else if (ui->projectView->isVisible() && mProject) {
GitManager vcsManager; GitManager vcsManager;
QModelIndexList indices = ui->projectView->selectionModel()->selectedRows(); QModelIndexList indices = ui->projectView->selectionModel()->selectedRows();
foreach (const QModelIndex index,indices) { foreach (const QModelIndex index,indices) {
@ -6577,12 +6597,41 @@ void MainWindow::on_actionGit_Add_Files_triggered()
vcsManager.add(info.absolutePath(),info.fileName()); vcsManager.add(info.absolutePath(),info.fileName());
} }
} }
}
//update icons in project view //update icons in project view
if (mProject) {
mProject->model()->beginUpdate(); mProject->model()->beginUpdate();
mProject->model()->endUpdate(); mProject->model()->endUpdate();
}
//update icons in files view too //update icons in files view too
mFileSystemModelIconProvider.update(); mFileSystemModelIconProvider.update();
mFileSystemModel.setIconProvider(&mFileSystemModelIconProvider); mFileSystemModel.setIconProvider(&mFileSystemModelIconProvider);
} }
void MainWindow::on_actionGit_Commit_triggered()
{
QString folder;
if (ui->treeFiles->isVisible()) {
folder = pSettings->environment().currentFolder();
} else if (ui->projectView->isVisible() && mProject) {
folder = mProject->folder();
}
if (folder.isEmpty())
return;
QString message = QInputDialog::getText(this,tr("Commit Message"),"Commit Message:");
if (message.isEmpty())
return;
GitRepository repository(folder);
repository.commit(message,true);
//update project view
if (mProject) {
mProject->model()->beginUpdate();
mProject->model()->endUpdate();
}
//update files view
mFileSystemModelIconProvider.update();
mFileSystemModel.setIconProvider(&mFileSystemModelIconProvider);
} }

View File

@ -218,6 +218,7 @@ public slots:
void onTodoParseFinished(); void onTodoParseFinished();
void setActiveBreakpoint(QString FileName, int Line, bool setFocus); void setActiveBreakpoint(QString FileName, int Line, bool setFocus);
void updateDPI(); void updateDPI();
void onFileSaved(const QString& path, bool inProject);
private: private:
void prepareProjectForCompile(); void prepareProjectForCompile();
@ -260,7 +261,6 @@ private slots:
void onAutoSaveTimeout(); void onAutoSaveTimeout();
void onFileChanged(const QString& path); void onFileChanged(const QString& path);
void onFilesViewPathChanged(); void onFilesViewPathChanged();
void onWatchViewContextMenu(const QPoint& pos); void onWatchViewContextMenu(const QPoint& pos);
void onBookmarkContextMenu(const QPoint& pos); void onBookmarkContextMenu(const QPoint& pos);
void onTableIssuesContextMenu(const QPoint& pos); void onTableIssuesContextMenu(const QPoint& pos);
@ -582,6 +582,8 @@ private slots:
void on_actionGit_Add_Files_triggered(); void on_actionGit_Add_Files_triggered();
void on_actionGit_Commit_triggered();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
EditorList *mEditorList; EditorList *mEditorList;

View File

@ -52,6 +52,14 @@ bool GitManager::hasRepository(const QString &folder, QString& currentBranch)
return result; return result;
} }
QString GitManager::rootFolder(const QString &folder)
{
QStringList args;
args.append("rev-parse");
args.append("--show-toplevel");
return runGit(folder,args).trimmed();
}
bool GitManager::isFileInRepository(const QFileInfo& fileInfo) bool GitManager::isFileInRepository(const QFileInfo& fileInfo)
{ {
QStringList args; QStringList args;

View File

@ -22,6 +22,8 @@ public:
void createRepository(const QString& folder); void createRepository(const QString& folder);
bool hasRepository(const QString& folder, QString& currentBranch); bool hasRepository(const QString& folder, QString& currentBranch);
QString rootFolder(const QString& folder);
bool isFileInRepository(const QFileInfo& fileInfo); bool isFileInRepository(const QFileInfo& fileInfo);
bool isFileStaged(const QFileInfo& fileInfo); bool isFileStaged(const QFileInfo& fileInfo);
bool isFileChanged(const QFileInfo& fileInfo); bool isFileChanged(const QFileInfo& fileInfo);

View File

@ -16,12 +16,12 @@ GitRepository::~GitRepository()
const QString &GitRepository::folder() const const QString &GitRepository::folder() const
{ {
return mFolder; return mRealFolder;
} }
void GitRepository::createRepository() void GitRepository::createRepository()
{ {
mManager->createRepository(mFolder); mManager->createRepository(mRealFolder);
} }
bool GitRepository::hasRepository(QString& currentBranch) bool GitRepository::hasRepository(QString& currentBranch)
@ -62,24 +62,32 @@ void GitRepository::clone(const QString &url)
mManager->clone(mFolder,url); mManager->clone(mFolder,url);
} }
void GitRepository::commit(const QString &message) void GitRepository::commit(const QString &message, bool autoAdd)
{ {
mManager->commit(mFolder, message); if (autoAdd) {
convertFilesListToSet(mManager->listChangedFiles(mRealFolder),mChangedFiles);
foreach(const QString& s, mChangedFiles) {
QFileInfo info(s);
mManager->add(info.absolutePath(),info.fileName());
}
}
mManager->commit(mRealFolder, message);
} }
void GitRepository::revert() void GitRepository::revert()
{ {
mManager->revert(mFolder); mManager->revert(mRealFolder);
} }
void GitRepository::reset(const QString &commit, GitResetStrategy strategy) void GitRepository::reset(const QString &commit, GitResetStrategy strategy)
{ {
mManager->reset(mFolder,commit,strategy); mManager->reset(mRealFolder,commit,strategy);
} }
void GitRepository::setFolder(const QString &newFolder) void GitRepository::setFolder(const QString &newFolder)
{ {
mFolder = newFolder; mFolder = newFolder;
mRealFolder = mManager->rootFolder(mFolder);
update(); update();
} }
@ -92,18 +100,23 @@ void GitRepository::update()
mChangedFiles.clear(); mChangedFiles.clear();
mStagedFiles.clear(); mStagedFiles.clear();
} else { } else {
mInRepository = mManager->hasRepository(mFolder,mBranch); mInRepository = mManager->hasRepository(mRealFolder,mBranch);
convertFilesListToSet(mManager->listFiles(mFolder),mFilesInRepositories); convertFilesListToSet(mManager->listFiles(mRealFolder),mFilesInRepositories);
convertFilesListToSet(mManager->listChangedFiles(mFolder),mChangedFiles); convertFilesListToSet(mManager->listChangedFiles(mRealFolder),mChangedFiles);
convertFilesListToSet(mManager->listStagedFiles(mFolder),mStagedFiles); convertFilesListToSet(mManager->listStagedFiles(mRealFolder),mStagedFiles);
} }
} }
const QString &GitRepository::realFolder() const
{
return mRealFolder;
}
void GitRepository::convertFilesListToSet(const QStringList &filesList, QSet<QString> &set) void GitRepository::convertFilesListToSet(const QStringList &filesList, QSet<QString> &set)
{ {
set.clear(); set.clear();
foreach (const QString& s, filesList) { foreach (const QString& s, filesList) {
set.insert(includeTrailingPathDelimiter(mFolder)+s); set.insert(includeTrailingPathDelimiter(mRealFolder)+s);
} }
} }

View File

@ -53,7 +53,7 @@ public:
QSet<QString> listFiles(bool refresh); QSet<QString> listFiles(bool refresh);
void clone(const QString& url); void clone(const QString& url);
void commit(const QString& message); void commit(const QString& message, bool autoAdd=true);
void revert(); void revert();
void reset(const QString& commit, GitResetStrategy strategy); void reset(const QString& commit, GitResetStrategy strategy);
@ -61,8 +61,11 @@ public:
void setFolder(const QString &newFolder); void setFolder(const QString &newFolder);
void update(); void update();
const QString &realFolder() const;
signals: signals:
private: private:
QString mRealFolder;
QString mFolder; QString mFolder;
bool mInRepository; bool mInRepository;
QString mBranch; QString mBranch;