Skip to content

Commit 93ae1ac

Browse files
committed
Added PassThrough component via right clicking on links
1 parent df18e9c commit 93ae1ac

14 files changed

+262
-53
lines changed

Components.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Components/RepeatComponent.h"
1515
#include "Components/ADSRComponent.h"
1616
#include "Components/OutputComponent.h"
17+
#include "Components/PassThroughComponent.h"
1718

1819
#endif // COMPONENTS_H
1920

Components/Component.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class Component
3636
QString m_name;
3737
bool m_hasOutput = true;
3838
bool m_removable = true;
39+
bool m_showTitle = true;
40+
bool m_showBackground = true;
41+
bool m_canDragPins = true;
3942

4043
public:
4144
Component();
@@ -45,6 +48,9 @@ class Component
4548

4649
bool isRemovable() const { return m_removable; }
4750
bool hasOutput() const { return m_hasOutput; }
51+
bool showTitle() const { return m_showTitle; }
52+
bool showBackground() const { return m_showBackground; }
53+
bool canDragPins() const { return m_canDragPins; }
4854
virtual qreal getOutput(qreal _time) = 0;
4955

5056
ComponentInput* getInput(int _index);

Components/ComponentFactory.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Benoit Pelletier
2+
* Copyright 2019-2021 Benoit Pelletier
33
*
44
* This file is part of Sound Generator.
55
*
@@ -21,6 +21,7 @@
2121
#include "ComponentFactory.h"
2222
#include "../Components.h"
2323

24+
// TODO: Change those components into data-driven (difficult part is the algorithms and variables for generators)
2425
Component * ComponentFactory::CreateComponent(const QString & _compName)
2526
{
2627
Component* pComp = nullptr;
@@ -29,6 +30,10 @@ Component * ComponentFactory::CreateComponent(const QString & _compName)
2930
{
3031
pComp = new OutputComponent();
3132
}
33+
else if (_compName == "PassThrough")
34+
{
35+
pComp = new PassThroughComponent();
36+
}
3237
else if (_compName == "Sinusoidal")
3338
{
3439
pComp = new SinusComponent();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2021 Benoit Pelletier
3+
*
4+
* This file is part of Sound Generator.
5+
*
6+
* Sound Generator is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Sound Generator is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with Sound Generator. If not, see <https://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
#include "PassThroughComponent.h"
22+
23+
PassThroughComponent::PassThroughComponent()
24+
: Component()
25+
{
26+
m_name = "PassThrough";
27+
28+
m_inputs.push_back(ComponentInput("", this));
29+
30+
m_showTitle = false;
31+
m_showBackground = false;
32+
m_canDragPins = false;
33+
m_inputs[0].setDefaultValue(0.0);
34+
m_inputs[0].setEditable(false);
35+
}
36+
37+
qreal PassThroughComponent::getOutput(qreal _time)
38+
{
39+
return m_inputs[0].getValue(_time);
40+
}

Components/PassThroughComponent.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2021 Benoit Pelletier
3+
*
4+
* This file is part of Sound Generator.
5+
*
6+
* Sound Generator is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Sound Generator is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with Sound Generator. If not, see <https://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
#ifndef PASSTRHOUGHCOMPONENT_H
22+
#define PASSTRHOUGHCOMPONENT_H
23+
24+
#include "Component.h"
25+
26+
class PassThroughComponent : public Component
27+
{
28+
public:
29+
PassThroughComponent();
30+
virtual qreal getOutput(qreal _time) override;
31+
};
32+
33+
#endif // PASSTRHOUGHCOMPONENT_H

NodalScene.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "UndoCommands/DeleteComponentCommand.h"
3232
#include "UndoCommands/MoveComponentCommand.h"
3333
#include "UndoCommands/SelectComponentCommand.h"
34+
#include "UndoCommands/LinkPinCommand.h"
35+
#include "UndoCommands/UnlinkPinCommand.h"
3436
#include "AudioSettings.h"
3537

3638
// JSON keys
@@ -69,13 +71,56 @@ NodeItem* NodalScene::createComponent(QString _componentName, qreal _width)
6971
{
7072
m_creationPosition = m_contextPosition;
7173
}
72-
qDebug() << "Create Component";
74+
qDebug() << "Create Component: " << _componentName;
7375
NodeItem* item = createNode(_componentName, _width);
7476
m_creationPosition += QPoint(20, 20);
7577
setDirty();
7678
return item;
7779
}
7880

81+
NodeItem* NodalScene::createPassThrough(LinkItem* _link)
82+
{
83+
Q_ASSERT(_link->firstPin() != nullptr && _link->secondPin() != nullptr);
84+
85+
PinInputItem* pinIn = nullptr;
86+
PinOutputItem* pinOut = nullptr;
87+
if (qgraphicsitem_cast<PinInputItem*>(_link->firstPin()) != nullptr)
88+
{
89+
pinIn = qgraphicsitem_cast<PinInputItem*>(_link->firstPin());
90+
pinOut = qgraphicsitem_cast<PinOutputItem*>(_link->secondPin());
91+
}
92+
else
93+
{
94+
pinIn = qgraphicsitem_cast<PinInputItem*>(_link->secondPin());
95+
pinOut = qgraphicsitem_cast<PinOutputItem*>(_link->firstPin());
96+
}
97+
98+
Q_ASSERT(pinIn != nullptr && pinOut != nullptr);
99+
100+
QPointF tmp = m_creationPosition;
101+
if(m_isContextMenu)
102+
{
103+
m_creationPosition = m_contextPosition;
104+
}
105+
else
106+
{
107+
m_creationPosition = 0.5 * (pinIn->pos() + pinOut->pos());
108+
}
109+
110+
qDebug() << "Create PassThrough";
111+
112+
m_undoStack->beginMacro("Create PassThrough");
113+
NodeItem* item = createNode("PassThrough", 20);
114+
m_undoStack->push(new UnlinkPinCommand(this, pinOut, pinIn));
115+
m_undoStack->push(new LinkPinCommand(this, pinOut, item->getInput(0)));
116+
m_undoStack->push(new LinkPinCommand(this, item->getOutput(), pinIn));
117+
m_undoStack->endMacro();
118+
setDirty();
119+
120+
m_creationPosition = tmp;
121+
return item;
122+
}
123+
79124
void NodalScene::addNodeItem(NodeItem *_item)
80125
{
81126
addItem(_item);
@@ -234,7 +279,6 @@ void NodalScene::reset()
234279
m_nodeList[0]->setSelected(false);
235280
}
236281

237-
238282
int NodalScene::deleteSelection()
239283
{
240284
int nbRemoved = 0;
@@ -387,7 +431,6 @@ void NodalScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
387431
}
388432
}
389433

