1 /****************************************************************************
2 This file is part of the LibreCAD project, a 2D CAD program
3 
4 ** Copyright (C) 2012 Dongxu Li (dongxuli2011@gmail.com)
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 **********************************************************************/
20 #include "rs_actionblockssave.h"
21 
22 #include <QAction>
23 #include <QApplication>
24 #include "qg_blockwidget.h"
25 #include "qg_filedialog.h"
26 #include "qc_applicationwindow.h"
27 #include "rs_graphic.h"
28 #include "rs_dialogfactory.h"
29 #include "rs_insert.h"
30 #include "rs_coordinateevent.h"
31 #include "qc_mdiwindow.h"
32 #include "rs_debug.h"
33 
34 
35 
RS_ActionBlocksSave(RS_EntityContainer & container,RS_GraphicView & graphicView)36 RS_ActionBlocksSave::RS_ActionBlocksSave(RS_EntityContainer& container,
37         RS_GraphicView& graphicView)
38         :RS_ActionInterface("Edit Block", container, graphicView) {}
39 
40 /*recursive add blocks in graphic*/
addBlock(RS_Insert * in,RS_Graphic * g)41 void RS_ActionBlocksSave::addBlock(RS_Insert* in, RS_Graphic* g) {
42 
43 	for(auto e: *in){
44 
45         if (e->rtti() == RS2::EntityInsert) {
46 			RS_Insert * in=static_cast<RS_Insert *>(e);
47 			addBlock(in,g);
48 			g->addBlock(in->getBlockForInsert());
49         }
50     }
51 }
52 
trigger()53 void RS_ActionBlocksSave::trigger() {
54     RS_DEBUG->print("save block to file");
55     QC_ApplicationWindow* appWindow = QC_ApplicationWindow::getAppWindow();
56 	if(!appWindow) {
57         finish(false);
58         return;
59     }
60     RS_BlockList* bList = appWindow->getBlockWidget() -> getBlockList();
61     if (bList) {
62         auto b=bList->getActive();
63         if(b) {
64 			RS_Graphic g(nullptr);
65             g.setOwner(false);
66             g.getBlockList()->setOwner(false);
67 
68            g.clearLayers();
69 //           g.addLayer(b->getLayer());
70             for (RS_Entity* e=b->firstEntity(RS2::ResolveNone);
71                  e;
72                  e = b->nextEntity(RS2::ResolveNone)) {
73                 g.addEntity(e);
74                 if (e->rtti() == RS2::EntityInsert) {
75 					RS_Insert *in = static_cast<RS_Insert *>(e);
76                     g.addBlock(in->getBlockForInsert());
77 					addBlock(in,&g);
78                 }
79 //           std::cout<<__FILE__<<" : "<<__func__<<" : line: "<<__LINE__<<" : "<<e->rtti()<<std::endl;
80 //                g.addLayer(e->getLayer());
81 //           std::cout<<__FILE__<<" : "<<__func__<<" : line: "<<__LINE__<<" : "<<e->rtti()<<std::endl;
82             }
83 //           std::cout<<__FILE__<<" : "<<__func__<<" : line: "<<__LINE__<<std::endl;
84 //           std::cout<<"add layer name="<<qPrintable(b->getLayer()->getName())<<std::endl;
85 
86             RS2::FormatType t = RS2::FormatDXFRW;
87 
88             QG_FileDialog dlg(appWindow->getMDIWindow(),0, QG_FileDialog::BlockFile);
89 			QString const& fn = dlg.getSaveFile(&t);
90             QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
91 //            g.setModified(true);
92             g.saveAs(fn, t);
93             QApplication::restoreOverrideCursor();
94 		} else
95 			RS_DIALOGFACTORY->commandMessage(tr("No block activated to save"));
96     } else {
97         RS_DEBUG->print(RS_Debug::D_WARNING,
98                         "RS_ActionBlocksSave::trigger():  blockList is NULL");
99     }
100     finish(false);
101 }
102 
103 
104 
init(int status)105 void RS_ActionBlocksSave::init(int status) {
106     RS_ActionInterface::init(status);
107     trigger();
108 }
109 
110