~illusions-hu/soundbox/trunk

« back to all changes in this revision

Viewing changes to scr/connector/co_flowsheet.cpp

  • Committer: illusions.hu at gmail
  • Date: 2010-12-18 10:09:41 UTC
  • Revision ID: illusions.hu@gmail.com-20101218100941-qygg337sqghpxoz2
Connector files added

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
Copyright (C) 2010 Laszlo Simon <laszlo.simon@gmail.com>
 
3
                   Esthefan Maleki <illusions.hu@gmail.com>
 
4
This file is part of the SOUNDBOX project.
 
5
 
 
6
SOUNDBOX 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 version 3 of the License.
 
9
 
 
10
SOUNDBOX is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with SOUNDBOX. If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
See http://www.illusions.hu for further details.
 
19
*****************************************************************************/
 
20
 
 
21
#include "co_flowsheet.h"
 
22
 
 
23
 
 
24
void TGFlowSheetForm::init();
 
25
{
 
26
  initPopups();
 
27
  this.PopupMenu = backgroundPopup;
 
28
  overedObject = null;
 
29
  selectedObjects = null;
 
30
}
 
31
 
 
32
// ADD-DEL
 
33
 
 
34
// SELECTING - OVERING FUNCTIONS
 
35
 
 
36
void TGFlowSheetForm::checkOvered(QPoint mouseCoord);
 
37
  int i;
 
38
{
 
39
  for (i = low(Segments); i <= high(Segments); i++)
 
40
    Segments[i].overed = false;
 
41
  for (i = low(Joints); i <= high(Joints); i++)
 
42
    Joints[i].overed = false;
 
43
  for (i = low(Units); i <= high(Units); i++)
 
44
    Units[i].overed = false;
 
45
 
 
46
  overedObject = null;
 
47
 
 
48
  for (i = low(Joints); i <= high(Joints); i++)
 
49
    if ((overedObject == null) && (Joints[i].isOvered(mouseCoord)))
 
50
    {
 
51
      Joints[i].overed = true;
 
52
      overedObject = Joints[i];
 
53
    } // for
 
54
 
 
55
  for (i = low(Units); i <= high(Units); i++)
 
56
    if ((overedObject == null) && (Units[i].isOvered(mouseCoord))) 
 
57
    {
 
58
      Units[i].overed = true;
 
59
      overedObject = Units[i];
 
60
    }
 
61
 
 
62
  for (i = low(Segments); i <= high(Segments); i++)
 
63
    if ((overedObject = null) && (Segments[i].isOvered(mouseCoord)))
 
64
    {
 
65
      Segments[i].overed = true;
 
66
      overedObject = Segments[i];
 
67
    }
 
68
} // checkOvered
 
69
 
 
70
// DRAW FUNCTIONS
 
71
 
 
72
void TGFlowSheetForm::refresh();
 
73
{
 
74
  paint();
 
75
}
 
76
 
 
77
void TGFlowSheetForm::paint();
 
78
  QBitmap bmp;
 
79
{
 
80
  // az inherited verzi�t kell h�vni
 
81
  paint();
 
82
  bmp = new QBitmap();
 
83
  bmp.Width = this.ClientWidth;
 
84
  bmp.Height = this.ClientHeight;
 
85
  drawGraph(bmp.Canvas);
 
86
  Canvas.Draw(0,0,bmp);
 
87
  bmp.Free();
 
88
} // paint
 
89
 
 
90
void TGFlowSheetForm::drawGraph(QCanvas canvas);
 
91
{
 
92
   initializeCanvas(canvas);
 
93
   drawBackGround(canvas);
 
94
   drawElements(canvas);
 
95
} // drawGraph 
 
96
 
 
97
void TGFlowSheetForm::initializeCanvas(QCanvas canvas);
 
98
{
 
99
  canvas.Pen.Color = RGB(0,0,0);
 
100
  canvas.Pen.Width = 1;
 
101
  canvas.Pen.Style = psSolid;
 
102
  canvas.Brush.Color = RGB(255,255,255);
 
103
  canvas.Brush.Style = bsSolid;
 
104
} // initializeCanvas
 
105
 
 
106
void TGFlowSheetForm::drawBackGround(QCanvas canvas);
 
107
{
 
108
  canvas.Rectangle(self.ClientRect);
 
109
}
 
110
 
 
111
void TGFlowSheetForm::drawElements(QCanvas canvas);
 
112
  int i;
 
