You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. MusicLibraryTable::MusicLibraryTable()
  25. : font (12.0f),
  26. currentLibrary (nullptr),
  27. dataList (MusicColumns::libraryIdentifier),
  28. filteredNumRows (dataList.getNumChildren()),
  29. finishedLoading (true)
  30. {
  31. // Create our table component and add it to this component..
  32. addAndMakeVisible (&table);
  33. table.setModel (this);
  34. table.setMultipleSelectionEnabled (true);
  35. // table.setColour (ListBox::backgroundColourId, Colour::greyLevel (0.2f));
  36. table.setHeaderHeight (18);
  37. table.setRowHeight (16);
  38. table.getViewport()->setScrollBarThickness (10);
  39. // give it a border
  40. // table.setColour (ListBox::outlineColourId, Colours::grey);
  41. table.setOutlineThickness (1);
  42. // Add some MusicColumns to the table header
  43. for (int i = 1; i < MusicColumns::numColumns; i++)
  44. {
  45. table.getHeader().addColumn (MusicColumns::columnNames[i].toString(),
  46. i,
  47. MusicColumns::columnWidths[i],
  48. 50,
  49. 800,
  50. TableHeaderComponent::defaultFlags);
  51. }
  52. // we could now change some initial settings..
  53. table.getHeader().setSortColumnId (MusicColumns::Artist, true); // sort forwards by the Artist column
  54. table.getHeader().setColumnVisible (MusicColumns::LibID, false);
  55. table.getHeader().setColumnVisible (MusicColumns::ID, false);
  56. table.getHeader().setColumnVisible (MusicColumns::Rating, false);
  57. table.getHeader().setColumnVisible (MusicColumns::Location, false);
  58. table.getHeader().setColumnVisible (MusicColumns::Modified, false);
  59. setFilterText (String());
  60. }
  61. MusicLibraryTable::~MusicLibraryTable()
  62. {
  63. if (currentLibrary != nullptr)
  64. currentLibrary->removeListener(this);
  65. }
  66. void MusicLibraryTable::setLibraryToUse (ITunesLibrary* library)
  67. {
  68. currentLibrary = library;
  69. filteredDataList = dataList = library->getLibraryTree();
  70. dataList = library->getLibraryTree();
  71. library->addListener(this);
  72. }
  73. void MusicLibraryTable::setFilterText (const String& filterString)
  74. {
  75. currentFilterText = filterString;
  76. if (currentLibrary != nullptr)
  77. currentLibrary->getParserLock().enter();
  78. if (filterString.isEmpty())
  79. {
  80. filteredDataList = dataList;
  81. filteredNumRows = filteredDataList.getNumChildren();
  82. }
  83. else
  84. {
  85. filteredDataList = ValueTree (dataList.getType());
  86. for (int e = 0; e < dataList.getNumChildren(); ++e)
  87. {
  88. for (int i = 0; i < dataList.getChild (e).getNumProperties(); i++)
  89. {
  90. if (dataList.getChild (e)[MusicColumns::columnNames[i]].toString().containsIgnoreCase (filterString))
  91. {
  92. filteredDataList.addChild (dataList.getChild(e).createCopy(), -1, 0);
  93. break;
  94. }
  95. }
  96. }
  97. filteredNumRows = filteredDataList.getNumChildren();
  98. }
  99. if (currentLibrary != nullptr)
  100. currentLibrary->getParserLock().exit();
  101. table.getHeader().reSortTable();
  102. }
  103. //==============================================================================
  104. void MusicLibraryTable::libraryChanged (ITunesLibrary* library)
  105. {
  106. if (library == currentLibrary)
  107. {
  108. finishedLoading = false;
  109. filteredDataList = dataList = currentLibrary->getLibraryTree();
  110. updateTableFilteredAndSorted();
  111. }
  112. }
  113. void MusicLibraryTable::libraryUpdated (ITunesLibrary* library)
  114. {
  115. if (library == currentLibrary)
  116. updateTableFilteredAndSorted();
  117. }
  118. void MusicLibraryTable::libraryFinished (ITunesLibrary* library)
  119. {
  120. if (library == currentLibrary)
  121. {
  122. finishedLoading = true;
  123. updateTableFilteredAndSorted();
  124. }
  125. }
  126. //==============================================================================
  127. int MusicLibraryTable::getNumRows()
  128. {
  129. return filteredNumRows;
  130. }
  131. void MusicLibraryTable::paintRowBackground (Graphics& g, int /*rowNumber*/,
  132. int /*width*/, int /*height*/, bool rowIsSelected)
  133. {
  134. if (rowIsSelected)
  135. g.fillAll (defaultColours.findColour (*this, table.hasKeyboardFocus (true) ? selectedBackgroundColourId
  136. : selectedUnfocusedBackgroundColourId));
  137. else
  138. g.fillAll (defaultColours.findColour (*this, table.hasKeyboardFocus (true) ? backgroundColourId
  139. : unfocusedBackgroundColourId));
  140. }
  141. void MusicLibraryTable::paintCell (Graphics& g,
  142. int rowNumber,
  143. int columnId,
  144. int width, int height,
  145. bool rowIsSelected)
  146. {
  147. if (table.hasKeyboardFocus (true))
  148. g.setColour (defaultColours.findColour (*this, rowIsSelected ? selectedTextColourId : textColourId));
  149. else
  150. g.setColour (defaultColours.findColour (*this, rowIsSelected ? selectedUnfocusedTextColourId : unfocusedTextColourId));
  151. g.setFont (font);
  152. {
  153. const ScopedLock sl (currentLibrary->getParserLock());
  154. const ValueTree& rowElement (filteredDataList.getChild (rowNumber));
  155. if (rowElement.isValid())
  156. {
  157. String text;
  158. if(columnId == MusicColumns::Length)
  159. text = secondsToTimeLength (rowElement[MusicColumns::columnNames[columnId]].toString().getIntValue());
  160. else if(columnId == MusicColumns::Added
  161. || columnId == MusicColumns::Modified)
  162. text = Time (int64 (rowElement[MusicColumns::columnNames[columnId]])).formatted ("%d/%m/%Y - %H:%M");
  163. else
  164. text = rowElement[MusicColumns::columnNames[columnId]].toString();
  165. g.drawText (text, 2, 0, width - 4, height, Justification::centredLeft, true);
  166. }
  167. }
  168. if (table.hasKeyboardFocus (true))
  169. g.setColour (defaultColours.findColour (*this, rowIsSelected ? selectedOutlineColourId : outlineColourId));
  170. else
  171. g.setColour (defaultColours.findColour (*this, rowIsSelected ? selectedUnfocusedOutlineColourId : unfocusedOutlineColourId));
  172. g.fillRect (width - 1, 0, 1, height);
  173. g.fillRect (0, height - 1, width, 1);
  174. }
  175. void MusicLibraryTable::sortOrderChanged (int newSortColumnId, bool isForwards)
  176. {
  177. findSelectedRows();
  178. if (newSortColumnId != 0)
  179. {
  180. const ScopedLock sl (currentLibrary->getParserLock());
  181. if (newSortColumnId == MusicColumns::Length
  182. || newSortColumnId == MusicColumns::BPM
  183. || newSortColumnId == MusicColumns::LibID
  184. || newSortColumnId == MusicColumns::ID
  185. || newSortColumnId == MusicColumns::Added
  186. || newSortColumnId == MusicColumns::Modified)
  187. {
  188. ValueTreeComparators::Numerical<double> sorter (MusicColumns::columnNames[newSortColumnId], isForwards);
  189. filteredDataList.sort (sorter, 0, false);
  190. }
  191. else
  192. {
  193. ValueTreeComparators::LexicographicWithBackup sorter (MusicColumns::columnNames[newSortColumnId],
  194. MusicColumns::columnNames[MusicColumns::LibID],
  195. isForwards);
  196. filteredDataList.sort (sorter, 0, false);
  197. }
  198. table.updateContent();
  199. }
  200. setSelectedRows();
  201. }
  202. //==========================================================================================
  203. int MusicLibraryTable::getColumnAutoSizeWidth (int columnId)
  204. {
  205. int widest = 32;
  206. // find the widest bit of text in this column..
  207. for (int i = getNumRows(); --i >= 0;)
  208. {
  209. {
  210. const ScopedLock sl (currentLibrary->getParserLock());
  211. const ValueTree& rowElement (filteredDataList.getChild (i));
  212. if (rowElement.isValid())
  213. {
  214. const String text (rowElement[MusicColumns::columnNames[columnId]].toString());
  215. widest = jmax (widest, font.getStringWidth (text));
  216. }
  217. }
  218. }
  219. return widest + 8;
  220. }
  221. //==============================================================================
  222. void MusicLibraryTable::resized()
  223. {
  224. table.setBounds (getLocalBounds());
  225. }
  226. void MusicLibraryTable::focusOfChildComponentChanged (FocusChangeType /*cause*/)
  227. {
  228. repaint();
  229. }
  230. var MusicLibraryTable::getDragSourceDescription (const SparseSet<int>& currentlySelectedRows)
  231. {
  232. var itemsArray;
  233. if(! currentlySelectedRows.isEmpty())
  234. {
  235. for (int i = 0; i < currentlySelectedRows.size(); ++i)
  236. {
  237. const ScopedLock sl (currentLibrary->getParserLock());
  238. // get child from main tree with same LibID
  239. const ValueTree& tree (filteredDataList.getChild (currentlySelectedRows[i]));
  240. ReferenceCountedValueTree::Ptr childTree = new ReferenceCountedValueTree (tree);
  241. itemsArray.append (childTree.getObject());
  242. }
  243. }
  244. return itemsArray;
  245. }
  246. //==============================================================================
  247. void MusicLibraryTable::updateTableFilteredAndSorted()
  248. {
  249. // make sure we still apply our filter
  250. // this will also re-sort and update the table
  251. setFilterText (currentFilterText);
  252. }
  253. void MusicLibraryTable::findSelectedRows()
  254. {
  255. selectedRowsLibIds.clear();
  256. const SparseSet<int> selectedRowNumbers (table.getSelectedRows());
  257. for (int i = 0; i < selectedRowNumbers.size(); ++i)
  258. {
  259. const int oldIndex = selectedRowNumbers[i];
  260. const int libId = int (filteredDataList.getChild (oldIndex)[MusicColumns::columnNames[MusicColumns::LibID]]);
  261. selectedRowsLibIds.add (libId);
  262. }
  263. }
  264. void MusicLibraryTable::setSelectedRows()
  265. {
  266. SparseSet<int> newSelectedRowNumbers;
  267. for (int i = 0; i < selectedRowsLibIds.size(); ++i)
  268. {
  269. const int libId = selectedRowsLibIds.getReference (i);
  270. const int index = filteredDataList.indexOf (filteredDataList.getChildWithProperty (MusicColumns::columnNames[MusicColumns::LibID],
  271. libId));
  272. newSelectedRowNumbers.addRange (Range<int> (index, index + 1));
  273. }
  274. table.setSelectedRows (newSelectedRowNumbers, sendNotification);
  275. }