The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #pragma once
  14. //==============================================================================
  15. class ModulesInformationComponent : public Component,
  16. private ListBoxModel,
  17. private ValueTree::Listener
  18. {
  19. public:
  20. ModulesInformationComponent (Project& p)
  21. : project (p),
  22. modulesValueTree (project.getEnabledModules().getState())
  23. {
  24. listHeader = new ListBoxHeader ( { "Module", "Version", "Make Local Copy", "Paths" },
  25. { 0.25f, 0.2f, 0.2f, 0.35f } );
  26. list.setHeaderComponent (listHeader);
  27. list.setModel (this);
  28. list.setColour (ListBox::backgroundColourId, Colours::transparentBlack);
  29. addAndMakeVisible (list);
  30. list.updateContent();
  31. list.setRowHeight (30);
  32. list.setMultipleSelectionEnabled (true);
  33. addAndMakeVisible (header);
  34. addAndMakeVisible (setCopyModeButton);
  35. setCopyModeButton.setTriggeredOnMouseDown (true);
  36. setCopyModeButton.onClick = [this] { showCopyModeMenu(); };
  37. addAndMakeVisible (copyPathButton);
  38. copyPathButton.setTriggeredOnMouseDown (true);
  39. copyPathButton.onClick = [this] { showSetPathsMenu(); };
  40. addAndMakeVisible (globalPathsButton);
  41. globalPathsButton.onClick = [this] { showGlobalPathsMenu(); };
  42. modulesValueTree.addListener (this);
  43. lookAndFeelChanged();
  44. }
  45. void paint (Graphics& g) override
  46. {
  47. g.setColour (findColour (secondaryBackgroundColourId));
  48. g.fillRect (getLocalBounds().reduced (12, 0));
  49. }
  50. void resized() override
  51. {
  52. auto bounds = getLocalBounds().reduced (12, 0);
  53. header.setBounds (bounds.removeFromTop (40));
  54. bounds.reduce (10, 0);
  55. list.setBounds (bounds.removeFromTop (list.getRowPosition (getNumRows() - 1, true).getBottom() + 20));
  56. if (bounds.getHeight() < 35)
  57. {
  58. parentSizeChanged();
  59. }
  60. else
  61. {
  62. auto buttonRow = bounds.removeFromTop (35);
  63. setCopyModeButton.setBounds (buttonRow.removeFromLeft (jmin (200, bounds.getWidth() / 3)));
  64. buttonRow.removeFromLeft (8);
  65. copyPathButton.setBounds (buttonRow.removeFromLeft (jmin (200, bounds.getWidth() / 3)));
  66. buttonRow.removeFromLeft (8);
  67. globalPathsButton.setBounds (buttonRow.removeFromLeft (jmin (200, bounds.getWidth() / 3)));
  68. }
  69. }
  70. void parentSizeChanged() override
  71. {
  72. auto width = jmax (550, getParentWidth());
  73. auto y = list.getRowPosition (getNumRows() - 1, true).getBottom() + 200;
  74. y = jmax (getParentHeight(), y);
  75. setSize (width, y);
  76. }
  77. int getNumRows() override
  78. {
  79. return project.getEnabledModules().getNumModules();
  80. }
  81. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
  82. {
  83. ignoreUnused (height);
  84. Rectangle<int> bounds (0, 0, width, height);
  85. g.setColour (rowIsSelected ? findColour (defaultHighlightColourId) : findColour (rowNumber % 2 == 0 ? widgetBackgroundColourId
  86. : secondaryWidgetBackgroundColourId));
  87. g.fillRect (bounds.withTrimmedBottom (1));
  88. bounds.removeFromLeft (5);
  89. g.setColour (rowIsSelected ? findColour (defaultHighlightedTextColourId) : findColour (widgetTextColourId));
  90. //==============================================================================
  91. auto moduleID = project.getEnabledModules().getModuleID (rowNumber);
  92. g.drawFittedText (moduleID, bounds.removeFromLeft (roundToInt (listHeader->getProportionAtIndex (0) * width)), Justification::centredLeft, 1);
  93. //==============================================================================
  94. auto version = project.getEnabledModules().getModuleInfo (moduleID).getVersion();
  95. if (version.isEmpty())
  96. version = "?";
  97. g.drawFittedText (version, bounds.removeFromLeft (roundToInt (listHeader->getProportionAtIndex (1) * width)), Justification::centredLeft, 1);
  98. //==============================================================================
  99. g.drawFittedText (String (project.getEnabledModules().shouldCopyModuleFilesLocally (moduleID) ? "Yes" : "No"),
  100. bounds.removeFromLeft (roundToInt (listHeader->getProportionAtIndex (2) * width)), Justification::centredLeft, 1);
  101. //==============================================================================
  102. String pathText;
  103. if (project.getEnabledModules().shouldUseGlobalPath (moduleID))
  104. {
  105. pathText = "Global";
  106. }
  107. else
  108. {
  109. StringArray paths;
  110. for (Project::ExporterIterator exporter (project); exporter.next();)
  111. paths.addIfNotAlreadyThere (exporter->getPathForModuleString (moduleID).trim());
  112. pathText = paths.joinIntoString (", ");
  113. }
  114. g.drawFittedText (pathText, bounds.removeFromLeft (roundToInt (listHeader->getProportionAtIndex (3) * width)), Justification::centredLeft, 1);
  115. }
  116. void listBoxItemDoubleClicked (int row, const MouseEvent&) override
  117. {
  118. auto moduleID = project.getEnabledModules().getModuleID (row);
  119. if (moduleID.isNotEmpty())
  120. if (auto* pcc = findParentComponentOfClass<ProjectContentComponent>())
  121. pcc->showModule (moduleID);
  122. }
  123. void deleteKeyPressed (int row) override
  124. {
  125. project.getEnabledModules().removeModule (project.getEnabledModules().getModuleID (row));
  126. }
  127. void lookAndFeelChanged() override
  128. {
  129. setCopyModeButton.setColour (TextButton::buttonColourId, findColour (secondaryButtonBackgroundColourId));
  130. copyPathButton.setColour (TextButton::buttonColourId, findColour (defaultButtonBackgroundColourId));
  131. globalPathsButton.setColour (TextButton::buttonColourId, findColour (defaultButtonBackgroundColourId));
  132. }
  133. private:
  134. enum
  135. {
  136. nameCol = 1,
  137. versionCol,
  138. copyCol,
  139. pathCol
  140. };
  141. Project& project;
  142. ValueTree modulesValueTree;
  143. ContentViewHeader header { "Modules", { getIcons().modules, Colours::transparentBlack } };
  144. ListBox list;
  145. ListBoxHeader* listHeader;
  146. TextButton setCopyModeButton { "Set copy-mode for all modules..." };
  147. TextButton copyPathButton { "Set paths for all modules..." };
  148. TextButton globalPathsButton { "Enable/disable global path for modules..." };
  149. std::map<String, var> modulePathClipboard;
  150. void valueTreePropertyChanged (ValueTree&, const Identifier&) override { itemChanged(); }
  151. void valueTreeChildAdded (ValueTree&, ValueTree&) override { itemChanged(); }
  152. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override { itemChanged(); }
  153. void valueTreeChildOrderChanged (ValueTree&, int, int) override { itemChanged(); }
  154. void valueTreeParentChanged (ValueTree&) override { itemChanged(); }
  155. void itemChanged()
  156. {
  157. list.updateContent();
  158. resized();
  159. repaint();
  160. }
  161. static void setLocalCopyModeForAllModules (Project& project, bool copyLocally)
  162. {
  163. auto& modules = project.getEnabledModules();
  164. for (auto i = modules.getNumModules(); --i >= 0;)
  165. modules.shouldCopyModuleFilesLocallyValue (modules.getModuleID (i)) = copyLocally;
  166. }
  167. void showCopyModeMenu()
  168. {
  169. PopupMenu m;
  170. m.addItem (PopupMenu::Item ("Set all modules to copy locally")
  171. .setAction ([&] { setLocalCopyModeForAllModules (project, true); }));
  172. m.addItem (PopupMenu::Item ("Set all modules to not copy locally")
  173. .setAction ([&] { setLocalCopyModeForAllModules (project, false); }));
  174. m.showMenuAsync (PopupMenu::Options().withTargetComponent (setCopyModeButton));
  175. }
  176. static void setAllModulesToUseGlobalPaths (Project& project, bool useGlobal)
  177. {
  178. auto& modules = project.getEnabledModules();
  179. for (auto moduleID : modules.getAllModules())
  180. modules.shouldUseGlobalPathValue (moduleID) = useGlobal;
  181. }
  182. static void setSelectedModulesToUseGlobalPaths (Project& project, SparseSet<int> selected, bool useGlobal)
  183. {
  184. auto& modules = project.getEnabledModules();
  185. for (int i = 0; i < selected.size(); ++i)
  186. modules.shouldUseGlobalPathValue (modules.getModuleID (selected[i])) = useGlobal;
  187. }
  188. void showGlobalPathsMenu()
  189. {
  190. PopupMenu m;
  191. m.addItem (PopupMenu::Item ("Set all modules to use global paths")
  192. .setAction ([&] { setAllModulesToUseGlobalPaths (project, true); }));
  193. m.addItem (PopupMenu::Item ("Set all modules to not use global paths")
  194. .setAction ([&] { setAllModulesToUseGlobalPaths (project, false); }));
  195. m.addItem (PopupMenu::Item ("Set selected modules to use global paths")
  196. .setEnabled (list.getNumSelectedRows() > 0)
  197. .setAction ([&] { setSelectedModulesToUseGlobalPaths (project, list.getSelectedRows(), true); }));
  198. m.addItem (PopupMenu::Item ("Set selected modules to not use global paths")
  199. .setEnabled (list.getNumSelectedRows() > 0)
  200. .setAction ([&] { setSelectedModulesToUseGlobalPaths (project, list.getSelectedRows(), false); }));
  201. m.showMenuAsync (PopupMenu::Options().withTargetComponent (globalPathsButton));
  202. }
  203. void showSetPathsMenu()
  204. {
  205. PopupMenu m;
  206. auto moduleToCopy = project.getEnabledModules().getModuleID (list.getSelectedRow());
  207. if (moduleToCopy.isNotEmpty())
  208. {
  209. m.addItem (PopupMenu::Item ("Copy the paths from the module '" + moduleToCopy + "' to all other modules")
  210. .setAction ([this, moduleToCopy]
  211. {
  212. auto& moduleList = project.getEnabledModules();
  213. for (Project::ExporterIterator exporter (project); exporter.next();)
  214. {
  215. for (int i = 0; i < moduleList.getNumModules(); ++i)
  216. {
  217. auto modID = moduleList.getModuleID (i);
  218. if (modID != moduleToCopy)
  219. exporter->getPathForModuleValue (modID) = exporter->getPathForModuleValue (moduleToCopy).get();
  220. }
  221. }
  222. list.repaint();
  223. }));
  224. m.addItem (PopupMenu::Item ("Copy paths from selected module")
  225. .setEnabled (list.getNumSelectedRows() == 1)
  226. .setAction ([this, moduleToCopy]
  227. {
  228. modulePathClipboard.clear();
  229. for (Project::ExporterIterator exporter (project); exporter.next();)
  230. modulePathClipboard[exporter->getName()] = exporter->getPathForModuleValue (moduleToCopy).get();
  231. list.repaint();
  232. }));
  233. m.addItem (PopupMenu::Item ("Paste paths to selected modules")
  234. .setEnabled (! modulePathClipboard.empty())
  235. .setAction ([this]
  236. {
  237. for (int selectionId = 0; selectionId < list.getNumSelectedRows(); ++selectionId)
  238. {
  239. auto rowNumber = list.getSelectedRow (selectionId);
  240. auto modID = project.getEnabledModules().getModuleID (rowNumber);
  241. for (Project::ExporterIterator exporter (project); exporter.next();)
  242. exporter->getPathForModuleValue (modID) = modulePathClipboard[exporter->getName()];
  243. }
  244. list.repaint();
  245. }));
  246. }
  247. else
  248. {
  249. m.addItem (PopupMenu::Item ("(Select a module in the list above to use this option)")
  250. .setEnabled (false));
  251. }
  252. m.showMenuAsync (PopupMenu::Options()
  253. .withDeletionCheck (*this)
  254. .withTargetComponent (copyPathButton));
  255. }
  256. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModulesInformationComponent)
  257. };