113
{
 
114
  for (i = low(Segments); i <= high(Segments); i++)
 
115
    Segments[i].draw(canvas);
 
116
  for (i = low(Units); i <= high(Units); i++)
 
117
    Units[i].draw(canvas);
 
118
  for (i = low(Joints); i <= high(Joints); i++)
 
119
    Joints[i].draw(canvas);
 
120
}
 
121
 
 
122
void TGFlowSheetForm::DrawStart(QObject Sender, int x,y);
 
123
{
 
124
  if (lineDrawing == false) {
 
125
    lineDrawing = true;
 
126
    if (Sender is TGUnit) {
 
127
      lasTGJoint2 = new TGJoint(x,y,this);
 
128
      AddJoint(lasTGJoint2);
 
129
      lasTGJoint2.onMouseMove = peMouseMove;
 
130
      lasTGJoint2.onMouseUp = peMouseUp;
 
131
      lasTGJoint2.onMouseDown = peMouseDown;
 
132
      lasTGJoint2.onDrawClick = DrawStart();
 
133
      lasTGJoint2.prevElement = Sender;
 
134
      TGUnit(Sender).knots[TGUnit(Sender).activetap] = lasTGJoint2;
 
135
    }
 
136
    if (Sender is TGJoint) {
 
137
      lasTGJoint2 = TGJoint(Sender);
 
138
    }
 
139
    lasTGJoint = new TGJoint(x,y,this);
 
140
    AddJoint(lasTGJoint);
 
141
    lasTGJoint.onMouseMove = peMouseMove;
 
142
    lasTGJoint.onMouseUp = peMouseUp;
 
143
    lasTGJoint.onMouseDown = peMouseDown;
 
144
    lasTGJoint.onDrawClick = DrawStart();
 
145
    LasTGSegment = new TGSegment(lasTGJoint2,lasTGJoint,this);
 
146
    AddSegment(lasTGSegment);
 
147
  } else {
 
148
    lineDrawing = false;
 
149
    if Sender is TGUnit then {
 
150
      TGUnit(Sender).Drop(lasTGJoint);
 
151
    } // if Sender
 
152
  } // else
 
153
} // DrawStart
 
154
 
 
155
// USER MENU
 
156
 
 
157
void TGFlowSheetForm::InsertWaveGenerator(QObject Sender);
 
158
  TGUnit unit
 
159
{
 
160
   unit = new TGWaveGenerator('Wavegen',2,ZoomFactor,this);
 
161
   unit.Left = mouseCoord.x();
 
162
   unit.Top = mouseCoord.y();
 
163
   unit.onDrawClick = DrawStart();
 
164
   this.AddUnit(unit);
 
165
   refresh();
 
166
}
 
167
 
 
168
void TGFlowSheetForm::Zoomin(QObject Sender);
 
169
  int i
 
170
{
 
171
  ZoomFactor = trunc(ZoomFactor*1.5);
 
172
 
 
173
  for (i = low(Joints); i <= high(Joints); i++) {
 
174
    Joints[i].Left = trunc(1.5*(Joints[i].Left-Width/2)+Width/2);
 
175
    Joints[i].Top = trunc(1.5*(Joints[i].Top-Height/2)+Height/2);
 
176
  }
 
177
  for (i = low(Segments); i <= high(Segments); i++) {
 
178
    Segments[i].Left = trunc(1.5*(Segments[i].Left-Width/2)+Width/2);
 
179
    Segments[i].Top = trunc(1.5*(Segments[i].Top-Height/2)+Height/2);
 
180
  }
 
181
  for (i = low(Units); i <= high(Units); i++) {
 
182
    Units[i].Left = trunc(1.5*(Units[i].Left-Width/2)+Width/2);
 
183
    Units[i].Top = trunc(1.5*(Units[i].Top-Height/2)+Height/2);
 
184
    Units[i].zoomFactor = ZoomFactor;
 
185
  }
 
186
  refresh();
 
187
} // Zoomin
 
188
 
 
189
void TGFlowSheetForm::Zoomout(QObject Sender);
 
190
  int i;
 
