-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
220 lines (185 loc) · 7.42 KB
/
Copy pathmainwindow.cpp
File metadata and controls
220 lines (185 loc) · 7.42 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QTime>
#include <QMenu>
#include <QProcess>
#include <QFileInfo>
#include <QDesktopServices>
Worker::Worker(const std::string& directory, bool isRecursive, QObject* parent)
: QObject(parent), directory(directory), isRecursive(isRecursive)
{
connect(&scanner, &VideoScanner::progressUpdated, this, &Worker::on_progressUpdate);
}
void Worker::process()
{
std::vector<VideoInfo> videoList = scanner.TraverseVideos(directory, isRecursive);
emit videoListReady(videoList);
}
void Worker::on_progressUpdate(double progress) {
emit updatProg(progress);
}
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
qRegisterMetaType<VideoInfo>("VideoInfo");
qRegisterMetaType<std::vector<VideoInfo>>("std::vector<VideoInfo>");
setupUI();
}
void MainWindow::setupUI()
{
setWindowTitle("Bulk recursive retrieval of video info");
setWindowIcon(QIcon(":/vi.ico"));
QWidget* centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QHBoxLayout* topLayout = new QHBoxLayout();
QLabel* pathLabel = new QLabel("Path:", this);
pathLineEdit = new QLineEdit(this);
browseButton = new QPushButton("Browse", this);
topLayout->addWidget(pathLabel);
topLayout->addWidget(pathLineEdit);
topLayout->addWidget(browseButton);
tableView = new QTableView(this);
model = new QStandardItemModel(this);
headers << "Full Path" << "Size (MB)" << "Bitrate (Kbps)" << "Frame Rate (fps)" << "Codec" << "Ratio" << "Duration";
model->setHorizontalHeaderLabels(headers);
tableView->setModel(model);
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
connect(tableView->horizontalHeader(), &QHeaderView::sectionResized, this, [this]() {
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
});
tableView->setSortingEnabled(true);
tableView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tableView, &QTableView::customContextMenuRequested, this, &MainWindow::showContextMenu);
processButton = new QPushButton("Process", this);
recursiveCheckBox = new QCheckBox("Recursive", this);
progressBar = new QProgressBar(this);
progressBar->setRange(0, 100);
progressBar->setValue(0);
QHBoxLayout* bottomLayout = new QHBoxLayout();
bottomLayout->addWidget(processButton);
bottomLayout->addWidget(recursiveCheckBox);
QVBoxLayout* mainLayout = new QVBoxLayout(centralWidget);
mainLayout->addLayout(topLayout);
mainLayout->addWidget(tableView);
mainLayout->addLayout(bottomLayout);
mainLayout->addWidget(progressBar);
connect(browseButton, &QPushButton::clicked, this, &MainWindow::on_browsePath);
connect(processButton, &QPushButton::clicked, this, &MainWindow::on_processVideos);
}
void MainWindow::on_browsePath()
{
QString directory = QFileDialog::getExistingDirectory(this, tr("Select Directory"));
pathLineEdit->setText(directory);
}
void MainWindow::on_processVideos()
{
QString directory = pathLineEdit->text();
bool isRecursive = recursiveCheckBox->isChecked();
if (directory.isEmpty())
{
QMessageBox::warning(this, "Warning", "Please select a directory.");
return;
}
browseButton->setEnabled(false);
processButton->setEnabled(false);
runScannerThread(directory.toStdString(), isRecursive);
}
void MainWindow::populateTable(const std::vector<VideoInfo>& videoList)
{
model->setRowCount(0);
model->setHorizontalHeaderLabels(headers);
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
for (const VideoInfo& video : videoList)
{
QList<QStandardItem*> row;
row << new QStandardItem(QString::fromStdString(video.path));
row << new NumericStandardItem(QString::number(video.size));
row << new NumericStandardItem(QString::number(video.bitrate));
row << new NumericStandardItem(QString::number(video.frameRate));
row << new QStandardItem(QString::fromStdString(video.codec));
row << new NumericStandardItem(QString::number(video.ratio));
row << new QStandardItem(QTime::fromMSecsSinceStartOfDay(video.duration * 1000).toString());
model->appendRow(row);
}
tableView->setModel(model);
tableView->sortByColumn(1, Qt::DescendingOrder);
browseButton->setEnabled(true);
processButton->setEnabled(true);
}
void MainWindow::on_progressChanged(double progress)
{
progressBar->setValue(progress * 100);
}
void MainWindow::runScannerThread(const std::string& directory, bool isRecursive) {
QThread* scannerThread = new QThread;
Worker* worker = new Worker(directory, isRecursive);
worker->moveToThread(scannerThread);
connect(scannerThread, &QThread::started, worker, &Worker::process);
connect(worker, &Worker::videoListReady, this, &MainWindow::populateTable); // Receive signals from Worker threads
connect(worker, &Worker::updatProg, this, &MainWindow::on_progressChanged); // Receive progress update signals
connect(worker, &Worker::videoListReady, scannerThread, &QThread::quit);
connect(worker, &Worker::videoListReady, worker, &QObject::deleteLater);
connect(scannerThread, &QThread::finished, scannerThread, &QObject::deleteLater);
scannerThread->start();
}
// Context Menu Slot Implementation
void MainWindow::showContextMenu(const QPoint& pos)
{
QModelIndex index = tableView->indexAt(pos);
if (!index.isValid())
return;
int fullPathColumn = 0;
QModelIndex fullPathIndex = index.sibling(index.row(), fullPathColumn);
selectedFilePath = model->data(fullPathIndex).toString();
QMenu contextMenu(this);
QAction* openAction = contextMenu.addAction("Open in Explorer");
connect(openAction, &QAction::triggered, this, &MainWindow::openInExplorer);
contextMenu.exec(tableView->viewport()->mapToGlobal(pos));
}
void MainWindow::openInExplorer()
{
if (selectedFilePath.isEmpty())
return;
QFileInfo fileInfo(selectedFilePath);
QString absolutePath = fileInfo.absoluteFilePath();
QString parentDir = fileInfo.absolutePath();
#ifdef Q_OS_WIN
QStringList params;
params << "/select," << QDir::toNativeSeparators(absolutePath);
QProcess::startDetached("explorer", params);
#elif defined(Q_OS_MAC)
QStringList args;
args << "-R" << absolutePath;
QProcess::startDetached("open", args);
#elif defined(Q_OS_LINUX)
// Try different file managers
QStringList fileManagers = { "nautilus", "xdg-open", "dolphin", "thunar", "pcmanfm" };
bool success = false;
for (const QString& fm : fileManagers)
{
if (QProcess::execute(QString("which %1").arg(fm)) != 0)
continue;
if (fm == "nautilus" || fm == "dolphin" || fm == "thunar" || fm == "pcmanfm")
{
QStringList args;
if (fm == "nautilus" || fm == "dolphin")
args << "--select" << absolutePath;
else
args << absolutePath;
QProcess::startDetached(fm, args);
success = true;
break;
}
}
if (!success)
{
// If a file cannot be selected, just open the containing directory.
QDesktopServices::openUrl(QUrl::fromLocalFile(parentDir));
}
#else
// For other unknown OS, just open the containing directory.
QDesktopServices::openUrl(QUrl::fromLocalFile(parentDir));
#endif
}