File size: 4,067 Bytes
a5ffdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/****************************************************************************
This file is part of the LibreCAD project, a 2D CAD program

** Copyright (C) 2012 Dongxu Li (dongxuli2011@gmail.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**********************************************************************/

#include "rs_actionblockssave.h"

#include <QApplication>

#include "lc_documentsstorage.h"
#include "lc_containertraverser.h"
#include "qc_applicationwindow.h"
#include "qc_mdiwindow.h"
#include "qg_blockwidget.h"
#include "qg_filedialog.h"
#include "rs_block.h"
#include "rs_debug.h"
#include "rs_graphic.h"
#include "rs_insert.h"

class RS_Block;
// fixme - sand - files - refactor action, move the logic outside as it might be reused
RS_ActionBlocksSave::RS_ActionBlocksSave(LC_ActionContext *actionContext)
        :RS_ActionInterface("Edit Block", actionContext, RS2::ActionBlocksSave) {}

/*recursive add blocks in graphic*/
void RS_ActionBlocksSave::addBlock(RS_Insert *in, RS_Graphic *g) {
    for (auto e: *in) {
        if (e->rtti() == RS2::EntityInsert) {
            auto *insert = static_cast<RS_Insert *>(e);
            addBlock(insert, g);
            g->addBlock(insert->getBlockForInsert());
        }
    }
}

// fixme - sand - investigate why layers from this block are not added to graphic..
RS_Graphic* RS_ActionBlocksSave::createGraphicForBlock(RS_Block *activeBlock){
    auto* result = new RS_Graphic();
    result->setOwner(false);
    result->getBlockList()->setOwner(false);
    result->clearLayers();
    // g.addLayer(b->getLayer());

    for (RS_Entity* e : lc::LC_ContainerTraverser{*activeBlock, RS2::ResolveNone}.entities()) {
        result->addEntity(e);
        if (e->rtti() == RS2::EntityInsert) {
            auto *insert = static_cast<RS_Insert *>(e);
            result->addBlock(insert->getBlockForInsert());
            addBlock(insert,result);
        }
        // g.addLayer(e->getLayer());
    }
    return result;
}

void RS_ActionBlocksSave::trigger() {
    auto& appWindow = QC_ApplicationWindow::getAppWindow();
    if(!appWindow) {
        finish(false);
        return;
    }
    RS_BlockList* blockList = appWindow->getBlockWidget() -> getBlockList();
    if (blockList != nullptr) {
        auto activeBlock= blockList->getActive();
        if(activeBlock != nullptr) {
            RS2::FormatType format = RS2::FormatDXFRW;
            QG_FileDialog dlg(appWindow->getCurrentMDIWindow(), {}, QG_FileDialog::BlockFile);
            const QString fileName = dlg.getSaveFile(&format, activeBlock->getName());
            if (fileName.isEmpty()) {
                // canceled, do nothing
            }
            else {
                QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
                RS_Graphic* graphic = createGraphicForBlock(activeBlock);
                graphic->setModified(true);

                LC_DocumentsStorage storage;
                storage.saveBlockAs(graphic, fileName);

                delete graphic;
                QApplication::restoreOverrideCursor();
            }
        } else
            commandMessage(tr("No block activated to save"));
    } else {
        RS_DEBUG->print(RS_Debug::D_WARNING,
                        "RS_ActionBlocksSave::trigger():  blockList is NULL");
    }
    finish(false);
}

void RS_ActionBlocksSave::init(int status) {
    RS_ActionInterface::init(status);
    trigger();
}