RTransaction RChangePropertyOperation::apply(RDocument& document, bool preview) const { Q_UNUSED(preview) RTransaction transaction(document.getStorage(), text); // always allow property changes (e.g. move entity to hidden layer) transaction.setAllowInvisible(true); transaction.setGroup(transactionGroup); QVariant val = value; // optimization: change layer ID instead of changing layer name: if (propertyTypeId==REntity::PropertyLayer && value.type() == QVariant::String) { val = QVariant(document.getLayerId(value.toString())); } //RDebug::startTimer(); //qDebug() << "filter: " << entityTypeFilter; QSet<RObject::Id> selectedObjects = document.queryPropertyEditorObjects(); QSet<RObject::Id>::iterator it; for (it = selectedObjects.begin(); it != selectedObjects.end(); it++) { QSharedPointer<RObject> obj = document.queryObject(*it); if (obj.isNull()) { continue; } if (entityTypeFilter!=RS::EntityAll) { // special filter for block references and attributes: if (entityTypeFilter==RS::EntityBlockRefAttr) { if (obj->getType()!=RS::EntityBlockRef && obj->getType()!=RS::EntityAttribute) { continue; } } else { if (entityTypeFilter!=obj->getType()) { continue; } } } // apply operation to object: bool modified = obj->setProperty(propertyTypeId, val, &transaction); if (modified) { transaction.addObject(obj, false, false, QSet<RPropertyTypeId>() << propertyTypeId); } } transaction.end(); //RDebug::stopTimer("RChangePropertyOperation::apply"); return transaction; }
bool REntity::setProperty(RPropertyTypeId propertyTypeId, const QVariant& value, RTransaction* transaction) { bool ret = RObject::setProperty(propertyTypeId, value, transaction); if (propertyTypeId == PropertyBlock) { ret = ret || RObject::setMember(getData().blockId, value.toInt(), true); } else if (propertyTypeId == PropertyLayer) { if (value.type() == QVariant::Int || value.type() == QVariant::LongLong) { ret = ret || RObject::setMember(getData().layerId, value.toInt(), true); } else if (value.type() == QVariant::String) { RDocument* document = getData().getDocument(); if (document != NULL) { ret = ret || RObject::setMember(getData().layerId, document->getLayerId(value.toString()), true); } } } else if (propertyTypeId == PropertyLinetype) { if (value.type() == QVariant::Int || value.type() == QVariant::LongLong) { ret = ret || RObject::setMember(getData().linetypeId, value.toInt(), true); } else { RDocument* document = getData().getDocument(); if (document != NULL) { RLinetypePattern t = value.value<RLinetypePattern> (); int id = document->getLinetypeId(t.getName()); ret = ret || RObject::setMember(getData().linetypeId, id, true); } } } else if (propertyTypeId == PropertyLinetypeScale) { ret = ret || RObject::setMember(getData().linetypeScale, value.toDouble(), true); } else if (propertyTypeId == PropertyLineweight) { if (value.type()==QVariant::Int || value.type()==QVariant::Double) { ret = ret || RObject::setMember((int&)getData().lineweight, value.value<int>(), true); } else { ret = ret || RObject::setMember((int&)getData().lineweight, (int)value.value<RLineweight::Lineweight>(), true); } } else if (propertyTypeId == PropertyColor) { ret = ret || RObject::setMember(getData().color, value, true); } else if (propertyTypeId == PropertyDrawOrder) { ret = ret || RObject::setMember(getData().drawOrder, value, true); } return ret; }
RTransaction RChangePropertyOperation::apply(RDocument& document, bool preview) const { RTransaction transaction(document.getStorage(), "Changing property"); // 20111110: always allow property changes (e.g. move entity to hidden layer) transaction.setAllowInvisible(true); QVariant val = value; // optimization: change layer ID instead of changing layer name: if (propertyTypeId==REntity::PropertyLayer && value.type() == QVariant::String) { val = QVariant(document.getLayerId(value.toString())); } //RDebug::startTimer(); QSet<REntity::Id> selectedEntities = document.querySelectedEntities(); QSet<REntity::Id>::iterator it; for (it = selectedEntities.begin(); it != selectedEntities.end(); it++) { QSharedPointer<REntity> entity = document.queryEntity(*it); if (entity.isNull()) { continue; } if (entityTypeFilter!=RS::EntityAll && entityTypeFilter!=entity->getType()) { continue; } // apply operation to entity: bool modified = entity->setProperty(propertyTypeId, val); if (modified) { transaction.addObject(entity, false, QSet<RPropertyTypeId>() << propertyTypeId); } } transaction.end(); //RDebug::stopTimer("RChangePropertyOperation::apply"); return transaction; }
void RClipboardOperation::copy( RDocument& src, RDocument& dest, const RVector& offset, double scale, double rotation, bool flipHorizontal, bool flipVertical, bool toCurrentLayer, bool toCurrentBlock, bool overwriteLayers, bool overwriteBlocks, const QString& blockName, const QString& layerName, RTransaction& transaction, bool selectionOnly, bool clear, bool toModelSpaceBlock, bool preview, const RQMapQStringQString& attributes) const { bool overwriteLinetypes = false; double unitScale; if (src.getUnit()==RS::None) { unitScale = 1.0; } else { unitScale = RUnit::convert(1.0, src.getUnit(), dest.getUnit()); } if (clear) { dest.clear(); } QSet<REntity::Id> entityIdsSet; if (selectionOnly) { entityIdsSet = src.querySelectedEntities(); } else { entityIdsSet = src.queryAllEntities(); } QList<REntity::Id> entityIdsList = src.getStorage().orderBackToFront(entityIdsSet); // Non-const offset. reset to 0/0/0 if copying to block // (offset implemented as block reference offset). RVector off = offset; bool hasBlock = false; QSet<REntity::Id> attributeIds; // this part is used to insert ('paste') blocks from the part library // as new blocks: QSharedPointer<RBlockReferenceEntity> refp; if (!blockName.isNull()) { QSharedPointer<RBlock> block; hasBlock = dest.hasBlock(blockName); // block does not exist in dest - or - // block exists in dest and must be overwritten: if (!hasBlock || overwriteBlocks) { block = QSharedPointer<RBlock> (new RBlock(&dest, blockName, RVector(0, 0, 0))); transaction.overwriteBlock(block); } // block exists and must not be overwritten: else { block = dest.queryBlock(blockName); } Q_ASSERT(!block.isNull()); // create new block reference that references new, overwritten or existing block // (insert later, when block is complete, so we have bounding box for spatial index): RBlockReferenceEntity* ref = new RBlockReferenceEntity(&dest, RBlockReferenceData(block->getId(), RVector(0,0,0), RVector(1.0, 1.0, 1.0), 0.0)); refp = QSharedPointer<RBlockReferenceEntity>(ref); refp->setBlockId(dest.getCurrentBlockId()); off = RVector(0, 0, 0); if (flipHorizontal) { refp->flipHorizontal(); } if (flipVertical) { refp->flipVertical(); } //ref->scale(scale * unitScale); refp->scale(scale); refp->rotate(rotation); refp->move(offset); // create attribute for each attribute definition in block with // invalid parent ID (fixed later, when block reference ID is known): QSet<REntity::Id> ids = src.queryAllEntities(); QSet<REntity::Id>::iterator it; for (it=ids.begin(); it!=ids.end(); it++) { REntity::Id id = *it; QSharedPointer<RAttributeDefinitionEntity> attDef = src.queryEntity(id).dynamicCast<RAttributeDefinitionEntity>(); if (attDef.isNull()) { continue; } QSharedPointer<RAttributeEntity> att( new RAttributeEntity( &dest, RAttributeData(attDef->getData(), REntity::INVALID_ID, attDef->getTag()) ) ); att->scale(unitScale); refp->applyTransformationTo(*att); // assign values to attributes: QString tag = att->getTag(); if (attributes.contains(tag)) { att->setText(attributes[tag]); } // make sure the attribute has the correct layer ID of the // corresponding layer in dest: QSharedPointer<RLayer> destLayer = copyEntityLayer(*attDef, src, dest, overwriteLayers, transaction); att->setLayerId(destLayer->getId()); QSharedPointer<RLinetype> destLinetype = copyEntityLinetype(*attDef, src, dest, overwriteLinetypes, transaction); att->setLinetypeId(destLinetype->getId()); transaction.addObject(att, false); attributeIds.insert(att->getId()); } scale = 1.0; rotation = 0.0; flipHorizontal = false; flipVertical = false; toCurrentLayer = false; //toCurrentBlock = false; } // copy entities from src to dest: // if the block existed already in dest and is not overwritten, // there's nothing to do here: if (!hasBlock || overwriteBlocks || preview) { copiedLayers.clear(); copiedLinetypes.clear(); copiedBlocks.clear(); int counter = 0; QList<REntity::Id>::iterator it; for (it=entityIdsList.begin(); it!=entityIdsList.end(); ++it) { if (preview && ++counter>RSettings::getPreviewEntities()) { break; } QSharedPointer<REntity> entity = src.queryEntityDirect(*it); if (entity.isNull() || entity->isUndone()) { continue; } copyEntity( *entity.data(), src, dest, off, scale, unitScale, rotation, flipHorizontal, flipVertical, toCurrentLayer, toCurrentBlock, overwriteLayers, overwriteBlocks, blockName, transaction, toModelSpaceBlock // to model space: true for copy // (allow copy from inside any block definition), // false for paste ); } } // only overwrite layers: else if (overwriteLayers) { copiedLayers.clear(); int counter = 0; QList<REntity::Id>::iterator it; for (it=entityIdsList.begin(); it!=entityIdsList.end(); ++it) { if (preview && ++counter>RSettings::getPreviewEntities()) { break; } QSharedPointer<REntity> entity = src.queryEntityDirect(*it); if (entity.isNull() || entity->isUndone()) { continue; } copyEntityLayer( *entity.data(), src, dest, overwriteLayers, transaction ); } } // copying of entire block complete, insert block reference now since // we now have the bounding box for the spatial index: if (!refp.isNull()) { bool useCurrentAttributes = true; if (!layerName.isEmpty()) { useCurrentAttributes = false; refp->setLayerId(dest.getLayerId(layerName)); } transaction.addObject(refp, useCurrentAttributes); // fix parent ID of attributes created by the new inserted block: REntity::Id refId = refp->getId(); //QSet<REntity::Id> ids = dest.queryAllEntities(); QSet<REntity::Id>::iterator it; for (it=attributeIds.begin(); it!=attributeIds.end(); it++) { REntity::Id id = *it; QSharedPointer<RAttributeEntity> e = dest.queryEntityDirect(id).dynamicCast<RAttributeEntity>(); if (e.isNull()) { continue; } if (e->getParentId()==REntity::INVALID_ID) { e->setParentId(refId); } } } transaction.endCycle(); }