191
{
 
192
  ZoomFactor = trunc(ZoomFactor/1.5);
 
193
 
 
194
  for (i = low(Joints); i <= high(Joints); i++) {
 
195
    Joints[i].Left = trunc((Joints[i].Left-Width/2)/1.5+Width/2);
 
196
    Joints[i].Top = trunc((Joints[i].Top-Height/2)/1.5+Height/2);
 
197
  }
 
198
  for (i = low(Segments); i<= high(Segments); i++) {
 
199
    Segments[i].Left = trunc((Segments[i].Left-Width/2)/1.5+Width/2);
 
200
    Segments[i].Top = trunc((Segments[i].Top-Height/2)/1.5+Height/2);
 
201
  }
 
202
  for (i = low(Units); i <= high(Units); i++) {
 
203
    Units[i].Left = trunc((Units[i].Left-Width/2)/1.5+Width/2);
 
204
    Units[i].Top = trunc((Units[i].Top-Height/2)/1.5+Height/2);
 
205
    Units[i].zoomFactor=ZoomFactor;
 
206
  }
 
207
  refresh();
 
208
}
 
209
 
 
210
// ADD-REMOVE OBJECTS
 
211
 
 
212
void TGFlowSheetForm::AddUnit(TGUnit unit);
 
213
{
 
214
  setLength(Units,length(Units)+1);
 
215
  Units[high(Units)] = unit;
 
216
} // AddUnit
 
217
 
 
218
void TGFlowSheetForm::AddSegment(TGSegment segment);
 
219
{
 
220
  setLength(Segments,length(Segments)+1);
 
221
  Segments[high(Segments)] = segment;
 
222
}
 
223
 
 
224
void TGFlowSheetForm::AddJoint(TGJoint joint);
 
225
{
 
226
  setLength(Joints,length(Joints)+1);
 
227
  Joints[high(Joints)] = joint;
 
228
}
 
229
 
 
230
void TGFlowSheetForm::DelUnit(TGUnit unit);
 
231
  bool found;
 
232
  int i;
 
233
{
 
234
  found = false;
 
235
  for (i = low(Units); i <= high(Units); i++) {
 
236
    if (Units[i] == unit) found = true;
 
237
    if (found && (i < high(Units))) Units[i] = Units[i+1];
 
238
  }
 
239
  if found setLength(Units,length(Units)-1);
 
240
  unit.free();
 
241
  this.checkOvered(mouseCoord);
 
242
}
 
243
 
 
244
void TGFlowSheetForm::DelSegment(TGSegment segment);
 
245
  bool found;
 
246
  int i;
 
247
{
 
248
  found = false;
 
249
  for (i = low(Segments); i <= high(Segments); i++) {
 
250
    if (Segments[i] == segment) found = true;
 
251
    if (found && (i < high(Segments))) Segments[i] = Segments[i+1];
 
252
  }
 
253
  if found setLength(Segments,length(Segments)-1);
 
254
    segment.free();
 
255
  this.checkOvered(mouseCoord);
 
256
}
 
257
 
 
258
void TGFlowSheetForm:: DelJoint(TGJoint joint);
 
259
  bool found;
 
260
  int i;
 
261
{
 
262
  found = false;
 
263
  for (i=low(Joints); i <= high(Joints); i++) {
 
264
    if (Joints[i] = joint) found = true;
 
265
    if (found && (i < high(Joints))) Joints[i] = Joints[i+1];
 
266
  }
 
267
  if found setLength(Joints,length(Joints)-1);
 
268
  joint.free();
 
269
  this.checkOvered(mouseCoord);
 
270
}
 
271
 
 
272
// EVENTS
 
273
 
 
274
void TGFlowSheetForm::FormCreate(QObject Sender);
 
275
{
 
276
  Init();
 
277
  ZoomFactor = 100;
 
278
  lineDrawing = false;
 
279
  refresh();
 
280
}
 
281
 
 
282
void TGFlowSheetForm::FormResize(Sender: TObject);
 
283
{
 
284
   refresh();
 
285
}
 
286
 
 
287
void TGFlowSheetForm::FormMouseDown(QObject Sender, TMouseButton Button, TShiftState Shift: int X, Y);
 
