#include "FileManageWidget.h" #include "ui_FileManageWidget.h" #include #include "SysPara.h" #include #include #include #include #include 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(m_focusedListView->parent()); auto palette = backgroundPanel->palette(); palette.setColor(backgroundPanel->backgroundRole(), QColor("white")); backgroundPanel->setPalette(palette); m_focusedListView = dynamic_cast(watched); backgroundPanel = dynamic_cast(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; } }