Skip to content

Commit

Permalink
FileBrowser: modify undo
Browse files Browse the repository at this point in the history
  • Loading branch information
KangLin committed Nov 26, 2024
1 parent d1f5d8f commit 81e4e78
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 14 deletions.
66 changes: 52 additions & 14 deletions Src/FileBrowser/FileBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QSettings>
#include <QPushButton>
#include <QScrollBar>
#include <QToolButton>
#include <QMenu>
#include <QWidgetAction>
Expand Down Expand Up @@ -77,6 +78,7 @@ CFileBrowser::CFileBrowser(QWidget *parent)
if(!m_pSpliter) break;

m_pTree = new CFileBroserTreeView(m_pSpliter);
m_pTree->setAutoScroll(true);
m_pTable = new QTableView(m_pSpliter);
//m_pList = new QListView(pSpliter);
m_pTextEdit = new QTextEdit(m_pSpliter);
Expand All @@ -101,6 +103,7 @@ CFileBrowser::CFileBrowser(QWidget *parent)
pToolBar->addActions(lstUndo);
#endif

/*
szTitle = tr("New folder");
pAction = pToolBar->addAction(
QIcon::fromTheme("folder-new"), szTitle,
Expand Down Expand Up @@ -131,6 +134,31 @@ CFileBrowser::CFileBrowser(QWidget *parent)
m_pTree->update(index);
});
pAction->setStatusTip(szTitle);
*/

pToolBar->addAction(QIcon::fromTheme("go-up"), tr("Up"),
this, [&](){
QString szDir;
QModelIndex index = m_pTree->currentIndex();
szDir = m_pModel->filePath(index);
QDir d(szDir);
if(d.exists()) {
szDir = szDir + QDir::separator() + "..";
} else {
QFileInfo fi(szDir);
szDir = fi.absolutePath();
}
qDebug(log) << "Dir" << szDir;
auto i = m_pModel->index(szDir);
if(i.isValid())
{
m_pUndoStack->push(new CChange(i, this));
m_pTree->setCurrentIndex(i);
slotClicked(i);
m_pTree->doItemsLayout();
m_pTree->scrollTo(i);
}
});

szTitle = tr("Option");
QToolButton* pButtonOption = new QToolButton(pToolBar);
Expand Down Expand Up @@ -236,6 +264,7 @@ CFileBrowser::CFileBrowser(QWidget *parent)
m_pTree->header()->hideSection(3);
m_pSpliter->addWidget(m_pTree);

/*
if(m_pTable) {
check = connect(m_pTree, &QTreeView::clicked,
m_pTable, &QTableView::setRootIndex);
Expand All @@ -245,22 +274,12 @@ CFileBrowser::CFileBrowser(QWidget *parent)
check = connect(m_pTree, &QTreeView::clicked,
m_pList, &QListView::setRootIndex);
Q_ASSERT(check);
}
}//*/
check = connect(
m_pTree, &QTreeView::clicked,
this, [&](const QModelIndex &index) {
if (m_pModel) {
if(m_pModel->isDir(index)) {
m_pTable->show();
m_pTextEdit->hide();
return;
}
m_pTable->hide();
#if defined(Q_OS_ANDROID)
QString szFile = m_pModel->filePath(index);
ShowFile(szFile);
#endif
}
this, [&](const QModelIndex& index){
m_pUndoStack->push(new CChange(index, this));
slotClicked(index);
});
Q_ASSERT(check);
check = connect(
Expand Down Expand Up @@ -297,6 +316,7 @@ CFileBrowser::CFileBrowser(QWidget *parent)
m_pTable, &QTableView::clicked,
this, [&](const QModelIndex &index) {
if (m_pModel) {
m_pUndoStack->push(new CChange(index, this));
QString szDir = m_pModel->filePath(index);
m_pTree->setCurrentIndex(index);
m_pTree->expand(index);
Expand Down Expand Up @@ -417,6 +437,24 @@ int CFileBrowser::ShowFile(const QString &szFile)
return 0;
}

void CFileBrowser::slotClicked(const QModelIndex &index)
{
qDebug(log) << "CFileBrowser::slotClicked" << index;
if (m_pModel) {
if(m_pModel->isDir(index)) {
m_pTable->setRootIndex(index);
m_pTable->show();
m_pTextEdit->hide();
return;
}
m_pTable->hide();
#if defined(Q_OS_ANDROID)
QString szFile = m_pModel->filePath(index);
ShowFile(szFile);
#endif
}
}

CDlgFileBrowser::CDlgFileBrowser(QWidget *parent) : QDialog(parent)
,m_pFileBrowser(nullptr)
{
Expand Down
6 changes: 6 additions & 0 deletions Src/FileBrowser/FileBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "rabbitcommon_export.h"

class CFileBroserTreeView;
class CChange;
/*!
* \brief File browser
* \ingroup API
Expand All @@ -40,6 +41,9 @@ class RABBITCOMMON_EXPORT CFileBrowser : public QWidget
void sigDoubleClicked(const QString &szItem, bool bDir);
void sigChanged(const QString &szItem, bool bDir);

public Q_SLOTS:
void slotClicked(const QModelIndex &index);

private:
QString readFile(const QString &filePath);
int ShowFile(const QString &szFile);
Expand All @@ -57,6 +61,8 @@ class RABBITCOMMON_EXPORT CFileBrowser : public QWidget
QAction* m_pHiddenFile;
QAction* m_pAssociated;
QAction* m_pOrientation;

friend class CChange;
};

/*!
Expand Down
33 changes: 33 additions & 0 deletions Src/FileBrowser/UndoCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,36 @@ void CDeleteFolder::redo()
setObsolete(!bRet);
qDebug(log) << "CDeleteFolder::redo()" << bRet << m_szPath;
}

CChange::CChange(const QModelIndex &index, CFileBrowser *pThis)
: QUndoCommand()
, m_pThis(pThis)
, m_Index(index)
{
QFileSystemModel* model = m_pThis->m_pModel;
if(model)
setText(model->filePath(index));
}

CChange::~CChange()
{
qDebug(log) << "CChange::~CChange()";
}

void CChange::undo()
{
qDebug(log) << "CChange::undo()" << m_Index;
m_pThis->m_pTree->setCurrentIndex(m_Index);
m_pThis->slotClicked(m_Index);
m_pThis->m_pTree->doItemsLayout();
m_pThis->m_pTree->scrollTo(m_Index);
}

void CChange::redo()
{
qDebug(log) << "CChange::redo()" << m_Index;
m_pThis->slotClicked(m_Index);
m_pThis->m_pTree->setCurrentIndex(m_Index);
m_pThis->m_pTree->doItemsLayout();
m_pThis->m_pTree->scrollTo(m_Index);
}
15 changes: 15 additions & 0 deletions Src/FileBrowser/UndoCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <QUndoCommand>
#include <QFileSystemModel>
#include "FileBroserTreeView.h"
#include "FileBrowser.h"

class CNewFolder : public QUndoCommand
{
Expand All @@ -29,4 +31,17 @@ class CDeleteFolder : public QUndoCommand
QString m_szPath;
};

class CChange: public QUndoCommand
{
public:
CChange(const QModelIndex &index, CFileBrowser* pThis);
virtual ~CChange();
virtual void undo() override;
virtual void redo() override;

private:
CFileBrowser* m_pThis;
QModelIndex m_Index;
};

#endif // UNDOCOMMAND_H

0 comments on commit 81e4e78

Please sign in to comment.