Ejemplo n.º 1
0
RTransaction RMoveReferencePointOperation::apply(RDocument& document, bool preview) const {
    Q_UNUSED(preview)

    RTransaction transaction(document.getStorage(), text);
    transaction.setGroup(transactionGroup);

    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;
        }
        
        // apply operation to cloned entity:
        bool modified = entity->moveReferencePoint(referencePoint, targetPoint);
        
        if (modified) {
            transaction.addObject(entity, false);
        }
    }
        
    transaction.end();

    return transaction;
}
Ejemplo n.º 2
0
RVector RSnapEntityBase::snap(
        const RVector& position,
        RGraphicsView& view,
        const QSet<REntity::Id>& candidates,
        const RBox& queryBox) {

    RDocument* document = view.getDocument();
    if (document==NULL) {
        return lastSnap;
    }

    REntity::Id entityId;
    double minDist = RMAXDOUBLE;
    double dist;

    QSet<REntity::Id>::const_iterator it;
    for (it=candidates.begin(); it!=candidates.end(); it++) {
        // 20111112: query direct:
        //QSharedPointer<REntity> e = document->queryEntityDirect(*it);
        QSharedPointer<REntity> e = document->queryEntity(*it);
        if (e.isNull()) {
            continue;
        }

        RVector candidate =
            position.getClosest2D(
                snapEntity(e, position, queryBox, view)
            );

        dist = candidate.getDistanceTo2D(position);
        if (dist<minDist) {
            lastSnap = candidate;
            minDist = dist;
            entityId = e->getId();
        }
    }

    if (!lastSnap.isValid()) {
        lastSnap = position;
        lastSnap.valid = false;
        return lastSnap;
    }
    else {
        entityIds.insert(entityId);
        return lastSnap;
    }
}
Ejemplo n.º 3
0
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;
}
Ejemplo n.º 4
0
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();
}