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.

384 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 ExporterItem : public ProjectTreeItemBase,
  22. private Value::Listener
  23. {
  24. public:
  25. ExporterItem (Project& p, ProjectExporter* e, int index)
  26. : project (p), exporter (e), configListTree (exporter->getConfigurations()),
  27. exporterIndex (index)
  28. {
  29. exporter->initialiseDependencyPathValues();
  30. configListTree.addListener (this);
  31. targetLocationValue.referTo (exporter->getTargetLocationValue());
  32. targetLocationValue.addListener (this);
  33. }
  34. int getItemHeight() const override { return 25; }
  35. bool canBeSelected() const override { return true; }
  36. bool mightContainSubItems() override { return exporter->getNumConfigurations() > 0; }
  37. String getUniqueName() const override { return "exporter_" + String (exporterIndex); }
  38. String getRenamingName() const override { return getDisplayName(); }
  39. String getDisplayName() const override { return exporter->getName(); }
  40. void setName (const String&) override {}
  41. bool isMissing() const override { return false; }
  42. String getTooltip() override { return getDisplayName(); }
  43. static Icon getIconForExporter (ProjectExporter* e)
  44. {
  45. if (e != nullptr)
  46. {
  47. if (e->isXcode()) return Icon (getIcons().xcode, Colours::transparentBlack);
  48. else if (e->isVisualStudio()) return Icon (getIcons().visualStudio, Colours::transparentBlack);
  49. else if (e->isAndroid()) return Icon (getIcons().android, Colours::transparentBlack);
  50. else if (e->isCodeBlocks()) return Icon (getIcons().codeBlocks, Colours::transparentBlack);
  51. else if (e->isMakefile()) return Icon (getIcons().linux, Colours::transparentBlack);
  52. else if (e->isCLion()) return Icon (getIcons().clion, Colours::transparentBlack);
  53. }
  54. return Icon();
  55. }
  56. Icon getIcon() const override
  57. {
  58. return getIconForExporter (exporter.get()).withColour (getContentColour (true));
  59. }
  60. void showDocument() override
  61. {
  62. showSettingsPage (new SettingsComp (*exporter));
  63. }
  64. void deleteItem() override
  65. {
  66. if (AlertWindow::showOkCancelBox (AlertWindow::WarningIcon, "Delete Exporter",
  67. "Are you sure you want to delete this export target?"))
  68. {
  69. closeSettingsPage();
  70. ValueTree parent (exporter->settings.getParent());
  71. parent.removeChild (exporter->settings, project.getUndoManagerFor (parent));
  72. }
  73. }
  74. void addSubItems() override
  75. {
  76. for (ProjectExporter::ConfigIterator config (*exporter); config.next();)
  77. addSubItem (new ConfigItem (config.config, *exporter));
  78. }
  79. void showPopupMenu() override
  80. {
  81. PopupMenu menu;
  82. menu.addItem (1, "Add a new configuration", exporter->supportsUserDefinedConfigurations());
  83. menu.addItem (2, "Save this exporter");
  84. menu.addSeparator();
  85. menu.addItem (3, "Delete this exporter");
  86. launchPopupMenu (menu);
  87. }
  88. void showAddMenu() override
  89. {
  90. PopupMenu menu;
  91. menu.addItem (1, "Add a new configuration", exporter->supportsUserDefinedConfigurations());
  92. launchPopupMenu (menu);
  93. }
  94. void handlePopupMenuResult (int resultCode) override
  95. {
  96. if (resultCode == 1)
  97. {
  98. exporter->addNewConfiguration (false);
  99. }
  100. else if (resultCode == 2)
  101. {
  102. const ScopedValueSetter<String> valueSetter (project.specifiedExporterToSave, exporter->getName(), {});
  103. project.save (true, true);
  104. }
  105. else if (resultCode == 3)
  106. {
  107. deleteAllSelectedItems();
  108. }
  109. }
  110. var getDragSourceDescription() override
  111. {
  112. return getParentItem()->getUniqueName() + "/" + String (exporterIndex);
  113. }
  114. bool isInterestedInDragSource (const DragAndDropTarget::SourceDetails& dragSourceDetails) override
  115. {
  116. return dragSourceDetails.description.toString().startsWith (getUniqueName());
  117. }
  118. void itemDropped (const DragAndDropTarget::SourceDetails& dragSourceDetails, int insertIndex) override
  119. {
  120. auto oldIndex = indexOfConfig (dragSourceDetails.description.toString().fromLastOccurrenceOf ("||", false, false));
  121. if (oldIndex >= 0)
  122. configListTree.moveChild (oldIndex, insertIndex, project.getUndoManagerFor (configListTree));
  123. }
  124. int indexOfConfig (const String& configName)
  125. {
  126. int i = 0;
  127. for (ProjectExporter::ConfigIterator config (*exporter); config.next(); ++i)
  128. if (config->getName() == configName)
  129. return i;
  130. return -1;
  131. }
  132. //==============================================================================
  133. void valueTreeChildAdded (ValueTree& parentTree, ValueTree&) override { refreshIfNeeded (parentTree); }
  134. void valueTreeChildRemoved (ValueTree& parentTree, ValueTree&, int) override { refreshIfNeeded (parentTree); }
  135. void valueTreeChildOrderChanged (ValueTree& parentTree, int, int) override { refreshIfNeeded (parentTree); }
  136. void refreshIfNeeded (ValueTree& changedTree)
  137. {
  138. if (changedTree == configListTree)
  139. refreshSubItems();
  140. }
  141. private:
  142. Project& project;
  143. std::unique_ptr<ProjectExporter> exporter;
  144. ValueTree configListTree;
  145. int exporterIndex;
  146. Value targetLocationValue;
  147. void valueChanged (Value& value) override
  148. {
  149. if (value == exporter->getTargetLocationValue())
  150. refreshSubItems();
  151. }
  152. //==============================================================================
  153. struct SettingsComp : public Component
  154. {
  155. SettingsComp (ProjectExporter& exp)
  156. : group (exp.getName(),
  157. ExporterItem::getIconForExporter (&exp),
  158. exp.getDescription())
  159. {
  160. addAndMakeVisible (group);
  161. PropertyListBuilder props;
  162. exp.createPropertyEditors (props);
  163. group.setProperties (props);
  164. parentSizeChanged();
  165. }
  166. void parentSizeChanged() override { updateSize (*this, group); }
  167. void resized() override { group.setBounds (getLocalBounds().withTrimmedLeft (12)); }
  168. PropertyGroupComponent group;
  169. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SettingsComp)
  170. };
  171. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ExporterItem)
  172. };
  173. //==============================================================================
  174. class ConfigItem : public ProjectTreeItemBase
  175. {
  176. public:
  177. ConfigItem (const ProjectExporter::BuildConfiguration::Ptr& conf, ProjectExporter& e)
  178. : config (conf), exporter (e), configTree (config->config)
  179. {
  180. jassert (config != nullptr);
  181. configTree.addListener (this);
  182. }
  183. bool isMissing() const override { return false; }
  184. bool canBeSelected() const override { return true; }
  185. bool mightContainSubItems() override { return false; }
  186. String getUniqueName() const override { return "config_" + config->getName(); }
  187. String getRenamingName() const override { return getDisplayName(); }
  188. String getDisplayName() const override { return config->getName(); }
  189. void setName (const String&) override {}
  190. Icon getIcon() const override { return Icon (getIcons().config, getContentColour (true)); }
  191. void itemOpennessChanged (bool) override {}
  192. void showDocument() override
  193. {
  194. showSettingsPage (new SettingsComp (*config));
  195. }
  196. void deleteItem() override
  197. {
  198. if (AlertWindow::showOkCancelBox (AlertWindow::WarningIcon, "Delete Configuration",
  199. "Are you sure you want to delete this configuration?"))
  200. {
  201. closeSettingsPage();
  202. config->removeFromExporter();
  203. }
  204. }
  205. void showPopupMenu() override
  206. {
  207. bool enabled = exporter.supportsUserDefinedConfigurations();
  208. PopupMenu menu;
  209. menu.addItem (1, "Create a copy of this configuration", enabled);
  210. menu.addSeparator();
  211. menu.addItem (2, "Delete this configuration", enabled);
  212. launchPopupMenu (menu);
  213. }
  214. void handlePopupMenuResult (int resultCode) override
  215. {
  216. if (resultCode == 1)
  217. exporter.addNewConfigurationFromExisting (*config);
  218. else if (resultCode == 2)
  219. deleteAllSelectedItems();
  220. }
  221. var getDragSourceDescription() override
  222. {
  223. return getParentItem()->getUniqueName() + "||" + config->getName();
  224. }
  225. void valueTreePropertyChanged (ValueTree&, const Identifier&) override { repaintItem(); }
  226. private:
  227. ProjectExporter::BuildConfiguration::Ptr config;
  228. ProjectExporter& exporter;
  229. ValueTree configTree;
  230. //==============================================================================
  231. class SettingsComp : public Component
  232. {
  233. public:
  234. SettingsComp (ProjectExporter::BuildConfiguration& conf)
  235. : group (conf.exporter.getName() + " - " + conf.getName(), Icon (getIcons().config, Colours::transparentBlack))
  236. {
  237. addAndMakeVisible (group);
  238. PropertyListBuilder props;
  239. conf.createPropertyEditors (props);
  240. group.setProperties (props);
  241. parentSizeChanged();
  242. }
  243. void parentSizeChanged() override { updateSize (*this, group); }
  244. void resized() override { group.setBounds (getLocalBounds().withTrimmedLeft (12)); }
  245. private:
  246. PropertyGroupComponent group;
  247. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SettingsComp)
  248. };
  249. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConfigItem)
  250. };
  251. //==============================================================================
  252. class ExportersTreeRoot : public ProjectTreeItemBase
  253. {
  254. public:
  255. ExportersTreeRoot (Project& p)
  256. : project (p),
  257. exportersTree (project.getExporters())
  258. {
  259. exportersTree.addListener (this);
  260. }
  261. bool isRoot() const override { return true; }
  262. bool canBeSelected() const override { return true; }
  263. bool isMissing() const override { return false; }
  264. bool mightContainSubItems() override { return project.getNumExporters() > 0; }
  265. String getUniqueName() const override { return "exporters"; }
  266. String getRenamingName() const override { return getDisplayName(); }
  267. String getDisplayName() const override { return "Exporters"; }
  268. void setName (const String&) override {}
  269. Icon getIcon() const override { return project.getMainGroup().getIcon (isOpen()).withColour (getContentColour (true)); }
  270. void showPopupMenu() override
  271. {
  272. if (auto* pcc = getProjectContentComponent())
  273. pcc->showNewExporterMenu();
  274. }
  275. void addSubItems() override
  276. {
  277. int i = 0;
  278. for (Project::ExporterIterator exporter (project); exporter.next(); ++i)
  279. addSubItem (new TreeItemTypes::ExporterItem (project, exporter.exporter.release(), i));
  280. }
  281. bool isInterestedInDragSource (const DragAndDropTarget::SourceDetails& dragSourceDetails) override
  282. {
  283. return dragSourceDetails.description.toString().startsWith (getUniqueName());
  284. }
  285. void itemDropped (const DragAndDropTarget::SourceDetails& dragSourceDetails, int insertIndex) override
  286. {
  287. int oldIndex = dragSourceDetails.description.toString().getTrailingIntValue();
  288. exportersTree.moveChild (oldIndex, jmax (0, insertIndex), project.getUndoManagerFor (exportersTree));
  289. }
  290. void itemOpennessChanged (bool isNowOpen) override
  291. {
  292. if (isNowOpen)
  293. refreshSubItems();
  294. }
  295. void removeExporter (int index)
  296. {
  297. if (auto* exporter = dynamic_cast<TreeItemTypes::ExporterItem*> (getSubItem (index)))
  298. exporter->deleteItem();
  299. }
  300. private:
  301. Project& project;
  302. ValueTree exportersTree;
  303. //==========================================================================
  304. void valueTreeChildAdded (ValueTree& parentTree, ValueTree&) override { refreshIfNeeded (parentTree); }
  305. void valueTreeChildRemoved (ValueTree& parentTree, ValueTree&, int) override { refreshIfNeeded (parentTree); }
  306. void valueTreeChildOrderChanged (ValueTree& parentTree, int, int) override { refreshIfNeeded (parentTree); }
  307. void refreshIfNeeded (ValueTree& changedTree)
  308. {
  309. if (changedTree == exportersTree)
  310. refreshSubItems();
  311. }
  312. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ExportersTreeRoot)
  313. };