Files
Radio/module/radio/FileDialog/FileExplorer.h
T
2026-06-16 13:38:08 +08:00

134 lines
4.0 KiB
C++

#include <QApplication>
#include <QDesktopServices>
#include <QTreeView>
#include <QFileSystemModel>
#include <QVBoxLayout>
#include <QMainWindow>
#include <QLabel>
#include <QUrl>
#include <QDebug>
#include <QDesktopWidget>
#include <QProxyStyle>
#include <QTreeView>
#include <QFileSystemModel>
#include <qheaderview.h>
#include <QVBoxLayout>
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QMouseEvent>
#include <QPushButton>
#include <QScreen>
#include "CustomFileDelegate.h" // 包含自定义代理类
// class CustomStyle : public QProxyStyle {
// public:
// explicit CustomStyle(QStyle *baseStyle = nullptr) : QProxyStyle(baseStyle) {}
//
// void drawPrimitive(QStyle::PrimitiveElement element,
// const QStyleOption *option,
// QPainter *painter,
// const QWidget *widget = nullptr) const override {
// if (element == QStyle::PE_IndicatorBranch) {
// // 自定义绘制箭头
// QRect rect = option->rect;
// QPoint center = rect.center();
// if (option->state & QStyle::State_Open) {
// // 绘制展开的箭头(向下)
// QPoint points[3] = {
// QPoint(center.x() - 5, center.y() - 2),
// QPoint(center.x() + 5, center.y() - 2),
// QPoint(center.x(), center.y() + 5)
// };
// painter->setBrush(Qt::black);
// painter->drawPolygon(points, 3);
// } else {
// // 绘制收起的箭头(向右)
// QPoint points[3] = {
// QPoint(center.x() - 2, center.y() - 5),
// QPoint(center.x() - 2, center.y() + 5),
// QPoint(center.x() + 5, center.y())
// };
// painter->setBrush(Qt::black);
// painter->drawPolygon(points, 3);
// }
// } else {
// // 调用默认绘制逻辑
// QProxyStyle::drawPrimitive(element, option, painter, widget);
// }
// }
// };
//
class CustomFileDelegate;
class FileExplorer : public QWidget {
Q_OBJECT
public:
QTreeView *treeView;
QFileSystemModel *model;
QVBoxLayout *layout;
QLineEdit* showRootDir;
QFileInfo focus;
QLabel *showFocusFile;
QPushButton *confirmButton, *cancelButton;
int height = 60;
CustomFileDelegate *it;
void showCenter() {
QScreen *screen = QGuiApplication::primaryScreen();
QRect screenRect = screen->availableGeometry();
QRect windowRect = frameGeometry();
int x = (screenRect.width() - windowRect.width()) / 2;
int y = (screenRect.height() - windowRect.height()) / 2;
move(x, y);
show();
}
void resizeEvent(QResizeEvent* event) override {
}
protected:
void paintEvent(QPaintEvent* event) override {
QPainter painter(this);
painter.fillRect(rect(), Qt::white);
painter.end();
}
public:
void changeRoot(QString dir_path) {
dir_path.replace("\\", "/");
QFileInfo fileInfo(dir_path); // 用 QFileInfo 检测路径
if (fileInfo.isDir()) {
//qDebug() << "dir " << dir_path;
} else if (fileInfo.isFile()) {
//qDebug() << "file " << dir_path;
focus = fileInfo;
showFocusFile->setText(focus.absolutePath());
dir_path = fileInfo.dir().absolutePath();
} else {
//qDebug() << "error " << dir_path;
showRootDir->setText(model->rootPath());
return;
}
if (!dir_path.endsWith("/")) {
dir_path.append("/");
}
showRootDir->setText(dir_path);
model->setRootPath(dir_path);
treeView->setRootIndex(model->index(dir_path));
model->setHeaderData(0,Qt::Horizontal,"文件名称");
model->setHeaderData(1,Qt::Horizontal,"文件大小");
treeView->setColumnWidth(0, 500);
}
FileExplorer();
};