int SensorManager::AddSensor(Sensor &sensor) { if (!hasId(sensor.getId())) { container[size] = sensor; size++; } }
QmlItemNode QmlModelView::createQmlItemNode(const ItemLibraryEntry &itemLibraryEntry, const QPointF &position, QmlItemNode parentNode) { if (!parentNode.isValid()) parentNode = rootQmlItemNode(); Q_ASSERT(parentNode.isValid()); QmlItemNode newNode; RewriterTransaction transaction = beginRewriterTransaction(); { QList<QPair<QString, QVariant> > propertyPairList; propertyPairList.append(qMakePair(QString("x"), QVariant(round(position.x(), 4)))); propertyPairList.append(qMakePair(QString("y"), QVariant(round(position.y(), 4)))); foreach (const PropertyContainer &property, itemLibraryEntry.properties()) propertyPairList.append(qMakePair(property.name(), property.value())); newNode = createQmlItemNode(itemLibraryEntry.typeName(), itemLibraryEntry.majorVersion(), itemLibraryEntry.minorVersion(), propertyPairList); parentNode.nodeAbstractProperty("data").reparentHere(newNode); Q_ASSERT(newNode.isValid()); QString id; int i = 1; QString name(itemLibraryEntry.name().toLower()); //remove forbidden characters name.replace(QRegExp(QLatin1String("[^a-zA-Z0-9_]")), QLatin1String("_")); do { id = name + QString::number(i); i++; } while (hasId(id)); //If the name already exists count upwards try { newNode.setId(id); } catch (InvalidIdException &e) { // should never happen QMessageBox::warning(0, tr("Invalid Id"), e.description()); } if (!currentState().isBaseState()) { newNode.modelNode().variantProperty("opacity") = 0; newNode.setVariantProperty("opacity", 1); } Q_ASSERT(newNode.isValid()); } Q_ASSERT(newNode.isValid()); return newNode; }
QmlItemNode QmlModelView::createQmlItemNodeFromImage(const QString &imageName, const QPointF &position, QmlItemNode parentNode) { if (!parentNode.isValid()) parentNode = rootQmlItemNode(); if (!parentNode.isValid()) return QmlItemNode(); QmlItemNode newNode; RewriterTransaction transaction = beginRewriterTransaction(); { QList<QPair<QString, QVariant> > propertyPairList; propertyPairList.append(qMakePair(QString("x"), QVariant( round(position.x(), 4)))); propertyPairList.append(qMakePair(QString("y"), QVariant( round(position.y(), 4)))); QString relativeImageName = imageName; //use relative path if (QFileInfo(model()->fileUrl().toLocalFile()).exists()) { QDir fileDir(QFileInfo(model()->fileUrl().toLocalFile()).absolutePath()); relativeImageName = fileDir.relativeFilePath(imageName); } propertyPairList.append(qMakePair(QString("source"), QVariant(relativeImageName))); newNode = createQmlItemNode("Qt/Image",4, 7, propertyPairList); parentNode.nodeAbstractProperty("data").reparentHere(newNode); Q_ASSERT(newNode.isValid()); QString id; int i = 1; QString name("image"); name.remove(QLatin1Char(' ')); do { id = name + QString::number(i); i++; } while (hasId(id)); //If the name already exists count upwards newNode.setId(id); if (!currentState().isBaseState()) { newNode.modelNode().variantProperty("opacity") = 0; newNode.setVariantProperty("opacity", 1); } Q_ASSERT(newNode.isValid()); } Q_ASSERT(newNode.isValid()); return newNode; }
QString AbstractView::generateNewId(const QString prefixName) const { int counter = 1; QString newId = QString("%1%2").arg(firstCharToLower(prefixName)).arg(counter); newId.remove(QRegExp(QLatin1String("[^a-zA-Z0-9_]"))); while (hasId(newId)) { counter += 1; newId = QString("%1%2").arg(firstCharToLower(prefixName)).arg(counter); } return newId; }
int SensorManager::RemoveSensor(int _id) { // Search for index in container, remove the sensor. if (hasId(_id)) for (int i = 0; i < size; i++) if (container[i].getId() == _id) { for (int j = i; j <= size-1; j++) container[j] = container[j+1]; //container[size-1] = NULL; size--; return 1; } return 0; }
int SensorManager::AddSensor(int _id, int _index, int _type, double lowThreshold, double highThreshold) { // Check for possible duplicate index, add sensor to container. if (!hasId(_id)) { Sensor sensor; sensor.setId(_id); sensor.setIndex(_index); sensor.setType(_type); sensor.setLowThresholdValue(lowThreshold); sensor.setHighThresholdValue(highThreshold); container[size] = sensor; size++; } else return 0; return 1; }
bool GlobalTrackCache::evict(Track* plainPtr) { DEBUG_ASSERT(plainPtr); // Make the cached track object invisible to avoid reusing // it before starting to save it. This is achieved by // removing it from both cache indices. bool evicted = false; const auto trackRef = createTrackRef(*plainPtr); if (debugLogEnabled()) { kLogger.debug() << "Evicting track" << trackRef << plainPtr; } if (trackRef.hasId()) { const auto trackById = m_tracksById.find(trackRef.getId()); if (trackById != m_tracksById.end()) { DEBUG_ASSERT(trackById->second->getPlainPtr() == plainPtr); m_tracksById.erase(trackById); evicted = true; } } if (trackRef.hasCanonicalLocation()) { const auto trackByCanonicalLocation( m_tracksByCanonicalLocation.find(trackRef.getCanonicalLocation())); if (m_tracksByCanonicalLocation.end() != trackByCanonicalLocation) { DEBUG_ASSERT(trackByCanonicalLocation->second->getPlainPtr() == plainPtr); m_tracksByCanonicalLocation.erase( trackByCanonicalLocation); evicted = true; } } DEBUG_ASSERT(isEvicted(plainPtr)); // Don't erase the pointer from m_cachedTracks here, because // this function is invoked from 2 different contexts. The // caller is responsible for doing this. Until then the cache // is inconsistent and verifyConsitency() is expected to fail. return evicted; }
QmlItemNode QmlModelView::createQmlItemNode(const ItemLibraryEntry &itemLibraryEntry, const QPointF &position, QmlItemNode parentNode) { if (!parentNode.isValid()) parentNode = rootQmlItemNode(); Q_ASSERT(parentNode.isValid()); QmlItemNode newNode; try { RewriterTransaction transaction = beginRewriterTransaction(); if (itemLibraryEntry.typeName().contains('.')) { const QString newImportUrl = itemLibraryEntry.requiredImport(); if (!itemLibraryEntry.requiredImport().isEmpty()) { const QString newImportVersion = QString("%1.%2").arg(QString::number(itemLibraryEntry.majorVersion()), QString::number(itemLibraryEntry.minorVersion())); Import newImport = Import::createLibraryImport(newImportUrl, newImportVersion); foreach (const Import &import, model()->imports()) { if (import.isLibraryImport() && import.url() == newImport.url() && import.version() == newImport.version()) { // reuse this import newImport = import; break; } } if (!model()->hasImport(newImport, true, true)) { model()->changeImports(QList<Import>() << newImport, QList<Import>()); } } } QList<QPair<QString, QVariant> > propertyPairList; propertyPairList.append(qMakePair(QString("x"), QVariant(round(position.x(), 4)))); propertyPairList.append(qMakePair(QString("y"), QVariant(round(position.y(), 4)))); if (itemLibraryEntry.qml().isEmpty()) { foreach (const PropertyContainer &property, itemLibraryEntry.properties()) propertyPairList.append(qMakePair(property.name(), property.value())); newNode = createQmlItemNode(itemLibraryEntry.typeName(), itemLibraryEntry.majorVersion(), itemLibraryEntry.minorVersion(), propertyPairList); } else { QScopedPointer<Model> inputModel(Model::create("QtQuick.Rectangle", 1, 0, model())); inputModel->setFileUrl(model()->fileUrl()); QPlainTextEdit textEdit; textEdit.setPlainText(Utils::FileReader::fetchQrc(itemLibraryEntry.qml())); NotIndentingTextEditModifier modifier(&textEdit); QScopedPointer<RewriterView> rewriterView(new RewriterView(RewriterView::Amend, 0)); rewriterView->setCheckSemanticErrors(false); rewriterView->setTextModifier(&modifier); inputModel->attachView(rewriterView.data()); if (rewriterView->errors().isEmpty() && rewriterView->rootModelNode().isValid()) { ModelNode rootModelNode = rewriterView->rootModelNode(); inputModel->detachView(rewriterView.data()); rootModelNode.variantProperty("x") = propertyPairList.first().second; rootModelNode.variantProperty("y") = propertyPairList.at(1).second; ModelMerger merger(this); newNode = merger.insertModel(rootModelNode); } } if (parentNode.hasDefaultProperty()) { parentNode.nodeAbstractProperty(parentNode.defaultProperty()).reparentHere(newNode); } if (!newNode.isValid()) return newNode; QString id; int i = 1; QString name(itemLibraryEntry.name().toLower()); //remove forbidden characters name.replace(QRegExp(QLatin1String("[^a-zA-Z0-9_]")), QLatin1String("_")); do { id = name + QString::number(i); i++; } while (hasId(id)); //If the name already exists count upwards newNode.setId(id); if (!currentState().isBaseState()) { newNode.modelNode().variantProperty("opacity") = 0; newNode.setVariantProperty("opacity", 1); } Q_ASSERT(newNode.isValid()); }
QmlItemNode QmlModelView::createQmlItemNodeFromImage(const QString &imageName, const QPointF &position, QmlItemNode parentNode) { if (!parentNode.isValid()) parentNode = rootQmlItemNode(); if (!parentNode.isValid()) return QmlItemNode(); QmlItemNode newNode; RewriterTransaction transaction = beginRewriterTransaction(); { const QString newImportUrl = QLatin1String("QtQuick"); const QString newImportVersion = QLatin1String("1.0"); Import newImport = Import::createLibraryImport(newImportUrl, newImportVersion); foreach (const Import &import, model()->imports()) { if (import.isLibraryImport() && import.url() == newImport.url() && import.version() == newImport.version()) { // reuse this import newImport = import; break; } } if (!model()->imports().contains(newImport)) { model()->changeImports(QList<Import>() << newImport, QList<Import>()); } QList<QPair<QString, QVariant> > propertyPairList; propertyPairList.append(qMakePair(QString("x"), QVariant( round(position.x(), 4)))); propertyPairList.append(qMakePair(QString("y"), QVariant( round(position.y(), 4)))); QString relativeImageName = imageName; //use relative path if (QFileInfo(model()->fileUrl().toLocalFile()).exists()) { QDir fileDir(QFileInfo(model()->fileUrl().toLocalFile()).absolutePath()); relativeImageName = fileDir.relativeFilePath(imageName); } propertyPairList.append(qMakePair(QString("source"), QVariant(relativeImageName))); newNode = createQmlItemNode("QtQuick.Image", 1, 0, propertyPairList); parentNode.nodeAbstractProperty("data").reparentHere(newNode); Q_ASSERT(newNode.isValid()); QString id; int i = 1; QString name("image"); name.remove(QLatin1Char(' ')); do { id = name + QString::number(i); i++; } while (hasId(id)); //If the name already exists count upwards newNode.setId(id); if (!currentState().isBaseState()) { newNode.modelNode().variantProperty("opacity") = 0; newNode.setVariantProperty("opacity", 1); } Q_ASSERT(newNode.isValid()); } Q_ASSERT(newNode.isValid()); return newNode; }
void PropertyEditorView::changeValue(const QString &name) { PropertyName propertyName = name.toUtf8(); if (propertyName.isNull()) return; if (m_locked) return; if (propertyName == "type") return; if (!m_selectedNode.isValid()) return; if (propertyName == "id") { PropertyEditorValue *value = m_qmlBackEndForCurrentType->propertyValueForName(propertyName); const QString newId = value->value().toString(); if (newId == m_selectedNode.id()) return; if (m_selectedNode.isValidId(newId) && !hasId(newId)) { m_selectedNode.setIdWithRefactoring(newId); } else { m_locked = true; value->setValue(m_selectedNode.id()); m_locked = false; if (!m_selectedNode.isValidId(newId)) Core::AsynchronousMessageBox::warning(tr("Invalid Id"), tr("%1 is an invalid id.").arg(newId)); else Core::AsynchronousMessageBox::warning(tr("Invalid Id"), tr("%1 already exists.").arg(newId)); } return; } PropertyName underscoreName(propertyName); underscoreName.replace('.', '_'); PropertyEditorValue *value = m_qmlBackEndForCurrentType->propertyValueForName(underscoreName); if (value ==0) return; QmlObjectNode qmlObjectNode(m_selectedNode); QVariant castedValue; if (qmlObjectNode.modelNode().metaInfo().isValid() && qmlObjectNode.modelNode().metaInfo().hasProperty(propertyName)) { castedValue = qmlObjectNode.modelNode().metaInfo().propertyCastedValue(propertyName, value->value()); } else if (propertyIsAttachedLayoutProperty(propertyName)) { castedValue = value->value(); } else { qWarning() << "PropertyEditor:" <<propertyName << "cannot be casted (metainfo)"; return ; } if (value->value().isValid() && !castedValue.isValid()) { qWarning() << "PropertyEditor:" << propertyName << "not properly casted (metainfo)"; return ; } if (qmlObjectNode.modelNode().metaInfo().isValid() && qmlObjectNode.modelNode().metaInfo().hasProperty(propertyName)) if (qmlObjectNode.modelNode().metaInfo().propertyTypeName(propertyName) == "QUrl" || qmlObjectNode.modelNode().metaInfo().propertyTypeName(propertyName) == "url") { //turn absolute local file paths into relative paths QString filePath = castedValue.toUrl().toString(); if (QFileInfo(filePath).exists() && QFileInfo(filePath).isAbsolute()) { QDir fileDir(QFileInfo(model()->fileUrl().toLocalFile()).absolutePath()); castedValue = QUrl(fileDir.relativeFilePath(filePath)); } } if (castedValue.type() == QVariant::Color) { QColor color = castedValue.value<QColor>(); QColor newColor = QColor(color.name()); newColor.setAlpha(color.alpha()); castedValue = QVariant(newColor); } try { if (!value->value().isValid()) { //reset qmlObjectNode.removeProperty(propertyName); } else { if (castedValue.isValid() && !castedValue.isNull()) { m_locked = true; qmlObjectNode.setVariantProperty(propertyName, castedValue); m_locked = false; } } } catch (const RewritingException &e) { e.showException(); } }