From e4bfa239eb5ee1ba887b2ef6f81f233e43a29e35 Mon Sep 17 00:00:00 2001
From: jsiegle <jsiegle@mit.edu>
Date: Fri, 24 Jan 2014 21:33:12 -0500
Subject: [PATCH] Allow editors to be renamed

---
 Source/Processors/Editors/ChannelSelector.cpp |  2 +-
 Source/Processors/Editors/GenericEditor.cpp   | 28 ++++++--
 Source/Processors/Editors/GenericEditor.h     |  7 ++
 Source/UI/EditorViewport.cpp                  | 64 ++++++++++++++++++-
 Source/UI/EditorViewport.h                    |  9 ++-
 Source/UI/GraphViewer.cpp                     | 61 ++----------------
 Source/UI/GraphViewer.h                       |  7 +-
 7 files changed, 111 insertions(+), 67 deletions(-)

diff --git a/Source/Processors/Editors/ChannelSelector.cpp b/Source/Processors/Editors/ChannelSelector.cpp
index 569018be7..de724f5d1 100755
--- a/Source/Processors/Editors/ChannelSelector.cpp
+++ b/Source/Processors/Editors/ChannelSelector.cpp
@@ -586,7 +586,7 @@ void ChannelSelector::buttonClicked(Button* button)
             {
                 editor->getProcessorGraph()->
                 getRecordNode()->
-                setChannelStatus(ch, status);
+                    setChannelStatus(ch, status);
             }
             else     // change parameter directly
             {
diff --git a/Source/Processors/Editors/GenericEditor.cpp b/Source/Processors/Editors/GenericEditor.cpp
index a06d7330e..7590062cb 100755
--- a/Source/Processors/Editors/GenericEditor.cpp
+++ b/Source/Processors/Editors/GenericEditor.cpp
@@ -66,6 +66,7 @@ void GenericEditor::constructorInitialize(GenericProcessor* owner, bool useDefau
 {
 
     name = getAudioProcessor()->getName();
+    displayName = name;
 
     nodeId = owner->getNodeId();
 
@@ -118,6 +119,20 @@ void GenericEditor::updateName()
     repaint();
 }
 
+void GenericEditor::setDisplayName(const String& string)
+{
+    displayName = string;
+    getGraphViewer()->updateNodeLocations();
+    repaint();
+}
+
+
+String GenericEditor::getDisplayName()
+{
+    return displayName;
+}
+
+
 void GenericEditor::addParameterEditors(bool useDefaultParameterEditors=true)
 {
     if (useDefaultParameterEditors)
@@ -345,14 +360,14 @@ void GenericEditor::paint(Graphics& g)
     // draw title
     if (!isCollapsed)
     {
-        if (!getProcessor()->isMerger() && !getProcessor()->isSplitter())
-            g.drawText(name+" ("+String(nodeId)+")", 6, 5, 500, 15, Justification::left, false);
-        else
-            g.drawText(name, 6, 5, 500, 15, Justification::left, false);
+       // if (!getProcessor()->isMerger() && !getProcessor()->isSplitter())
+      //      g.drawText(name+" ("+String(nodeId)+")", 6, 5, 500, 15, Justification::left, false);
+       // else
+            g.drawText(displayName, 6, 5, 500, 15, Justification::left, false);
 
     } else {
         g.addTransform(AffineTransform::rotation(-M_PI/2.0));
-        g.drawText(name, -getHeight()+6, 5, 500, 15, Justification::left, false);
+        g.drawText(displayName, -getHeight()+6, 5, 500, 15, Justification::left, false);
         g.addTransform(AffineTransform::rotation(M_PI/2.0));
     }
 
@@ -607,6 +622,7 @@ void GenericEditor::saveEditorParameters(XmlElement* xml)
 {
 
     xml->setAttribute("isCollapsed", isCollapsed);
+    xml->setAttribute("displayName", displayName);
 
     saveCustomParameters(xml);
 
@@ -622,6 +638,8 @@ void GenericEditor::loadEditorParameters(XmlElement* xml)
         switchCollapsedState();
     }
 
+    displayName = xml->getStringAttribute("displayName", name);
+
     loadCustomParameters(xml);
 
 }
diff --git a/Source/Processors/Editors/GenericEditor.h b/Source/Processors/Editors/GenericEditor.h
index 18a459a4b..9b1311075 100755
--- a/Source/Processors/Editors/GenericEditor.h
+++ b/Source/Processors/Editors/GenericEditor.h
@@ -123,6 +123,12 @@ public:
     /** Updates name if processor ID changes. */
     void updateName();
 
+    /** Updates name on title bar. */
+    void setDisplayName(const String& string);
+
+    /** Get name on title bar. */
+    String getDisplayName();
+
     /** Determines how wide the editor will be drawn. */
     int desiredWidth;
 
@@ -332,6 +338,7 @@ private:
     void constructorInitialize(GenericProcessor* owner, bool useDefaultParameterEditors);
 
     String name;
+    String displayName;
 
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(GenericEditor);
 
diff --git a/Source/UI/EditorViewport.cpp b/Source/UI/EditorViewport.cpp
index eedb242a7..4957e92fe 100755
--- a/Source/UI/EditorViewport.cpp
+++ b/Source/UI/EditorViewport.cpp
@@ -70,6 +70,11 @@ EditorViewport::EditorViewport()
     currentId = 100;
     maxId = 100;
 
+    editorNamingLabel.setEditable(true);
+    editorNamingLabel.setBounds(0,0,100,20);
+    editorNamingLabel.setColour(Label::textColourId, Colours::white);
+    editorNamingLabel.addListener(this);
+
 }
 
 EditorViewport::~EditorViewport()
@@ -137,7 +142,7 @@ void EditorViewport::paint(Graphics& g)
     int x = insertionX + 15;
     int y = borderSize + 2;
     //int w = 30;
-    //int h = getHeight() - 2*(borderSize+2);
+    //int h = getHeight() - 2*(borderSize+2);get
 
     //if (editorArray.size() > 0)
     //{
@@ -671,6 +676,12 @@ void EditorViewport::selectEditor(GenericEditor* editor)
     }
 }
 
