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.

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