~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to arts/builder/structure.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    /*
 
2
 
 
3
    Copyright (C) 1999 Stefan Westerfeld
 
4
                       stefan@space.twc.de
 
5
 
 
6
    This program 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 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program 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 this program; if not, write to the Free Software
 
18
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 
 
20
    */
 
21
 
 
22
#include "structure.h"
 
23
#include "soundserver.h"
 
24
#include <stdio.h>
 
25
#include <iostream.h>
 
26
#include <fstream>
 
27
#include <arts/debug.h>
 
28
 
 
29
using namespace std;
 
30
 
 
31
ExecutableStructure::ExecutableStructure()
 
32
{
 
33
        execID = 0;
 
34
}
 
35
 
 
36
ExecutableStructure::~ExecutableStructure()
 
37
{
 
38
        // to make destructor virtual
 
39
        // stop execution here?
 
40
}
 
41
 
 
42
void ExecutableStructure::stopExecute()
 
43
{
 
44
        assert(execID);
 
45
        arts_debug("TODO: PORT: freeStructure");
 
46
        //Synthesizer->freeStructure(execID);
 
47
        execID = 0;
 
48
}
 
49
 
 
50
bool ExecutableStructure::isExecuting()
 
51
{
 
52
        if(!execID) return(false);
 
53
        arts_debug("TODO:PORT:isExecuting()");
 
54
        return false;
 
55
        //return(Synthesizer->isExecuting(execID));
 
56
}
 
57
 
 
58
bool ExecutableStructure::saveSession(const char *filename)
 
59
{
 
60
        arts_debug("TODO:PORT:saveSession()");
 
61
#if 0
 
62
        assert(execID);
 
63
 
 
64
        ArtsCorba::StringSeq_var data;
 
65
        arts_debug("saveSession");
 
66
        if(Synthesizer->saveSession(execID,true,data))
 
67
        {
 
68
                arts_debug("ok");
 
69
                FILE *file = fopen(filename,"w");
 
70
                if(!file) return false;
 
71
 
 
72
                unsigned long i;
 
73
                for(i=0;i<data->length();i++) fprintf(file,"%s\n",(char *)(*data)[i]);
 
74
                fclose(file);
 
75
 
 
76
                return true;
 
77
        }
 
78
        arts_debug("failed");
 
79
#endif
 
80
        return false;
 
81
}
 
82
 
 
83
Structure::Structure() :ExecutableStructure()
 
84
{
 
85
        canvas = 0;
 
86
}
 
87
 
 
88
void Structure::setCanvas(StructureCanvas *canvas)
 
89
{
 
90
        this->canvas = canvas;
 
91
}
 
92
 
 
93
Structure::~Structure()
 
94
{
 
95
        clear();
 
96
        arts_debug("~Structure (releasing structuredesc from structure)");
 
97
}
 
98
 
 
99
bool Structure::startExecute()
 
100
{
 
101
#if 0
 
102
        assert(!execID);
 
103
 
 
104
        arts_debug("PORT: TODO startExecute()");
 
105
        // just in case synthesis has been halted before,
 
106
        // restart it and hope we'll have enough computing power now
 
107
        //Synthesizer->Reset();
 
108
        //execID = Synthesizer->createStructure(StructureDesc,preferredservers);
 
109
        assert(execID);
 
110
#endif
 
111
 
 
112
        /* connect to the sound server */
 
113
        Arts::SimpleSoundServer server;
 
114
 
 
115
        server = Arts::Reference("global:Arts_SimpleSoundServer");
 
116
        if(server.isNull())
 
117
                return false;
 
118
 
 
119
        /* move a copy of the structure to the server, so that there will be
 
120
           no latencies in querying what to connect to what */
 
121
        vector<string> *savePtr = StructureDesc.saveToList();
 
122
        Arts::StructureDesc remoteSD;
 
123
        remoteSD = Arts::DynamicCast(server.createObject("Arts::StructureDesc"));
 
124
        assert(!remoteSD.isNull());
 
125
        remoteSD.loadFromList(*savePtr);
 
126
        delete savePtr;
 
127
 
 
128
        /* create a structure builder on the server */
 
129
        Arts::StructureBuilder builder;
 
130
        builder = Arts::DynamicCast(server.createObject("Arts::StructureBuilder"));
 
131
 
 
132
        /* create a local factory - this will enable the builder to create gui qt
 
133
           widgets (which need to reside within an qt application to work) */
 
134
 
 
135
        Arts::LocalFactory factory;
 
136
        builder.addFactory(factory);
 
137
        
 
138
        /* create the structure on the server */
 
139
        structure = Arts::DynamicCast(builder.createObject(remoteSD));
 
140
        structure.start();
 
141
 
 
142
        return true;
 
143
}
 
