112 lines
4.2 KiB
C++
112 lines
4.2 KiB
C++
#include "File_Tool.h"
|
|
Psc::File_Tool::File_Tool(const QString& title, Mode mode, QWidget* parent) : 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);
|
|
set_help_text(browse_btn, "点击打开文件\n支持手动输入路径");
|
|
clear_btn = new QPushButton("清空", this);
|
|
clear_btn->setFixedWidth(48);
|
|
set_help_text(clear_btn, "点击清空当前文件内容");
|
|
backup_clear_btn = new QPushButton("备份清空", this);
|
|
backup_clear_btn->setFixedWidth(72);
|
|
set_help_text(backup_clear_btn, "点击清空,并且备份当前文件");
|
|
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(clear_btn);
|
|
layout->addWidget(backup_clear_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(clear_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;
|
|
}
|
|
QFile file(p);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
Message::show(Message::error, "无法清空文件!",
|
|
this, 2000, Message::Top_Right);
|
|
return;
|
|
}
|
|
file.close();
|
|
Message::show(Message::success, "文件已清空",
|
|
this, 1500, Message::Top_Right);
|
|
});
|
|
// 备份并清空
|
|
connect(backup_clear_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;
|
|
}
|
|
QString backup_path =
|
|
info.absolutePath() + "/" +
|
|
info.completeBaseName() + "_" +
|
|
QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss") +
|
|
"." + info.suffix();
|
|
if (!QFile::copy(p, backup_path)) {
|
|
Message::show(Message::error, "备份失败!",
|
|
this, 2000, Message::Top_Right);
|
|
return;
|
|
}
|
|
QFile file(p);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
Message::show(Message::error, "清空失败!",
|
|
this, 2000, Message::Top_Right);
|
|
return;
|
|
}
|
|
file.close();
|
|
Message::show(Message::success, "已备份并清空",
|
|
this, 1500, Message::Top_Right);
|
|
});
|
|
connect(path_edit, &QLineEdit::editingFinished, this,
|
|
[this]() {
|
|
emit path_changed(path_edit->text());
|
|
});
|
|
}
|
|
void Psc::File_Tool::set_title(const QString& t) {
|
|
title_label->setText(t);
|
|
title_label->setVisible(!t.isEmpty());
|
|
}
|
|
QString Psc::File_Tool::path() const {
|
|
return path_edit->text();
|
|
}
|
|
void Psc::File_Tool::set_path(const QString& p) {
|
|
path_edit->setText(p);
|
|
emit path_changed(p);
|
|
}
|