]> Shamusworld >> Repos - architektonas/blob - src/actions/actionblocksremove.cpp
Last checkin before major refactor...
[architektonas] / src / actions / actionblocksremove.cpp
1 // actionblocksremove.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/03/2010  Added this text. :-)
13 //
14
15 #include "actionblocksremove.h"
16
17 #include "rs_block.h"
18 #include "rs_dialogfactory.h"
19 #include "drawing.h"
20 #include "graphicview.h"
21 #include "rs_insert.h"
22
23 ActionBlocksRemove::ActionBlocksRemove(RS_EntityContainer & container, GraphicView & graphicView):
24         ActionInterface("Remove Block", container, graphicView)
25 {
26 }
27
28 ActionBlocksRemove::~ActionBlocksRemove()
29 {
30 }
31
32 void ActionBlocksRemove::trigger()
33 {
34         RS_DEBUG->print("ActionBlocksRemove::trigger");
35
36         if (graphic)
37         {
38                 RS_Block * block = RS_DIALOGFACTORY->requestBlockRemovalDialog(graphic->getBlockList());
39
40                 // list of containers that might refer to the block via inserts:
41                 QList<RS_EntityContainer *> containerList;
42                 containerList.append(graphic);
43                 RS_BlockList * blkLst = graphic->getBlockList();
44
45                 for(uint bi=0; bi<blkLst->count(); bi++)
46                         containerList.append(blkLst->at(bi));
47
48                 if (block)
49                 {
50                         for(int i=0; i<containerList.size(); i++)
51                         {
52                                 RS_EntityContainer * cont = containerList[i];
53                                 // remove all inserts from the graphic:
54                                 bool done;
55
56                                 do
57                                 {
58                                         done = true;
59
60                                         for(RS_Entity * e=cont->firstEntity(RS2::ResolveNone); e!=NULL;
61                                              e=cont->nextEntity(RS2::ResolveNone))
62                                         {
63                                                 if (e->rtti() == RS2::EntityInsert)
64                                                 {
65                                                         RS_Insert * ins = (RS_Insert *)e;
66
67                                                         if (ins->getName() == block->getName())
68                                                         {
69                                                                 cont->removeEntity(ins);
70                                                                 done = false;
71                                                                 break;
72                                                         }
73                                                 }
74                                         }
75                                 }
76                                 while (!done);
77                         }
78
79                         // close all windows that are editing this block:
80                         if (RS_DIALOGFACTORY)
81                                 RS_DIALOGFACTORY->closeEditBlockWindow(block);
82
83                         // Now remove the block from the block list:
84                         graphic->removeBlock(block);
85                         graphic->updateInserts();
86                         graphicView->redraw();
87                 }
88         }
89
90         finish();
91         RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
92 }
93
94 void ActionBlocksRemove::init(int status)
95 {
96         ActionInterface::init(status);
97         trigger();
98 }