-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.cpp
More file actions
1046 lines (952 loc) · 31 KB
/
component.cpp
File metadata and controls
1046 lines (952 loc) · 31 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <QGraphicsDropShadowEffect>
#include <QGraphicsScene>
#include <QGraphicsSceneDragDropEvent>
#include <QPainter>
#include <QtEndian>
#include <QDebug>
#include <QCoreApplication>
#include "component.h"
#include "componentGraph.h"
#include "componentDataModel.h"
#include "edge.h"
#include "mainWindow.h"
namespace CutePathSim
{
/**
* \class Component
* Models a component of a digital system with inputs and outputs.
*/
/**
* Constructs a component with \a name as the name.
*/
Component::Component(const QString &name, QGraphicsItem *parent) : QGraphicsItem(parent)
{
m_name = name;
/*
// add a drop shadow effect
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
shadow->setBlurRadius(5);
shadow->setOffset(2, 3);
setGraphicsEffect(shadow);
*/
// set up the font
if(m_font == 0)
{
m_font = new QFont("Helvetica");
m_font->setPixelSize(FONT_SIZE);
}
m_textWidth = QFontMetrics(*m_font).size(Qt::TextSingleLine, name).width();
m_parentGraph = 0;
m_subGraph = 0;
m_layout = LABELED;
m_toolBox = 0;
m_dataTable = 0;
setAcceptHoverEvents(true);
m_hovered = false;
}
Component::~Component()
{
qDebug() << "destroying component:" << name();
// remove all of the items from the scene and delete them
foreach(Input *input, m_inputs)
{
if(input->scene() != 0)
{
input->scene()->removeItem(input);
}
delete input;
}
foreach(Output *output, m_outputs)
{
if(output->scene() != 0)
{
output->scene()->removeItem(output);
}
delete output;
}
if(m_subGraph != 0 && m_subGraph->scene() != 0)
{
m_subGraph->scene()->removeItem(m_subGraph);
}
delete m_subGraph;
}
/**
* \fn Component::name()
* Returns the name of the component.
*/
/**
* \fn Component::getInputs()
* Returns a list containing pointers to the inputs owned by the component.
*
* \sa getInput() getOutputs()
*/
/**
* \fn Component::getOutputs()
* Returns a list containing pointers to the outputs owned by the component.
*
* \sa getOutput() getInputs()
*/
/**
* Returns a pointer to the input associated with the \a name.
*
* \sa getInputs() getOutput()
*/
Component::Input *Component::getInput(const QString &name)
{
return m_inputs.value(name);
}
/**
* Returns a pointer to the output associated with the \a name.
*
* \sa getOutputs() getInput()
*/
Component::Output *Component::getOutput(const QString &name)
{
return m_outputs.value(name);
}
QRectF Component::boundingRect() const
{
// FIXME: properly handle the edge case with no child items or long component names
// find the max width and the total height of our child items, because they are interfaces that we will display in a vertical list
switch(m_layout)
{
case MINIMIZED:
{
qreal width = LEFT_MARGIN + m_textWidth + RIGHT_MARGIN;
qreal height = TOP_MARGIN;
return QRect(-width / 2, -height / 2, width, height);
}
case LABELED:
{
qreal totalInterfaceHeight = 0;
foreach(QGraphicsItem *item, m_inputs)
{
totalInterfaceHeight += item->boundingRect().height();
}
foreach(QGraphicsItem *item, m_outputs)
{
totalInterfaceHeight += item->boundingRect().height();
}
qreal width = LEFT_MARGIN + qMax(maxInterfaceWidth(), m_textWidth) + RIGHT_MARGIN;
qreal height = TOP_MARGIN + totalInterfaceHeight + INTERFACE_MARGIN * (m_inputs.size() + m_outputs.size() - 1) + BOTTOM_MARGIN;
return QRect(-width / 2, -height / 2, width, height);
}
case EXPANDED:
{
qreal totalInterfaceWidth = 0;
foreach(QGraphicsItem *item, m_inputs)
{
totalInterfaceWidth += item->boundingRect().width();
}
foreach(QGraphicsItem *item, m_outputs)
{
totalInterfaceWidth += item->boundingRect().width();
}
// TODO: make the graph not fixed to a square, and handle the cascading graph changes associated with this feature
qreal width = LEFT_MARGIN + qMax(qMax(totalInterfaceWidth + INTERFACE_MARGIN * (m_inputs.size() + m_outputs.size() - 1), m_textWidth), MIN_GRAPH_SIZE) + RIGHT_MARGIN;
qreal graphDimensions = width - LEFT_MARGIN - RIGHT_MARGIN;
qreal height = TOP_MARGIN + maxInterfaceHeight() + BOTTOM_MARGIN + graphDimensions + BOTTOM_MARGIN;
return QRect(-width / 2, -height / 2, width, height);
}
default:
Q_ASSERT(false);
}
return QRect();
}
void Component::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
QRectF drawingRect(boundingRect().x() + BORDER_PEN_WIDTH / 2, boundingRect().y() + BORDER_PEN_WIDTH / 2, boundingRect().width() - BORDER_PEN_WIDTH, boundingRect().height() - BORDER_PEN_WIDTH);
// draw a gradient background
QLinearGradient gradient(0, 0, 0, drawingRect.height());
if(m_hovered)
{
gradient.setColorAt(0, color().lighter(110));
}
else
{
gradient.setColorAt(0, color());
}
gradient.setColorAt(1, Qt::white);
QBrush gradientBrush(gradient);
painter->setBrush(gradientBrush);
painter->setPen(QPen(QBrush(Qt::SolidPattern), BORDER_PEN_WIDTH));
painter->drawRect(drawingRect);
// draw the name
// FIXME: figure out how to make the font hinting look optimal
painter->setPen(QPen());
painter->setFont(*m_font);
painter->drawText(QRect(drawingRect.x(), drawingRect.y(), drawingRect.width(), TOP_MARGIN), Qt::AlignCenter, name());
// draw the expansion buttons if hovered
if(m_hovered)
{
switch(m_layout)
{
case MINIMIZED:
case LABELED:
painter->drawRect(QRectF(drawingRect.x() + drawingRect.width() - EXPAND_BUTTON_SIZE - INTERFACE_MARGIN, drawingRect.y() + INTERFACE_MARGIN, EXPAND_BUTTON_SIZE, EXPAND_BUTTON_SIZE));
default:
break;
}
switch(m_layout)
{
case LABELED:
case EXPANDED:
painter->drawRect(QRectF(drawingRect.x() + INTERFACE_MARGIN, drawingRect.y() + INTERFACE_MARGIN, EXPAND_BUTTON_SIZE, EXPAND_BUTTON_SIZE));
default:
break;
}
}
}
/**
* \fn parentGraph()
* Returns a pointer to the ComponentGraph that this component belongs to.
*/
/**
* \fn subGraph()
* Returns a pointer to the ComponentGraph that is contained within this component.
*
* If there is no sub-graph yet, it is constructed.
*
* \sa addSubComponent()
*/
ComponentGraph *Component::subGraph()
{
if(m_subGraph == 0)
{
m_subGraph = new ComponentGraph(this);
}
return m_subGraph;
}
/**
* \fn layout()
* Returns the layout used to display the component, either minimized, labeled, or expanded.
*
* \sa setLayout() Layout
*/
/**
* Sets the layout used to display the component, either minimized, labeled, or expanded.
*
* This method initiates a re-layout of the parent graph, and the component's layout won't be changed until GraphLayoutManager is finished with the re-layout.
*
* \sa layout()
*/
void Component::setLayout(Layout layout)
{
m_layout = layout;
if(m_parentGraph)
{
m_parentGraph->scheduleComponentResize(this);
}
}
void Component::mouseDragEvent(QGraphicsSceneDragDropEvent * /*event*/)
{
// TODO: implement dragging of components on the graphics scene
}
/**
* Adds an input to the list of inputs used by the component. Should be called from the derived class for every input it has.
*
* Component assumes ownership of \a input. The input will be destroyed when the component is destroyed.
* \sa addOutput()
*/
void Component::addInput(Component::Input * input)
{
prepareGeometryChange();
input->setParentItem(this);
m_inputs.insert(input->name(), input);
if(m_parentGraph != 0)
{
m_parentGraph->scheduleComponentResize(this);
}
}
/**
* Adds an output to the list of outputs used by the component.
*
* Component assumes ownership of \a output. The output will be destroyed when the component is destroyed.
* \sa addInput()
*/
void Component::addOutput(Component::Output *output)
{
prepareGeometryChange();
output->setParentItem(this);
m_outputs.insert(output->name(), output);
if(m_parentGraph != 0)
{
m_parentGraph->scheduleComponentResize(this);
}
}
/**
* Removes the input with the name \a name from the list of inputs used by the component.
*
* A pointer to the input removed is returned, for convenience.
*/
Component::Input *Component::removeInput(const QString &name)
{
if(m_parentGraph != 0)
{
m_parentGraph->scheduleComponentResize(this);
}
return m_inputs.take(name);
}
/**
* Removes the output with the name \a name from the list of outputs used by the component.
*
* A pointer to the output removed is returned, for convenience.
*/
Component::Output *Component::removeOutput(const QString &name)
{
if(m_parentGraph != 0)
{
m_parentGraph->scheduleComponentResize(this);
}
return m_outputs.take(name);
}
/**
* Adds a sub-component to the component's sub-graph.
*
* If a sub-graph does not yet exist, it is created.
*
* Component (the sub-graph, really) assumes ownership of \a component. Derived classes should \b not destroy \a component.
* \sa subGraph()
*/
void Component::addSubComponent(Component *component)
{
subGraph()->addComponent(component);
}
/**
* \fn run()
* Pure virtual method that is called from the component's work thread. This is where derived classes should do their processing.
*/
/**
* \fn color()
* Pure virtual method that returns the color of the component. Derived classes should return a suitable color for the component.
*/
/**
* \class Component::Input
* The Input class represents an input interface on a Component.
*
* The input can be connected to one Output object at a time with connect(), and data sent from that output can be read with the read() method.
*
* \sa Output
*/
/**
* Constructs an Input object with the name \a name.
*
* The constructed object represents an input that accepts \a width bits, with \a component as the recipient of those bits.
*/
Component::Input::Input(const QString &name, int width, Component *component) : Interface(name, component)
{
m_width = width;
m_bufferSize = width / 8 + ((width % 8) ? 1 : 0);
m_inputBuffer = new unsigned char[m_bufferSize];
m_component = component;
m_connection = 0;
m_from = 0;
m_internal = false;
m_externalOutput = 0;
}
Component::Input::~Input()
{
disconnect();
delete m_inputBuffer;
delete m_from;
}
/**
* \fn Component::Input::width()
* Returns the number of bits that the input accepts.
*/
/**
* \fn Component::Input::component()
* Returns a pointer to the component this input corresponds to, the recipient of the data.
*/
/**
* \fn Component::Input::connection()
* Returns a pointer to the output that is connected to this input.
*/
/**
* Connects this input to \a output. Any existing connection is disconnected, as inputs can only recieve from one output at a time.
*/
void Component::Input::connect(Output *output)
{
disconnect();
if(output == 0)
return;
output->m_connect(this);
m_connect(output);
// FIXME: check for connections between different graphs
ComponentGraph *graph = internal() ? component()->subGraph() : component()->parentGraph();
// tell the component graph and Graphviz about our new edge
graph->addEdge(output, this);
}
/**
* Disconnects the input.
*/
void Component::Input::disconnect()
{
if(m_connection == 0)
return;
// tell the component graph and Graphviz to remove the edge
parentGraph()->removeEdge(m_connection, this);
m_connection->m_disconnect(this);
m_disconnect();
}
/**
* Reads the data that was sent from the output that this input is connected to.
*
* The data is simply copied from the input buffer into \a buffer.
*
* \warning \a buffer should be at least as large as bufferSize().
*
* \sa bufferSize() readBool() readInt() Component::Output::write()
*/
void Component::Input::read(unsigned char *buffer)
{
memcpy(buffer, m_inputBuffer, m_bufferSize);
}
/**
* \fn Component::Input::readBool()
* Convenience method that reads a boolean value from the input buffer.
*
* \warning This should not be used unless width() == 1.
*
* \sa read() readInt() Output::writeBool()
*/
/**
* Convenience method that returns an unsigned int value from the input buffer.
*
* One can specify whether or not to interpret the integer stored in the buffer as big endian or little endian by changing the \a bigEndian flag. If true, the first byte in the input buffer will be used as the most significant byte in the integer, and the last byte as the least significant. If false, the first byte will be used as the least significant, and the last byte as the most significant. Endianness of the returned integer depends on the system.
*
* \warning This should not be used unless width() == 32.
*
* \sa read() readBool()
*/
unsigned int Component::Input::readInt(bool bigEndian)
{
Q_ASSERT(width() == 32);
if(bigEndian)
{
return qFromBigEndian<qint32>(m_inputBuffer);
}
return qFromLittleEndian<qint32>(m_inputBuffer);
}
/**
* \fn Component::Input::bufferSize()
* Returns the size in bytes of the input buffer used by read().
*
* \sa read()
*/
/**
* Returns a pointer to the internal Output object which allows sub-components on the sub-graph to utilize this input.
*
* Returns null if this input is an internal input on a component's sub-graph.
*
* \sa internal() Component::addSubComponent()
*/
Component::Output *Component::Input::from()
{
if(m_internal)
return 0;
if(m_from == 0)
{
m_from = new Output("from " + name(), width(), component()); // TODO: figure out how to translate this
// m_from->setParentItem(component()->subGraph()); // FIXME: this doesn't work for some reason
m_from->m_internal = true;
}
return m_from;
}
/**
* Returns a pointer to the component graph that this input belongs to.
*
* If this input is an internal input corresponding to an external output, this returns a pointer to the sub component graph. Otherwise, this returns a pointer to the same graph that this input's component belongs to.
*/
ComponentGraph *Component::Input::parentGraph() const
{
if(internal())
{
Q_ASSERT(component()->m_subGraph != 0);
return component()->m_subGraph;
}
return component()->parentGraph();
}
/**
* \fn Component::Input::internal()
* Returns true if this input is an internal input on a component's sub-graph. Otherwise, returns false.
*
* \sa from() Component::Output::internal()
*/
void Component::Input::writeToInput(const unsigned char *data)
{
memcpy(m_inputBuffer, data, m_bufferSize);
// forward the data to the internal output, if we have one
if(m_from != 0)
{
m_from->write(data);
}
// forward the data to the external output, if we're an internal input
if(m_internal)
{
m_externalOutput->write(data);
}
}
/**
* \class Component::Output
* The Output class represents an output interface on a Component.
*
* The output can be connected to any number of Input objects with connect(), and it will send data to those inputs whenever write() is called.
*
* \sa Input
*/
/**
* Constructs an Output object with the name \a name.
*
* The constructed object represents an output that sends out \a width bits, with \a component as the sender of those bits.
*/
Component::Output::Output(const QString &name, int width, Component *component) : Interface(name, component)
{
m_width = width;
m_bufferSize = width / 8 + ((width % 8) ? 1 : 0);
m_component = component;
m_to = 0;
m_internal = false;
}
Component::Output::~Output()
{
foreach(Input *input, m_connections)
{
input->disconnect();
}
delete m_to;
}
/**
* \fn Component::Output::width()
* Returns the number of bits that the output sends out.
*/
/**
* \fn Component::Output::component()
* Returns a pointer to the component this output corresponds to, the sender of the data.
*/
/**
* Connects this output to \a input. Disconnects \a input from anything if it is already connected.
*/
void Component::Output::connect(Input *input)
{
if(input == 0)
return;
input->disconnect();
m_connect(input);
input->m_connect(this);
// FIXME: check for connections between different graphs
ComponentGraph *graph = internal() ? component()->subGraph() : component()->parentGraph();
// tell the component graph and Graphviz about our new edge
graph->addEdge(this, input);
}
/**
* Disconnects \a input from this output.
*/
void Component::Output::disconnect(Input *input)
{
if(input == 0)
return;
if(m_connections.contains(input))
{
// tell the component graph and Graphviz to remove the edge
component()->parentGraph()->removeEdge(this, input);
input->m_disconnect();
m_disconnect(input);
}
}
/**
* Writes \a data to all of the inputs that this output is connected to.
*
* \a data is simply copied to the input buffers of all of the connected inputs.
*
* \warning \a data should be at least as large as bufferSize().
*
* \warning This method should be called for each output on each cycle, otherwise the receiving input buffers will stay the same as they were the last cycle!
*
* Classes calling write() should politely make sure that any remainder bits in \a data (i.e. bits greater than width() if width() % 8 != 0) are zeroed out.
*
* \sa bufferSize() Component::Input::read()
*/
void Component::Output::write(const unsigned char *data)
{
foreach(Input *input, m_connections)
{
input->writeToInput(data);
}
if(parentGraph()->isAnimated())
{
// animate the edges
for(double d = 0; d < 1; d += 1.0 / (ANIMATION_SECONDS * ANIMATION_FPS))
{
foreach(Input *input, m_connections)
{
parentGraph()->getEdge(this, input)->setAnimationStep(d);
parentGraph()->getEdge(this, input)->update();
}
}
}
}
/**
* Convenience method that writes a boolean value to all of the inputs connected to this output.
*
* \warning This should not be used unless width() == 1.
*
* \sa write() writeInt() Input::readBool()
*/
void Component::Output::writeBool(bool boolean)
{
Q_ASSERT(width() == 1);
write(reinterpret_cast<unsigned char *>(&boolean));
}
/**
* Convenience method that writes an integer value to all of the inputs connected to this output.
*
* One can specify whether or not to write the integer to the inputs in big endian or little endian byte order by changing the \a bigEndian flag. If true, the most significant byte will be written first, and the least significant byte last. If false, the least significant byte will be written first, and the most significant byte last. Endianness of \a integer should be in the system's native byte order.
*
* \warning This should not be used unless width() == 32.
*
* \sa write() writeBool() Input::readInt()
*/
void Component::Output::writeInt(unsigned int integer, bool bigEndian)
{
Q_ASSERT(width() == 32);
unsigned char buffer[sizeof(unsigned int)];
if(bigEndian)
{
qToBigEndian(integer, buffer);
}
else
{
qToLittleEndian(integer, buffer);
}
write(buffer);
}
/**
* \fn Component::Output::bufferSize()
* Returns the size in bytes of the data sent by write().
*
* \sa write()
*/
/**
* Returns a pointer to the internal Input object which allows sub-components on the sub-graph to utilize this output.
*
* Returns null if this output is an internal output on a component's sub-graph.
*
* \sa internal() Component::addSubComponent()
*/
Component::Input *Component::Output::to()
{
if(m_internal)
return 0;
if(m_to == 0)
{
// FIXME: this shouldn't be done here...
m_to = new Input("to " + name(), width(), component()); // TODO: figure out how to translate this
// m_to->setParentItem(component()->subGraph()); // FIXME: this doesn't work for some reason
m_to->m_internal = true;
m_to->m_externalOutput = this;
}
return m_to;
}
/**
* Returns a pointer to the component graph that this output belongs to.
*
* If this output is an internal output corresponding to an external input, this returns a pointer to the sub component graph. Otherwise, this returns a pointer to the same graph that this output's component belongs to.
*/
ComponentGraph *Component::Output::parentGraph() const
{
if(internal())
{
Q_ASSERT(component()->m_subGraph != 0);
return component()->m_subGraph;
}
return component()->parentGraph();
}
/**
* \fn Component::Output::internal()
* Returns true if this output is an internal output on a component's sub-graph. Otherwise, returns false.
*
* \sa to() Component::Input::internal()
*/
void Component::repositionInterfaces()
{
// TODO: rename this routine to something more appropriate
switch(m_layout)
{
case MINIMIZED:
{
// remove the interfaces from the scene as we're not displaying them
foreach(Input *input, m_inputs)
{
input->setParentItem(0);
if(input->scene() != 0)
{
input->scene()->removeItem(input);
}
}
foreach(Output *output, m_outputs)
{
output->setParentItem(0);
if(output->scene() != 0)
{
output->scene()->removeItem(output);
}
}
// remove the graph from the scene as we're not displaying it
if(m_subGraph != 0)
{
m_subGraph->setParentItem(0);
if(m_subGraph->scene() != 0)
{
m_subGraph->scene()->removeItem(m_subGraph);
}
}
}
break;
case LABELED:
{
// add the interfaces back to the scene
foreach(Input *input, m_inputs)
{
input->setParentItem(this);
}
foreach(Output *output, m_outputs)
{
output->setParentItem(this);
}
// repositions the interfaces into a vertical list in alphabetical order
qreal currentY = boundingRect().y() + TOP_MARGIN;
foreach(Interface *interface, m_inputs)
{
if(interface->scene() != scene())
{
scene()->addItem(interface); // add the item back into the scene
}
interface->setPos(0, currentY + interface->boundingRect().height() / 2);
currentY += interface->boundingRect().height();
currentY += INTERFACE_MARGIN;
}
foreach(Interface *interface, m_outputs)
{
if(interface->scene() != scene())
{
scene()->addItem(interface); // add the item back into the scene
}
interface->setPos(0, currentY + interface->boundingRect().height() / 2);
currentY += interface->boundingRect().height();
currentY += INTERFACE_MARGIN;
}
// remove the graph from the scene as we're not displaying it
if(m_subGraph != 0)
{
m_subGraph->setParentItem(0);
if(m_subGraph->scene() != 0)
{
m_subGraph->scene()->removeItem(m_subGraph);
}
}
}
break;
case EXPANDED:
{
// add the interfaces back to the scene
foreach(Input *input, m_inputs)
{
input->setParentItem(this);
}
foreach(Output *output, m_outputs)
{
output->setParentItem(this);
}
// repositions the interfaces into a horizontal list at the top, with the sub-graph underneath them
// FIXME: center the interfaces
QRectF boundingRect = this->boundingRect();
qreal currentX = boundingRect.x() + LEFT_MARGIN;
qreal maxInterfaceHeight = this->maxInterfaceHeight();
foreach(Interface *interface, m_inputs)
{
if(interface->scene() != scene())
{
scene()->addItem(interface); // add the item back into the scene
}
interface->setPos(currentX + interface->boundingRect().width() / 2, boundingRect.y() + TOP_MARGIN + maxInterfaceHeight / 2);
currentX += interface->boundingRect().width();
currentX += INTERFACE_MARGIN;
}
foreach(Interface *interface, m_outputs)
{
if(interface->scene() != scene())
{
scene()->addItem(interface); // add the item back into the scene
}
interface->setPos(currentX + interface->boundingRect().width() / 2, boundingRect.y() + TOP_MARGIN + maxInterfaceHeight / 2);
currentX += interface->boundingRect().width();
currentX += INTERFACE_MARGIN;
}
// add the graph back to the scene so it can be displayed
if(m_subGraph != 0)
{
m_subGraph->setParentItem(this);
// scale the graph so that it fits on the component
// FIXME: scale this whenever the graph changes
qreal graphDimensions = boundingRect.width() - LEFT_MARGIN - RIGHT_MARGIN;
m_subGraph->setScale(graphDimensions / qMax(m_subGraph->boundingRect().width(), m_subGraph->boundingRect().height()));
// m_subGraph->setScale(0.25);
m_subGraph->setPos(LEFT_MARGIN - boundingRect.width() / 2, TOP_MARGIN + maxInterfaceHeight + BOTTOM_MARGIN - boundingRect.height() / 2);
}
}
break;
default:
;
}
}
void Component::prepareGeometryChange()
{
if(m_parentGraph)
{
m_parentGraph->scheduleComponentResize(this);
}
}
/**
* Sets the data to be displayed in the component's data tab.
*
* \a data should cointain either an integer, boolean, or pointer to a byte array and a correct type set.
*
* If \a data contains a pointer to a byte array, then the contents of the byte array are copied and Component does not assume ownership of it.
*
* \sa getData
*/
void Component::setData(const QString &name, const ComponentData data)
{
}
/**
* Returns the data set by the setData() method.
*
* \sa setData()
*/
ComponentData Component::getData(const QString &name)
{
}
/**
* Returns an instance of the toolbox containing all of the tools needed to manipulate the component. Derived component classes can reimplement this method to add tools to the toolbox as they see fit.
*
* As the toolbox can be destroyed by closeToolBox(), this method should construct a new toolbox if no constructed toolbox exists. If a constructed toolbox does exist, a pointer to that toolbox should be returned.
*
* This method retains the ownership of the toolbox returned.
*
* \sa closeToolBox()
*/
QToolBox *Component::getToolBox()
{
if(m_toolBox == 0)
{
m_toolBox = new QToolBox();
m_toolBox->setWindowTitle(name());
m_dataTable = new QTableView(m_toolBox);
m_dataTable->setModel(new ComponentDataModel(this));
m_toolBox->addItem(m_dataTable, QObject::tr("Component Data"));
}
return m_toolBox;
}
/**
* Closes and frees memory associated with the toolbox. Derived classes should reimplement this method if they have made changes to the toolbox that need to be cleaned up.
*
* Because keeping a toolbox for each and every component would be a prohibitively large waste of memory, the toolbox can be destroyed at any time with this method.
*
* \sa getToolBox()
*/
void Component::closeToolBox()
{
delete m_toolBox;
m_toolBox = 0;
if(m_dataTable != 0)
{
QAbstractItemModel *model = m_dataTable->model();
delete m_dataTable;
delete model;
}
m_dataTable = 0;
}
qreal Component::maxInterfaceWidth() const
{
// returns the width of the widest interface in the component
qreal maxInterfaceWidth = 0;
foreach(QGraphicsItem *item, m_inputs)
{
if(item->boundingRect().width() > maxInterfaceWidth)
{
maxInterfaceWidth = item->boundingRect().width();
}
}
foreach(QGraphicsItem *item, m_outputs)
{
if(item->boundingRect().width() > maxInterfaceWidth)
{
maxInterfaceWidth = item->boundingRect().width();
}
}
return maxInterfaceWidth;
}
qreal Component::maxInterfaceHeight() const
{
// returns the height of the tallest interface in the component
qreal maxInterfaceHeight = 0;
foreach(QGraphicsItem *item, m_inputs)
{
if(item->boundingRect().height() > maxInterfaceHeight)
{
maxInterfaceHeight = item->boundingRect().height();
}
}
foreach(QGraphicsItem *item, m_outputs)
{
if(item->boundingRect().height() > maxInterfaceHeight)
{
maxInterfaceHeight = item->boundingRect().height();
}
}
return maxInterfaceHeight;
}
void Component::hoverEnterEvent(QGraphicsSceneHoverEvent *)
{