1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 
27 #include "rs_actionblocksremove.h"
28 
29 #include <QAction>
30 #include "rs_dialogfactory.h"
31 #include "rs_graphicview.h"
32 #include "rs_graphic.h"
33 #include "rs_insert.h"
34 #include "rs_debug.h"
35 
36 
RS_ActionBlocksRemove(RS_EntityContainer & container,RS_GraphicView & graphicView)37 RS_ActionBlocksRemove::RS_ActionBlocksRemove(RS_EntityContainer& container,
38         RS_GraphicView& graphicView)
39         :RS_ActionInterface("Remove Block", container, graphicView) {}
40 
trigger()41 void RS_ActionBlocksRemove::trigger() {
42 	RS_DEBUG->print("RS_ActionBlocksRemove::trigger");
43 
44 	if (!(graphic && document)) {
45 		finish(false);
46 		return;
47 	}
48 
49 	RS_BlockList* bl = graphic->getBlockList();
50 	QList<RS_Block*> blocks =
51 		RS_DIALOGFACTORY->requestSelectedBlocksRemovalDialog(bl);
52 
53     if (blocks.isEmpty()) {
54         finish(false);
55         return;
56     }
57 
58 	// list of containers that might refer to the block via inserts:
59 	std::vector<RS_EntityContainer*> containerList;
60 	containerList.push_back(graphic);
61     for (int bi = 0; bi < bl->count(); bi++) {
62         containerList.push_back(bl->at(bi));
63 	}
64 
65     document->startUndoCycle();
66 
67 	for (auto block: blocks) {
68         if (nullptr == block) {
69             continue;
70         }
71         for(auto cont: containerList){
72 			// remove all inserts from the graphic:
73 			bool done;
74 			do {
75 				done = true;
76 				for(auto e: *cont){
77 
78 					if (e->rtti()==RS2::EntityInsert) {
79 						RS_Insert* ins = (RS_Insert*)e;
80 						if (ins->getName()==block->getName() && !ins->isUndone()) {
81                             document->addUndoable(ins);
82                             ins->setUndoState(true);
83 							done = false;
84 							break;
85 						}
86 					}
87 				}
88 			} while (!done);
89 		}
90 
91 		// clear selection and active state
92 		block->selectedInBlockList(false);
93 		if (block == bl->getActive()) {
94 			bl->activate(nullptr);
95 		}
96 
97 		// close all windows that are editing this block:
98 		RS_DIALOGFACTORY->closeEditBlockWindow(block);
99 
100         // Now remove block from the block list, but do not delete:
101         block->setUndoState(true);
102         document->addUndoable(block);
103     }
104     document->endUndoCycle();
105 
106     graphic->addBlockNotification();
107 	graphic->updateInserts();
108 	graphicView->redraw(RS2::RedrawDrawing);
109     bl->activate(nullptr);
110 
111 	finish(false);
112 	RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected(),container->totalSelectedLength());
113 }
114 
115 
116 
init(int status)117 void RS_ActionBlocksRemove::init(int status) {
118     RS_ActionInterface::init(status);
119     trigger();
120 }
121 
122 // EOF
123