+void EditorViewport::labelTextChanged(Label* label)
+{
+
+    editorToUpdate->setDisplayName(label->getText());
+}
+
 void EditorViewport::mouseDown(const MouseEvent& e)
 {
 
@@ -703,6 +714,57 @@ void EditorViewport::mouseDown(const MouseEvent& e)
                 return;
             }
 
+            if (e.mods.isRightButtonDown())
+            {
+
+                if (!editorArray[i]->getCollapsedState() && e.y > 22)
+                     return;
+
+                if (editorArray[i]->isMerger() || editorArray[i]->isSplitter())
+                    return;
+
+                PopupMenu m;
+
+                if (editorArray[i]->getCollapsedState())
+                     m.addItem(3, "Uncollapse", true);
+                 else
+                    m.addItem(3, "Collapse", true);
+
+                if (canEdit)
+                    m.addItem(2, "Delete", true);
+                else
+                    m.addItem(2, "Delete", false);
+
+                m.addItem(1, "Rename", true);
+
+                const int result = m.show();
+
+                if (result == 1)
+                {
+                    editorNamingLabel.setText("", dontSendNotification);
+                    
+                    juce::Rectangle<int> rect1 = juce::Rectangle<int>(editorArray[i]->getScreenX()+20,editorArray[i]->getScreenY()+11,1,1);
+
+                    CallOutBox callOut(editorNamingLabel, rect1, nullptr);
+                    editorToUpdate = editorArray[i];
+                    callOut.runModalLoop();
+                   // editorNamingLabel.showEditor();
+                    //CallOutBox& myBox = CallOutBox::launchAsynchronously(&editorNamingLabel, rect1, nullptr);
+                    
+                    return;
+
+                } else if (result == 2)
+                {
+                    deleteNode(editorArray[i]);
+                    return;
+                } else if (result == 3)
+                {
+                    editorArray[i]->switchCollapsedState();
+                    refreshEditors();
+                    return;
+                }
+            }
+
             // make sure uncollapsed editors don't accept clicks outside their title bar
             if (!editorArray[i]->getCollapsedState() && e.y > 22)
                 return;
diff --git a/Source/UI/EditorViewport.h b/Source/UI/EditorViewport.h
index 242befc7d..d094ae177 100755
--- a/Source/UI/EditorViewport.h
+++ b/Source/UI/EditorViewport.h
@@ -64,7 +64,8 @@ class UIComponent;
 class EditorViewport  : public Component,
     public DragAndDropTarget,
     public AccessClass,
