Files
Renderive/Widget/Qt/File_Tool2.h
T
2026-07-29 12:30:40 +08:00

120 lines
4.1 KiB
C++

#pragma once
#include <QDateTime>
#include <QDesktopServices>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QUrl>
#include <QWidget>
#include "Message.h"
namespace Psc {
class File_Tool2 : public QWidget {
Q_OBJECT
public:
enum class Mode { OpenFile,
SaveFile,
Directory };
explicit File_Tool2(const QString& title = {},
Mode mode = Mode::OpenFile,
QWidget* parent = nullptr) : QWidget(parent), mode_(mode) {
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
title_label = new QLabel(title, this);
path_edit = new QLineEdit(this);
path_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
browse_btn = new QPushButton("选择", this);
browse_btn->setFixedWidth(48);
open_file_btn = new QPushButton("文件", this);
open_file_btn->setFixedWidth(40);
open_dir_btn = new QPushButton("目录", this);
open_dir_btn->setFixedWidth(40);
auto layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(4);
if (!title.isEmpty())
layout->addWidget(title_label);
layout->addWidget(path_edit);
layout->addWidget(browse_btn);
layout->addWidget(open_file_btn);
layout->addWidget(open_dir_btn);
// 选择路径
connect(browse_btn, &QPushButton::clicked, this, [this]() {
QString selected;
switch (mode_) {
case Mode::OpenFile:
selected = QFileDialog::getOpenFileName(this, "Select File");
break;
case Mode::SaveFile:
selected = QFileDialog::getSaveFileName(this, "Save File");
break;
case Mode::Directory:
selected = QFileDialog::getExistingDirectory(this, "Select Directory");
break;
}
if (!selected.isEmpty()) {
path_edit->setText(selected);
emit path_changed(selected);
}
});
// 打开文件
connect(open_file_btn, &QPushButton::clicked, this, [this]() {
const QString p = path_edit->text();
if (p.isEmpty())
return;
QFileInfo info(p);
if (!info.exists() || !info.isFile()) {
Message::show(Message::error, "文件不存在!",
this, 2000, Message::Top_Right);
return;
}
QDesktopServices::openUrl(
QUrl::fromLocalFile(info.absoluteFilePath()));
});
// 打开目录
connect(open_dir_btn, &QPushButton::clicked, this, [this]() {
const QString p = path_edit->text();
if (p.isEmpty())
return;
QFileInfo info(p);
if (!info.exists()) {
Message::show(Message::error, "目录不存在!",
this, 2000, Message::Top_Right);
return;
}
QString dir_path = info.isDir()
? info.absoluteFilePath()
: info.absolutePath();
QDesktopServices::openUrl(
QUrl::fromLocalFile(dir_path));
});
connect(path_edit, &QLineEdit::editingFinished, this,
[this]() {
emit path_changed(path_edit->text());
});
}
void set_title(const QString& t) {
title_label->setText(t);
title_label->setVisible(!t.isEmpty());
}
[[nodiscard]] QString path() const {
return path_edit->text();
}
void set_path(const QString& p) {
path_edit->setText(p);
emit path_changed(p);
}
signals:
void path_changed(const QString&);
private:
Mode mode_;
QLabel* title_label{};
QLineEdit* path_edit{};
QPushButton* browse_btn{};
QPushButton* open_file_btn{};
QPushButton* open_dir_btn{};
};
} // namespace Psc