Skip to content

Commit

Permalink
FileBrowser: add undo
Browse files Browse the repository at this point in the history
  • Loading branch information
KangLin committed Sep 24, 2024
1 parent 3e9e097 commit 17d6fac
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 31 deletions.
2 changes: 1 addition & 1 deletion FileBrowser/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ MainWindow::MainWindow(QWidget *parent)
Q_ASSERT(check);
check = connect(pFB, &CFileBrowser::sigChanged, this,
[&](const QString& szDir, bool bDir){
qDebug(log) << "CFileBrowser::sigChanged" << szDir << bDir;
//qDebug(log) << "CFileBrowser::sigChanged" << szDir << bDir;
});
Q_ASSERT(check);

Expand Down
6 changes: 0 additions & 6 deletions FileBrowser/android/res/values-zh-CN/strings.xml

This file was deleted.

2 changes: 2 additions & 0 deletions Src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if(WITH_GUI)
Log/DlgFilter.h
Log/DlgEdit.h
FileBrowser/FileBroserTreeView.h
FileBrowser/UndoCommand.h
)
list(APPEND RABBITCOMMON_SOURCE_FILES
RabbitRecentMenu.cpp
Expand All @@ -77,6 +78,7 @@ if(WITH_GUI)
DockFolderBrowser/DockFolderBrowser.cpp
FileBrowser/FileBrowser.cpp
FileBrowser/FileBroserTreeView.cpp
FileBrowser/UndoCommand.cpp
Log/DockDebugLog.cpp
Log/DlgFilter.cpp
Log/DlgEdit.cpp
Expand Down
76 changes: 53 additions & 23 deletions Src/FileBrowser/FileBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
#include <QCheckBox>
#include "FileBrowser.h"
#include "RabbitCommonDir.h"
#include "UndoCommand.h"
#include <FileBroserTreeView.h>

static Q_LOGGING_CATEGORY(log, "RabbitCommon.Browser.File")
CFileBrowser::CFileBrowser(QWidget *parent)
: QWidget{parent}
, m_pSpliter(nullptr)
, m_pUndoStack(nullptr)
, m_pModel(nullptr)
, m_pTree(nullptr)
, m_pList(nullptr)
Expand Down Expand Up @@ -58,51 +61,74 @@ CFileBrowser::CFileBrowser(QWidget *parent)
setGeometry(rect);

QVBoxLayout* pLayout = new QVBoxLayout(this);
m_pSpliter = new QSplitter(this);
m_pTree = new CFileBroserTreeView(m_pSpliter);
m_pTable = new QTableView(m_pSpliter);
//m_pList = new QListView(pSpliter);
m_pTextEdit = new QTextEdit(m_pSpliter);
if(!pLayout) return;

do {
m_pModel = new QFileSystemModel(this);
if(!m_pModel) return;
if(!m_pModel) break;;
// m_pModel->setReadOnly(false);
/*m_pModel->setFilter(QDir::AllDirs | QDir::Drives
| QDir::NoDotAndDotDot);*/
if(!pLayout) break;
setLayout(pLayout);
m_pSpliter = new QSplitter(this);
if(!m_pSpliter) break;

m_pTree = new CFileBroserTreeView(m_pSpliter);
m_pTable = new QTableView(m_pSpliter);
//m_pList = new QListView(pSpliter);
m_pTextEdit = new QTextEdit(m_pSpliter);

QString szTitle;
QToolBar* pToolBar = new QToolBar();
QToolBar* pToolBar = new QToolBar(this);
szTitle = tr("Close");
QAction* pAction = pToolBar->addAction(QIcon::fromTheme("window-close"),
szTitle, this, &CFileBrowser::close);
pAction->setStatusTip(szTitle);

m_pUndoStack = new QUndoStack(this);
if(!m_pUndoStack) break;
#ifndef QT_NO_ACTION
QList<QAction*> lstUndo;
pAction = m_pUndoStack->createUndoAction(this);
pAction->setIcon(QIcon::fromTheme("edit-undo"));
lstUndo << pAction;
pAction = m_pUndoStack->createRedoAction(this);
pAction->setIcon(QIcon::fromTheme("edit-redo"));
lstUndo << pAction;
pToolBar->addActions(lstUndo);
#endif

szTitle = tr("New folder");
pAction = pToolBar->addAction(QIcon::fromTheme("folder-new"), szTitle,
this, [&](){
QModelIndex index = m_pTree->currentIndex();
index = m_pModel->mkdir(index, tr("NewFolder"));
if(!index.isValid())
qCritical(log) << "New folder fail";
});
pAction = pToolBar->addAction(
QIcon::fromTheme("folder-new"), szTitle,
this, [&](){
QModelIndex index = m_pTree->currentIndex();
if(m_pModel->isDir(index))
m_pUndoStack->push(new CNewFolder(m_pModel->filePath(index)
+ QDir::separator() + tr("NewFolder")));
});
pAction->setStatusTip(szTitle);

szTitle = tr("Delete folder");
pAction = pToolBar->addAction(
QIcon::fromTheme("edit-delete"), szTitle,
this, [&](){
QString szDir;
QModelIndex index = m_pTree->currentIndex();
szDir = m_pModel->data(index).toString();
bool bRet = m_pModel->rmdir(index);
if(bRet)
qDebug(log) << "Delete folder:" << szDir;
else
qCritical(log) << "Delete folder fail" << szDir;
szDir = m_pModel->filePath(index);
m_pUndoStack->push(new CDeleteFolder(szDir));
});
pAction->setStatusTip(szTitle);

szTitle = tr("Update");
pAction = pToolBar->addAction(
QIcon::fromTheme("system-software-update"), szTitle,
this, [&](){
QModelIndex index = m_pTree->currentIndex();
m_pTree->update(index);
});
pAction->setStatusTip(szTitle);

szTitle = tr("Option");
QToolButton* pButtonOption = new QToolButton(pToolBar);
pButtonOption->setIcon(QIcon::fromTheme("emblem-system"));
Expand Down Expand Up @@ -283,18 +309,22 @@ CFileBrowser::CFileBrowser(QWidget *parent)
return;
} while(0);

if(m_pSpliter)
delete m_pSpliter;
if(m_pUndoStack)
delete m_pUndoStack;
if(m_pModel)
delete m_pModel;
if(pLayout)
delete pLayout;
if(m_pSpliter)
delete m_pSpliter;
if(m_pTree)
delete m_pTree;
if(m_pList)
delete m_pList;
if(m_pTable)
delete m_pTable;
if(m_pTextEdit)
delete m_pTextEdit;
}

