playlist_window.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright (C) 2025 mio <stigma@disroot.org>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. */
  14. #include "playlist_window.h"
  15. #include "playlist.h"
  16. #include <tqlayout.h>
  17. #include <tqpalette.h>
  18. #include <tqtable.h>
  19. #include <tqpainter.h>
  20. #include <tdefilemetainfo.h>
  21. #include <tdelocale.h>
  22. #include <tdemenubar.h>
  23. class PlaylistTable : public TQTable
  24. {
  25. public:
  26. PlaylistTable(TQWidget *parent, const char *name)
  27. : TQTable(parent, name)
  28. , m_currentRow(0)
  29. {}
  30. void paintCell(TQPainter *p, int row, int col, const TQRect& cr, bool selected, const TQColorGroup& cg) override
  31. {
  32. if (row == m_currentRow)
  33. {
  34. // TODO: This should be configurable via settings.
  35. TQColorGroup group = cg;
  36. group.setColor(TQColorGroup::Text, palette().active().buttonText());
  37. group.setBrush(TQColorGroup::Base, palette().active().button());
  38. TQTable::paintCell(p, row, col, cr, selected, group);
  39. }
  40. else
  41. {
  42. TQTable::paintCell(p, row, col, cr, selected, cg);
  43. }
  44. }
  45. void setCurrentRow(int row)
  46. {
  47. m_currentRow = row;
  48. repaintContents(visibleRect());
  49. }
  50. private:
  51. int m_currentRow;
  52. };
  53. PlaylistWindow::PlaylistWindow(Playlist *playlist, TQWidget *parent, const char *name)
  54. : TQWidget(parent, name, WType_TopLevel)
  55. , m_playlist(playlist)
  56. {
  57. setCaption(i18n("Aster - Playlist"));
  58. auto layout = new TQVBoxLayout(this);
  59. m_menuBar = new KMenuBar(this, "playlist_window_menubar");
  60. m_menuBar->insertItem("Open File...", 0); // TODO: Should this just emit a signal?
  61. m_menuBar->insertItem("Remove Item", this, TQ_SLOT(slotRemoveCurrent()), Key_Delete, 1);
  62. m_menuBar->setItemEnabled(1, m_playlist->count() > 1);
  63. m_table = new PlaylistTable(this, "playlist_window_table");
  64. m_table->setFocusStyle(TQTable::FollowStyle);
  65. m_table->setLeftMargin(0);
  66. m_table->setNumCols(2);
  67. m_table->setReadOnly(true);
  68. m_table->setSelectionMode(TQTable::SingleRow);
  69. m_table->horizontalHeader()->setLabel(0, i18n("Title"));
  70. m_table->horizontalHeader()->setLabel(1, i18n("Duration"));
  71. m_table->setColumnStretchable(0, true);
  72. connect(m_playlist, TQ_SIGNAL(currentIndexChanged(int)), TQ_SLOT(slotIndexChanged(int)));
  73. connect(m_playlist, TQ_SIGNAL(itemsInserted(int, int)), TQ_SLOT(slotItemsInserted(int, int)));
  74. connect(m_playlist, TQ_SIGNAL(itemsRemoved(int, int)), TQ_SLOT(slotItemsRemoved(int, int)));
  75. connect(m_table, TQ_SIGNAL(doubleClicked(int, int, int, const TQPoint&)),
  76. TQ_SLOT(slotTableDoubleClicked(int, int, int, const TQPoint&)));
  77. layout->add(m_menuBar);
  78. layout->add(m_table);
  79. /* FIXME: No way to iterate playlist */
  80. m_table->insertRows(0, m_playlist->count());
  81. for (size_t i = 0; i < m_playlist->count(); ++i)
  82. {
  83. KFileMetaInfo info(m_playlist->at(i));
  84. if (info.isEmpty())
  85. {
  86. m_table->setText(i, 0, m_playlist->at(i).fileName());
  87. m_table->setText(i, 1, TQString::fromLatin1("00:00:00"));
  88. }
  89. else
  90. {
  91. m_table->setText(i, 0, "TITLE!");
  92. m_table->setText(i, 1, TQString::fromLatin1("00:00:00"));
  93. }
  94. }
  95. static_cast<PlaylistTable*>(m_table)->setCurrentRow(m_playlist->currentIndex());
  96. resize(500, 200);
  97. move(parent->x() + (parent->width() / 2) - (width() / 2),
  98. parent->y() + (parent->height() / 2) - (height() / 2));
  99. }
  100. void PlaylistWindow::keyReleaseEvent(TQKeyEvent *event)
  101. {
  102. switch (event->key())
  103. {
  104. case Key_Enter:
  105. case Key_Return: {
  106. int current = m_table->selection(m_table->currentSelection()).topRow();
  107. if (current == m_playlist->currentIndex()) {
  108. // Should this restart? (as with double-clicking)
  109. return;
  110. }
  111. m_playlist->setCurrentIndex(current);
  112. break;
  113. }
  114. default:
  115. break;
  116. }
  117. }
  118. void PlaylistWindow::slotIndexChanged(int index)
  119. {
  120. static_cast<PlaylistTable*>(m_table)->setCurrentRow(index);
  121. }
  122. void PlaylistWindow::slotItemsInserted(int start, int end)
  123. {
  124. const int count = end - start;
  125. m_table->insertRows(m_table->numRows(), count);
  126. for (int i = 0; i < count; ++i)
  127. {
  128. KFileMetaInfo info(m_playlist->at(start + i));
  129. if (info.isEmpty())
  130. {
  131. m_table->setText(start + i, 0, m_playlist->at(start + i).fileName());
  132. m_table->setText(start + i, 1, TQString::fromLatin1("00:00:00"));
  133. }
  134. else
  135. {
  136. m_table->setText(start + i, 0, "TITLE!");
  137. m_table->setText(start + i, 1, TQString::fromLatin1("00:00:00"));
  138. }
  139. }
  140. // Ensure the 'remove item' is active.
  141. m_menuBar->setItemEnabled(1, true);
  142. }
  143. void PlaylistWindow::slotItemsRemoved(int start, int end)
  144. {
  145. m_table->removeRow(start);
  146. if (m_playlist->count() == 0)
  147. m_menuBar->setItemEnabled(1, false);
  148. tqWarning("PlaylistWindow::slotItemsRemoved: implementation isn't finished as multiple selections are not supported.");
  149. }
  150. void PlaylistWindow::slotRemoveCurrent()
  151. {
  152. m_playlist->remove(m_table->currentRow());
  153. }
  154. void PlaylistWindow::slotTableDoubleClicked(int row, int, int button, const TQPoint&)
  155. {
  156. if (button != TQt::LeftButton)
  157. {
  158. return;
  159. }
  160. // Should this restart?
  161. if (m_playlist->currentIndex() == row)
  162. {
  163. return;
  164. }
  165. m_playlist->setCurrentIndex(row);
  166. }
  167. #include "playlist_window.moc"