Commit 023deff4 authored by Aurindam Jana's avatar Aurindam Jana

QmlJSInspectorAgent: Refactor code

Try to use the watchHandler for referring to the object tree
instead of caching the object tree locally.

Change-Id: I68624771ef49d50f2cf0d6d580b1b34d57483178
Reviewed-by: default avatarChristiaan Janssen <christiaan.janssen@nokia.com>
parent 6ef2c092
......@@ -286,14 +286,14 @@ quint32 BaseEngineDebugClient::addWatch(const ObjectReference &object,
return id;
}
quint32 BaseEngineDebugClient::addWatch(const ObjectReference &object)
quint32 BaseEngineDebugClient::addWatch(int objectId)
{
quint32 id = 0;
if (status() == Enabled) {
id = getId();
QByteArray message;
QDataStream ds(&message, QIODevice::WriteOnly);
ds << QByteArray("WATCH_OBJECT") << id << object.m_debugId;
ds << QByteArray("WATCH_OBJECT") << id << objectId;
sendMessage(message);
}
return id;
......@@ -341,28 +341,28 @@ quint32 BaseEngineDebugClient::queryRootContexts(const EngineReference &engine)
return id;
}
quint32 BaseEngineDebugClient::queryObject(const ObjectReference &object)
quint32 BaseEngineDebugClient::queryObject(int objectId)
{
quint32 id = 0;
if (status() == Enabled && object.m_debugId != -1) {
if (status() == Enabled && objectId != -1) {
id = getId();
QByteArray message;
QDataStream ds(&message, QIODevice::WriteOnly);
ds << QByteArray("FETCH_OBJECT") << id << object.m_debugId << false <<
ds << QByteArray("FETCH_OBJECT") << id << objectId << false <<
true;
sendMessage(message);
}
return id;
}
quint32 BaseEngineDebugClient::queryObjectRecursive(const ObjectReference &object)
quint32 BaseEngineDebugClient::queryObjectRecursive(int objectId)
{
quint32 id = 0;
if (status() == Enabled && object.m_debugId != -1) {
if (status() == Enabled && objectId != -1) {
id = getId();
QByteArray message;
QDataStream ds(&message, QIODevice::WriteOnly);
ds << QByteArray("FETCH_OBJECT") << id << object.m_debugId << true <<
ds << QByteArray("FETCH_OBJECT") << id << objectId << true <<
true;
sendMessage(message);
}
......
......@@ -56,15 +56,15 @@ public:
quint32 addWatch(const PropertyReference &property);
quint32 addWatch(const ContextReference &context, const QString &id);
quint32 addWatch(const ObjectReference &object, const QString &expr);
quint32 addWatch(const ObjectReference &object);
quint32 addWatch(int objectId);
quint32 addWatch(const FileReference &file);
void removeWatch(quint32 watch);
quint32 queryAvailableEngines();
quint32 queryRootContexts(const EngineReference &context);
quint32 queryObject(const ObjectReference &object);
quint32 queryObjectRecursive(const ObjectReference &object);
quint32 queryObject(int objectId);
quint32 queryObjectRecursive(int objectId);
quint32 queryExpressionResult(int objectDebugId,
const QString &expr, int engineId = -1);
virtual quint32 setBindingForObject(int objectDebugId, const QString &propertyName,
......@@ -104,6 +104,12 @@ class FileReference
{
public:
FileReference() : m_lineNumber(-1), m_columnNumber(-1) {}
FileReference(const QUrl &url, int line, int column)
: m_url(url),
m_lineNumber(line),
m_columnNumber(column)
{
}
QUrl url() const { return m_url; }
int lineNumber() const { return m_lineNumber; }
......@@ -142,6 +148,7 @@ public:
QString className() const { return m_className; }
QString idString() const { return m_idString; }
QString name() const { return m_name; }
bool isValid() const { return m_debugId != -1; }
FileReference source() const { return m_source; }
int contextDebugId() const { return m_contextDebugId; }
......
......@@ -274,27 +274,14 @@ void QmlInspectorAdapter::engineClientStatusChanged(QmlDebug::ClientStatus statu
void QmlInspectorAdapter::selectObjectsFromEditor(const QList<int> &debugIds)
{
int debugId = debugIds.first();
if (m_selectionCallbackExpected) {
m_selectionCallbackExpected = false;
return;
}
m_cursorPositionChangedExternally = true;
ObjectReference clientRef
= agent()->objectForId(debugId);
// if children haven't been loaded yet do so first, the editor
// might actually be interested in the children!
if (clientRef.debugId() != debugId
|| clientRef.needsMoreData()) {
m_targetToSync = ToolTarget;
m_debugIdToSelect = debugId;
agent()->fetchObject(debugId);
} else {
selectObject(clientRef, ToolTarget);
}
m_targetToSync = ToolTarget;
m_debugIdToSelect = debugIds.first();
selectObject(ObjectReference(m_debugIdToSelect), ToolTarget);
}
void QmlInspectorAdapter::selectObjectsFromToolsClient(const QList<int> &debugIds)
......@@ -302,18 +289,9 @@ void QmlInspectorAdapter::selectObjectsFromToolsClient(const QList<int> &debugId
if (debugIds.isEmpty())
return;
int debugId = debugIds.first();
ObjectReference clientRef
= agent()->objectForId(debugId);
if (clientRef.debugId() != debugId) {
m_targetToSync = EditorTarget;
m_debugIdToSelect = debugId;
agent()->fetchObject(debugId);
} else {
selectObject(clientRef, EditorTarget);
}
m_targetToSync = EditorTarget;
m_debugIdToSelect = debugIds.first();
selectObject(ObjectReference(m_debugIdToSelect), EditorTarget);
}
void QmlInspectorAdapter::onObjectFetched(const ObjectReference &ref)
......@@ -520,8 +498,7 @@ void QmlInspectorAdapter::gotoObjectReferenceDefinition(
m_selectionCallbackExpected = true;
if (textEditor) {
ObjectReference ref = objectReferenceForLocation(fileName);
if (ref.debugId() != obj.debugId()) {
if (objectIdForLocation(fileName) != obj.debugId()) {
m_selectionCallbackExpected = true;
editorManager->addCurrentPositionToNavigationHistory();
textEditor->gotoLine(source.lineNumber());
......@@ -530,7 +507,7 @@ void QmlInspectorAdapter::gotoObjectReferenceDefinition(
}
}
ObjectReference QmlInspectorAdapter::objectReferenceForLocation(
int QmlInspectorAdapter::objectIdForLocation(
const QString &fileName, int cursorPosition) const
{
Core::IEditor *editor = Core::EditorManager::openEditor(fileName);
......@@ -552,41 +529,13 @@ ObjectReference QmlInspectorAdapter::objectReferenceForLocation(
= semanticInfo.declaringMemberNoProperties(cursorPosition)) {
if (QmlJS::AST::UiObjectMember *objMember
= node->uiObjectMemberCast()) {
return agent()->objectForLocation(
return agent()->objectIdForLocation(
objMember->firstSourceLocation().startLine,
objMember->firstSourceLocation().startColumn);
}
}
}
return ObjectReference();
}
inline QString displayName(const ObjectReference &obj)
{
// special! state names
if (obj.className() == "State") {
foreach (const PropertyReference &prop, obj.properties()) {
if (prop.name() == "name")
return prop.value().toString();
}
}
// has id?
if (!obj.idString().isEmpty())
return obj.idString();
// return the simplified class name then
QString objTypeName = obj.className();
QStringList declarativeStrings;
declarativeStrings << QLatin1String("QDeclarative")
<< QLatin1String("QQml");
foreach (const QString &str, declarativeStrings) {
if (objTypeName.startsWith(str)) {
objTypeName = objTypeName.mid(str.length()).section('_', 0, 0);
break;
}
}
return QString("<%1>").arg(objTypeName);
return -1;
}
void QmlInspectorAdapter::selectObject(const ObjectReference &obj,
......@@ -602,7 +551,7 @@ void QmlInspectorAdapter::selectObject(const ObjectReference &obj,
agent()->selectObjectInTree(obj.debugId());
m_currentSelectedDebugId = obj.debugId();
m_currentSelectedDebugName = displayName(obj);
m_currentSelectedDebugName = agent()->displayName(obj.debugId());
emit selectionChanged();
}
......@@ -648,7 +597,7 @@ void QmlInspectorAdapter::onReloaded()
void QmlInspectorAdapter::onDestroyedObject()
{
m_agent->refreshObjectTree();
m_agent->queryEngineContext();
}
} // namespace Internal
......
......@@ -107,7 +107,7 @@ private:
void showConnectionStatusMessage(const QString &message);
void gotoObjectReferenceDefinition(const QmlDebug::ObjectReference &obj);
QmlDebug::ObjectReference objectReferenceForLocation(
int objectIdForLocation(
const QString &fileName, int cursorPosition = -1) const;
enum SelectionTarget { NoTarget, ToolTarget, EditorTarget };
......
......@@ -34,6 +34,8 @@
#define QMLINSPECTORAGENT_H
#include <QObject>
#include <QStack>
#include <QTimer>
#include <qmldebug/baseenginedebugclient.h>
#include <watchdata.h>
......@@ -57,7 +59,6 @@ public:
explicit QmlInspectorAgent(DebuggerEngine *engine, QObject *parent = 0);
void refreshObjectTree();
void fetchObject(int debugId);
quint32 queryExpressionResult(int debugId, const QString &expression);
......@@ -75,11 +76,9 @@ public:
quint32 resetBindingForObject(int objectDebugId,
const QString &propertyName);
QList<QmlDebug::ObjectReference> objects() const;
QmlDebug::ObjectReference objectForId(int debugId) const;
QmlDebug::ObjectReference objectForId(const QString &objectId) const;
QmlDebug::ObjectReference objectForLocation(int line, int column) const;
QList<QmlDebug::ObjectReference> rootObjects() const { return m_rootObjects; }
int objectIdForLocation(int line, int column) const;
QHash<int, QString> rootObjectIds() const;
DebugIdHash debugIdHash() const { return m_debugIdHash; }
bool addObjectWatch(int objectDebugId);
......@@ -88,10 +87,13 @@ public:
void removeAllObjectWatches();
void setEngineClient(QmlDebug::BaseEngineDebugClient *client);
QString displayName(int objectDebugId) const;
public slots:
void fetchContextObjectsForLocation(const QString &file,
int lineNumber, int columnNumber);
void queryEngineContext();
signals:
void objectTreeUpdated();
void objectFetched(const QmlDebug::ObjectReference &ref);
......@@ -106,28 +108,13 @@ private slots:
private:
void reloadEngines();
void queryEngineContext(int id);
quint32 fetchContextObject(const QmlDebug::ObjectReference &obj);
void fetchRootObjects(const QmlDebug::ContextReference &context, bool clear);
void fetchObjectsInContextRecursive(const QmlDebug::ContextReference &context);
void updateEngineList(const QList<QmlDebug::EngineReference> &engines);
void rootContextChanged(const QmlDebug::ContextReference &context);
void objectTreeFetched(const QmlDebug::ObjectReference &result);
void onCurrentObjectsFetched(const QmlDebug::ObjectReference &result);
bool getObjectHierarchy(const QmlDebug::ObjectReference &object);
void buildDebugIdHashRecursive(const QmlDebug::ObjectReference &ref);
QList<WatchData> buildWatchData(const QmlDebug::ObjectReference &obj,
const WatchData &parent);
void addObjectToTree(const QmlDebug::ObjectReference &obj, bool notify);
QmlDebug::ObjectReference objectForId(
int debugId,
const QmlDebug::ObjectReference &ref) const;
QList<QmlDebug::ObjectReference> objects(
const QmlDebug::ObjectReference &objectRef) const;
const QByteArray &parentIname);
enum LogDirection {
LogSend,
......@@ -135,25 +122,27 @@ private:
};
void log(LogDirection direction, const QString &message);
bool isConnected();
bool isConnected() const;
void clearObjectTree();
private:
DebuggerEngine *m_engine;
DebuggerEngine *m_debuggerEngine;
QmlDebug::BaseEngineDebugClient *m_engineClient;
quint32 m_engineQueryId;
quint32 m_rootContextQueryId;
int m_objectToSelect;
QList<quint32> m_objectTreeQueryIds;
QList<QmlDebug::ObjectReference> m_rootObjects;
QList<quint32> m_fetchCurrentObjectsQueryIds;
QList<QmlDebug::ObjectReference> m_fetchCurrentObjects;
QList<QmlDebug::EngineReference> m_engines;
QStack<QmlDebug::ObjectReference> m_objectStack;
QmlDebug::EngineReference m_engine;
QHash<int, QByteArray> m_debugIdToIname;
QHash<int, QList<int> > m_debugIdChildIds; // This is for 4.x
QHash<int, QmlDebug::FileReference> m_debugIdLocations;
DebugIdHash m_debugIdHash;
QList<int> m_objectWatches;
QList<int> m_fetchDataIds;
QTimer m_delayQueryTimer;
};
} // Internal
......
......@@ -440,19 +440,6 @@ void QmlLiveTextPreview::setApplyChangesToQmlInspector(bool applyChanges)
m_applyChangesToQmlInspector = applyChanges;
}
static QList<int> findRootObjectRecursive(const ObjectReference &object,
const Document::Ptr &doc)
{
QList<int> result;
if (object.className() == doc->componentName())
result += object.debugId();
foreach (const ObjectReference &it, object.children()) {
result += findRootObjectRecursive(it, doc);
}
return result;
}
void QmlLiveTextPreview::updateDebugIds()
{
if (!m_initialDoc->qmlProgram())
......@@ -485,11 +472,13 @@ void QmlLiveTextPreview::updateDebugIds()
// Map the root nodes of the document.
if (doc->qmlProgram()->members && doc->qmlProgram()->members->member) {
UiObjectMember *root = doc->qmlProgram()->members->member;
QHashIterator<int,QString> rIds(m_inspectorAdapter->agent()->rootObjectIds());
QList<int> r;
foreach (const ObjectReference& it,
m_inspectorAdapter->agent()->rootObjects()) {
r += findRootObjectRecursive(it, doc);
}
while (rIds.hasNext()) {
rIds.next();
if (rIds.value() == doc->componentName())
r += rIds.key();
}
if (!r.isEmpty())
m_debugIds[root] += r;
}
......@@ -561,7 +550,7 @@ bool QmlLiveTextPreview::changeSelectedElements(const QList<int> offsets,
QList<int> list = objectReferencesForOffset(offset);
if (!containsReferenceUnderCursor
&& objectRefUnderCursor.debugId() != -1) {
&& objectRefUnderCursor.isValid()) {
foreach (int id, list) {
if (id == objectRefUnderCursor.debugId()) {
containsReferenceUnderCursor = true;
......@@ -577,7 +566,7 @@ bool QmlLiveTextPreview::changeSelectedElements(const QList<int> offsets,
// fallback: use ref under cursor if nothing else is found
if (selectedReferences.isEmpty()
&& !containsReferenceUnderCursor
&& objectRefUnderCursor.debugId() != -1) {
&& objectRefUnderCursor.isValid()) {
selectedReferences << objectRefUnderCursor.debugId();
}
......@@ -613,7 +602,7 @@ void QmlLiveTextPreview::documentChanged(QmlJS::Document::Ptr doc)
m_debugIds = delta(m_previousDoc, doc, m_debugIds);
if (delta.referenceRefreshRequired)
m_inspectorAdapter->agent()->refreshObjectTree();
m_inspectorAdapter->agent()->queryEngineContext();
if (delta.unsyncronizableChanges != NoUnsyncronizableChanges) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment