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.

342 lines
14KB

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