390-
391434
void NodalScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
392435
{
393436
QGraphicsScene::mouseReleaseEvent(event);

NodalScene.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <QGraphicsScene>
2525

2626
class NodeItem;
27+
class LinkItem;
2728
class QUndoStack;
2829
class AudioSettings;
2930

@@ -36,6 +37,7 @@ class NodalScene : public QGraphicsScene
3637
virtual ~NodalScene() override;
3738

3839
NodeItem* createComponent(QString _componentName, qreal _width = 200.0);
40+
NodeItem* createPassThrough(LinkItem* _link);
3941
void addNodeItem(NodeItem *_item);
4042
void removeNodeItem(NodeItem* _item);
4143

SoundGenerator.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ INCLUDEPATH += UI
2727
CONFIG += c++11
2828

2929
SOURCES += \
30+
Components/PassThroughComponent.cpp \
3031
NodalScene.cpp \
3132
UndoCommands/ChangeInputValueCommand.cpp \
3233
UndoCommands/CreateComponentCommand.cpp \
@@ -83,6 +84,7 @@ HEADERS += \
8384
Components/GeneratorComponent.h \
8485
Components/MultiplyComponent.h \
8586
Components/OutputComponent.h \
87+
Components/PassThroughComponent.h \
8688
Components/RandomComponent.h \
8789
Components/RepeatComponent.h \
8890
Components/SawToothComponent.h \

UI/NodalView.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ void NodalView::dropEvent(QDropEvent* _event)
185185
// must be implemented
186186
void NodalView::dragMoveEvent(QDragMoveEvent* _event)
187187
{
188-
Q_UNUSED(_event);
188+
Q_UNUSED(_event)
189189
}
190190

191191
// must be implemented
192192
void NodalView::dragLeaveEvent(QDragLeaveEvent* _event)
193193
{
194-
Q_UNUSED(_event);
194+
Q_UNUSED(_event)
195195
}
196196

197197
void NodalView::drawBackground(QPainter* _painter, const QRectF& _rect)
@@ -348,6 +348,7 @@ void NodalView::showCustomContextMenu(const QPoint& _pos)
348348
QAction act_breakAllLinks("Break all link(s)");
349349
QAction act_breakLink("Break link");
350350
QAction act_removeComponents("Remove Component(s)");
351+
QAction act_createPassThrough("Add passthrough");
351352

352353
QMenu myMenu;
353354

@@ -363,6 +364,7 @@ void NodalView::showCustomContextMenu(const QPoint& _pos)
363364
if(link != nullptr)
364365
{
365366
myMenu.addAction(&act_breakLink);
367+
myMenu.addAction(&act_createPassThrough);
366368
}
367369
else if(pin != nullptr && pin->isLinked())
368370
{
@@ -389,9 +391,12 @@ void NodalView::showCustomContextMenu(const QPoint& _pos)
389391
}
390392
else if (selectedAction == &act_removeComponents)
391393
{
392-
// something was chosen, do stuff
393394
getScene()->deleteSelection();
394395
}
396+
else if (selectedAction == &act_createPassThrough)
397+
{
398+
getScene()->createPassThrough(link);
399+
}
395400
else
396401
{
397402
// nothing was chosen

0 commit comments

Comments
 (0)