playlist_window.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //
  2. #include "playlist_window.h"
  3. #include "common.h"
  4. #include "mainwindowa.h"
  5. #include "packets_sync.h"
  6. #include "play_control_window.h"
  7. PlayListWnd::PlayListWnd(QWidget* parent) : QWidget(parent), ui(std::make_unique<Ui::PlayList>())
  8. {
  9. ui->setupUi(this);
  10. setLayout(ui->gridLayout);
  11. auto flags = windowFlags();
  12. flags |= Qt::Window;
  13. flags |= Qt::WindowStaysOnTopHint;
  14. flags &= (~Qt::WindowContextHelpButtonHint);
  15. setWindowFlags(flags);
  16. setWindowModality(Qt::NonModal);
  17. setAcceptDrops(true);
  18. create_temp_menu();
  19. init_list();
  20. clear_data_files();
  21. update_table_list();
  22. }
  23. void PlayListWnd::init_list()
  24. {
  25. auto pTable = get_table();
  26. QStringList headerLabels;
  27. // headerLabels << "#" << "Title" << "Duration" << "Path";
  28. headerLabels << "Title"
  29. << "Duration"
  30. << "Path";
  31. pTable->setColumnCount(headerLabels.size());
  32. pTable->setHorizontalHeaderLabels(headerLabels);
  33. auto header = pTable->horizontalHeader();
  34. header->setFixedHeight(35);
  35. // header->setSectionResizeMode(QHeaderView::Stretch);
  36. // header->setSectionResizeMode(1, QHeaderView::ResizeToContents); //column
  37. // duration.
  38. header->setStretchLastSection(true);
  39. pTable->verticalHeader()->setVisible(false);
  40. pTable->setShowGrid(false);
  41. // pTable->setStyleSheet("QTableView {selection-background-color: red;}");
  42. pTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
  43. pTable->setSelectionBehavior(QAbstractItemView::SelectRows);
  44. pTable->setSelectionMode(QAbstractItemView::SingleSelection);
  45. connect(pTable, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(cellSelected(int, int)));
  46. pTable->setContextMenuPolicy(Qt::CustomContextMenu);
  47. connect(pTable, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayMenu(const QPoint&)));
  48. }
  49. bool PlayListWnd::add_data_file(const QString& file)
  50. {
  51. PlayListLine data;
  52. data.file = file;
  53. if (is_local(data.file))
  54. {
  55. data.fileName = get_file_name(data.file);
  56. data.duration = get_file_duration(data.file);
  57. }
  58. else
  59. {
  60. data.fileName = "Unknow"; // not handled
  61. data.duration = "--:--";
  62. // if (auto parent = (MainWindowA*) parentWidget()) {
  63. // YoutubeJsonParser::YtStreamData st_data;
  64. // if (parent->find_yt_list(data.file, st_data))
  65. // {
  66. // data.fileName = st_data.title;
  67. // data.duration = get_file_duration(data.file);
  68. // }
  69. // }
  70. }
  71. m_dataItems.insert({file, data});
  72. return true;
  73. }
  74. inline bool PlayListWnd::already_in(const QString& file) const
  75. {
  76. return m_dataItems.find(file) != m_dataItems.end();
  77. }
  78. void PlayListWnd::del_data_file(const QString& file)
  79. {
  80. if (already_in(file))
  81. m_dataItems.erase(file);
  82. }
  83. inline QString PlayListWnd::get_data_file(int id) const
  84. {
  85. auto it = m_dataItems.begin();
  86. std::advance(it, id);
  87. return it->first;
  88. }
  89. void PlayListWnd::clear_data_files()
  90. {
  91. m_dataItems.clear();
  92. }
  93. void PlayListWnd::add_table_line(const PlayListLine& data)
  94. {
  95. const auto pTable = get_table();
  96. const int count = pTable->rowCount();
  97. pTable->setRowCount(count + 1);
  98. int col = 0;
  99. pTable->setItem(count, col++, new QTableWidgetItem(data.fileName));
  100. pTable->setItem(count, col++, new QTableWidgetItem(data.duration));
  101. auto file = toNativePath(data.file);
  102. pTable->setItem(count, col++, new QTableWidgetItem(file));
  103. pTable->setRowHeight(count, 16);
  104. }
  105. void PlayListWnd::clear_table_list()
  106. {
  107. const auto pTable = get_table();
  108. while (pTable->rowCount() > 0)
  109. pTable->removeRow(0);
  110. }
  111. void PlayListWnd::update_table_list()
  112. {
  113. clear_table_list();
  114. for (auto const& i : m_dataItems)
  115. add_table_line(i.second);
  116. set_cur_palyingfile();
  117. }
  118. void PlayListWnd::cellSelected(int row, int col)
  119. {
  120. auto file = get_data_file(row);
  121. qDebug() << "file clicked:"
  122. << "row:" << row << "col:" << col << "file:" << file;
  123. emit play_file(file);
  124. }
  125. QString PlayListWnd::get_cursel_file() const
  126. {
  127. const auto pTable = get_table();
  128. return get_data_file(pTable->currentRow());
  129. }
  130. QString PlayListWnd::get_row_file(int row) const
  131. {
  132. return get_data_file(row);
  133. }
  134. void PlayListWnd::add_files(const QStringList& files)
  135. {
  136. for (int i = 0; i < files.size(); i++)
  137. add_file(files[i]);
  138. }
  139. void PlayListWnd::add_file(const QString& file)
  140. {
  141. if (file.isEmpty())
  142. {
  143. qWarning() << "File is empty!\n";
  144. return;
  145. }
  146. if (!is_media(file))
  147. {
  148. qWarning() << "This is not media file, file:" << file;
  149. return;
  150. }
  151. add_data_file(file);
  152. update_table_list();
  153. }
  154. void PlayListWnd::closeEvent(QCloseEvent* event)
  155. {
  156. hide();
  157. event->ignore();
  158. emit hiden();
  159. }
  160. void PlayListWnd::dropEvent(QDropEvent* event)
  161. {
  162. const auto mimeData = event->mimeData();
  163. if (!mimeData->hasUrls())
  164. return;
  165. auto urlList = mimeData->urls();
  166. QStringList files;
  167. for (int i = 0; i < urlList.size(); i++)
  168. files.append(urlList.at(i).toLocalFile().trimmed());
  169. add_files(files);
  170. }
  171. QString PlayListWnd::get_file_name(const QString& path)
  172. {
  173. QFileInfo fileInfo(path);
  174. return fileInfo.baseName();
  175. }
  176. QString PlayListWnd::get_file_duration(const QString& file) const
  177. {
  178. int64_t duration = 0;
  179. get_file_info(file.toStdString().c_str(), duration);
  180. return get_file_duration(duration);
  181. }
  182. QString PlayListWnd::get_file_duration(int64_t duration) const
  183. {
  184. int64_t hours = 0, mins = 0, secs = 0, us = 0;
  185. get_duration_time(duration, hours, mins, secs, us);
  186. return PlayControlWnd::get_play_time(hours, mins, secs);
  187. }
  188. void PlayListWnd::dragEnterEvent(QDragEnterEvent* event)
  189. {
  190. const auto mimeData = event->mimeData();
  191. if (mimeData->hasUrls())
  192. event->acceptProposedAction();
  193. event->accept();
  194. }
  195. void PlayListWnd::keyPressEvent(QKeyEvent* event)
  196. {
  197. if (event->key() == Qt::Key_Escape)
  198. {
  199. hide();
  200. event->accept();
  201. }
  202. else
  203. {
  204. QWidget::keyPressEvent(event);
  205. }
  206. }
  207. void PlayListWnd::set_sel_file(const QString& file)
  208. {
  209. if (const auto pTable = get_table())
  210. {
  211. pTable->selectRow(0); // default
  212. for (int i = 0; i < pTable->rowCount(); i++)
  213. {
  214. if (get_row_file(i) == file)
  215. pTable->selectRow(i);
  216. }
  217. }
  218. }
  219. void PlayListWnd::get_files(QStringList& files) const
  220. {
  221. for (auto const& i : m_dataItems)
  222. files.append(i.first);
  223. }
  224. void PlayListWnd::deleteBtn_clicked()
  225. {
  226. if (auto sel = get_cursel_file(); !sel.isEmpty())
  227. {
  228. del_data_file(sel);
  229. update_table_list();
  230. }
  231. }
  232. void PlayListWnd::clearBtn_clicked()
  233. {
  234. clear_data_files();
  235. update_table_list();
  236. }
  237. bool PlayListWnd::saveBtn_clicked()
  238. {
  239. QStringList files;
  240. get_files(files);
  241. if (files.size() <= 0)
  242. {
  243. qWarning() << "Nothing in playlist!";
  244. return false;
  245. }
  246. auto dir = QDir::currentPath();
  247. auto fileName = QFileDialog::getSaveFileName(this, tr("Save Playlist File"), dir, tr("Playlist (*.pl)"));
  248. if (fileName.isEmpty())
  249. return false;
  250. dir = QDir(fileName).absolutePath();
  251. QFile file(fileName);
  252. if (file.open(QIODevice::WriteOnly))
  253. {
  254. QTextStream stream(&file);
  255. stream.setCodec("UTF-8");
  256. stream << files.join(PLAYLIST_SEPERATE_CHAR) << Qt::endl;
  257. stream.flush();
  258. file.close();
  259. emit playlist_file_saved(fileName);
  260. return true;
  261. }
  262. return false;
  263. }
  264. void PlayListWnd::update_files(const QStringList& files)
  265. {
  266. clear_data_files();
  267. add_files(files);
  268. }
  269. void PlayListWnd::displayMenu(const QPoint& pos)
  270. {
  271. if (auto pTable = get_table())
  272. {
  273. if (pTable->rowCount() <= 0)
  274. return;
  275. if (m_tmpMenu)
  276. m_tmpMenu->exec(pTable->viewport()->mapToGlobal(pos));
  277. }
  278. }
  279. void PlayListWnd::create_temp_menu()
  280. {
  281. m_tmpMenu = std::make_unique<QMenu>(this);
  282. auto del_act = m_tmpMenu->addAction("Delete");
  283. auto clear_act = m_tmpMenu->addAction("Clear");
  284. auto save_act = m_tmpMenu->addAction("Save");
  285. connect(del_act, &QAction::triggered, this, &PlayListWnd::deleteBtn_clicked);
  286. connect(clear_act, &QAction::triggered, this, &PlayListWnd::clearBtn_clicked);
  287. connect(save_act, &QAction::triggered, this, &PlayListWnd::saveBtn_clicked);
  288. }
  289. void PlayListWnd::set_cur_palyingfile()
  290. {
  291. if (auto pParent = (MainWindowA*) parent())
  292. set_sel_file(pParent->get_playingfile());
  293. }
  294. bool PlayListWnd::is_local(const QString& file)
  295. {
  296. return QUrl::fromUserInput(file).isLocalFile();
  297. }
  298. QString PlayListWnd::mimeType(const QString& file)
  299. {
  300. auto mimeType = QMimeDatabase().mimeTypeForFile(file);
  301. qDebug() << file << ", MIME info:";
  302. qDebug() << "name:" << mimeType.name();
  303. qDebug() << "comment:" << mimeType.comment();
  304. qDebug() << "genericIconName:" << mimeType.genericIconName();
  305. qDebug() << "iconName:" << mimeType.iconName();
  306. qDebug() << "globPatterns:" << mimeType.globPatterns();
  307. qDebug() << "parentMimeTypes:" << mimeType.parentMimeTypes();
  308. qDebug() << "allAncestors:" << mimeType.allAncestors();
  309. qDebug() << "aliases:" << mimeType.aliases();
  310. qDebug() << "suffixes:" << mimeType.suffixes();
  311. qDebug() << "preferredSuffix:" << mimeType.preferredSuffix();
  312. qDebug() << "filterString:" << mimeType.filterString();
  313. return QMimeDatabase().mimeTypeForFile(file).name();
  314. }
  315. bool PlayListWnd::is_media(const QString& file) const
  316. {
  317. if (!is_local(file)) // assume all network url are media files
  318. return true;
  319. auto mimetype = mimeType(file);
  320. auto mimetypes = mimetype.split("/");
  321. if (mimetypes[0] == "video" || mimetypes[0] == "audio")
  322. return true;
  323. #if 0
  324. QStringList mimes = { "video/mp4","video/x-matroska","video/webm","audio/x-wav" };
  325. if (mimes.contains(mimetype, Qt::CaseInsensitive)) {
  326. return true;
  327. }
  328. else {
  329. qWarning() << "Not handled, MIME type:" << mimetype;
  330. }
  331. #endif
  332. return false;
  333. }