-    public Button::Listener
+    public Button::Listener,
+    public Label::Listener
 
 {
 public:
@@ -139,6 +140,9 @@ public:
     /** Called when one of the buttons the EditorViewport listens to has been clicked.*/
     void buttonClicked(Button* button);
 
+    /** Called when a label is changed.*/
+    void labelTextChanged(Label* label);
+
     /** Returns an array of pointers to SignalChainTabButtons (which themselves hold pointers to the sources of each signal chain). */
     Array<SignalChainTabButton*, CriticalSection> requestSignalChain()
     {
@@ -180,6 +184,7 @@ private:
     bool canEdit;
     GenericEditor* lastEditor;
     GenericEditor* lastEditorClicked;
+    GenericEditor* editorToUpdate;
 
     int selectionIndex;
 
@@ -213,6 +218,8 @@ private:
     int currentId;
     int maxId;
 
+    Label editorNamingLabel;
+
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(EditorViewport);
 
 };
diff --git a/Source/UI/GraphViewer.cpp b/Source/UI/GraphViewer.cpp
index 63d17bfab..edde3ac6e 100644
--- a/Source/UI/GraphViewer.cpp
+++ b/Source/UI/GraphViewer.cpp
@@ -64,6 +64,12 @@ void GraphViewer::removeAllNodes()
     
 }
 
+// void GenericEditor::updateNodeName(GenericEditor* editor)
+// {
+//     GraphNode* n = getNodeForEditor(editor);
+//     n->repaint();
+// }
+
 void GraphViewer::updateNodeLocations()
 {
     // set the initial locations
@@ -398,7 +404,7 @@ Array<GenericEditor*> GraphNode::getConnectedEditors()
 
 const String GraphNode::getName()
 {
-    return editor->getName();
+    return editor->getDisplayName();
 }
 
 Point<float> GraphNode::getCenterPoint()
@@ -422,59 +428,6 @@ void GraphNode::updateBoundaries()
     
     setBounds(20+horzShift*140, 20+getLevel()*40, 150, 50);
 
-    // float vertShift = -1;
-    // float horzShift = 0;
-    
-    // GenericEditor* ed = editor;
-    
-    // while (ed != nullptr)
-    // {
-    //     vertShift += 1;
-
-    //     GenericEditor* sourceEditor = ed->getSourceEditor();
-        
-    //     if (sourceEditor != nullptr)
-    //     {
-    //         if (sourceEditor->isSplitter())
-    //         {
-    //             if (sourceEditor->getPathForEditor(ed) == 1)
-    //                 horzShift += 1;
-    //             //else
-    //             //    horzShift -= 0.5;
-    //         }
-    //     }
-    //     ed = sourceEditor;
-    
-    // }
-
-    // ed = editor;
-
-    // if (ed->isSplitter())
-    // {
-    //     horzShift += 0.5;
-    // }
-    
-    // while (ed != nullptr)
-    // {
-
-    //     GenericEditor* destEditor = ed->getDestEditor();
-        
-    //     if (destEditor != nullptr)
-    //     {
-    //         if (destEditor->isSplitter())
-    //         {
-    //             horzShift += 0.5;
-    //         } else if (destEditor->isMerger())
-    //         {
-    //             if (destEditor->getPathForEditor(ed) == 1)
-    //                 horzShift += 1;
-    //         }
-    //     }
-    //     ed = destEditor;
-    
-    // }
-
-
 }
 
 void GraphNode::paint(Graphics& g)
diff --git a/Source/UI/GraphViewer.h b/Source/UI/GraphViewer.h
index 65017ee90..6d19f00c0 100644
--- a/Source/UI/GraphViewer.h
+++ b/Source/UI/GraphViewer.h
@@ -80,7 +80,6 @@ private:
     GenericEditor* editor;
     
     Font labelFont;
-
     
     bool mouseOver;
 
@@ -89,7 +88,6 @@ private:
 
 
 class GraphViewer : public Component
-
 {
 public:
     GraphViewer();
@@ -101,7 +99,7 @@ public:
     void addNode(GenericEditor* editor);
     void removeNode(GenericEditor* editor);
     void removeAllNodes();
-    void updateNodeLocations();;
+    void updateNodeLocations();
 
     int nodesAtLevel(int lvl);
     int getHorizontalShift(GraphNode*);
@@ -111,8 +109,7 @@ private:
 
     void connectNodes(int, int, Graphics&);
     void checkLayout(GraphNode*);
-    
-    
+
     int indexOfEditor(GenericEditor* editor);
     
     Font labelFont;
-- 
GitLab