144
 
 
145
void Structure::stopExecute()
 
146
{       // TODO:PORT: verify this code
 
147
        structure = Arts::SynthModule::null();
 
148
}
 
149
 
 
150
void Structure::publish()
 
151
{
 
152
        arts_debug("PORT: TODO publish()");
 
153
        //Synthesizer->publishStructureDesc(StructureDesc);
 
154
}
 
155
 
 
156
bool Structure::valid()
 
157
{
 
158
        return StructureDesc.valid();
 
159
}
 
160
 
 
161
string Structure::name()
 
162
{
 
163
        return StructureDesc.name();
 
164
}
 
165
 
 
166
void Structure::rename(const char *newname)
 
167
{
 
168
        StructureDesc.name(newname);
 
169
}
 
170
 
 
171
bool Structure::save(const char *filename)
 
172
{
 
173
        vector<string> *savePtr = StructureDesc.saveToList();
 
174
        vector<string>& list = *savePtr;
 
175
 
 
176
        FILE *file = fopen(filename,"w");
 
177
        if (!file)
 
178
                return false;
 
179
 
 
180
        vector<string>::iterator i;
 
181
 
 
182
        for(i=list.begin();i != list.end(); i++)
 
183
                fprintf(file,"%s\n",(*i).c_str());
 
184
        fclose(file);
 
185
 
 
186
        delete savePtr;
 
187
 
 
188
/* tell the server to rescan for structures */
 
189
        Arts::SoundServerV2 server = Arts::Reference("global:Arts_SoundServerV2");
 
190
        if(!server.isNull()) server.checkNewObjects();
 
191
 
 
192
        return true;
 
193
}
 
194
 
 
195
void Structure::clear()
 
196
{
 
197
        list<StructureComponent *>::iterator ci;
 
198
 
 
199
        arts_debug("clc");
 
200
/*
 
201
        for(ci = ComponentList.begin(); ci != ComponentList.end(); ci++)
 
202
                delete (*ci);
 
203
 
 
204
        ComponentList.erase(ComponentList.begin(), ComponentList.end());
 
205
        ModuleList.erase(ModuleList.begin(), ModuleList.end());
 
206
*/
 
207
        for(ci = ComponentList.begin(); ci != ComponentList.end(); ci++)
 
208
                (*ci)->setSelected(true);
 
209
        deleteSelected();
 
210
 
 
211
        arts_debug("sdc");
 
212
        // shouldn't do much, now that we deleted every single component of
 
213
        // the structure, but we to it anyway, just to be sure.
 
214
        StructureDesc.clear();
 
215
}
 
216
 
 
217
void Structure::retrieve(const char *pubname)
 
218
{
 
219
        arts_debug("PORT: TODO: retrieve");
 
220
#if 0
 
221
        arts_debug("retrieve... %s",pubname);
 
222
        ArtsCorba::StructureDesc_var psd = Synthesizer->lookupStructureDesc(pubname);
 
223
 
 
224
        arts_debug("psdlookup ok");
 
225
        if(psd)
 
226
        {
 
227
                arts_debug("starting savetolist");
 
228
                ArtsCorba::StringSeq_var strseq=psd->saveToList();
 
229
                arts_debug("savetolist ok");
 
230
                loadFromList(strseq);
 
231
                arts_debug("loadfromlist ok");
 
232
        }
 
233
        arts_debug("retrieve... ok");
 
234
#endif
 
235
}
 