CFileBrowser::~CFileBrowser()
Expand Down
3 changes: 3 additions & 0 deletions Src/FileBrowser/FileBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <QTextEdit>
#include <QSplitter>
#include <QAction>
#include <QUndoCommand>
#include <QUndoStack>
#include "rabbitcommon_export.h"

class CFileBroserTreeView;
Expand Down Expand Up @@ -41,6 +43,7 @@ class RABBITCOMMON_EXPORT CFileBrowser : public QWidget

private:
QSplitter* m_pSpliter;
QUndoStack* m_pUndoStack;
QFileSystemModel *m_pModel;
CFileBroserTreeView* m_pTree;
QListView* m_pList;
Expand Down
52 changes: 52 additions & 0 deletions Src/FileBrowser/UndoCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "UndoCommand.h"
#include <QLoggingCategory>

static Q_LOGGING_CATEGORY(log, "RabbitCommon.Browser.File.Undo")
CNewFolder::CNewFolder(QString szPath)
: QUndoCommand()
, m_szPath(szPath)
{}

void CNewFolder::undo()
{
bool bRet = false;
QDir d(m_szPath);
if(d.exists())
bRet = d.rmdir(m_szPath);
qDebug(log) << "CNewFolder::undo()" << bRet << m_szPath;
}

void CNewFolder::redo()
{
bool bRet = false;
QDir d(m_szPath);
if(!d.exists())
bRet = d.mkdir(m_szPath);
setObsolete(!bRet);
qDebug(log) << "CNewFolder::redo()" << bRet << m_szPath;
}

