-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
195 lines (157 loc) · 5.81 KB
/
main.cpp
File metadata and controls
195 lines (157 loc) · 5.81 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
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QListWidgetItem>
#include <QLineEdit>
#include <QPushButton>
#include <QPixmap>
#include <QDirIterator>
#include <QFileInfo>
#include <QProcess>
#include <QFileInfoList>
#include <QDesktopServices>
#include <QUrl>
#include <QDebug>
#include <QtWin>
#include <Windows.h>
#include <Shobjidl.h>
#include <Shlobj.h>
#include <initguid.h>
#include <thumbcache.h>
class VideoListApp : public QWidget {
Q_OBJECT
public:
VideoListApp(QWidget *parent = nullptr) : QWidget(parent) {
CoInitialize(nullptr);
CoCreateInstance(CLSID_LocalThumbnailCache, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&thumbnailCache));
initUI();
}
virtual ~VideoListApp() {
thumbnailCache->Release();
CoUninitialize();
}
private slots:
void changeDirectory() {
QString newDirectory = directoryEdit->text();
if (QDir(newDirectory).exists()) {
qInfo() << "Changing directory";
QDir::setCurrent(newDirectory);
populateList();
}
}
void searchVideos(const QString &searchText) {
if (searchText.isEmpty()) {
populateList();
return;
}
listWidget->clear();
QStringList videoExtensions = {"*.mp4", "*.mpg", "*.avi", "*.mkv"};
QDirIterator it(QDir::currentPath(), videoExtensions, QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString videoPath = it.next();
QString videoName = QFileInfo(videoPath).fileName();
if (videoName.contains(searchText, Qt::CaseInsensitive)) {
populateListItem(videoPath);
}
}
}
void playVideo(QListWidgetItem *item) {
QString videoPath = item->whatsThis();
QDesktopServices::openUrl(QUrl::fromLocalFile(videoPath));
}
private:
void initUI() {
setWindowTitle("Video List");
setGeometry(100, 100, 400, 500);
// Create a vertical layout for the widgets
QVBoxLayout *layout = new QVBoxLayout(this);
// Create a line edit for entering the directory path
directoryEdit = new QLineEdit(this);
layout->addWidget(directoryEdit);
// Create a button to confirm the directory change
QPushButton *directoryButton = new QPushButton("Change Directory", this);
layout->addWidget(directoryButton);
// Create a line edit for searching videos
QLineEdit *searchEdit = new QLineEdit(this);
layout->addWidget(searchEdit);
// Create a list widget to display the videos
listWidget = new QListWidget(this);
layout->addWidget(listWidget);
setLayout(layout);
// Set the initial directory to the current directory
directoryEdit->setText(QDir::currentPath());
// Populate the list with videos from the current directory
populateList();
// Connect the signals and slots
connect(directoryButton, &QPushButton::clicked, this, &VideoListApp::changeDirectory);
connect(searchEdit, &QLineEdit::textChanged, this, &VideoListApp::searchVideos);
connect(listWidget, &QListWidget::itemDoubleClicked, this, &VideoListApp::playVideo);
}
void populateList() {
listWidget->clear();
QStringList videoExtensions = {"*.mp4", "*.mpg", "*.avi", "*.mkv"};
QDirIterator it(QDir::currentPath(), videoExtensions, QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString videoPath = it.next();
populateListItem(videoPath);
}
}
void populateListItem(QString videoPath) {
QFileInfo videoInfo(videoPath);
auto item = new QListWidgetItem();
item->setWhatsThis(videoInfo.filePath());
item->setSizeHint(QSize(0, 240));
auto pixmap = extractThumbnail(videoInfo.absoluteFilePath());
auto wight = new QWidget();
auto layout = new QHBoxLayout();
auto label0 = new QLabel();
label0->setPixmap(pixmap);
label0->setFixedSize(320, 240);
auto label1 = new QLabel(videoInfo.fileName());
layout->addWidget(label0);
layout->addWidget(label1);
wight->setLayout(layout);
listWidget->addItem(item);
listWidget->setItemWidget(item, wight);
}
QPixmap extractThumbnail(QString videoPath) {
IShellItem* shellItem;
videoPath.replace("/", "\\");
HRESULT hr = SHCreateItemFromParsingName(reinterpret_cast<LPCWSTR>(videoPath.utf16()), nullptr, IID_PPV_ARGS(&shellItem));
if (FAILED(hr)) {
//qWarning() << "Failed to create shell item for the video file." << videoPath;
return QPixmap();
}
ISharedBitmap* bitmap = nullptr;
int cXY = 200;
WTS_FLAGS flags = WTS_EXTRACT;
hr = thumbnailCache->GetThumbnail(shellItem, cXY, WTS_EXTRACT, &bitmap, nullptr, nullptr);
if (FAILED(hr) || bitmap == nullptr) {
//qWarning() << "Failed to extract thumbnail for the video file." << videoPath;
return QPixmap();
}
HBITMAP hBitmap;
hr = bitmap->GetSharedBitmap(&hBitmap);
if (FAILED(hr)) {
//qWarning() << "Failed to extract thumbnail for the video file." << videoPath;
return QPixmap();
}
QPixmap pixmap = QtWin::fromHBITMAP(hBitmap);
DeleteObject(hBitmap);
bitmap->Release();
shellItem->Release();
return pixmap;
}
QLineEdit *directoryEdit;
QListWidget *listWidget;
IThumbnailCache* thumbnailCache;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
VideoListApp window;
window.show();
return app.exec();
}
#include "main.moc"