236
 
 
237
void Structure::load(const char *filename)
 
238
{
 
239
        ifstream infile(filename);
 
240
        string line;
 
241
        vector<string> strseq;
 
242
 
 
243
        while(getline(infile,line))
 
244
                strseq.push_back(line);
 
245
 
 
246
        loadFromList(strseq);
 
247
#if 0
 
248
        FILE *infile = fopen(filename,"r");
 
249
        ArtsCorba::StringSeq_var strseq = new ArtsCorba::StringSeq;
 
250
 
 
251
        char line[1024];
 
252
        unsigned long i = 0;
 
253
 
 
254
        while(fgets(line,1024,infile))
 
255
        {
 
256
                // cut eventual CR and/or LFs at the end of the line
 
257
                while(strlen(line) && line[strlen(line)-1] < 14)
 
258
                        line[strlen(line)-1] = 0;
 
259
 
 
260
                strseq->length(i+1);
 
261
                (*strseq)[i++] = CORBA::string_dup(line);
 
262
        }
 
263
        fclose(infile);
 
264
 
 
265
        arts_debug(">>loadfromlist...");
 
266
        loadFromList(strseq);
 
267
        arts_debug("<<loadfromlist...");
 
268
#endif
 
269
}
 
270
 
 
271
void Structure::loadFromList(const vector<string>& strseq)
 
272
{
 
273
        assert(canvas);
 
274
 
 
275
        arts_debug(">>clear");
 
276
        clear();
 
277
        arts_debug("<<clear");
 
278
 
 
279
        StructureDesc.loadFromList(strseq);
 
280
 
 
281
        vector<Arts::ModuleDesc> *modules = StructureDesc.modules();
 
282
        vector<Arts::ModuleDesc>::iterator mi;
 
283
 
 
284
        for(mi=modules->begin(); mi != modules->end(); mi++)
 
285
        {
 
286
                Module *m = new Module(*mi,StructureDesc,canvas);
 
287
 
 
288
                m->show();
 
289
                ModuleList.push_back(m);
 
290
                ComponentList.push_back(m);
 
291
        }
 
292
        delete modules;
 
293
 
 
294
        vector<Arts::StructurePortDesc> *ports = StructureDesc.ports();
 
295
        vector<Arts::StructurePortDesc>::iterator pi;
 
296
 
 
297
        for(pi=ports->begin(); pi != ports->end(); pi++)
 
298
        {
 
299
                StructurePort *p = new StructurePort(*pi,StructureDesc,canvas);
 
300
 
 
301
                p->show();
 
302
                ComponentList.push_back(p);
 
303
        }
 
304
        delete ports;
 
305
}
 
306
 
 
307
Module *Structure::createModule(const Arts::ModuleInfo& minfo)
 
308
{
 
309
        assert(canvas);
 
310
        Module *m = new Module(minfo,StructureDesc,canvas);
 
311
 
 
312
        ComponentList.push_back(m);
 
313
        ModuleList.push_back(m);
 
314
        return m;
 
315
}
 
316
 
 
317
StructurePort *Structure::createStructurePort(const Arts::PortType& type)
 
