forked from qt-creator/qt-creator
QmlDesigner: Add helper function to StylesheetMerger
Change-Id: If3857af4a3541cb1b77a87d168f33571954dd5e4 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Vikas Pachdha <vikas.pachdha@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -1505,68 +1505,6 @@ QString getTemplateDialog(const Utils::FilePath &projectPath)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void styleMerge(const SelectionContext &selectionContext, const QString &templateFile)
|
|
||||||
{
|
|
||||||
Model *parentModel = selectionContext.view()->model();
|
|
||||||
|
|
||||||
QTC_ASSERT(parentModel, return);
|
|
||||||
|
|
||||||
QScopedPointer<Model> templateModel(Model::create("QtQuick.Item", 2, 1, parentModel));
|
|
||||||
Q_ASSERT(templateModel.data());
|
|
||||||
|
|
||||||
templateModel->setFileUrl(selectionContext.view()->model()->fileUrl());
|
|
||||||
|
|
||||||
QPlainTextEdit textEditTemplate;
|
|
||||||
Utils::FileReader reader;
|
|
||||||
|
|
||||||
QTC_ASSERT(reader.fetch(Utils::FilePath::fromString(templateFile)), return);
|
|
||||||
QString qmlTemplateString = QString::fromUtf8(reader.data());
|
|
||||||
QString imports;
|
|
||||||
|
|
||||||
for (const Import &import : parentModel->imports())
|
|
||||||
imports += QStringLiteral("import ") + import.toString(true) + QLatin1Char(';') + QLatin1Char('\n');
|
|
||||||
|
|
||||||
textEditTemplate.setPlainText(imports + qmlTemplateString);
|
|
||||||
NotIndentingTextEditModifier textModifierTemplate(&textEditTemplate);
|
|
||||||
|
|
||||||
QScopedPointer<RewriterView> templateRewriterView(new RewriterView(RewriterView::Amend, nullptr));
|
|
||||||
templateRewriterView->setTextModifier(&textModifierTemplate);
|
|
||||||
templateModel->attachView(templateRewriterView.data());
|
|
||||||
templateRewriterView->setCheckSemanticErrors(false);
|
|
||||||
|
|
||||||
ModelNode templateRootNode = templateRewriterView->rootModelNode();
|
|
||||||
QTC_ASSERT(templateRootNode.isValid(), return);
|
|
||||||
|
|
||||||
QScopedPointer<Model> styleModel(Model::create("QtQuick.Item", 2, 1, parentModel));
|
|
||||||
Q_ASSERT(styleModel.data());
|
|
||||||
|
|
||||||
styleModel->setFileUrl(selectionContext.view()->model()->fileUrl());
|
|
||||||
|
|
||||||
QPlainTextEdit textEditStyle;
|
|
||||||
RewriterView *parentRewriterView = selectionContext.view()->model()->rewriterView();
|
|
||||||
QTC_ASSERT(parentRewriterView, return);
|
|
||||||
textEditStyle.setPlainText(parentRewriterView->textModifierContent());
|
|
||||||
NotIndentingTextEditModifier textModifierStyle(&textEditStyle);
|
|
||||||
|
|
||||||
QScopedPointer<RewriterView> styleRewriterView(new RewriterView(RewriterView::Amend, nullptr));
|
|
||||||
styleRewriterView->setTextModifier(&textModifierStyle);
|
|
||||||
styleModel->attachView(styleRewriterView.data());
|
|
||||||
|
|
||||||
StylesheetMerger merger(templateRewriterView.data(), styleRewriterView.data());
|
|
||||||
|
|
||||||
try {
|
|
||||||
merger.merge();
|
|
||||||
} catch (Exception &e) {
|
|
||||||
e.showException();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
parentRewriterView->textModifier()->textDocument()->setPlainText(templateRewriterView->textModifierContent());
|
|
||||||
} catch (Exception &e) {
|
|
||||||
e.showException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void mergeWithTemplate(const SelectionContext &selectionContext)
|
void mergeWithTemplate(const SelectionContext &selectionContext)
|
||||||
{
|
{
|
||||||
const Utils::FilePath projectPath = Utils::FilePath::fromString(baseDirectory(selectionContext.view()->model()->fileUrl()));
|
const Utils::FilePath projectPath = Utils::FilePath::fromString(baseDirectory(selectionContext.view()->model()->fileUrl()));
|
||||||
@@ -1574,7 +1512,7 @@ void mergeWithTemplate(const SelectionContext &selectionContext)
|
|||||||
const QString templateFile = getTemplateDialog(projectPath);
|
const QString templateFile = getTemplateDialog(projectPath);
|
||||||
|
|
||||||
if (QFileInfo::exists(templateFile))
|
if (QFileInfo::exists(templateFile))
|
||||||
styleMerge(selectionContext, templateFile);
|
StylesheetMerger::styleMerge(selectionContext.view()->model(), templateFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeGroup(const SelectionContext &selectionContext)
|
void removeGroup(const SelectionContext &selectionContext)
|
||||||
|
@@ -48,6 +48,8 @@ class QMLDESIGNERCORE_EXPORT StylesheetMerger
|
|||||||
public:
|
public:
|
||||||
StylesheetMerger(AbstractView*, AbstractView*);
|
StylesheetMerger(AbstractView*, AbstractView*);
|
||||||
void merge();
|
void merge();
|
||||||
|
static void styleMerge(Model *model, const QString &templateFile);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void preprocessStyleSheet();
|
void preprocessStyleSheet();
|
||||||
bool idExistsInBothModels(const QString& id);
|
bool idExistsInBothModels(const QString& id);
|
||||||
|
@@ -34,8 +34,14 @@
|
|||||||
#include <nodelistproperty.h>
|
#include <nodelistproperty.h>
|
||||||
#include <nodemetainfo.h>
|
#include <nodemetainfo.h>
|
||||||
#include <nodeproperty.h>
|
#include <nodeproperty.h>
|
||||||
|
#include <plaintexteditmodifier.h>
|
||||||
|
#include <rewriterview.h>
|
||||||
#include <variantproperty.h>
|
#include <variantproperty.h>
|
||||||
|
|
||||||
|
#include <utils/fileutils.h>
|
||||||
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
|
#include <QPlainTextEdit>
|
||||||
#include <QQueue>
|
#include <QQueue>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
@@ -527,4 +533,69 @@ void StylesheetMerger::merge()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StylesheetMerger::styleMerge(Model *model, const QString &templateFile)
|
||||||
|
{
|
||||||
|
Model *parentModel = model;
|
||||||
|
|
||||||
|
QTC_ASSERT(parentModel, return );
|
||||||
|
|
||||||
|
QScopedPointer<Model> templateModel(Model::create("QtQuick.Item", 2, 1, parentModel));
|
||||||
|
Q_ASSERT(templateModel.data());
|
||||||
|
|
||||||
|
templateModel->setFileUrl(parentModel->fileUrl());
|
||||||
|
|
||||||
|
QPlainTextEdit textEditTemplate;
|
||||||
|
Utils::FileReader reader;
|
||||||
|
|
||||||
|
QTC_ASSERT(reader.fetch(Utils::FilePath::fromString(templateFile)), return );
|
||||||
|
QString qmlTemplateString = QString::fromUtf8(reader.data());
|
||||||
|
QString imports;
|
||||||
|
|
||||||
|
for (const Import &import : parentModel->imports())
|
||||||
|
imports += QStringLiteral("import ") + import.toString(true) + QLatin1Char(';')
|
||||||
|
+ QLatin1Char('\n');
|
||||||
|
|
||||||
|
textEditTemplate.setPlainText(imports + qmlTemplateString);
|
||||||
|
NotIndentingTextEditModifier textModifierTemplate(&textEditTemplate);
|
||||||
|
|
||||||
|
QScopedPointer<RewriterView> templateRewriterView(
|
||||||
|
new RewriterView(RewriterView::Amend, nullptr));
|
||||||
|
templateRewriterView->setTextModifier(&textModifierTemplate);
|
||||||
|
templateModel->attachView(templateRewriterView.data());
|
||||||
|
templateRewriterView->setCheckSemanticErrors(false);
|
||||||
|
|
||||||
|
ModelNode templateRootNode = templateRewriterView->rootModelNode();
|
||||||
|
QTC_ASSERT(templateRootNode.isValid(), return );
|
||||||
|
|
||||||
|
QScopedPointer<Model> styleModel(Model::create("QtQuick.Item", 2, 1, parentModel));
|
||||||
|
Q_ASSERT(styleModel.data());
|
||||||
|
|
||||||
|
styleModel->setFileUrl(parentModel->fileUrl());
|
||||||
|
|
||||||
|
QPlainTextEdit textEditStyle;
|
||||||
|
RewriterView *parentRewriterView = parentModel->rewriterView();
|
||||||
|
QTC_ASSERT(parentRewriterView, return );
|
||||||
|
textEditStyle.setPlainText(parentRewriterView->textModifierContent());
|
||||||
|
NotIndentingTextEditModifier textModifierStyle(&textEditStyle);
|
||||||
|
|
||||||
|
QScopedPointer<RewriterView> styleRewriterView(new RewriterView(RewriterView::Amend, nullptr));
|
||||||
|
styleRewriterView->setTextModifier(&textModifierStyle);
|
||||||
|
styleModel->attachView(styleRewriterView.data());
|
||||||
|
|
||||||
|
StylesheetMerger merger(templateRewriterView.data(), styleRewriterView.data());
|
||||||
|
|
||||||
|
try {
|
||||||
|
merger.merge();
|
||||||
|
} catch (Exception &e) {
|
||||||
|
e.showException();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
parentRewriterView->textModifier()->textDocument()->setPlainText(
|
||||||
|
templateRewriterView->textModifierContent());
|
||||||
|
} catch (Exception &e) {
|
||||||
|
e.showException();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user