void ModelMerger::replaceModel(const ModelNode &modelNode) { view()->model()->changeImports(modelNode.model()->imports(), QList<Import>()); view()->model()->setFileUrl(modelNode.model()->fileUrl()); try { RewriterTransaction transaction(view()->beginRewriterTransaction()); ModelNode rootNode(view()->rootModelNode()); foreach (const QString &propertyName, rootNode.propertyNames()) rootNode.removeProperty(propertyName); QHash<QString, QString> idRenamingHash; setupIdRenamingHash(modelNode, idRenamingHash, view()); syncVariantProperties(rootNode, modelNode); syncBindingProperties(rootNode, modelNode, idRenamingHash); syncId(rootNode, modelNode, idRenamingHash); syncNodeProperties(rootNode, modelNode, idRenamingHash, view()); syncNodeListProperties(rootNode, modelNode, idRenamingHash, view()); m_view->changeRootNodeType(modelNode.type(), modelNode.majorVersion(), modelNode.minorVersion()); } catch (RewritingException &e) { qWarning() << e.description(); //silent error } }
void NameItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (option.state & QStyle::State_Selected) drawSelectionBackground(painter, option); QString displayString; QPoint displayStringOffset; painter->save(); QFontMetrics fm(option.font); int width = 0; if (index.data(Qt::UserRole).isValid()) { int pixmapSide = 16; if (m_TreeModel->isNodeInvisible( index )) painter->setOpacity(0.5); ModelNode node = m_TreeModel->nodeForIndex(index); QIcon icon; if (node.isValid()) { // if node has no own icon, search for it in the itemlibrary const ItemLibraryInfo *libraryInfo = node.model()->metaInfo().itemLibraryInfo(); QList <ItemLibraryEntry> infoList = libraryInfo->entriesForType(node.type(), node.majorVersion(), node.minorVersion()); foreach (const ItemLibraryEntry &entry, infoList) { if (icon.isNull()) { icon = entry.icon(); break; } } } // if the library was also empty, use the default icon if (icon.isNull()) icon = QIcon(QLatin1String(":/ItemLibrary/images/item-default-icon.png")); if (!node.metaInfo().isValid()) icon = QIcon(QLatin1String(":/ItemLibrary/images/item-invalid-icon.png")); // If no icon is present, leave an empty space of 24 pixels anyway QPixmap pixmap = icon.pixmap(pixmapSide, pixmapSide); painter->drawPixmap(option.rect.x()+1,option.rect.y()+2,pixmap); displayString = node.id(); if (displayString.isEmpty()) displayString = node.simplifiedTypeName(); // Check text length does not exceed available space int extraSpace=12+pixmapSide; displayString = fm.elidedText(displayString,Qt::ElideMiddle,option.rect.width()-extraSpace); displayStringOffset = QPoint(5+pixmapSide,-5); width = fm.width(displayString); }
static QIcon getTypeIcon(const ModelNode &modelNode) { QIcon icon; if (modelNode.isValid()) { // if node has no own icon, search for it in the itemlibrary const ItemLibraryInfo *libraryInfo = modelNode.model()->metaInfo().itemLibraryInfo(); QList <ItemLibraryEntry> itemLibraryEntryList = libraryInfo->entriesForType(modelNode.type(), modelNode.majorVersion(), modelNode.minorVersion()); if (!itemLibraryEntryList.isEmpty()) return itemLibraryEntryList.first().typeIcon(); else if (modelNode.metaInfo().isValid()) return QIcon(QStringLiteral(":/ItemLibrary/images/item-default-icon.png")); else return QIcon(QStringLiteral(":/ItemLibrary/images/item-invalid-icon.png")); } return QIcon(QStringLiteral(":/ItemLibrary/images/item-invalid-icon.png")); }
void ModelMerger::replaceModel(const ModelNode &modelNode) { RewriterTransaction transaction(view()->beginRewriterTransaction()); foreach (const Import &import, modelNode.model()->imports()) view()->model()->addImport(import); view()->model()->setFileUrl(modelNode.model()->fileUrl()); ModelNode rootNode(view()->rootModelNode()); foreach (const QString &propertyName, rootNode.propertyNames()) rootNode.removeProperty(propertyName); QHash<QString, QString> idRenamingHash; setupIdRenamingHash(modelNode, idRenamingHash, view()); syncVariantProperties(rootNode, modelNode); syncBindingProperties(rootNode, modelNode, idRenamingHash); syncId(rootNode, modelNode, idRenamingHash); syncNodeProperties(rootNode, modelNode, idRenamingHash, view()); syncNodeListProperties(rootNode, modelNode, idRenamingHash, view()); m_view->changeRootNodeType(modelNode.type(), modelNode.majorVersion(), modelNode.minorVersion()); }
QString QmlTextGenerator::toQml(const ModelNode &node, int indentDepth) const { QString type = node.type(); QString url; if (type.contains('.')) { QStringList nameComponents = type.split('.'); url = nameComponents.first(); type = nameComponents.last(); } QString alias; if (!url.isEmpty()) { const QString &versionUrl = QString("%1.%2").arg(QString::number(node.majorVersion()), QString::number(node.minorVersion())); foreach (const Import &import, node.model()->imports()) { if (import.url() == url && import.version() == versionUrl) { alias = import.alias(); break; } } } QString result; if (!alias.isEmpty()) result = alias + '.'; result += type; result += QLatin1String(" {\n"); const int propertyIndentDepth = indentDepth + 4; const QString properties = propertiesToQml(node, propertyIndentDepth); return result + properties + QString(indentDepth, QLatin1Char(' ')) + QLatin1Char('}'); }
void AddTabDesignerAction::addNewTab() { QString tabName = AddTabToTabViewDialog::create(QStringLiteral("Tab"), Core::ICore::mainWindow()); if (!tabName.isEmpty()) { QString directoryPath = QFileInfo(selectionContext().view()->model()->fileUrl().toLocalFile()).absolutePath(); QString newFilePath = directoryPath +QStringLiteral("/") + tabName + QStringLiteral(".qml"); if (QFileInfo::exists(newFilePath)) { Core::AsynchronousMessageBox::warning(tr("Naming Error"), tr("Component already exists.")); } else { const QString sourceString = QStringLiteral("import QtQuick 2.1\nimport QtQuick.Controls 1.0\n\nItem {\n anchors.fill: parent\n}"); bool fileCreated = DocumentManager::createFile(newFilePath, sourceString); if (fileCreated) { DocumentManager::addFileToVersionControl(directoryPath, newFilePath); ModelNode tabViewModelNode = findTabViewModelNode(selectionContext().currentSingleSelectedNode()); PropertyListType propertyList; propertyList.append(QPair<PropertyName, QVariant>("source", QString(tabName + QStringLiteral(".qml")))); propertyList.append(QPair<PropertyName, QVariant>("title", tabName)); ModelNode newTabModelNode = selectionContext().view()->createModelNode("QtQuick.Controls.Tab", tabViewModelNode.majorVersion(), tabViewModelNode.minorVersion(), propertyList); newTabModelNode.setIdWithRefactoring(newTabModelNode.view()->generateNewId(tabName)); tabViewModelNode.defaultNodeAbstractProperty().reparentHere(newTabModelNode); } } } }
static ModelNode createNodeFromNode(const ModelNode &modelNode,const QHash<QString, QString> &idRenamingHash, AbstractView *view) { QList<QPair<QString, QVariant> > propertyList; QList<QPair<QString, QVariant> > variantPropertyList; foreach (const VariantProperty &variantProperty, modelNode.variantProperties()) { propertyList.append(QPair<QString, QVariant>(variantProperty.name(), variantProperty.value())); } ModelNode newNode(view->createModelNode(modelNode.type(),modelNode.majorVersion(),modelNode.minorVersion(), propertyList, variantPropertyList, modelNode.nodeSource(), modelNode.nodeSourceType())); syncBindingProperties(newNode, modelNode, idRenamingHash); syncId(newNode, modelNode, idRenamingHash); syncNodeProperties(newNode, modelNode, idRenamingHash, view); syncNodeListProperties(newNode, modelNode, idRenamingHash, view); return newNode; }
void TabViewDesignerAction::addNewTab() { QString tabName = AddTabToTabViewDialog::create(QLatin1String("Tab"), Core::ICore::mainWindow()); if (!tabName.isEmpty()) { QString directoryPath = QFileInfo(selectionContext().view()->model()->fileUrl().toLocalFile()).absolutePath(); QString newFilePath = directoryPath +QLatin1String("/") + tabName + QLatin1String(".qml"); if (QFileInfo(newFilePath).exists()) { QMessageBox::warning(Core::ICore::mainWindow(), tr("Naming Error"), tr("Component already exists.")); } else { bool fileCreated = createFile(newFilePath); if (fileCreated) { addNewFileToVersionControl(directoryPath, newFilePath); ModelNode tabViewModelNode = findTabViewModelNode(selectionContext().currentSingleSelectedNode()); PropertyListType propertyList; propertyList.append(QPair<PropertyName, QVariant>("source", QString(tabName + QLatin1String(".qml")))); propertyList.append(QPair<PropertyName, QVariant>("title", tabName)); ModelNode newTabModelNode = selectionContext().view()->createModelNode("QtQuick.Controls.Tab", tabViewModelNode.majorVersion(), tabViewModelNode.minorVersion(), propertyList); newTabModelNode.setId(newTabModelNode.view()->generateNewId(tabName)); tabViewModelNode.defaultNodeAbstractProperty().reparentHere(newTabModelNode); } } } }