318
{       // TODO:PORT: verify this code
 
319
#if 0
 
320
        arts_debug("TODO:PORT:createStructurePort");
 
321
#endif
 
322
// portname generation
 
323
        unsigned long portindex = 1,l,baselen;;
 
324
        char name[100];
 
325
 
 
326
        string namebase;
 
327
        if(type.direction == Arts::input) {
 
328
                // this is a port where our structure puts its results
 
329
                // so it is an input port, that is named out
 
330
                namebase = "out"; baselen = 3;
 
331
    } else {
 
332
                // this is a port where our structure gets data from
 
333
                // so it is an output port, that is named in
 
334
                namebase = "in"; baselen = 2;
 
335
        }
 
336
 
 
337
        vector<Arts::StructurePortDesc> *sps = StructureDesc.ports();
 
338
 
 
339
        for(l=0;l<sps->size();l++) {
 
340
                string thisname = (*sps)[l].name();
 
341
                if(strncmp(thisname.c_str(), namebase.c_str(), baselen) == 0 &&
 
342
           strlen(thisname.c_str()) > baselen)
 
343
                {
 
344
                        unsigned long index2 = atol(&thisname.c_str()[baselen]);
 
345
                        if(index2 >= portindex) portindex = index2+1;
 
346
                }
 
347
        }
 
348
        delete sps;
 
349
 
 
350
        sprintf(name,"%s%ld",namebase.c_str(),portindex);
 
351
        arts_debug("new Portname: %s",name);
 
352
        Arts::StructurePortDesc spd =
 
353
                StructureDesc.createStructurePortDesc(type,name);
 
354
 
 
355
        assert(canvas);
 
356
        StructurePort *s = new StructurePort(spd,StructureDesc,canvas);
 
357
        ComponentList.push_back(s);
 
358
        return s;
 
359
}
 
360
 
 
361
list<Module *> *Structure::getModuleList()
 
362
{
 
363
        return(&ModuleList);
 
364
}
 
365
 
 
366
list<StructureComponent *> *Structure::getComponentList()
 
367
{
 
368
        return(&ComponentList);
 
369
}
 
370
 
 
371
long Structure::countSelected()
 
372
{
 
373
        list<StructureComponent *>::iterator ci;
 
374
        long targets = 0;
 
375
 
 
376
        for(ci=ComponentList.begin();ci!=ComponentList.end();ci++)
 
377
                if((*ci)->selected()) targets++;
 
378
 
 
379
        return targets;
 
380
}
 
381
 
 
382
void Structure::deleteSelected()
 
383
{
 
384
        arts_debug("deleteSelected...");
 
385
 
 
386
        // remove module from the ModuleList
 
387
        list<Module *>::iterator mi;
 
388
        for(mi=ModuleList.begin();mi!=ModuleList.end();)
 
389
        {
 
390
                if((*mi)->selected())
 
391
                {
 
392
                        ModuleList.erase(mi);
 
393
                        mi = ModuleList.begin();
 
394
                }
 
395
                else mi++;
 
396
        }
 
397
 
 
398
        // and from the ComponentList (the future ;)
 
399
 
 
400
        list<StructureComponent *>::iterator ci = ComponentList.begin();
 
401
 
 
402
        while(ci!=ComponentList.end())
 
403
        {
 
404
                if((*ci)->selected())
 
405
                {
 
406
                        delete (*ci);
 
407
                        ComponentList.erase(ci);
 
408
                        ci = ComponentList.begin();
 
409
                }
 
410
                else ci++;
 
411
        }
 
412
 
 
413
        arts_debug("deleteSelected ok.");
 
414
}
 
415
 
 
416
StructureComponent *Structure::componentAt(long x, long y, bool ignore_selected)
 
417
{
 
418
        list<StructureComponent *>::iterator ci;
 
419
 
 
420
        for(ci=ComponentList.begin();ci!=ComponentList.end();ci++)
 
421
        {
 
422
                StructureComponent *c = *ci;
 
423
 
 
424
                if(x >= c->x() && x < c->x()+c->width() &&
 
425
           y >= c->y() && y < c->y()+c->height())
 
426
                {
 
427
                        if((c->selected() && !ignore_selected) || !c->selected()) return c;
 
428
                }
 
429
        }
 
430
 
 
431
        return 0;
 
432
}