first commit

This commit is contained in:
2026-04-29 14:10:10 +08:00
commit b28556d43d
1558 changed files with 241386 additions and 0 deletions
+204
View File
@@ -0,0 +1,204 @@
#include "FileManageWidget.h"
#include "ui_FileManageWidget.h"
#include <QDebug>
#include "SysPara.h"
#include <QDir>
#include <QStringList>
#include <QFileInfo>
#include <QMessageBox>
#include <QTextStream>
FileManageWidget::FileManageWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::FileManageWidget)
{
ui->setupUi(this);
// 安装事件过滤器用于判断当前焦点位于系统文件列表还是U盘文件列表,高亮获得焦点的列表
ui->systemFileListView->installEventFilter(this);
ui->udiskFileListView->installEventFilter(this);
// 将各自列表所查找的根目录存放到各自的动态属性中
// updateFileListView方法将会从属性中获取对应的查找根路径用于更新文件列表
ui->systemFileListView->setProperty("searchPath", DOC_ROOT_DIR);
ui->udiskFileListView->setProperty("searchPath", UDISK_DOC_ROOT_DIR);
updateUDiskDevList();
m_focusedListView = ui->systemFileListView;
m_focusedListView->setFocus();
onFilterCBoxSelectChanged(ui->filterCBox->currentIndex());
}
FileManageWidget::~FileManageWidget()
{
ui->systemFileListView->removeEventFilter(this);
ui->udiskFileListView->removeEventFilter(this);
delete ui;
}
bool FileManageWidget::eventFilter(QObject *watched, QEvent *event)
{
// 仅处理系统文件列表或U盘文件列表
if(watched != ui->systemFileListView && watched != ui->udiskFileListView) {
return false;
}
if(event->type() != QEvent::FocusIn) {
return false;
}
// 为获得焦点的列表视图改变边框色,高亮显示
if(m_focusedListView == watched) {
return false;
}
QWidget * backgroundPanel = dynamic_cast<QWidget *>(m_focusedListView->parent());
auto palette = backgroundPanel->palette();
palette.setColor(backgroundPanel->backgroundRole(), QColor("white"));
backgroundPanel->setPalette(palette);
m_focusedListView = dynamic_cast<QListWidget *>(watched);
backgroundPanel = dynamic_cast<QWidget *>(watched->parent());
palette = backgroundPanel->palette();
palette.setColor(backgroundPanel->backgroundRole(), QColor(175, 218, 223));
backgroundPanel->setPalette(palette);
return false;
}
void FileManageWidget::onFilterCBoxSelectChanged(int i)
{
if(i < JobFile || i > All) {
qWarning() << "无效的过滤类型编号: " << i;
i = All;
ui->filterCBox->setCurrentIndex(i);
}
m_currFilterType = i;
updateFileListView(ui->systemFileListView);
updateFileListView(ui->udiskFileListView);
}
void FileManageWidget::onExportBtnClicked()
{
copyFiles(ui->systemFileListView, ui->udiskFileListView);
updateFileListView(ui->udiskFileListView);
}
void FileManageWidget::onImportBtnClicked()
{
copyFiles(ui->udiskFileListView, ui->systemFileListView);
updateFileListView(ui->systemFileListView);
}
void FileManageWidget::onSelectAllBtnClicked()
{
if(!m_focusedListView)
return;
m_focusedListView->selectAll();
}
void FileManageWidget::onCancelAllBtnClicked()
{
if(!m_focusedListView)
return;
m_focusedListView->clearSelection();
}
void FileManageWidget::onDelSelectedBtnClicked()
{
if(!m_focusedListView)
return;
auto items = m_focusedListView->selectedItems();
for(auto item : items) {
auto filename = item->data(AbsPathRole).toString();
QString cmd = QString("rm %1").arg(filename);
int ret = system(cmd.toLatin1().constData());
qInfo() << "Command: " << cmd << ", result: " << ret;
}
updateFileListView(m_focusedListView);
}
void FileManageWidget::updateUDiskDevList()
{
QDir dir(UDISK_DOC_ROOT_DIR);
if(!dir.exists()) {
ui->udiskSelectBox->clear();
return;
}
if(ui->udiskSelectBox->currentText() != tr("U盘")){
ui->udiskSelectBox->clear();
ui->udiskSelectBox->addItem(tr("U盘"));
ui->udiskSelectBox->setCurrentIndex(0);
}
updateFileListView(ui->udiskFileListView);
}
int FileManageWidget::updateFileListView(QListWidget* listView)
{
QString searchPath = listView->property("searchPath").toString();
// 判断目录是否存在
QDir dir(searchPath);
if(!dir.exists()) {
qCritical() << "Cannot open path: " << searchPath;
return -1;
}
// 根据过滤类型设置过滤器字符串
QStringList filters;
switch(m_currFilterType) {
case JobFile:
filters << "*.xml";
break;
case Image:
filters << "*.png" << "*.jpeg" << "*.gif" << "*.jpg" << "*.bmp"<< "*.BMP";
break;
case Font:
filters << "*.ttf" << "*.otf";
break;
case All:
default:
filters << "*.xml" << "*.png" << "*.jpeg" << "*.gif" << "*.jpg" << "*.bmp"<< "*.BMP" << "*.html" << "*.ttf" << "*.otf";
break;
}
// 获取过滤后的文件集合
auto fileInfoList = dir.entryInfoList(filters, QDir::Files);
// 清理视图并为其重新添加文件列表
listView->clear();
for(const auto& fileInfo : fileInfoList) {
auto absPath = fileInfo.absoluteFilePath();
QListWidgetItem *item = new QListWidgetItem();
item->setText(fileInfo.fileName());
item->setData(AbsPathRole, absPath);
listView->addItem(item);
}
qInfo() << "Scan path: " << searchPath << ", files: " << fileInfoList.size() << ", target: " << listView->objectName();
return fileInfoList.size();
}
void FileManageWidget::copyFiles(QListWidget *from, QListWidget *to)
{
// 获取目的地目录
auto destPath = to->property("searchPath").toString();
auto items = from->selectedItems();
for(auto item : items) {
auto filename = item->data(AbsPathRole).toString();
QString cmd = QString("cp %1 %2").arg(filename).arg(destPath);
auto ret = system(cmd.toLatin1().constData());
qInfo() << "Command: " << cmd << ", result: " << ret;
}
}
+53
View File
@@ -0,0 +1,53 @@
#ifndef FILEMANAGEWIDGET_H
#define FILEMANAGEWIDGET_H
#include <QWidget>
#include <QListWidget>
namespace Ui {
class FileManageWidget;
}
class FileManageWidget : public QWidget
{
Q_OBJECT
public:
explicit FileManageWidget(QWidget *parent = 0);
~FileManageWidget();
enum FilterType {
JobFile,
Image,
Font,
All
};
protected:
virtual bool eventFilter(QObject *watched, QEvent *event) override;
private slots:
void onFilterCBoxSelectChanged(int i);
void onExportBtnClicked();
void onImportBtnClicked();
void onSelectAllBtnClicked();
void onCancelAllBtnClicked();
void onDelSelectedBtnClicked();
void updateUDiskDevList();
private:
int updateFileListView(QListWidget* listView);
void copyFiles(QListWidget* from, QListWidget* to);
Ui::FileManageWidget *ui;
int m_currFilterType;
QListWidget* m_focusedListView = nullptr;
// 用于文件显示列表中,存放每个文件名对应的绝对路径所用的数据键
static constexpr int AbsPathRole = Qt::UserRole + 1;
};
#endif // FILEMANAGEWIDGET_H
+547
View File
@@ -0,0 +1,547 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>FileManageWidget</class>
<widget class="QWidget" name="FileManageWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="titlePanel" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>40</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>40</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color:#4E679F;</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="topMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="styleSheet">
<string notr="true">QLabel {
font-size: 30px;
color: #efefef;
}</string>
</property>
<property name="text">
<string>管理器</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QGridLayout" name="gridLayout_3">
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0" colspan="3">
<widget class="QWidget" name="widget_4" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>50</number>
</property>
<item>
<widget class="QPushButton" name="exportBtn">
<property name="text">
<string>导出到U盘</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="importBtn">
<property name="text">
<string>从U盘导入</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="selectAllBtn">
<property name="text">
<string>全选</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cancelAllBtn">
<property name="text">
<string>取消全选</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="delSelectedBtn">
<property name="text">
<string>删除已选</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QWidget" name="widget_5" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>过滤器</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="filterCBox">
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<item>
<property name="text">
<string>文件</string>
</property>
</item>
<item>
<property name="text">
<string>图标</string>
</property>
</item>
<item>
<property name="text">
<string>字体</string>
</property>
</item>
<item>
<property name="text">
<string>全部</string>
</property>
</item>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item row="2" column="0" colspan="3">
<widget class="QWidget" name="widget_6" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>硬盘</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>设备</string>
</property>
</widget>
</item>
<item>
<widget class="UDiskComboBox" name="udiskSelectBox">
<property name="minimumSize">
<size>
<width>220</width>
<height>0</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QWidget" name="systemFileListPanel" native="true">
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="leftMargin">
<number>2</number>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="rightMargin">
<number>2</number>
</property>
<property name="bottomMargin">
<number>2</number>
</property>
<property name="spacing">
<number>6</number>
</property>
<item row="0" column="0">
<widget class="QListWidget" name="systemFileListView">
<property name="styleSheet">
<string notr="true">QListWidget {
border: 1px;
padding: 5px;
}
QListWidget::item {
padding: 3px;
}</string>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::MultiSelection</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="1">
<widget class="QWidget" name="udiskFileListPanel" native="true">
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>2</number>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="rightMargin">
<number>2</number>
</property>
<property name="bottomMargin">
<number>2</number>
</property>
<item row="0" column="0">
<widget class="QListWidget" name="udiskFileListView">
<property name="styleSheet">
<string notr="true">QListWidget {
border: 1px;
padding: 5px;
}
QListWidget::item {
padding: 3px;
}</string>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::MultiSelection</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QWidget" name="widget_2" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>45</height>
</size>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="closeBtn">
<property name="minimumSize">
<size>
<width>100</width>
<height>40</height>
</size>
</property>
<property name="styleSheet">
<string notr="true"> QPushButton#closeBtn {
background-color: #E6F7FF;
border: 1px solid #87CEEB;
border-radius: 4px;
font-size: 24px;
color: #228B22;
}
QPushButton#closeBtn:hover {
background-color: #D1E9FF;
}
QPushButton#closeBtn:pressed {
background-color: #BAE7FF;
}</string>
</property>
<property name="text">
<string>✕</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>UDiskComboBox</class>
<extends>QComboBox</extends>
<header>fileManage/UDiskComboBox.h</header>
<slots>
<signal>popupShowed()</signal>
<slot>onUDiskCBoxPopupShowed()</slot>
</slots>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>closeBtn</sender>
<signal>clicked()</signal>
<receiver>FileManageWidget</receiver>
<slot>hide()</slot>
<hints>
<hint type="sourcelabel">
<x>743</x>
<y>569</y>
</hint>
<hint type="destinationlabel">
<x>866</x>
<y>565</y>
</hint>
</hints>
</connection>
<connection>
<sender>exportBtn</sender>
<signal>clicked()</signal>
<receiver>FileManageWidget</receiver>
<slot>onExportBtnClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>97</x>
<y>74</y>
</hint>
<hint type="destinationlabel">
<x>-61</x>
<y>86</y>
</hint>
</hints>
</connection>
<connection>
<sender>importBtn</sender>
<signal>clicked()</signal>
<receiver>FileManageWidget</receiver>
<slot>onImportBtnClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>194</x>
<y>74</y>
</hint>
<hint type="destinationlabel">
<x>-32</x>
<y>107</y>
</hint>
</hints>
</connection>
<connection>
<sender>selectAllBtn</sender>
<signal>clicked()</signal>
<receiver>FileManageWidget</receiver>
<slot>onSelectAllBtnClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>285</x>
<y>77</y>
</hint>
<hint type="destinationlabel">
<x>-49</x>
<y>137</y>
</hint>
</hints>
</connection>
<connection>
<sender>cancelAllBtn</sender>
<signal>clicked()</signal>
<receiver>FileManageWidget</receiver>
<slot>onCancelAllBtnClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>358</x>
<y>78</y>
</hint>
<hint type="destinationlabel">
<x>-35</x>
<y>159</y>
</hint>
</hints>
</connection>
<connection>
<sender>delSelectedBtn</sender>
<signal>clicked()</signal>
<receiver>FileManageWidget</receiver>
<slot>onDelSelectedBtnClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>443</x>
<y>85</y>
</hint>
<hint type="destinationlabel">
<x>-22</x>
<y>181</y>
</hint>
</hints>
</connection>
<connection>
<sender>udiskSelectBox</sender>
<signal>popupShowed()</signal>
<receiver>FileManageWidget</receiver>
<slot>updateUDiskDevList()</slot>
<hints>
<hint type="sourcelabel">
<x>620</x>
<y>167</y>
</hint>
<hint type="destinationlabel">
<x>877</x>
<y>205</y>
</hint>
</hints>
</connection>
<connection>
<sender>filterCBox</sender>
<signal>currentIndexChanged(int)</signal>
<receiver>FileManageWidget</receiver>
<slot>onFilterCBoxSelectChanged(int)</slot>
<hints>
<hint type="sourcelabel">
<x>410</x>
<y>120</y>
</hint>
<hint type="destinationlabel">
<x>850</x>
<y>219</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>onFilterCBoxSelectChanged(int)</slot>
<slot>onExportBtnClicked()</slot>
<slot>onImportBtnClicked()</slot>
<slot>onSelectAllBtnClicked()</slot>
<slot>onCancelAllBtnClicked()</slot>
<slot>onDelSelectedBtnClicked()</slot>
<slot>updateUDiskDevList()</slot>
</slots>
</ui>
+14
View File
@@ -0,0 +1,14 @@
#include "UDiskComboBox.h"
UDiskComboBox::UDiskComboBox(QWidget *parent)
:QComboBox(parent)
{
}
void UDiskComboBox::showPopup()
{
emit popupShowed();
QComboBox::showPopup();
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef UDISKCOMBOBOX_H
#define UDISKCOMBOBOX_H
#include <QComboBox>
class UDiskComboBox : public QComboBox
{
Q_OBJECT
public:
explicit UDiskComboBox(QWidget* parent = nullptr);
protected:
virtual void showPopup() override;
signals:
void popupShowed();
};
#endif // UDISKCOMBOBOX_H