-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagram_edit_part.cpp
802 lines (642 loc) · 18.8 KB
/
diagram_edit_part.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
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
#include "diagram_edit_part.h"
#include <boost/foreach.hpp>
#include <algorithm>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <glibmm/i18n.h>
#include <command_manager.h>
#include <save_diagram_command.h>
#include <insert_model_element_command.h>
#include <group_command.h>
#include <selection_drag_tracker.h>
#include <move_drag_tracker.h>
#include <edit_part_factory.h>
namespace cppgef
{
DiagramEditPart::DiagramEditPart() :
selected_( false ),
select_new_child_( false )
{
}
DiagramEditPart::~DiagramEditPart()
{
}
weak_ptr< IContainerEditPart > DiagramEditPart::getParent()
{
return weak_ptr< IContainerEditPart >();
}
void DiagramEditPart::setParent(weak_ptr< IContainerEditPart > parent)
{
// Diagram has no parent
}
shared_ptr< ModelElementBase > DiagramEditPart::getModel()
{
return diagram_;
}
void DiagramEditPart::setModel(shared_ptr< ModelElementBase > value)
{
diagram_ = dynamic_pointer_cast< Diagram >(value);
// Connect model signals
title_changed_disconnect = auto_ptr< SignalDisconnect >( new SignalDisconnect(
diagram_->signalTitleChanged().connect (
sigc::mem_fun( this, &DiagramEditPart::onModelTitleChanged )) ) );
child_added_disconnect = auto_ptr< SignalDisconnect >( new SignalDisconnect(
diagram_->signalChildAdded().connect (
sigc::mem_fun( this, &DiagramEditPart::onModelChildAdded )) ) );
child_erased_disconnect = auto_ptr< SignalDisconnect >( new SignalDisconnect(
diagram_->signalChildErased().connect (
sigc::mem_fun( this, &DiagramEditPart::onModelChildErased )) ) );
// Initialize view
rectangle_figure_ = dynamic_pointer_cast< RectangleFigure >( createFigure() );
// Create children EditPart from Model children
for (Diagram::iterator shape = diagram_->getChildrenBegin();
shape != diagram_->getChildrenEnd();
shape++)
{
addChild (*shape);
}
}
shared_ptr<Figure> DiagramEditPart::getFigure()
{
return rectangle_figure_;
}
void DiagramEditPart::setFigure(shared_ptr<Figure> value)
{
rectangle_figure_ = dynamic_pointer_cast< RectangleFigure >(value);
}
bool DiagramEditPart::isSelected() const
{
return selected_;
}
void DiagramEditPart::setSelected(bool value)
{
if (value != selected_)
{
selected_ = value;
if (value)
{
signal_selection_add_.emit (shared_from_this());
}
else
{
signal_selection_remove_.emit (shared_from_this());
}
}
}
bool DiagramEditPart::isAncestorSelected() const
{
return false;
}
bool DiagramEditPart::isMoveSupported() const
{
return false;
}
bool DiagramEditPart::isResizeSupported() const
{
return false;
}
bool DiagramEditPart::isMovePointSupported() const
{
return false;
}
shared_ptr<ModelElementBase> DiagramEditPart::createModelElement()
{
return shared_ptr< ModelElementBase >(new Diagram() );
}
shared_ptr< Figure > DiagramEditPart::createFigure()
{
shared_ptr< RectangleFigure > rectangle_figure( new RectangleFigure() );
rectangle_figure->getFillStyle()->setColor (Color("white"));
rectangle_figure->getStrokeStyle()->setColor (Color("white"));
return rectangle_figure;
}
shared_ptr< Figure > DiagramEditPart::getClientFigure()
{
return rectangle_figure_;
}
bool DiagramEditPart::open(const std::string& file_name)
{
std::ifstream ifs(file_name.c_str());
if (ifs.good())
{
boost::archive::xml_iarchive ia(ifs);
Diagram* diagram = new Diagram();
ia >> boost::serialization::make_nvp ("Diagram", *diagram);
setModel (shared_ptr< Diagram >( diagram ));
return true;
}
else
{
throw Error(
Glib::ustring::compose(_("Fail to access file '%1'"), file_name));
}
return false;
}
bool DiagramEditPart::save(const std::string& file_name)
{
shared_ptr< SaveDiagramCommand > cmd( new SaveDiagramCommand(diagram_, file_name) );
bool res = CommandManager::getInstance()->execute (cmd);
return res;
}
void DiagramEditPart::querySelectedChildrenNoChild(children_container_t& container)
{
BOOST_FOREACH(shared_ptr< IEditPart > edit_part, selected_children_)
{
if (std::find (
selected_children_.begin(),
selected_children_.end(),
edit_part->getParent().lock()) == selected_children_.end())
{
container.push_back (edit_part);
}
}
}
bool DiagramEditPart::isSelectNewChild() const
{
return select_new_child_;
}
void DiagramEditPart::setSelectNewChild(bool value)
{
select_new_child_ = value;
}
void DiagramEditPart::onModelTitleChanged()
{
//TODO: do something!
}
void DiagramEditPart::onModelChildAdded(shared_ptr< ModelElementBase > element)
{
addChild (element);
}
void DiagramEditPart::onModelChildErased(shared_ptr< ModelElementBase > element)
{
// Search the edit part child from the source model
for (iterator child_itor = children_.begin();
child_itor != children_.end();
child_itor++)
{
if ((*child_itor)->getModel() == element)
{
// Erase child from selection
if ((*child_itor)->isSelected())
(*child_itor)->setSelected (false);
// Erase child view
rectangle_figure_->eraseChild ((*child_itor)->getFigure());
// Erase child edit part from children
children_.erase (child_itor);
return;
}
}
}
bool DiagramEditPart::hasLayoutManager() const
{
return false;
}
void DiagramEditPart::setLayoutConstraint(shared_ptr< ModelElementBase > element, const Rectangle& bounds)
{
}
shared_ptr< IContainerEditPart > DiagramEditPart::findInnerContainer(const Point& point)
{
shared_ptr< IContainerEditPart > container;
for (reverse_iterator itor = children_.rbegin();
itor != children_.rend();
itor++)
{
container = (*itor)->findInnerContainer (point);
if (container)
return container;
}
return shared_from_this();
}
shared_ptr< ICommand > DiagramEditPart::createMoveCommand(int dx, int dy)
{
std::list< shared_ptr< ICommand > > commands;
BOOST_FOREACH(shared_ptr< IEditPart > child, selected_children_)
{
shared_ptr< ICommand > cmd = child->createMoveCommand (dx, dy);
if (cmd)
commands.push_back (cmd);
}
return shared_ptr< GroupCommand >(new GroupCommand (diagram_, commands) );
}
shared_ptr< ICommand > DiagramEditPart::createResizeCommand(AbstractResizePolicy::ResizeDirection resize_direction, int dx, int dy)
{
std::list< shared_ptr< ICommand > > commands;
BOOST_FOREACH(shared_ptr< IEditPart > child, selected_children_)
{
shared_ptr< ICommand > cmd = child->createResizeCommand (resize_direction, dx, dy);
if (cmd)
commands.push_back (cmd);
}
return shared_ptr< GroupCommand >(new GroupCommand (diagram_, commands) );
}
shared_ptr< ICommand > DiagramEditPart::createReparentCommand(shared_ptr< ComplexModelElementBase > new_parent, int dx, int dy)
{
std::list< shared_ptr< ICommand > > commands;
BOOST_FOREACH(shared_ptr< IEditPart > child, selected_children_)
{
shared_ptr< ICommand > cmd = child->createReparentCommand (new_parent, dx, dy);
if (cmd)
commands.push_back (cmd);
}
return shared_ptr< GroupCommand >(new GroupCommand (diagram_, commands) );
}
shared_ptr< ICommand > DiagramEditPart::createMovePointCommand(IMovePointPolicy::PointSequence sequence, int dx, int dy)
{
std::list< shared_ptr< ICommand > > commands;
BOOST_FOREACH(shared_ptr< IEditPart > child, selected_children_)
{
shared_ptr< ICommand > cmd = child->createMovePointCommand (sequence, dx, dy);
if (cmd)
commands.push_back (cmd);
}
return shared_ptr< GroupCommand >(new GroupCommand (diagram_, commands) );
}
shared_ptr< ICommand > DiagramEditPart::createInsertModelElementCommand(shared_ptr< ModelElementBase > element)
{
// Create the new command and execute it
shared_ptr< InsertModelElementCommand > cmd( new InsertModelElementCommand( diagram_, element ) );
return cmd;
}
shared_ptr< ICommand > DiagramEditPart::createSetLineColorCommand(const Color& color)
{
std::list< shared_ptr< ICommand > > commands;
BOOST_FOREACH(shared_ptr< IEditPart > child, selected_children_)
{
shared_ptr< ICommand > cmd = child->createSetLineColorCommand (color);
if (cmd)
commands.push_back (cmd);
}
if (commands.size() > 0)
return shared_ptr< GroupCommand >(new GroupCommand (diagram_, commands) );
return shared_ptr< ICommand >();
}
shared_ptr< ICommand > DiagramEditPart::createSetLineWidthCommand(double width)
{
std::list< shared_ptr< ICommand > > commands;
BOOST_FOREACH(shared_ptr< IEditPart > child, selected_children_)
{
shared_ptr< ICommand > cmd = child->createSetLineWidthCommand (width);
if (cmd)
commands.push_back (cmd);
}
if (commands.size() > 0)
return shared_ptr< GroupCommand >(new GroupCommand (diagram_, commands) );
return shared_ptr< ICommand >();
}
shared_ptr< ICommand > DiagramEditPart::createSetFillColorCommand(const Color& color)
{
std::list< shared_ptr< ICommand > > commands;
BOOST_FOREACH(shared_ptr< IEditPart > child, selected_children_)
{
shared_ptr< ICommand > cmd = child->createSetFillColorCommand (color);
if (cmd)
commands.push_back (cmd);
}
if (commands.size() > 0)
return shared_ptr< GroupCommand >(new GroupCommand (diagram_, commands) );
return shared_ptr< ICommand >();
}
shared_ptr< ICommand > DiagramEditPart::createSetDashStyleCommand(const std::vector< double > dash_style, double dash_offset)
{
std::list< shared_ptr< ICommand > > commands;
BOOST_FOREACH(shared_ptr< IEditPart > child, selected_children_)
{
shared_ptr< ICommand > cmd = child->createSetDashStyleCommand (dash_style, dash_offset);
if (cmd)
commands.push_back (cmd);
}
if (commands.size() > 0)
return shared_ptr< GroupCommand >(new GroupCommand (diagram_, commands) );
return shared_ptr< ICommand >();
}
shared_ptr< ICommand > DiagramEditPart::createDeleteCommand()
{
std::list< shared_ptr< ICommand > > commands;
// Create a vector of selected items sorted by index
SortVector< shared_ptr< IEditPart >, int > sorted_selection;
BOOST_FOREACH(shared_ptr< IEditPart > child, selected_children_)
{
// If the child parent is selected do not add child
if (std::find (
selected_children_.begin(),
selected_children_.end(),
child->getParent().lock()) == selected_children_.end())
{
SortItem< shared_ptr< IEditPart >, int > new_item;
new_item.item = child;
new_item.key = diagram_->getChildIndex (child->getModel());
sorted_selection.items.push_back (new_item);
}
}
// Sort selection by index
sorted_selection.sort();
// Create delete commands
for (size_t i = 0; i < sorted_selection.items.size(); i++)
{
shared_ptr< ICommand > cmd = sorted_selection.items[i].item->createDeleteCommand();
if (cmd)
commands.push_back (cmd);
}
if (commands.size() > 0)
{
clearSelection();
return shared_ptr< GroupCommand >(new GroupCommand (diagram_, commands) );
}
return shared_ptr< ICommand >();
}
void DiagramEditPart::clearSelection()
{
while (!selected_children_.empty())
{
// Set only selected to false, the selected child is effectively removed
// when the selection_remove signal is caught by onChildSelectionRemove
(*selected_children_.begin())->setSelected (false);
}
}
void DiagramEditPart::selectAll()
{
for (iterator itor = children_.begin();
itor != children_.end();
itor++)
{
(*itor)->setSelected (true);
// If the child is a container invoke selectAll()
shared_ptr< IContainerEditPart > container = dynamic_pointer_cast< IContainerEditPart >( *itor );
if (container)
container->selectAll();
}
}
shared_ptr< IEditPart > DiagramEditPart::getChildAt(int index)
{
return children_.at (index);
}
bool DiagramEditPart::selectFromPoint(const Point& point)
{
for (reverse_iterator itor = children_.rbegin();
itor != children_.rend();
itor++)
{
if ((*itor)->selectFromPoint (point))
return true;
}
return false;
}
bool DiagramEditPart::selectFromRectangle(const Rectangle& rectangle)
{
bool res = false;
BOOST_FOREACH(shared_ptr< IEditPart > edit_part, children_)
{
res |= edit_part->selectFromRectangle (rectangle);
}
return res;
}
bool DiagramEditPart::toggleSelectionFromPoint(const Point& point)
{
for (reverse_iterator itor = children_.rbegin();
itor != children_.rend();
itor++)
{
if ((*itor)->toggleSelectionFromPoint (point))
return true;
}
return false;
}
bool DiagramEditPart::toggleSelectionFromRectangle(const Rectangle& rectangle)
{
bool res = false;
BOOST_FOREACH(shared_ptr< IEditPart > edit_part, children_)
{
res |= edit_part->toggleSelectionFromRectangle (rectangle);
}
return res;
}
bool DiagramEditPart::queryStartMove(const Point& point)
{
return false;
}
shared_ptr< IDragTracker > DiagramEditPart::queryDragTracker(const Point& point, const KeyModifier& key_modifier)
{
// 1. point is on a selected child drag tacker
BOOST_FOREACH(shared_ptr< IEditPart > edit_part, selected_children_)
{
shared_ptr< IDragTracker > drag_tracker = edit_part->queryDragTracker (point, key_modifier);
if (drag_tracker)
{
return drag_tracker;
}
}
// 2. point is on a selected child
if (key_modifier.isShift())
{
// Toggle selection
for (reverse_iterator itor = children_.rbegin();
itor != children_.rend();
itor++)
{
if ((*itor)->toggleSelectionFromPoint (point))
return shared_ptr< IDragTracker >();
}
}
else
{
// Start move
BOOST_FOREACH(shared_ptr< IEditPart > edit_part, selected_children_)
{
if (edit_part->queryStartMove (point))
{
return shared_ptr< IDragTracker >(new MoveDragTracker( shared_from_this(), edit_part ));
}
}
}
// 3. point is on a not selected child:
if (key_modifier.isShift())
{
// Toggle selection
for (reverse_iterator itor = children_.rbegin();
itor != children_.rend();
itor++)
{
if ((*itor)->toggleSelectionFromPoint (point))
return shared_ptr< IDragTracker >();
}
}
else
{
// Select child and start move
for (reverse_iterator itor = children_.rbegin();
itor != children_.rend();
itor++)
{
if ((*itor)->queryStartMove (point))
{
clearSelection();
// Use selectFromPoint because the selected item can be a child of a child
(*itor)->selectFromPoint (point);
return shared_ptr< IDragTracker >(new MoveDragTracker( shared_from_this(), *(selected_children_.begin()) ));
}
}
}
// 4. else start new selection
return shared_ptr< IDragTracker >(new SelectionDragTracker(
shared_from_this(),
key_modifier.isShift() ? SelectionDragTracker::AddToSelection : SelectionDragTracker::NewSelection ));
}
void DiagramEditPart::paintSelectedDragTrackers(Cairo::RefPtr< Cairo::Context > context)
{
BOOST_FOREACH(shared_ptr< IEditPart > edit_part, selected_children_)
{
edit_part->paintSelectedDragTrackers (context);
}
}
shared_ptr< IInplaceEditor > DiagramEditPart::queryInplaceEditor(const Point& point)
{
for (reverse_iterator itor = children_.rbegin();
itor != children_.rend();
itor++)
{
shared_ptr< IInplaceEditor > editor = (*itor)->queryInplaceEditor (point);
if (editor)
{
clearSelection();
return editor;
}
}
return shared_ptr< IInplaceEditor >();
}
DiagramEditPart::iterator DiagramEditPart::getChildrenBegin()
{
return children_.begin();
}
DiagramEditPart::const_iterator DiagramEditPart::getChildrenBegin() const
{
return children_.begin();
}
DiagramEditPart::iterator DiagramEditPart::getChildrenEnd()
{
return children_.end();
}
DiagramEditPart::const_iterator DiagramEditPart::getChildrenEnd() const
{
return children_.end();
}
DiagramEditPart::size_type DiagramEditPart::getChildrenSize() const
{
return children_.size();
}
DiagramEditPart::iterator DiagramEditPart::getSelectedChildrenBegin()
{
return selected_children_.begin();
}
DiagramEditPart::const_iterator DiagramEditPart::getSelectedChildrenBegin() const
{
return selected_children_.begin();
}
DiagramEditPart::iterator DiagramEditPart::getSelectedChildrenEnd()
{
return selected_children_.end();
}
DiagramEditPart::const_iterator DiagramEditPart::getSelectedChildrenEnd() const
{
return selected_children_.end();
}
DiagramEditPart::size_type DiagramEditPart::getSelectedChildrenSize() const
{
return selected_children_.size();
}
DiagramEditPart::signal_selection_change_t DiagramEditPart::signalSelectionAdd()
{
return signal_selection_add_;
}
DiagramEditPart::signal_selection_change_t DiagramEditPart::signalSelectionRemove()
{
return signal_selection_remove_;
}
void DiagramEditPart::onChildSelectionAdd(shared_ptr< IEditPart > edit_part)
{
selected_children_.push_back (edit_part);
signal_selection_add_.emit (edit_part);
}
void DiagramEditPart::onChildSelectionRemove(shared_ptr< IEditPart > edit_part)
{
iterator itor =
std::find (selected_children_.begin(), selected_children_.end(), edit_part);
if (itor != selected_children_.end())
{
selected_children_.erase (itor);
}
signal_selection_remove_.emit (edit_part);
}
void DiagramEditPart::fillSelectedShapeList(std::list< shared_ptr< ModelElementBase > >& vector)
{
BOOST_FOREACH(shared_ptr< IEditPart > edit_part, selected_children_)
{
vector.push_back (edit_part->getModel());
}
}
void DiagramEditPart::addChild(shared_ptr< ModelElementBase > element)
{
// Add the child edit part
shared_ptr< IEditPart > edit_part =
EditPartFactory::getInstance()->createEditPart (element);
edit_part->setParent (shared_from_this());
// Get index from model element
int index = -1;
if (!children_.empty())
{
int element_index = diagram_->getChildIndex (element);
index = 0;
// Search for a child edit part relative to a model element
// with the index less than the index of given model element
for (int i = children_.size() - 1; i >= 0; i--)
{
if (diagram_->getChildIndex (children_[i]->getModel()) < element_index)
{
index = i + 1;
break;
}
}
}
// Test if the new element is to be added at the
// end of the children list or in the middle of the list
if (index == -1 || index >= children_.size())
{
// Add the element at the end of the list
children_.push_back (edit_part);
// Add the child view
rectangle_figure_->addChild (edit_part->getFigure());
}
else
{
shared_ptr< IEditPart > old_child_at_pos = children_[index];
children_.insert (children_.begin() + index, edit_part);
// Add the child view before the next view
int figure_index = rectangle_figure_->getChildIndex (old_child_at_pos->getFigure());
rectangle_figure_->insertChild (edit_part->getFigure(), figure_index);
}
edit_part->signalSelectionAdd().connect (
sigc::mem_fun(this, &DiagramEditPart::onChildSelectionAdd));
edit_part->signalSelectionRemove().connect (
sigc::mem_fun(this, &DiagramEditPart::onChildSelectionRemove));
if (select_new_child_)
{
edit_part->setSelected (true);
}
}
shared_ptr< DiagramEditPart > DiagramEditPart::queryDiagramFromChild(shared_ptr< IEditPart > child)
{
// Search for the root item
if (shared_ptr< IContainerEditPart > root = child->getParent().lock())
{
while (shared_ptr< IContainerEditPart > root_parent = root->getParent().lock())
{
root = root_parent;
}
return dynamic_pointer_cast< DiagramEditPart >(root);
}
// The child is the DiagramEditPart
return dynamic_pointer_cast< DiagramEditPart >( child );
}
}