288
{
 
289
  this.checkOvered(QPoint(X,Y));
 
290
  popupRefresh();
 
291
  if (overedObject != null) {
 
292
    if (overedObject is TGSegment)
 
293
      TGSegment(overedObject).MouseDown(Button,Shift,X-TGSegment(overedObject).Left,Y-TGSegment(overedObject).Top);
 
294
    if (overedObject is TGJoint)
 
295
      TGJoint(overedObject).MouseDown(Button,Shift,X-TGJoint(overedObject).Left,Y-TGJoint(overedObject).Top);
 
296
    if (overedObject is TGUnit)
 
297
      TGUnit(overedObject).MouseDown(Button,Shift,X-TGUnit(overedObject).Left,Y-TGUnit(overedObject).Top);
 
298
  } else {
 
299
    if lineDrawing {
 
300
      if (Button == mbLeft) {
 
301
        lasTGJoint2 = lasTGJoint;
 
302
        lasTGJoint = new TGJoint(x,y,this);
 
303
        AddJoint(lasTGJoint);
 
304
        lasTGJoint.onMouseMove = peMouseMove();
 
305
        lasTGJoint.onMouseUp = peMouseUp();
 
306
        lasTGJoint.onMouseDown = peMouseDown();
 
307
        lasTGJoint.onDrawClick = DrawStart();
 
308
        lasTGSegment = new TGSegment(lasTGJoint2,lasTGJoint,this);
 
309
        AddSegment(lasTGSegment);
 
310
      } // if (B
 
311
      if (Button == mbRight) {
 
312
        DelJoint(lasTGJoint);
 
313
        lasTGJoint2.unconnect(lasTGSegment);
 
314
        DelSegment(lasTGSegment);
 
315
        lineDrawing = false;
 
316
      }
 
317
    }
 
318
  }
 
319
  refresh();
 
320
} // FormMouseDown
 
321
 
 
322
void TGFlowSheetForm::FormMouseMove(QObject Sender, TShiftState Shift: int X, Y);
 
323
  QPoint c;
 
324
{
 
325
  mouseCoord = new QPoint(X,Y);
 
326
  this.checkOvered(mouseCoord);
 
327
  popupRefresh();
 
328
  if (overedObject != null) {
 
329
    if (overedObject is TGSegment)
 
330
      TGSegment(overedObject).MouseMove(Shift,X-TGSegment(overedObject).Left,Y-TGSegment(overedObject).Top);
 
331
    if (overedObject is TGJoint)
 
332
      TGJoint(overedObject).MouseMove(Shift, X-TGJoint(overedObject).Left,Y-TGJoint(overedObject).Top);
 
333
    if (overedObject is TGUnit)
 
334
      TGUnit(overedObject).MouseMove(Shift, X-TGUnit(overedObject).Left,Y-TGUnit(overedObject).Top);
 
335
  } else {
 
336
    if linedrawing  {
 
337
      if (lasTGJoint2 != null) {
 
338
        c = lasTGJoint2.getCenter;
 
339
        if (ABS(x-c.x()) > ABS(y-c.y())) {
 
340
          X = X;
 
341
          Y = c.y();
 
342
          lasTGSegment.orient = 1;
 
343
        } else {
 
344
          X = c.x();
 
345
          Y = Y;
 
346
          lasTGSegment.orient = 2;
 
347
        }
 
348
        lasTGJoint.setCenter(X,Y);
 
349
      } // if (last...
 
350
    } // if line...
 
351
  }
 
352
  refresh();
 
353
}
 
354
 
 
355
void TGFlowSheetForm::FormMouseUp(QObject Sender, TMouseButton Button, TShiftState Shift: int X, Y);
 
356
{
 
357
  if (overedObject != null) {
 
358
    if (overedObject is TGSegment)
 
359
      TGSegment(overedObject).MouseUp(Button,Shift, X-TGSegment(overedObject).Left,Y-TGSegment(overedObject).Top);
 
360
    if (overedObject is TGJoint)
 
361
      TGJoint(overedObject).MouseUp(Button,Shift, X-TGJoint(overedObject).Left,Y-TGJoint(overedObject).Top);
 
362
    if (overedObject is TGUnit)
 
363
      TGUnit(overedObject).MouseUp(Button,Shift, X-TGUnit(overedObject).Left,Y-TGUnit(overedObject).Top);
 
364
  } else {
 
365
  }
 
366
  refresh();
 
367
} // FormMouseUp
 
368
 
 
369
void FormKeyDown(QObject Sender, var Key: Word, TShiftState Shift);
 
370
{
 
371
  // Place your code here
 
372
} // 
 
373
 
 
374
void TGFlowSheetForm:peMouseMove(QObject Sender, TShiftState Shift: int X, Y);
 
375
  int i,ex,ey;
 
376
  QPoint c;
 
377
{
 
378
  if linedrawing {
 
379
    if (lasTGJoint2 != null) {
 
380
      c = lasTGJoint2.getCenter();
 
381
      if (ABS(x-c.x()) > ABS(y-c.y())) {
 
382
        ex = X;
 
383
        ey = c.y;
 
384
        lasTGSegment.orient = 1;
 
385
      } else {
 
386
        ex = c.x;
 
387
        ey = Y;
 
388
        lasTGSegment.orient = 2;
 
389
      }
 
390
      lasTGJoint.setCenter(eX,eY);
 
391
    } // if (last...
 
392
  } else {
 
393
    for (i = low(Joints); i <= high(Joints); i++)
 
394
      Joints[i].simplifyPipe();
 
395
    for (i = low(Units); i <= high(Units); i++)
 
396
      Units[i].over(X,Y);
 
397
  }
 
398
} // peMouseMove
 
