-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpl.cpp
More file actions
333 lines (327 loc) · 10.2 KB
/
pl.cpp
File metadata and controls
333 lines (327 loc) · 10.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "pl.h"
#include "ui_pl.h"
#include "bass.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QUrl>
#include <QMessageBox>
#define loadproc qDebug() << "Loading proc in Pl:" <<
Pl::Pl(QWidget *parent) : QDialog(parent), ui(new Ui::Pl)
{
this->_curr = 0;
this->_currs = 0;
this->path = PATH + "/playlist.m3u";
loadproc "setup ui";
ui->setupUi(this);
loadproc "setup ui properties";
this->setWindowTitle("Playlist");
this->setWindowFlags(Qt::Tool | Qt::MSWindowsFixedSizeDialogHint);
this->ui->listWidget->setAcceptDrops(true);
this->ui->listWidget->setMaximumWidth(this->width());
this->ui->listWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
this->ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->ui->listWidget->setSelectionMode(QAbstractItemView::ContiguousSelection);
loadproc "create actions";
this->createActions();
this->ourl = new OpenUrl(this);
this->ourl->hide();
connect(this->ourl, SIGNAL(okPressed()), this, SLOT(addUrlf()));
connect(this->ourl, SIGNAL(canceled()), this, SLOT(urlCanceled()));
connect(this->ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(trackClick(QListWidgetItem*)));
connect(this->ui->searchInput, SIGNAL(textChanged(QString)), this, SLOT(search(QString)));
connect(this->ui->pushButton, SIGNAL(clicked()), this, SLOT(nextSearch()));
}
Pl::~Pl()
{
delete ui;
}
void Pl::createActions(){
this->del = new QAction("Delete", this);
this->del->setIcon(QIcon(":/res/icons/delete.png"));
connect(this->del, SIGNAL(triggered()), this, SLOT(deleteItem()));
this->addu = new QAction("Add URL", this);
this->addu->setIcon(QIcon(":/res/icons/url.png"));
connect(this->addu, SIGNAL(triggered()), this, SLOT(addUrl()));
}
void Pl::contextMenuEvent(QContextMenuEvent *event){
QMenu menu(this);
menu.addAction(this->del);
menu.addAction(this->addu);
menu.exec(event->globalPos());
}
void Pl::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void Pl::dragEnterEvent(QDragEnterEvent *event){
if(event->mimeData()->hasUrls()){
event->setDropAction(Qt::ActionMask);
event->accept();
}
}
void Pl::dropEvent(QDropEvent *event){
qDebug() << "dropped...";
QList<QUrl> urls = event->mimeData()->urls();
QString file;
for(int i = 0; i < urls.length(); i++){
if(urls.value(i).scheme() == URLCHEME){
this->addURL(urls.value(i));
}
file = urls.value(i).toString(QUrl::None);
#ifdef Q_WS_WIN //windows, cut file:///
file = file.mid(8);
#else
file = file.mid(7); //other, cut file://
#endif
QDir dir(file);
if(dir.exists()){
this->addPath(file);
}
else{
QFile f(file);
if(f.exists()){
this->addTrack(file);
}
}
}
}
bool Pl::addTrack(QString path)
{
if(path.isEmpty() || !QFile(path).exists()){
return false;
}
QFileInfo *f = new QFileInfo(path);
QListWidgetItem *trk = new QListWidgetItem(this->ui->listWidget);
trk->setText(f->fileName());
trk->setData(Qt::UserRole, path.replace('\\', "/", Qt::CaseInsensitive));
delete f;
return true;
}
bool Pl::addURL(QUrl url){
if(url.scheme() != URLCHEME || !url.isValid()){
return false;
}
QListWidgetItem *itm = new QListWidgetItem(this->ui->listWidget);
itm->setText(url.authority());
itm->setData(Qt::UserRole, url.toString(QUrl::None));
return true;
}
void Pl::addPath(QString path){
QDir dir(path);
if(dir.exists()){
QStringList listd = dir.entryList(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDir::Name);
for(int i = 0; i < listd.length(); i++){
this->addPath(dir.path() + "/" + listd.value(i));
}
QStringList listf = dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDir::Name);
for(int i = 0; i < listf.length(); i++){
this->addTrack(dir.path() + "/" + listf.value(i));
}
}
}
void Pl::trackClick(QListWidgetItem *qlwi)
{
this->_curr = this->ui->listWidget->row(qlwi);
this->changeTrack(qlwi->data(Qt::UserRole).toString());
this->select(this->_curr);
}
QString Pl::current(){
if(this->_curr >= this->ui->listWidget->count())
this->_curr = 0;
this->select(this->_curr);
return this->ui->listWidget->item(this->_curr)->data(Qt::UserRole).toString();
}
void Pl::setCurrent(int pos){
if(this->ui->listWidget->count() == 0){
return;
}
if(this->ui->listWidget->count() < 1){
return;
}
if(pos < 0){
pos = this->ui->listWidget->count() - 1;
}
if(pos >= this->ui->listWidget->count()){
pos = 0;
}
this->_curr = pos;
this->changeTrack(this->ui->listWidget->item(this->_curr)->data(Qt::UserRole).toString());
this->select(this->_curr);
}
int Pl::getCurrent(){
return this->_curr;
}
int Pl::getMax(){
return this->ui->listWidget->count() - 1;
}
void Pl::select(int idx){
if(this->isVisible()){
QList<QListWidgetItem *> items = this->ui->listWidget->selectedItems();
for(int i = 0; i < items.length(); i++){
items.value(i)->setSelected(false);
}
if(!this->ui->listWidget->item(idx)){
return;
}
this->ui->listWidget->item(idx)->setSelected(true);
this->ui->listWidget->scrollToItem(this->ui->listWidget->item(idx), QAbstractItemView::EnsureVisible);
}
}
bool Pl::save(){
if(path.isEmpty()){
return false;
}
if(this->ui->listWidget->count() < 1){
return false;
}
QFile file(path);
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.setTextModeEnabled(true);
file.write("#EXTM3U\n");
int l = this->ui->listWidget->count();
for(int i = 0; i < l; i++){
file.write(QByteArray().append("#EXTINF:0,").append(this->ui->listWidget->item(i)->text().toUtf8()).append("\n"));
file.write(QByteArray().append(this->ui->listWidget->item(i)->data(Qt::UserRole).toString().toUtf8()).append("\n"));
}
file.close();
return true;
}
bool Pl::load(QString path){
if(path.isEmpty()){
return false;
}
QFile file(path);
if(!file.exists()){
return false;
}
file.open(QIODevice::ReadOnly | QIODevice::Text);
file.setTextModeEnabled(true);
if(!file.isOpen()){
}
QString str;
QString title;
while(file.bytesAvailable() > 0){
str = QString().fromUtf8(file.readLine(1024)).trimmed();
if(str.isEmpty()){
continue;
}
else{
if(str.at(0) == '#' && title.isEmpty()){
if(str.left(10) == "#EXTINF:0,"){
title = str.mid(10);
}
else{
title = "";
}
}
}
if(this->addTrack(str)){
if(!title.isEmpty()){
this->setTitle(this->ui->listWidget->count() - 1, title);
title = "";
}
}
else if(this->addURL(str)){
if(!title.isEmpty()){
this->setTitle(this->ui->listWidget->count() - 1, title);
title = "";
}
}
}
file.close();
return true;
}
void Pl::setTitle(int idx, QString title){
if(idx >= 0 && idx < this->ui->listWidget->count()){
this->ui->listWidget->item(idx)->setText(title);
}
}
void Pl::deleteItem(){
bool change = false;
QList<QListWidgetItem *> items = this->ui->listWidget->selectedItems();
if(items.length() > 0){
for(int i = 0; i < items.length(); i++){
int pos = this->ui->listWidget->row(items.value(i));
this->ui->listWidget->removeItemWidget(items.value(i));
if(pos < this->_curr){
this->_curr--;
}
else if(pos == this->_curr){
change = true;
this->_curr++;
}
delete items.value(i);
}
}
if(change){
this->setCurrent(this->_curr);
}
}
void Pl::hideEvent(QHideEvent *event){
}
void Pl::showEvent(QShowEvent *event){
this->select(this->_curr);
}
void Pl::search(QString s){
QBrush brush(NORMALCOLOR, Qt::SolidPattern);
this->ui->listWidget->item(this->_currs)->setBackground(brush);
this->_currs = 0;
if(s.isEmpty() || s.length() < 2){
return;
}
this->searcha(s);
}
void Pl::searcha(QString s){
QBrush brush(SELECTEDCOLOR, Qt::Dense1Pattern);
this->lastSearch = s;
int len = this->ui->listWidget->count();
bool repeated = false;
if(this->_currs > len){
this->_currs = 0;
}
for(int i = this->_currs; i < len; i++){
if(this->ui->listWidget->item(i)->text().contains(s, Qt::CaseInsensitive)){
this->_currs = i;
this->ui->listWidget->item(i)->setBackground(brush);
this->ui->listWidget->scrollToItem(this->ui->listWidget->item(i), QAbstractItemView::EnsureVisible);
return;
}
if(i == len - 1 && !repeated){
i = 0;
repeated = true;
}
}
}
void Pl::nextSearch(){
this->ui->listWidget->item(this->_currs)->setBackground(QBrush(NORMALCOLOR, Qt::SolidPattern));
this->_currs++;
this->searcha(this->lastSearch);
}
void Pl::clear(){
int len = this->ui->listWidget->count();
for(int i = len - 1; i > -1; i--){
delete this->ui->listWidget->item(i);
}
this->_curr = 0;
this->_currs = 0;
}
void Pl::addUrl(){
this->ourl->show();
}
void Pl::addUrlf(){
QUrl url(this->ourl->getURL());
if(url.scheme() == URLCHEME && url.isValid()){
this->addURL(url);
}
this->ourl->hide();
}
void Pl::urlCanceled(){
this->ourl->hide();
}