/
jackcontextgraphicsscene.cpp
233 lines (208 loc) · 8.64 KB
/
jackcontextgraphicsscene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Copyright 2011 Arne Jacobs <jarne@jarne.de>
This file is part of elektrocillin.
Elektrocillin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Elektrocillin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Elektrocillin. If not, see <http://www.gnu.org/licenses/>.
*/
#include "jackcontextgraphicsscene.h"
#include "graphicsportconnectionitem.h"
#include "metajack/recursivejackcontext.h"
#include "metajack/metajackcontext.h"
#include <QMessageBox>
#include <QApplication>
#include <QFile>
JackContextGraphicsScene::JackContextGraphicsScene() :
graphicsClientItemsClient(new GraphicsClientItemsClient(this)),
waitForMacroPosition(false),
waitForModulePosition(false)
{
setBackgroundBrush(QBrush(QColor("lightsteelblue")));
QGraphicsRectItem *dummy = new QGraphicsRectItem(-5000, -5000, 10000, 10000, 0, this);
dummy->setVisible(false);
}
JackContextGraphicsScene::~JackContextGraphicsScene()
{
delete graphicsClientItemsClient;
}
void JackContextGraphicsScene::saveSession(QDataStream &stream)
{
graphicsClientItemsClient->saveState(stream);
}
void JackContextGraphicsScene::loadSession(QDataStream &stream)
{
graphicsClientItemsClient->loadState(stream);
}
void JackContextGraphicsScene::loadMacro(const QString &fileName)
{
macroFileName = fileName;
waitForMacroPosition = true;
// set a special cursor to show the user what we are waiting for:
QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
// also send a message for the user to read:
messageChanged("Left-click anywhere in the canvas to place the macro to load. Click with any other mouse button to abort macro creation.");
}
void JackContextGraphicsScene::changeToCurrentContext()
{
delete graphicsClientItemsClient;
clear();
QGraphicsRectItem *dummy = new QGraphicsRectItem(-5000, -5000, 10000, 10000, 0, this);
dummy->setVisible(false);
graphicsClientItemsClient = new GraphicsClientItemsClient(this);
}
void JackContextGraphicsScene::deleteClient(const QString &clientName)
{
graphicsClientItemsClient->deleteClient(clientName);
}
bool JackContextGraphicsScene::editMacro(GraphicsClientItem *clientItem)
{
if (clientItem->isMacroItem()) {
// get the macro's wrapper client:
JackContext *jackContext = RecursiveJackContext::getInstance()->getContextByClientName(clientItem->getClientName().toAscii().data());
if (jackContext) {
// make the macro's wrapper client the new context:
RecursiveJackContext::getInstance()->pushExistingContext(jackContext);
// change to that context:
changeToCurrentContext();
contextLevelChanged(RecursiveJackContext::getInstance()->getContextStackSize() - 1);
return true;
}
}
return false;
}
void JackContextGraphicsScene::showAllInnerItems(bool visible)
{
graphicsClientItemsClient->showAllInnerItems(visible);
}
void JackContextGraphicsScene::play()
{
graphicsClientItemsClient->play();
}
void JackContextGraphicsScene::stop()
{
graphicsClientItemsClient->stop();
}
void JackContextGraphicsScene::rewind()
{
graphicsClientItemsClient->rewind();
}
void JackContextGraphicsScene::exitCurrentMacro()
{
if (RecursiveJackContext::getInstance()->getContextStackSize() > 1) {
// test if the current context has any connections to the outside:
bool deleteMacro = false;
MetaJackContext *macroContext = (MetaJackContext*)RecursiveJackContext::getInstance()->getCurrentContext();
if (!macroContext->hasWrapperPorts()) {
// ask the user wether the macro should be left (which means it will be deleted):
deleteMacro = (QMessageBox::warning(0, "Delete macro?", "The macro has no outside connections and will therefore be deleted if exited. Are you sure you want to delete the macro?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes);
if (!deleteMacro) {
return;
}
}
// pop the current jack context:
RecursiveJackContext::getInstance()->popContext();
// change to that context:
changeToCurrentContext();
contextLevelChanged(RecursiveJackContext::getInstance()->getContextStackSize() - 1);
// delete the macro if necessary:
if (deleteMacro) {
RecursiveJackContext::getInstance()->deleteContext(macroContext);
}
}
}
void JackContextGraphicsScene::editSelectedMacro()
{
// determine if the item in focus is a macro:
QList<QGraphicsItem*> items = selectedItems();
for (QList<QGraphicsItem*>::iterator i = items.begin(); i != items.end(); i++) {
if (GraphicsClientItem *clientItem = dynamic_cast<GraphicsClientItem*>(*i)) {
if (editMacro(clientItem)) {
return;
}
}
}
}
void JackContextGraphicsScene::createNewMacro()
{
if (!waitForMacroPosition) {
waitForMacroPosition = true;
// set a special cursor to show the user what we are waiting for:
QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
// also send a message for the user to read:
messageChanged("Left-click anywhere in the canvas to place the new macro. Click with any other mouse button to abort macro creation.");
}
}
void JackContextGraphicsScene::createNewModule(QString factoryName)
{
if (!waitForModulePosition) {
waitForModulePosition = true;
this->factoryName = factoryName;
// set a special cursor to show the user what we are waiting for:
QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
// also send a message for the user to read:
messageChanged("Left-click anywhere in the canvas to place the new module. Click with any other mouse button to abort module creation.");
}
}
void JackContextGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
if (waitForMacroPosition || waitForModulePosition) {
mouseEvent->accept();
if (mouseEvent->button() == Qt::LeftButton) {
if (waitForMacroPosition) {
createNewMacro(mouseEvent->scenePos());
} else if (waitForModulePosition) {
createNewModule(factoryName, mouseEvent->scenePos());
}
}
waitForMacroPosition = waitForModulePosition = false;
// reset cursor and message to normal:
QApplication::restoreOverrideCursor();
messageChanged(QString());
} else {
QGraphicsScene::mousePressEvent(mouseEvent);
}
}
void JackContextGraphicsScene::createNewMacro(QPointF pos)
{
// create a new wrapper client:
MetaJackContext *context = (MetaJackContext*)RecursiveJackContext::getInstance()->pushNewContext("macro", 1);
graphicsClientItemsClient->setClientItemPositionByName(context->getWrapperClientName(), pos);
// is there anything to load?
if (!macroFileName.isNull()) {
// load into the new context:
QFile file(macroFileName);
file.open(QIODevice::ReadOnly);
QDataStream stream(&file);
// load system client positions and save them in settings:
QString contextName = context->get_name();
QMap<QString, QPointF> clientItemPositionMap;
stream >> clientItemPositionMap;
for (QMap<QString, QPointF>::iterator i = clientItemPositionMap.begin(); i != clientItemPositionMap.end(); i++) {
graphicsClientItemsClient->getSettings()->setValue("position/" + contextName + "/" + i.key(), i.value().toPoint());
}
// load the context clients:
RecursiveJackContext::getInstance()->loadCurrentContext(stream, JackClientSerializer::getInstance());
RecursiveJackContext::getInstance()->popContext();
} else {
// change to the new context:
changeToCurrentContext();
contextLevelChanged(RecursiveJackContext::getInstance()->getContextStackSize() - 1);
}
}
void JackContextGraphicsScene::createNewModule(QString factoryName, QPointF pos)
{
JackClientFactory *factory = JackClientSerializer::getInstance()->getFactoryByName(factoryName);
if (factory) {
clearSelection();
JackClient *client = factory->createClient(factory->getName());
client->setClientItemPosition(pos);
client->activate();
}
}