📄 rs_modification.cpp
字号:
/****************************************************************************** $Id: rs_modification.cpp 2419 2005-07-01 16:47:27Z andrew $**** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.**** This file is part of the qcadlib Library project.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid qcadlib Professional Edition licenses may use ** this file in accordance with the qcadlib Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.ribbonsoft.com for further details.**** Contact info@ribbonsoft.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "rs_modification.h"#include "rs_clipboard.h"#include "rs_creation.h"#include "rs_entity.h"#include "rs_graphic.h"#include "rs_information.h"#include "rs_insert.h"#include "rs_polyline.h"#include "rs_text.h"/** * Default constructor. * * @param container The container to which we will add * entities. Usually that's an RS_Graphic entity but * it can also be a polyline, text, ... * @param graphicView Pointer to graphic view or NULL if you don't want the * any views to be updated. * @param handleUndo true: Handle undo functionalitiy. */RS_Modification::RS_Modification(RS_EntityContainer& container, RS_GraphicView* graphicView, bool handleUndo) { this->container = &container; this->graphicView = graphicView; this->handleUndo = handleUndo; graphic = container.getGraphic(); document = container.getDocument();}/** * Deletes all selected entities. */void RS_Modification::remove() { if (container==NULL) { RS_DEBUG->print("RS_Modification::remove: no valid container", RS_Debug::D_WARNING); return; } if (document!=NULL) { document->startUndoCycle(); } // not safe (?) for (RS_Entity* e=container->firstEntity(); e!=NULL; e=container->nextEntity()) { if (e!=NULL && e->isSelected()) { e->setSelected(false); e->changeUndoState(); if (document!=NULL) { document->addUndoable(e); } } } if (document!=NULL) { document->endUndoCycle(); } graphicView->redraw();}/** * Changes the attributes of all selected */bool RS_Modification::changeAttributes(RS_AttributesData& data) { if (container==NULL) { RS_DEBUG->print("RS_Modification::changeAttributes: no valid container", RS_Debug::D_WARNING); return false; } RS_PtrList<RS_Entity> addList; addList.setAutoDelete(false); if (document!=NULL) { document->startUndoCycle(); } for (RS_Entity* e=container->firstEntity(); e!=NULL; e=container->nextEntity()) { //for (uint i=0; i<container->count(); ++i) { //RS_Entity* e = container->entityAt(i); if (e!=NULL && e->isSelected()) { RS_Entity* ec = e->clone(); ec->setSelected(false); RS_Pen pen = ec->getPen(false); if (data.changeLayer==true) { ec->setLayer(data.layer); } if (data.changeColor==true) { pen.setColor(data.pen.getColor()); } if (data.changeLineType==true) { pen.setLineType(data.pen.getLineType()); } if (data.changeWidth==true) { pen.setWidth(data.pen.getWidth()); } ec->setPen(pen); //if (data.useCurrentLayer) { // ec->setLayerToActive(); //} //if (data.useCurrentAttributes) { // ec->setPenToActive(); //} //if (ec->rtti()==RS2::EntityInsert) { // ((RS_Insert*)ec)->update(); //} ec->update(); addList.append(ec); } } deselectOriginals(true); addNewEntities(addList); if (document!=NULL) { document->endUndoCycle(); } if (graphicView!=NULL) { graphicView->redraw(); } return true;}/** * Copies all selected entities from the given container to the clipboard. * Layers and blocks that are needed are also copied if the container is * or is part of an RS_Graphic. * * @param container The entity container. * @param ref Reference point. The entities will be moved by -ref. * @param cut true: cut instead of copying, false: copy */void RS_Modification::copy(const RS_Vector& ref, const bool cut) { if (container==NULL) { RS_DEBUG->print("RS_Modification::copy: no valid container", RS_Debug::D_WARNING); return; } RS_CLIPBOARD->clear(); if (graphic!=NULL) { RS_CLIPBOARD->getGraphic()->setUnit(graphic->getUnit()); } else { RS_CLIPBOARD->getGraphic()->setUnit(RS2::None); } // start undo cycle for the container if we're cutting if (cut && document!=NULL) { document->startUndoCycle(); } // copy entities / layers / blocks for (RS_Entity* e=container->firstEntity(); e!=NULL; e=container->nextEntity()) { //for (uint i=0; i<container->count(); ++i) { //RS_Entity* e = container->entityAt(i); if (e!=NULL && e->isSelected()) { copyEntity(e, ref, cut); } } if (cut && document!=NULL) { document->endUndoCycle(); }}/** * Copies the given entity from the given container to the clipboard. * Layers and blocks that are needed are also copied if the container is * or is part of an RS_Graphic. * * @param e The entity. * @param ref Reference point. The entities will be moved by -ref. * @param cut true: cut instead of copying, false: copy */void RS_Modification::copyEntity(RS_Entity* e, const RS_Vector& ref, const bool cut) { if (e!=NULL && e->isSelected()) { // delete entity in graphic view: if (cut) { if (graphicView!=NULL) { graphicView->deleteEntity(e); } e->setSelected(false); } else { if (graphicView!=NULL) { graphicView->deleteEntity(e); } e->setSelected(false); if (graphicView!=NULL) { graphicView->drawEntity(e); } } // add entity to clipboard: RS_Entity* c = e->clone(); c->move(-ref); RS_CLIPBOARD->addEntity(c); copyLayers(e); copyBlocks(e); // set layer to the layer clone: RS_Layer* l = e->getLayer(); if (l!=NULL) { c->setLayer(l->getName()); } // make sure all sub entities point to layers of the clipboard if (c->isContainer()) { RS_EntityContainer* ec = (RS_EntityContainer*)c; for (RS_Entity* e2 = ec->firstEntity(RS2::ResolveAll); e2!=NULL; e2 = ec->nextEntity(RS2::ResolveAll)) { //RS_Entity* e2 = ec->entityAt(i); RS_Layer* l2 = e2->getLayer(); if (l2!=NULL) { e2->setLayer(l2->getName()); } } } if (cut) { e->changeUndoState(); if (document!=NULL) { document->addUndoable(e); } } }}/** * Copies all layers of the given entity to the clipboard. */void RS_Modification::copyLayers(RS_Entity* e) { if (e==NULL) { return; } // add layer(s) of the entity if it's not an insert // (inserts are on layer '0'): if (e->rtti()!=RS2::EntityInsert) { RS_Layer* l = e->getLayer(); if (l!=NULL) { if (!RS_CLIPBOARD->hasLayer(l->getName())) { RS_CLIPBOARD->addLayer(l->clone()); } } } // special handling of inserts: else { // insert: add layer(s) of subentities: RS_Block* b = ((RS_Insert*)e)->getBlockForInsert(); if (b!=NULL) { for (RS_Entity* e2=b->firstEntity(); e2!=NULL; e2=b->nextEntity()) { //for (uint i=0; i<b->count(); ++i) { //RS_Entity* e2 = b->entityAt(i); copyLayers(e2); } } }}/** * Copies all blocks of the given entity to the clipboard. */void RS_Modification::copyBlocks(RS_Entity* e) { if (e==NULL) { return; } // add block of the entity if it's an insert if (e->rtti()==RS2::EntityInsert) { RS_Block* b = ((RS_Insert*)e)->getBlockForInsert(); if (b!=NULL) { // add block of an insert: if (!RS_CLIPBOARD->hasBlock(b->getName())) { RS_CLIPBOARD->addBlock((RS_Block*)b->clone()); } for (RS_Entity* e2=b->firstEntity(); e2!=NULL; e2=b->nextEntity()) { //for (uint i=0; i<b->count(); ++i) { //RS_Entity* e2 = b->entityAt(i); copyBlocks(e2); } } }}/** * Pastes all entities from the clipboard into the container. * Layers and blocks that are needed are also copied if the container is * or is part of an RS_Graphic. * * @param data Paste data. * @param source The source from where to paste. NULL means the source * is the clipboard. */void RS_Modification::paste(const RS_PasteData& data, RS_Graphic* source) { if (graphic==NULL) { RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Modification::paste: Graphic is NULL"); return; } double factor = 1.0; if (source==NULL) { source = RS_CLIPBOARD->getGraphic(); // graphics from the clipboard need to be scaled. from the part lib not: RS2::Unit sourceUnit = source->getUnit(); RS2::Unit targetUnit = graphic->getUnit(); factor = RS_Units::convert(1.0, sourceUnit, targetUnit); } if (document!=NULL) { document->startUndoCycle(); } // insert layers: if (graphic!=NULL) { RS_Layer* layer = graphic->getActiveLayer(); for(uint i=0; i<source->countLayers(); ++i) { RS_Layer* l = source->layerAt(i); if (l!=NULL) { if (graphic->findLayer(l->getName())==NULL) { graphic->addLayer(l->clone()); } } } graphic->activateLayer(layer); } // insert blocks: if (graphic!=NULL) { for(uint i=0; i<source->countBlocks(); ++i) { RS_Block* b = source->blockAt(i); if (b!=NULL) { if (graphic->findBlock(b->getName())==NULL) { RS_Block* bc = (RS_Block*)b->clone(); bc->reparent(container);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -