Skip to content

Commit

Permalink
CFileBrowser: add signal interface
Browse files Browse the repository at this point in the history
  • Loading branch information
KangLin committed Sep 24, 2024
1 parent b348bf4 commit 599d7f4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 16 deletions.
13 changes: 10 additions & 3 deletions FileBrowser/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ MainWindow::MainWindow(QWidget *parent)
#endif

CFileBrowser* pFB = new CFileBrowser(this);
check = connect(pFB, SIGNAL(destroyed(QObject*)), this, SLOT(close()));
Q_ASSERT(check);
setCentralWidget(pFB);
setWindowIcon(pFB->windowIcon());
setWindowTitle(pFB->windowTitle());
check = connect(pFB, SIGNAL(destroyed(QObject*)), this, SLOT(close()));
Q_ASSERT(check);
check = connect(pFB, &CFileBrowser::sigChanged, this,
[&](const QString& szDir, bool bDir){
qDebug(log) << "CFileBrowser::sigChanged" << szDir << bDir;
});
Q_ASSERT(check);

QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
QSettings::IniFormat);
Expand All @@ -39,7 +44,8 @@ MainWindow::MainWindow(QWidget *parent)
if(!state.isEmpty())
restoreState(state);

bool bStatusBar = set.value("MainWindow/Status/Bar", !statusBar()->isHidden()).toBool();
bool bStatusBar = set.value("MainWindow/Status/Bar",
!statusBar()->isHidden()).toBool();
ui->actionStatus_S->setChecked(bStatusBar);
statusBar()->setVisible(bStatusBar);
}
Expand All @@ -53,6 +59,7 @@ MainWindow::~MainWindow()
set.setValue("MainWindow/Status/Bar", !statusBar()->isHidden());

delete ui;
qDebug(log) << "MainWindow::~MainWindow()";
}

