2010-07-12 09:44:42 +02:00
|
|
|
#include "qmloutlinemodel.h"
|
2010-07-19 11:39:41 +02:00
|
|
|
#include "qmljseditor.h"
|
2010-07-19 14:28:53 +02:00
|
|
|
#include "qmljsrefactoringchanges.h"
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
#include <qmljs/parser/qmljsastvisitor_p.h>
|
2010-07-13 17:05:47 +02:00
|
|
|
#include <qmljs/qmljsinterpreter.h>
|
|
|
|
|
#include <qmljs/qmljslookupcontext.h>
|
2010-07-19 14:28:53 +02:00
|
|
|
#include <qmljs/qmljsmodelmanagerinterface.h>
|
|
|
|
|
#include <qmljs/qmljsrewriter.h>
|
2010-07-12 09:44:42 +02:00
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2010-07-12 09:44:42 +02:00
|
|
|
#include <QtCore/QDebug>
|
2010-07-13 17:05:47 +02:00
|
|
|
#include <QtCore/QTime>
|
2010-07-12 09:44:42 +02:00
|
|
|
#include <typeinfo>
|
|
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJSEditor::Internal;
|
|
|
|
|
|
|
|
|
|
enum {
|
2010-07-14 13:36:18 +02:00
|
|
|
debug = false
|
2010-07-12 09:44:42 +02:00
|
|
|
};
|
|
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
namespace QmlJSEditor {
|
|
|
|
|
namespace Internal {
|
2010-07-12 09:44:42 +02:00
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
QmlOutlineItem::QmlOutlineItem(QmlOutlineModel *model) :
|
2010-07-19 14:28:53 +02:00
|
|
|
m_outlineModel(model),
|
|
|
|
|
m_node(0),
|
|
|
|
|
m_idNode(0)
|
2010-07-19 11:39:41 +02:00
|
|
|
{
|
2010-07-19 14:28:53 +02:00
|
|
|
Qt::ItemFlags defaultFlags = flags();
|
|
|
|
|
setFlags(Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags);
|
|
|
|
|
setEditable(false);
|
2010-07-19 11:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant QmlOutlineItem::data(int role) const
|
|
|
|
|
{
|
|
|
|
|
if (role == Qt::ToolTipRole) {
|
2010-07-19 14:28:53 +02:00
|
|
|
AST::SourceLocation location = sourceLocation();
|
|
|
|
|
AST::UiQualifiedId *uiQualifiedId = m_idNode;
|
2010-07-19 11:39:41 +02:00
|
|
|
if (!uiQualifiedId)
|
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
|
|
QList<AST::Node *> astPath = m_outlineModel->m_semanticInfo.astPath(location.begin());
|
|
|
|
|
|
|
|
|
|
QmlJS::Document::Ptr document = m_outlineModel->m_semanticInfo.document;
|
|
|
|
|
QmlJS::Snapshot snapshot = m_outlineModel->m_semanticInfo.snapshot;
|
|
|
|
|
LookupContext::Ptr lookupContext = LookupContext::create(document, snapshot, astPath);
|
|
|
|
|
const Interpreter::Value *value = lookupContext->evaluate(uiQualifiedId);
|
|
|
|
|
|
|
|
|
|
return prettyPrint(value, lookupContext->context());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QStandardItem::data(role);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 14:28:53 +02:00
|
|
|
int QmlOutlineItem::type() const
|
|
|
|
|
{
|
|
|
|
|
return UserType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlJS::AST::SourceLocation QmlOutlineItem::sourceLocation() const
|
|
|
|
|
{
|
|
|
|
|
return data(QmlOutlineModel::SourceLocationRole).value<QmlJS::AST::SourceLocation>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlOutlineItem::setSourceLocation(const QmlJS::AST::SourceLocation &location)
|
|
|
|
|
{
|
|
|
|
|
setData(QVariant::fromValue(location), QmlOutlineModel::SourceLocationRole);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlJS::AST::Node *QmlOutlineItem::node() const
|
|
|
|
|
{
|
|
|
|
|
return m_node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlOutlineItem::setNode(QmlJS::AST::Node *node)
|
|
|
|
|
{
|
|
|
|
|
m_node = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlJS::AST::UiQualifiedId *QmlOutlineItem::idNode() const
|
|
|
|
|
{
|
|
|
|
|
return m_idNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlOutlineItem::setIdNode(QmlJS::AST::UiQualifiedId *idNode)
|
|
|
|
|
{
|
|
|
|
|
m_idNode = idNode;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-16 11:03:42 +02:00
|
|
|
QmlOutlineItem &QmlOutlineItem::copyValues(const QmlOutlineItem &other)
|
|
|
|
|
{
|
|
|
|
|
*this = other;
|
2010-07-19 14:28:53 +02:00
|
|
|
m_node = other.m_node;
|
|
|
|
|
m_idNode = other.m_idNode;
|
2010-07-16 11:03:42 +02:00
|
|
|
emitDataChanged();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
QString QmlOutlineItem::prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Context *context) const
|
|
|
|
|
{
|
|
|
|
|
if (! value)
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
|
|
|
|
|
const QString className = objectValue->className();
|
|
|
|
|
if (!className.isEmpty()) {
|
|
|
|
|
return className;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString typeId = context->engine()->typeId(value);
|
|
|
|
|
if (typeId == QLatin1String("undefined")) {
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return typeId;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 14:28:53 +02:00
|
|
|
/**
|
|
|
|
|
Returns mapping of every UiObjectMember object to it's direct UiObjectMember parent object.
|
|
|
|
|
*/
|
|
|
|
|
class ObjectMemberParentVisitor : public AST::Visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QHash<AST::UiObjectMember*,AST::UiObjectMember*> operator()(Document::Ptr doc) {
|
|
|
|
|
parent.clear();
|
|
|
|
|
if (doc && doc->ast())
|
|
|
|
|
doc->ast()->accept(this);
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QHash<AST::UiObjectMember*,AST::UiObjectMember*> parent;
|
|
|
|
|
QList<AST::UiObjectMember *> stack;
|
|
|
|
|
|
|
|
|
|
bool preVisit(AST::Node *node)
|
|
|
|
|
{
|
|
|
|
|
if (AST::UiObjectMember *objMember = node->uiObjectMemberCast())
|
|
|
|
|
stack.append(objMember);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void postVisit(AST::Node *node)
|
|
|
|
|
{
|
|
|
|
|
if (AST::UiObjectMember *objMember = node->uiObjectMemberCast()) {
|
|
|
|
|
stack.removeLast();
|
|
|
|
|
if (!stack.isEmpty()) {
|
|
|
|
|
parent.insert(objMember, stack.last());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
class QmlOutlineModelSync : protected AST::Visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QmlOutlineModelSync(QmlOutlineModel *model) :
|
|
|
|
|
m_model(model),
|
|
|
|
|
indent(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
void operator()(Document::Ptr doc)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
|
|
|
|
m_nodeToIndex.clear();
|
|
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel ------";
|
|
|
|
|
if (doc && doc->ast())
|
|
|
|
|
doc->ast()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool preVisit(AST::Node *node)
|
|
|
|
|
{
|
|
|
|
|
if (!node)
|
|
|
|
|
return false;
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel -" << QByteArray(indent++, '-').constData() << node << typeid(*node).name();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void postVisit(AST::Node *)
|
|
|
|
|
{
|
|
|
|
|
indent--;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
typedef QPair<QString,QString> ElementType;
|
2010-07-12 09:44:42 +02:00
|
|
|
bool visit(AST::UiObjectDefinition *objDef)
|
|
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
QModelIndex index = m_model->enterObjectDefinition(objDef);
|
2010-07-12 09:44:42 +02:00
|
|
|
m_nodeToIndex.insert(objDef, index);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-16 13:34:49 +02:00
|
|
|
void endVisit(AST::UiObjectDefinition * /*objDef*/)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
m_model->leaveObjectDefiniton();
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(AST::UiScriptBinding *scriptBinding)
|
|
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
QModelIndex index = m_model->enterScriptBinding(scriptBinding);
|
2010-07-12 09:44:42 +02:00
|
|
|
m_nodeToIndex.insert(scriptBinding, index);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void endVisit(AST::UiScriptBinding * /*scriptBinding*/)
|
|
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
m_model->leaveScriptBinding();
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-14 13:32:42 +02:00
|
|
|
bool visit(AST::UiPublicMember *publicMember)
|
|
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
QModelIndex index = m_model->enterPublicMember(publicMember);
|
2010-07-14 13:32:42 +02:00
|
|
|
m_nodeToIndex.insert(publicMember, index);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void endVisit(AST::UiPublicMember * /*publicMember*/)
|
|
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
m_model->leavePublicMember();
|
2010-07-14 13:32:42 +02:00
|
|
|
}
|
2010-07-12 15:51:26 +02:00
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
QmlOutlineModel *m_model;
|
2010-07-13 17:05:47 +02:00
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
QHash<AST::Node*, QModelIndex> m_nodeToIndex;
|
|
|
|
|
int indent;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QmlOutlineModel::QmlOutlineModel(QObject *parent) :
|
|
|
|
|
QStandardItemModel(parent)
|
|
|
|
|
{
|
2010-07-13 17:05:47 +02:00
|
|
|
m_icons = Icons::instance();
|
|
|
|
|
const QString resourcePath = Core::ICore::instance()->resourcePath();
|
|
|
|
|
QmlJS::Icons::instance()->setIconFilesPath(resourcePath + "/qmlicons");
|
2010-07-19 14:28:53 +02:00
|
|
|
|
|
|
|
|
// TODO: Maybe add a Copy Action?
|
|
|
|
|
setSupportedDragActions(Qt::MoveAction);
|
|
|
|
|
setItemPrototype(new QmlOutlineItem(this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList QmlOutlineModel::mimeTypes() const
|
|
|
|
|
{
|
|
|
|
|
QStringList types;
|
|
|
|
|
types << QLatin1String("application/x-qtcreator-qmloutlinemodel");
|
|
|
|
|
return types;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QMimeData *QmlOutlineModel::mimeData(const QModelIndexList &indexes) const
|
|
|
|
|
{
|
|
|
|
|
if (indexes.count() <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
QStringList types = mimeTypes();
|
|
|
|
|
QMimeData *data = new QMimeData();
|
|
|
|
|
QString format = types.at(0);
|
|
|
|
|
QByteArray encoded;
|
|
|
|
|
QDataStream stream(&encoded, QIODevice::WriteOnly);
|
|
|
|
|
stream << indexes.size();
|
|
|
|
|
|
|
|
|
|
// We store pointers to the QmlOutlineItems dropped.
|
|
|
|
|
// This works because we're only supporting drag&drop inside the model
|
|
|
|
|
for (int i = 0; i < indexes.size(); ++i) {
|
|
|
|
|
QmlOutlineItem *item = static_cast<QmlOutlineItem*>(itemFromIndex(indexes.at(i)));
|
|
|
|
|
stream << (quint64)item;
|
|
|
|
|
}
|
|
|
|
|
data->setData(format, encoded);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QmlOutlineModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int /*column*/, const QModelIndex &parent)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << __FUNCTION__ << row << parent;
|
|
|
|
|
|
|
|
|
|
// check if the action is supported
|
|
|
|
|
if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// We cannot reparent outside of the root item
|
|
|
|
|
if (!parent.isValid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// check if the format is supported
|
|
|
|
|
QStringList types = mimeTypes();
|
|
|
|
|
if (types.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
QString format = types.at(0);
|
|
|
|
|
if (!data->hasFormat(format))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// decode and insert
|
|
|
|
|
QByteArray encoded = data->data(format);
|
|
|
|
|
QDataStream stream(&encoded, QIODevice::ReadOnly);
|
|
|
|
|
int indexSize;
|
|
|
|
|
stream >> indexSize;
|
|
|
|
|
QList<QmlOutlineItem*> itemsToMove;
|
|
|
|
|
for (int i = 0; i < indexSize; ++i) {
|
|
|
|
|
quint64 itemPtr;
|
|
|
|
|
stream >> itemPtr;
|
|
|
|
|
itemsToMove << reinterpret_cast<QmlOutlineItem*>(itemPtr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlOutlineItem *targetItem = static_cast<QmlOutlineItem*>(itemFromIndex(parent));
|
|
|
|
|
reparentNodes(targetItem, row, itemsToMove);
|
|
|
|
|
// Prevent view from calling insertRow(), removeRow() on it's own
|
|
|
|
|
return false;
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-13 11:18:42 +02:00
|
|
|
QmlJS::Document::Ptr QmlOutlineModel::document() const
|
|
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
return m_semanticInfo.document;
|
2010-07-13 11:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
void QmlOutlineModel::update(const SemanticInfo &semanticInfo)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
m_semanticInfo = semanticInfo;
|
2010-07-13 11:18:42 +02:00
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
m_treePos.clear();
|
|
|
|
|
m_treePos.append(0);
|
|
|
|
|
m_currentItem = invisibleRootItem();
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
// Set up lookup context once to do the element type lookup
|
|
|
|
|
//
|
|
|
|
|
// We're simplifying here by using the root context everywhere
|
|
|
|
|
// (empty node list). However, creating the LookupContext is quite expensive (about 3ms),
|
|
|
|
|
// and there is AFAIK no way to introduce new type names in a sub-context.
|
|
|
|
|
m_context = LookupContext::create(semanticInfo.document, semanticInfo.snapshot, QList<AST::Node*>());
|
|
|
|
|
m_typeToIcon.clear();
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
QmlOutlineModelSync syncModel(this);
|
2010-07-19 11:39:41 +02:00
|
|
|
syncModel(m_semanticInfo.document);
|
|
|
|
|
|
|
|
|
|
m_context.clear();
|
2010-07-12 14:45:22 +02:00
|
|
|
|
|
|
|
|
emit updated();
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
QModelIndex QmlOutlineModel::enterObjectDefinition(AST::UiObjectDefinition *objDef)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
QmlOutlineItem prototype(this);
|
2010-07-16 11:03:42 +02:00
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
const QString typeName = asString(objDef->qualifiedTypeNameId);
|
2010-07-22 10:12:04 +02:00
|
|
|
|
|
|
|
|
if (typeName.at(0).isUpper()) {
|
|
|
|
|
const QString id = getId(objDef);
|
|
|
|
|
if (!id.isEmpty()) {
|
|
|
|
|
prototype.setText(id);
|
|
|
|
|
} else {
|
|
|
|
|
prototype.setText(typeName);
|
|
|
|
|
}
|
|
|
|
|
if (!m_typeToIcon.contains(typeName)) {
|
|
|
|
|
m_typeToIcon.insert(typeName, getIcon(objDef));
|
|
|
|
|
}
|
|
|
|
|
prototype.setIcon(m_typeToIcon.value(typeName));
|
|
|
|
|
prototype.setData(ElementType, ItemTypeRole);
|
|
|
|
|
prototype.setIdNode(objDef->qualifiedTypeNameId);
|
2010-07-12 15:51:26 +02:00
|
|
|
} else {
|
2010-07-22 10:12:04 +02:00
|
|
|
// it's a grouped propery like 'anchors'
|
2010-07-19 11:39:41 +02:00
|
|
|
prototype.setText(typeName);
|
2010-07-22 10:12:04 +02:00
|
|
|
prototype.setIcon(m_icons->scriptBindingIcon());
|
|
|
|
|
prototype.setData(PropertyType, ItemTypeRole);
|
2010-07-12 15:51:26 +02:00
|
|
|
}
|
2010-07-19 14:28:53 +02:00
|
|
|
prototype.setSourceLocation(getLocation(objDef));
|
|
|
|
|
prototype.setNode(objDef);
|
2010-07-16 11:03:42 +02:00
|
|
|
|
|
|
|
|
return enterNode(prototype);
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
void QmlOutlineModel::leaveObjectDefiniton()
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
|
|
|
|
leaveNode();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
QModelIndex QmlOutlineModel::enterScriptBinding(AST::UiScriptBinding *scriptBinding)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
2010-07-19 11:39:41 +02:00
|
|
|
QmlOutlineItem prototype(this);
|
2010-07-16 11:03:42 +02:00
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
prototype.setText(asString(scriptBinding->qualifiedId));
|
|
|
|
|
prototype.setIcon(m_icons->scriptBindingIcon());
|
2010-07-16 11:03:42 +02:00
|
|
|
prototype.setData(PropertyType, ItemTypeRole);
|
2010-07-19 14:28:53 +02:00
|
|
|
prototype.setSourceLocation(getLocation(scriptBinding));
|
|
|
|
|
prototype.setNode(scriptBinding);
|
|
|
|
|
prototype.setIdNode(scriptBinding->qualifiedId);
|
2010-07-16 11:03:42 +02:00
|
|
|
|
|
|
|
|
return enterNode(prototype);
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
void QmlOutlineModel::leaveScriptBinding()
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
|
|
|
|
leaveNode();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
QModelIndex QmlOutlineModel::enterPublicMember(AST::UiPublicMember *publicMember)
|
|
|
|
|
{
|
|
|
|
|
QmlOutlineItem prototype(this);
|
|
|
|
|
|
2010-08-02 15:57:42 +02:00
|
|
|
if (publicMember->name)
|
|
|
|
|
prototype.setText(publicMember->name->asString());
|
2010-07-19 11:39:41 +02:00
|
|
|
prototype.setIcon(m_icons->publicMemberIcon());
|
|
|
|
|
prototype.setData(PropertyType, ItemTypeRole);
|
2010-07-19 14:28:53 +02:00
|
|
|
prototype.setSourceLocation(getLocation(publicMember));
|
|
|
|
|
prototype.setNode(publicMember);
|
2010-07-19 11:39:41 +02:00
|
|
|
|
|
|
|
|
return enterNode(prototype);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlOutlineModel::leavePublicMember()
|
|
|
|
|
{
|
|
|
|
|
leaveNode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlJS::AST::Node *QmlOutlineModel::nodeForIndex(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
if (index.isValid()) {
|
2010-07-19 14:28:53 +02:00
|
|
|
QmlOutlineItem *item = static_cast<QmlOutlineItem*>(itemFromIndex(index));
|
|
|
|
|
return item->node();
|
2010-07-19 11:39:41 +02:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-16 11:03:42 +02:00
|
|
|
QModelIndex QmlOutlineModel::enterNode(const QmlOutlineItem &prototype)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
|
|
|
|
int siblingIndex = m_treePos.last();
|
|
|
|
|
if (siblingIndex == 0) {
|
|
|
|
|
// first child
|
|
|
|
|
if (!m_currentItem->hasChildren()) {
|
|
|
|
|
if (debug)
|
2010-07-16 11:03:42 +02:00
|
|
|
qDebug() << "QmlOutlineModel - Adding" << "element to" << m_currentItem->text();
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
QmlOutlineItem *newItem = new QmlOutlineItem(this);
|
2010-07-16 11:03:42 +02:00
|
|
|
newItem->copyValues(prototype);
|
|
|
|
|
newItem->setEditable(false);
|
|
|
|
|
m_currentItem->appendRow(newItem);
|
|
|
|
|
|
|
|
|
|
m_currentItem = newItem;
|
2010-07-12 09:44:42 +02:00
|
|
|
} else {
|
|
|
|
|
m_currentItem = m_currentItem->child(0);
|
2010-07-16 11:03:42 +02:00
|
|
|
|
|
|
|
|
QmlOutlineItem *existingItem = static_cast<QmlOutlineItem*>(m_currentItem);
|
|
|
|
|
existingItem->copyValues(prototype);
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// sibling
|
|
|
|
|
if (m_currentItem->rowCount() <= siblingIndex) {
|
|
|
|
|
if (debug)
|
2010-07-16 11:03:42 +02:00
|
|
|
qDebug() << "QmlOutlineModel - Adding" << "element to" << m_currentItem->text();
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
QmlOutlineItem *newItem = new QmlOutlineItem(this);
|
2010-07-16 11:03:42 +02:00
|
|
|
newItem->copyValues(prototype);
|
|
|
|
|
newItem->setEditable(false);
|
|
|
|
|
m_currentItem->appendRow(newItem);
|
|
|
|
|
m_currentItem = newItem;
|
2010-07-12 09:44:42 +02:00
|
|
|
} else {
|
|
|
|
|
m_currentItem = m_currentItem->child(siblingIndex);
|
2010-07-16 11:03:42 +02:00
|
|
|
|
|
|
|
|
QmlOutlineItem *existingItem = static_cast<QmlOutlineItem*>(m_currentItem);
|
|
|
|
|
existingItem->copyValues(prototype);
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_treePos.append(0);
|
|
|
|
|
|
2010-07-16 11:03:42 +02:00
|
|
|
return m_currentItem->index();
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlOutlineModel::leaveNode()
|
|
|
|
|
{
|
|
|
|
|
int lastIndex = m_treePos.takeLast();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (lastIndex > 0) {
|
|
|
|
|
// element has children
|
|
|
|
|
if (lastIndex < m_currentItem->rowCount()) {
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel - removeRows from " << m_currentItem->text() << lastIndex << m_currentItem->rowCount() - lastIndex;
|
|
|
|
|
m_currentItem->removeRows(lastIndex, m_currentItem->rowCount() - lastIndex);
|
|
|
|
|
}
|
|
|
|
|
m_currentItem = parentItem();
|
|
|
|
|
} else {
|
|
|
|
|
if (m_currentItem->hasChildren()) {
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel - removeRows from " << m_currentItem->text() << 0 << m_currentItem->rowCount();
|
|
|
|
|
m_currentItem->removeRows(0, m_currentItem->rowCount());
|
|
|
|
|
}
|
|
|
|
|
m_currentItem = parentItem();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_treePos.last()++;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 14:28:53 +02:00
|
|
|
void QmlOutlineModel::reparentNodes(QmlOutlineItem *targetItem, int row, QList<QmlOutlineItem*> itemsToMove)
|
|
|
|
|
{
|
|
|
|
|
Utils::ChangeSet changeSet;
|
|
|
|
|
|
2010-07-22 09:51:07 +02:00
|
|
|
AST::UiObjectMember *targetObjectMember = targetItem->node()->uiObjectMemberCast();
|
2010-07-19 14:28:53 +02:00
|
|
|
if (!targetObjectMember)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QList<Utils::ChangeSet::Range> changedRanges;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < itemsToMove.size(); ++i) {
|
|
|
|
|
QmlOutlineItem *outlineItem = itemsToMove.at(i);
|
2010-07-22 09:51:07 +02:00
|
|
|
AST::UiObjectMember *sourceObjectMember = outlineItem->node()->uiObjectMemberCast();
|
2010-07-19 14:28:53 +02:00
|
|
|
if (!sourceObjectMember)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
bool insertionOrderSpecified = true;
|
|
|
|
|
AST::UiObjectMember *memberToInsertAfter = 0;
|
|
|
|
|
{
|
|
|
|
|
if (row == -1) {
|
|
|
|
|
insertionOrderSpecified = false;
|
|
|
|
|
} else if (row > 0) {
|
|
|
|
|
QmlOutlineItem *outlineItem = static_cast<QmlOutlineItem*>(targetItem->child(row - 1));
|
2010-07-22 09:51:07 +02:00
|
|
|
memberToInsertAfter = outlineItem->node()->uiObjectMemberCast();
|
2010-07-19 14:28:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::ChangeSet::Range range;
|
|
|
|
|
if (sourceObjectMember)
|
|
|
|
|
moveObjectMember(sourceObjectMember, targetObjectMember, insertionOrderSpecified,
|
|
|
|
|
memberToInsertAfter, &changeSet, &range);
|
|
|
|
|
changedRanges << range;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QmlJSRefactoringChanges refactoring(QmlJS::ModelManagerInterface::instance(), m_semanticInfo.snapshot);
|
|
|
|
|
refactoring.changeFile(m_semanticInfo.document->fileName(), changeSet);
|
|
|
|
|
foreach (const Utils::ChangeSet::Range &range, changedRanges) {
|
|
|
|
|
refactoring.reindent(m_semanticInfo.document->fileName(), range);
|
|
|
|
|
}
|
|
|
|
|
refactoring.apply();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlOutlineModel::moveObjectMember(AST::UiObjectMember *toMove,
|
|
|
|
|
AST::UiObjectMember *newParent,
|
|
|
|
|
bool insertionOrderSpecified,
|
|
|
|
|
AST::UiObjectMember *insertAfter,
|
|
|
|
|
Utils::ChangeSet *changeSet,
|
|
|
|
|
Utils::ChangeSet::Range *addedRange)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(toMove);
|
|
|
|
|
Q_ASSERT(newParent);
|
|
|
|
|
Q_ASSERT(changeSet);
|
|
|
|
|
|
|
|
|
|
QHash<QmlJS::AST::UiObjectMember*, QmlJS::AST::UiObjectMember*> parentMembers;
|
|
|
|
|
{
|
|
|
|
|
ObjectMemberParentVisitor visitor;
|
|
|
|
|
parentMembers = visitor(m_semanticInfo.document);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AST::UiObjectMember *oldParent = parentMembers.value(toMove);
|
|
|
|
|
Q_ASSERT(oldParent);
|
|
|
|
|
|
|
|
|
|
// make sure that target parent is actually a direct ancestor of target sibling
|
|
|
|
|
if (insertAfter)
|
|
|
|
|
newParent = parentMembers.value(insertAfter);
|
|
|
|
|
|
|
|
|
|
const QString documentText = m_semanticInfo.document->source();
|
|
|
|
|
|
2010-07-22 09:51:07 +02:00
|
|
|
if (AST::UiObjectDefinition *objDefinition = AST::cast<AST::UiObjectDefinition*>(newParent)) {
|
2010-07-19 14:28:53 +02:00
|
|
|
// target is an element
|
|
|
|
|
|
|
|
|
|
Rewriter rewriter(documentText, changeSet, QStringList());
|
|
|
|
|
rewriter.removeObjectMember(toMove, oldParent);
|
|
|
|
|
|
|
|
|
|
AST::UiObjectMemberList *listInsertAfter = 0;
|
|
|
|
|
if (insertionOrderSpecified) {
|
|
|
|
|
if (insertAfter) {
|
|
|
|
|
listInsertAfter = objDefinition->initializer->members;
|
|
|
|
|
while (listInsertAfter && (listInsertAfter->member != insertAfter))
|
|
|
|
|
listInsertAfter = listInsertAfter->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-22 09:51:07 +02:00
|
|
|
if (AST::UiScriptBinding *moveScriptBinding = AST::cast<AST::UiScriptBinding*>(toMove)) {
|
2010-07-19 14:28:53 +02:00
|
|
|
const QString propertyName = asString(moveScriptBinding->qualifiedId);
|
|
|
|
|
QString propertyValue;
|
|
|
|
|
{
|
|
|
|
|
const int offset = moveScriptBinding->statement->firstSourceLocation().begin();
|
|
|
|
|
const int length = moveScriptBinding->statement->lastSourceLocation().end() - offset;
|
|
|
|
|
propertyValue = documentText.mid(offset, length);
|
|
|
|
|
}
|
|
|
|
|
Rewriter::BindingType bindingType = Rewriter::ScriptBinding;
|
|
|
|
|
|
|
|
|
|
if (insertionOrderSpecified) {
|
|
|
|
|
*addedRange = rewriter.addBinding(objDefinition->initializer, propertyName, propertyValue, bindingType, listInsertAfter);
|
|
|
|
|
} else {
|
|
|
|
|
*addedRange = rewriter.addBinding(objDefinition->initializer, propertyName, propertyValue, bindingType);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
QString strToMove;
|
|
|
|
|
{
|
|
|
|
|
const int offset = toMove->firstSourceLocation().begin();
|
|
|
|
|
const int length = toMove->lastSourceLocation().end() - offset;
|
|
|
|
|
strToMove = documentText.mid(offset, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (insertionOrderSpecified) {
|
|
|
|
|
*addedRange = rewriter.addObject(objDefinition->initializer, strToMove, listInsertAfter);
|
|
|
|
|
} else {
|
|
|
|
|
*addedRange = rewriter.addObject(objDefinition->initializer, strToMove);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// target is a property
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
QStandardItem *QmlOutlineModel::parentItem()
|
|
|
|
|
{
|
|
|
|
|
QStandardItem *parent = m_currentItem->parent();
|
|
|
|
|
if (!parent)
|
|
|
|
|
parent = invisibleRootItem();
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 11:39:41 +02:00
|
|
|
|
|
|
|
|
QString QmlOutlineModel::asString(AST::UiQualifiedId *id)
|
|
|
|
|
{
|
|
|
|
|
QString text;
|
|
|
|
|
for (; id; id = id->next) {
|
|
|
|
|
if (id->name)
|
|
|
|
|
text += id->name->asString();
|
|
|
|
|
else
|
|
|
|
|
text += QLatin1Char('?');
|
|
|
|
|
|
|
|
|
|
if (id->next)
|
|
|
|
|
text += QLatin1Char('.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AST::SourceLocation QmlOutlineModel::getLocation(AST::UiObjectMember *objMember) {
|
|
|
|
|
AST::SourceLocation location;
|
|
|
|
|
location.offset = objMember->firstSourceLocation().offset;
|
|
|
|
|
location.length = objMember->lastSourceLocation().offset
|
|
|
|
|
- objMember->firstSourceLocation().offset
|
|
|
|
|
+ objMember->lastSourceLocation().length;
|
|
|
|
|
return location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QIcon QmlOutlineModel::getIcon(AST::UiObjectDefinition *objDef) {
|
|
|
|
|
const QmlJS::Interpreter::Value *value = m_context->evaluate(objDef->qualifiedTypeNameId);
|
|
|
|
|
|
|
|
|
|
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
|
|
|
|
|
do {
|
|
|
|
|
QString module;
|
|
|
|
|
QString typeName;
|
|
|
|
|
if (const Interpreter::QmlObjectValue *qmlObjectValue =
|
|
|
|
|
dynamic_cast<const Interpreter::QmlObjectValue*>(objectValue)) {
|
|
|
|
|
module = qmlObjectValue->packageName();
|
|
|
|
|
}
|
|
|
|
|
typeName = objectValue->className();
|
|
|
|
|
|
|
|
|
|
QIcon icon = m_icons->icon(module, typeName);
|
|
|
|
|
if (! icon.isNull())
|
|
|
|
|
return icon;
|
|
|
|
|
|
|
|
|
|
objectValue = objectValue->prototype(m_context->context());
|
|
|
|
|
} while (objectValue);
|
|
|
|
|
}
|
|
|
|
|
return QIcon();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString QmlOutlineModel::getId(AST::UiObjectDefinition *objDef) {
|
|
|
|
|
QString id;
|
|
|
|
|
for (AST::UiObjectMemberList *it = objDef->initializer->members; it; it = it->next) {
|
2010-07-22 09:51:07 +02:00
|
|
|
if (AST::UiScriptBinding *binding = AST::cast<AST::UiScriptBinding*>(it->member)) {
|
2010-07-19 11:39:41 +02:00
|
|
|
if (binding->qualifiedId->name->asString() == "id") {
|
2010-07-22 09:51:07 +02:00
|
|
|
AST::ExpressionStatement *expr = AST::cast<AST::ExpressionStatement*>(binding->statement);
|
2010-07-19 11:39:41 +02:00
|
|
|
if (!expr)
|
|
|
|
|
continue;
|
2010-07-22 09:51:07 +02:00
|
|
|
AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression*>(expr->expression);
|
2010-07-19 11:39:41 +02:00
|
|
|
if (!idExpr)
|
|
|
|
|
continue;
|
|
|
|
|
id = idExpr->name->asString();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace QmlJSEditor
|