399
 
 
400
void TGFlowSheetForm::peMouseDown(Sender: TObject; Button: TMouseButton;
 
401
  Shift: TShiftState; X, Y: Integer);
 
402
{
 
403
   if lineDrawing then {
 
404
      If BUTTON = mbleft then {
 
405
         lasTGJoint2 = lasTGJoint;
 
406
         lasTGJoint = TGJoint.create(x+lasTGJoint2.left,
 
407
                          y+lasTGJoint2.top,self);
 
408
         AddJoint(lasTGJoint);
 
409
         lasTGJoint.onMouseDown = peMouseDown;
 
410
         lasTGJoint.onMouseMove = peMouseMove;
 
411
         lasTGJoint.onMouseUp = peMouseUp;
 
412
         lasTGJoint.onDrawClick = DrawStart();
 
413
         lasTGSegment = TGSegment.create(lasTGJoint2,lasTGJoint,self);
 
414
          AddSegment(lasTGSegment);
 
415
      end;
 
416
      if button = mbRight then {
 
417
         lasTGJoint.Free;
 
418
         lasTGJoint2.unconnect(lasTGSegment);
 
419
         lasTGSegment.Free;
 
420
         lineDrawing = false;
 
421
      end;
 
422
   end;
 
423
end;
 
424
 
 
425
void TGFlowSheetForm:peMouseUp(QObject Sender, TMouseButton Button, TShiftState Shift: int X, Y);
 
426
  int i;
 
427
{
 
428
  for (i = 0; i <= ControlCount-1; i+) {
 
429
    if (overedObject is TGUnit)
 
430
      if (Sender is TGJoint)
 
431
        if TGUnit(Controls[i]).Drop(TGJoint(Sender)) return();
 
432
    if (overedObject is TGJoint)
 
433
      if (Sender is TGJoint)
 
434
        if (TGJoint(Controls[i]).Drop(TGJoint(Sender),TGJoint(Sender).Left+X,TGJoint(Sender).Top+Y) 
 
435
                  return();
 
436
  }  // for
 
437
} // peMouseUp
 
438
 
 
439
// POPUP MENU FUNCTIONS
 
440
 
 
441
void TGFlowSheetForm::popupRefresh();
 
442
{
 
443
  if (overedObject == null)
 
444
    this.PopupMenu = backgroundPopup;
 
445
  else {
 
446
    if (overedObject is TGSegment)
 
447
      this.PopupMenu = TGSegment(overedObject).popupmenu;
 
448
    if (overedObject is TGJoint)
 
449
      this.PopupMenu = TGJoint(overedObject).popupmenu;
 
450
    if (overedObject is TGUnit)
 
451
      this.PopupMenu = TGUnit(overedObject).popupmenu;
 
452
  }
 
453
  if (selectedObjects != null) PopupMenu = SelectedPopup;
 
454
end;
 
455
 
 
456
void TGFlowSheetForm::initPopups();
 
457
  TMenuItem mi,m;
 
458
{
 
459
//   SelectedPopup = ;
 
460
  backgroundPopup = new QPopupMenu(QComponent(parent));
 
461
  backgroundPopup.AutoPopup = true;
 
462
 
 
463
  m = new TMenuItem(backgroundPopup);
 
464
  m.Caption = "Add";
 
465
  backgroundPopup.Items.Add(m);
 
466
 
 
467
  mi = new TMenuItem(backgroundPopup);
 
468
  mi.Caption = "Wave generator";
 
469
  mi.OnClick = InsertWaveGenerator();
 
470
  m.Add(mi);
 
471
 
 
472
  m = new TMenuItem(backgroundPopup);
 
473
  m.Caption = "Zoom";
 
474
  backgroundPopup.Items.Add(m);
 
475
 
 
476
  mi = new TMenuItem(PopupMenu);
 
477
  mi.Caption = "Zoom In";
 
478
  mi.OnClick = ZoomIn;
 
479
  m.Add(mi);
 
480
 
 
481
  mi = TMenuItem.Create(popupmenu);
 
482
  mi.Caption = "Zoom Out";
 
483
  mi.OnClick = ZoomOut;
 
484
  m.Add(mi);
 
485
}
 
486
 
 
487