void MainWindow::on_actionStatus_S_triggered(bool checked)
Expand Down
2 changes: 2 additions & 0 deletions Src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ if(WITH_GUI)
Log/DockDebugLog.h
Log/DlgFilter.h
Log/DlgEdit.h
FileBrowser/FileBroserTreeView.h
)
list(APPEND RABBITCOMMON_SOURCE_FILES
RabbitRecentMenu.cpp
Expand All @@ -75,6 +76,7 @@ if(WITH_GUI)
TitleBar.cpp
DockFolderBrowser/DockFolderBrowser.cpp
FileBrowser/FileBrowser.cpp
FileBrowser/FileBroserTreeView.cpp
Log/DockDebugLog.cpp
Log/DlgFilter.cpp
Log/DlgEdit.cpp
Expand Down
25 changes: 25 additions & 0 deletions Src/FileBrowser/FileBroserTreeView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <QLoggingCategory>
#include <QFileSystemModel>
#include "FileBroserTreeView.h"

static Q_LOGGING_CATEGORY(log, "RabbitCommon.Browser.File.TreeView")
CFileBroserTreeView::CFileBroserTreeView(QWidget *parent) : QTreeView(parent)
{}

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

void CFileBroserTreeView::selectionChanged(
const QItemSelection &selected, const QItemSelection &deselected)
{
}

void CFileBroserTreeView::currentChanged(
const QModelIndex &current, const QModelIndex &previous)
{
QFileSystemModel* pModel = qobject_cast<QFileSystemModel*>(model());
if(!pModel) return;
emit sigChanged(pModel->filePath(current), pModel->isDir(current));
}
23 changes: 23 additions & 0 deletions Src/FileBrowser/FileBroserTreeView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef FILEBROSERTREEVIEW_H
#define FILEBROSERTREEVIEW_H

#include <QObject>
#include <QTreeView>

class CFileBroserTreeView : public QTreeView
{
Q_OBJECT
public:
CFileBroserTreeView(QWidget *parent = nullptr);
virtual ~CFileBroserTreeView();

Q_SIGNALS:
void sigChanged(const QString &szItem, bool bDir);

// QAbstractItemView interface
protected slots:
virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override;
virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
};

#endif // FILEBROSERTREEVIEW_H
33 changes: 22 additions & 11 deletions Src/FileBrowser/FileBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
#include <QCheckBox>
#include "FileBrowser.h"
#include "RabbitCommonDir.h"
#include <FileBroserTreeView.h>

static Q_LOGGING_CATEGORY(log, "RabbitCommon.Browser.File")
CFileBrowser::CFileBrowser(QWidget *parent)
: QWidget{parent}
, m_pModel(new QFileSystemModel(this))
, m_pModel(nullptr)
, m_pTree(nullptr)
, m_pList(nullptr)
, m_pTable(nullptr)
Expand Down Expand Up @@ -56,15 +57,16 @@ CFileBrowser::CFileBrowser(QWidget *parent)
rect.setHeight(nHeight);
setGeometry(rect);

QVBoxLayout* pLayout = new QVBoxLayout();
QVBoxLayout* pLayout = new QVBoxLayout(this);
m_pSpliter = new QSplitter(this);
m_pTree = new QTreeView(m_pSpliter);
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;
// m_pModel->setReadOnly(false);
/*m_pModel->setFilter(QDir::AllDirs | QDir::Drives
Expand Down Expand Up @@ -142,7 +144,10 @@ CFileBrowser::CFileBrowser(QWidget *parent)
m_pAssociated->setStatusTip(szTitle);
m_pAssociated->setToolTip(szTitle);
m_pAssociated->setCheckable(true);
m_pAssociated->setChecked(set.value("FileBrowser/SystemAssociatedProgram", m_pAssociated->isChecked()).toBool());
m_pAssociated->setChecked(
set.value(
"FileBrowser/SystemAssociatedProgram",
m_pAssociated->isChecked()).toBool());

szTitle = tr("Horizontal");
m_pOrientation = pMenuOption->addAction(
Expand Down Expand Up @@ -219,15 +224,19 @@ CFileBrowser::CFileBrowser(QWidget *parent)
m_pTree, &QTreeView::doubleClicked,
this, [&](const QModelIndex &index) {
if (m_pModel) {
QString szFile = m_pModel->filePath(index);
emit sigDoubleClicked(szFile, m_pModel->isDir(index));
if(m_pModel->isDir(index)) {
return;
}
m_pTable->hide();
QString szFile = m_pModel->filePath(index);
ShowFile(szFile);
}
});
Q_ASSERT(check);
check = connect(m_pTree, &CFileBroserTreeView::sigChanged,
this, &CFileBrowser::sigChanged);
Q_ASSERT(check);
}

if(m_pList) {
Expand All @@ -245,11 +254,12 @@ CFileBrowser::CFileBrowser(QWidget *parent)
m_pTable, &QTableView::clicked,
this, [&](const QModelIndex &index) {
if (m_pModel) {
QString szDir = m_pModel->filePath(index);
m_pTree->setCurrentIndex(index);
m_pTree->expand(index);
if(m_pModel->isDir(index)) {
QString dir = m_pModel->filePath(index);
m_pModel->setRootPath(dir);
m_pTable->setRootIndex(m_pModel->index(dir));
m_pModel->setRootPath(szDir);
m_pTable->setRootIndex(m_pModel->index(szDir));
if(!m_pTextEdit->isHidden())
m_pTextEdit->hide();
}
Expand All @@ -260,6 +270,7 @@ CFileBrowser::CFileBrowser(QWidget *parent)
this, [&](const QModelIndex &index){
QString szFile = m_pModel->filePath(index);
ShowFile(szFile);
emit sigDoubleClicked(szFile, m_pModel->isDir(index));
});
Q_ASSERT(check);
}
Expand All @@ -271,8 +282,7 @@ CFileBrowser::CFileBrowser(QWidget *parent)
setRootPath("");
return;
} while(0);
if(m_pModel)
delete m_pModel;

if(m_pModel)
delete m_pModel;
if(pLayout)
Expand Down Expand Up @@ -354,7 +364,8 @@ CDlgFileBrowser::CDlgFileBrowser(QWidget *parent) : QDialog(parent)
setWindowIcon(m_pFileBrowser->windowIcon());
setWindowTitle(m_pFileBrowser->windowTitle());

bool check = connect(m_pFileBrowser, SIGNAL(destroyed(QObject*)), this, SLOT(close()));
bool check = connect(m_pFileBrowser, SIGNAL(destroyed(QObject*)),
this, SLOT(close()));
Q_ASSERT(check);

QHBoxLayout *layout = new QHBoxLayout(this);
Expand Down
5 changes: 3 additions & 2 deletions Src/FileBrowser/FileBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QAction>
#include "rabbitcommon_export.h"

class CFileBroserTreeView;
class RABBITCOMMON_EXPORT CFileBrowser : public QWidget
{
Q_OBJECT
Expand All @@ -26,12 +27,12 @@ class RABBITCOMMON_EXPORT CFileBrowser : public QWidget
QString rootPath() const;

Q_SIGNALS:
//! emit when Double clicked
//! \param szItem: item of double clicked
//! \param bDir:
//! - true: item is directory
//! - false: item is folder
void sigDoubleClicked(const QString &szItem, bool bDir);
void sigChanged(const QString &szItem, bool bDir);

private:
QString readFile(const QString &filePath);
Expand All @@ -40,7 +41,7 @@ class RABBITCOMMON_EXPORT CFileBrowser : public QWidget
private:
QSplitter* m_pSpliter;
QFileSystemModel *m_pModel;
QTreeView* m_pTree;
CFileBroserTreeView* m_pTree;
QListView* m_pList;
QTableView* m_pTable;
QTextEdit* m_pTextEdit;
Expand Down

0 comments on commit 599d7f4

Please sign in to comment.