playlist_window.cpp 10 KB

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