-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputwidget.cpp
108 lines (101 loc) · 3.24 KB
/
outputwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "outputwidget.h"
#include "ui_outputwidget.h"
#include <QFileInfo>
#include <QDesktopServices>
#include <QStandardPaths>
#include <QMessageBox>
#include <QDir>
#include <QProcess>
#include <QDirIterator>
OutputWidget::OutputWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::OutputWidget)
{
ui->setupUi(this);
connect(ui->openInExplorerPushButton, &QPushButton::clicked, this, &OutputWidget::requestOpenContainingDirectory);
connect(ui->openPushButton, &QPushButton::clicked, this, &OutputWidget::requestOpen);
}
OutputWidget::~OutputWidget()
{
delete ui;
}
QDateTime OutputWidget::getLatestModificationInDir(const QString& dirpath)
{
QDateTime result;
QDirIterator iter(dirpath, QDir::Files | QDir::Dirs, QDirIterator::Subdirectories);
while (iter.hasNext()) {
QFileInfo info(iter.next());
if (!result.isValid() || result < info.lastModified()) {
result = info.lastModified();
}
}
return result;
}
void OutputWidget::setData(const QString &fieldName, const QString &path)
{
this->fieldName = fieldName;
this->path = path;
ui->fieldNameLabel->setText(fieldName);
ui->pathLabel->setText(path);
ui->statusLabel->setText(tr(u8"初始"));
QFileInfo f(path);
if (f.exists()) {
// 如果是目录的话,我们使用当前时间
// 其实应该遍历目录下所有文件、取最新的时间的,这里就先不这样做了
if (f.isDir()) {
lastModified = QDateTime::currentDateTime();
} else {
lastModified = f.lastModified();
}
} else {
lastModified = QDateTime();
}
}
void OutputWidget::updateStatus()
{
QFileInfo info(path);
if (info.exists()) {
QDateTime curtime = info.lastModified();
if (info.isDir()) {
curtime = getLatestModificationInDir(path);
}
if (!lastModified.isValid() || lastModified < curtime) {
ui->statusLabel->setText(tr(u8"已生成"));
return;
}
ui->statusLabel->setText(tr(u8"未更新"));
} else {
ui->statusLabel->setText(tr(u8"尚未生成"));
}
}
void OutputWidget::requestOpenContainingDirectory()
{
QFileInfo info(path);
if (!info.exists()) {
return;
}
// 参考 qt-creator/src/plugins/coreplugin/fileutils.cpp FileUtils::showInGraphicalShell()
#ifdef Q_OS_WIN32
QString explorer = QStandardPaths::findExecutable("explorer.exe");
if (explorer.length() == 0) {
QMessageBox::warning(this, tr(u8"无法打开文件浏览器"), tr("PATH 中找不到 explorer.exe, 无法打开目录"));
return;
}
QStringList param;
param.append(QLatin1String("/select,"));
param.append(QDir::toNativeSeparators(info.canonicalFilePath()));
//qDebug() << "prog=" << explorer << ", args=" << param;
QProcess::startDetached("explorer.exe", param);
#elif defined(Q_OS_MAC)
QProcess::startDetached("/usr/bin/open", {"-R", info.canonicalFilePath()});
#else
QMessageBox::warning(this, tr(u8"暂不支持"), tr("暂不支持在当前系统下打开目录"));
#endif
}
void OutputWidget::requestOpen()
{
QFileInfo info(path);
if (info.exists()) {
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
}
}