CDeleteFolder::CDeleteFolder(QString szPath)
: QUndoCommand()
, m_szPath(szPath)
{}

void CDeleteFolder::undo()
{
bool bRet = false;
QDir d(m_szPath);
if(!d.exists())
bRet = d.mkdir(m_szPath);
setObsolete(!bRet);
qDebug(log) << "CDeleteFolder::undo()" << bRet << m_szPath;
}

void CDeleteFolder::redo()
{
bool bRet = false;
QDir d(m_szPath);
if(d.exists())
bRet = d.rmdir(m_szPath);
setObsolete(!bRet);
qDebug(log) << "CDeleteFolder::redo()" << bRet << m_szPath;
}
29 changes: 29 additions & 0 deletions Src/FileBrowser/UndoCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef UNDOCOMMAND_H
#define UNDOCOMMAND_H

#include <QUndoCommand>
#include <QFileSystemModel>

class CNewFolder : public QUndoCommand
{
public:
CNewFolder(QString szPath);
public:
virtual void undo() override;
virtual void redo() override;
private:
QString m_szPath;
};

class CDeleteFolder : public QUndoCommand
{
public:
CDeleteFolder(QString szPath);
public:
virtual void undo() override;
virtual void redo() override;
private:
QString m_szPath;
};

#endif // UNDOCOMMAND_H
2 changes: 2 additions & 0 deletions Src/RabbitCommon.pri
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ DEFINES *= QT_MESSAGELOGCONTEXT
$$PWD/DockFolderBrowser/DockFolderBrowser.cpp \
$$PWD/FileBrowser/FileBrowser.cpp \
$$PWD/FileBrowser/FileBroserTreeView.cpp \
$$PWD/FileBrowser/UndoCommand.cpp \
$$PWD/Log/DockDebugLog.cpp \
$$PWD/Log/DlgFilter.cpp \
$$PWD/Log/DlgEdit.cpp
Expand All @@ -59,6 +60,7 @@ DEFINES *= QT_MESSAGELOGCONTEXT
$$PWD/Style/Style.h \
$$PWD/Log/DockDebugLog.h \
$$PWD/FileBrowser/FileBroserTreeView.h \
$$PWD/FileBrowser/UndoCommand.h \
$$PWD/Log/DlgFilter.h \
$$PWD/Log/DlgEdit.h
INCLUDEPATH += $$PWD/Style $$PWD/DockFolderBrowser $$PWD/FileBrowser
Expand Down
2 changes: 1 addition & 1 deletion deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ sed -i "s/ \"version\":[[:blank:]]*\"v\?[0-9]\+\.[0-9]\+\.[0-9]\+\"/
sed -i "s/rabbitcommon (.*)/rabbitcommon (${DEBIAN_VERSION})/g" ${SOURCE_DIR}/debian/changelog
sed -i "s/Version=.*/Version=${DEBIAN_VERSION}/g" ${SOURCE_DIR}/share/org.Rabbit.RabbitCommon.desktop
sed -i "s/[0-9]\+\.[0-9]\+\.[0-9]\+/${DEBIAN_VERSION}/g" ${SOURCE_DIR}/README*.md
#sed -i "s/[0-9]\+\.[0-9]\+\.[0-9]\+/${DEBIAN_VERSION}/g" ${SOURCE_DIR}/Tests/android/AndroidManifest.xml
sed -i "s/versionName=\"[0-9]\+\.[0-9]\+\.[0-9]\+\"/versionName=\"${DEBIAN_VERSION}\"/g" ${SOURCE_DIR}/FileBrowser/android/AndroidManifest.xml
sed -i "s/RabbitCommon_VERSION:.*/RabbitCommon_VERSION: ${DEBIAN_VERSION}/g" ${SOURCE_DIR}/.github/workflows/ubuntu.yml
sed -i "s/RabbitCommon_VERSION:.*/RabbitCommon_VERSION: ${DEBIAN_VERSION}/g" ${SOURCE_DIR}/.github/workflows/qmake.yml
if [ -f ${SOURCE_DIR}/vcpkg.json ]; then
Expand Down

0 comments on commit 17d6fac

Please sign in to comment.