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 93b652c
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 20 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ env:
artifact_path: artifact_path
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RabbitCommon_VERSION: v2.2.6
RabbitCommon_VERSION_PRE: v2.2.5

on:
push:
Expand Down Expand Up @@ -89,8 +90,7 @@ jobs:
echo "[:cn: 修改日志](https://github.com/KangLin/RabbitCommon/blob/${{env.RabbitCommon_VERSION}}/ChangeLog_zh_CN.md)" > ${{github.workspace}}/Note.md
echo "[:us: Change log](https://github.com/KangLin/RabbitCommon/blob/${{env.RabbitCommon_VERSION}}/ChangeLog.md)" >> ${{github.workspace}}/Note.md
echo "" >> ${{github.workspace}}/Note.md
PRE_TAG=`git tag --sort=-creatordate | head -n 1`
echo "Full Changelog: [${PRE_TAG}...${{env.RabbitCommon_VERSION}}](https://github.com/KangLin/RabbitCommon/compare/${PRE_TAG}...${{env.RabbitCommon_VERSION}})" >> ${{github.workspace}}/Note.md
echo "Full Changelog: [${{env.RabbitCommon_VERSION_PRE}}...${{env.RabbitCommon_VERSION}}](https://github.com/KangLin/RabbitCommon/compare/${{env.RabbitCommon_VERSION_PRE}}...${{env.RabbitCommon_VERSION}})" >> ${{github.workspace}}/Note.md
echo "" >> ${{github.workspace}}/Note.md
echo "File checksum:" >> ${{github.workspace}}/Note.md
for file in *
Expand All @@ -105,6 +105,8 @@ jobs:
rm -fr $file
fi
done
echo "${{github.workspace}}/Note.md"
cat ${{github.workspace}}/Note.md
- name: Upload To Github Release
if: ${{ startsWith(github.ref, 'refs/tags/') }}
Expand Down
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
11 changes: 9 additions & 2 deletions FileBrowser/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ int main(int argc, char *argv[])
RabbitCommon::CTools::Instance()->Init();
RabbitCommon::CTools::Instance()->InstallTranslator();
a.setApplicationDisplayName(QObject::tr("File browser"));
/*QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages(QLocale::TagSeparator::Underscore);

QStringList uiLanguages;
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
uiLanguages = QLocale::system().uiLanguages(QLocale::TagSeparator::Underscore);
#else
uiLanguages = QLocale::system().uiLanguages();
#endif
qDebug(log) << uiLanguages;
/*
QTranslator translator;
for (const QString &locale : uiLanguages) {
const QString baseName = "FileBrowser_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
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
1 change: 1 addition & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fi

sed -i "s/RabbitCommon_VERSION:.*/RabbitCommon_VERSION: ${VERSION}/g" ${SOURCE_DIR}/.github/workflows/msvc.yml
sed -i "s/RabbitCommon_VERSION:.*/RabbitCommon_VERSION: ${VERSION}/g" ${SOURCE_DIR}/.github/workflows/build.yml
sed -i "s/RabbitCommon_VERSION_PRE:.*/RabbitCommon_VERSION_PRE: ${PRE_TAG}/g" ${SOURCE_DIR}/.github/workflows/build.yml
sed -i "s/RabbitCommon_VERSION:.*/RabbitCommon_VERSION: ${VERSION}/g" ${SOURCE_DIR}/.github/workflows/doxygen.yml
sed -i "s/RabbitCommon_VERSION:.*/RabbitCommon_VERSION: ${VERSION}/g" ${SOURCE_DIR}/.github/workflows/mingw.yml
sed -i "s/RabbitCommon_VERSION:.*/RabbitCommon_VERSION: ${VERSION}/g" ${SOURCE_DIR}/.github/workflows/android.yml
Expand Down

0 comments on commit 93b652c

Please sign in to comment.