qmlinspectoradapter.cpp 22.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "qmlinspectoradapter.h"

#include "debuggeractions.h"
#include "debuggercore.h"
#include "debuggerstringutils.h"
#include "qmladapter.h"
#include "qmlengine.h"
#include "qmlinspectoragent.h"
#include "qmllivetextpreview.h"

#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
#include <qmldebug/declarativeenginedebugclient.h>
#include <qmldebug/declarativetoolsclient.h>
#include <qmldebug/qmlenginedebugclient.h>
#include <qmldebug/qmltoolsclient.h>
#include <qmljseditor/qmljseditorconstants.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljstools/qmljssemanticinfo.h>
#include <utils/qtcassert.h>
#include <utils/savedaction.h>

using namespace QmlDebug;

namespace Debugger {
namespace Internal {

// Get semantic info from QmlJSTextEditorWidget
// (we use the meta object system here to avoid having to link
// against qmljseditor)
static QmlJSTools::SemanticInfo getSemanticInfo(QPlainTextEdit *qmlJSTextEdit)
{
    QmlJSTools::SemanticInfo info;
    QTC_ASSERT(QLatin1String(qmlJSTextEdit->metaObject()->className())
               == QLatin1String("QmlJSEditor::QmlJSTextEditorWidget"),
               return info);
    QTC_ASSERT(qmlJSTextEdit->metaObject()->indexOfProperty("semanticInfo")
               != -1, return info);

    info = qmlJSTextEdit->property("semanticInfo")
            .value<QmlJSTools::SemanticInfo>();
    return info;
}

/*!
 * QmlInspectorAdapter manages the clients for the inspector, and the
 * integration with the text editor.
 */
QmlInspectorAdapter::QmlInspectorAdapter(QmlAdapter *debugAdapter,
                                         QmlEngine *engine,
                                         QObject *parent)
    : QObject(parent)
    , m_debugAdapter(debugAdapter)
    , m_engine(engine)
    , m_engineClient(0)
    , m_toolsClient(0)
    , m_agent(new QmlInspectorAgent(engine, this))
    , m_targetToSync(NoTarget)
    , m_debugIdToSelect(-1)
    , m_currentSelectedDebugId(-1)
    , m_listeningToEditorManager(false)
    , m_selectionCallbackExpected(false)
    , m_cursorPositionChangedExternally(false)
    , m_toolsClientConnected(false)
    , m_inspectorToolsContext("Debugger.QmlInspector")
    , m_selectAction(new QAction(this))
    , m_zoomAction(new QAction(this))
101
    , m_engineClientConnected(false)
102
{
Kai Koehne's avatar
Kai Koehne committed
103 104
    connect(m_agent, SIGNAL(objectFetched(QmlDebug::ObjectReference)),
            SLOT(onObjectFetched(QmlDebug::ObjectReference)));
105 106 107 108

    QmlDebugConnection *connection = m_debugAdapter->connection();
    DeclarativeEngineDebugClient *engineClient1
            = new DeclarativeEngineDebugClient(connection);
Kai Koehne's avatar
Kai Koehne committed
109 110 111 112
    connect(engineClient1, SIGNAL(newStatus(QmlDebug::ClientStatus)),
            this, SLOT(clientStatusChanged(QmlDebug::ClientStatus)));
    connect(engineClient1, SIGNAL(newStatus(QmlDebug::ClientStatus)),
            this, SLOT(engineClientStatusChanged(QmlDebug::ClientStatus)));
113 114

    QmlEngineDebugClient *engineClient2 = new QmlEngineDebugClient(connection);
Kai Koehne's avatar
Kai Koehne committed
115 116 117 118
    connect(engineClient2, SIGNAL(newStatus(QmlDebug::ClientStatus)),
            this, SLOT(clientStatusChanged(QmlDebug::ClientStatus)));
    connect(engineClient2, SIGNAL(newStatus(QmlDebug::ClientStatus)),
            this, SLOT(engineClientStatusChanged(QmlDebug::ClientStatus)));
119 120 121 122

    m_engineClients.insert(engineClient1->name(), engineClient1);
    m_engineClients.insert(engineClient2->name(), engineClient2);

Kai Koehne's avatar
Kai Koehne committed
123
    if (engineClient1->status() == QmlDebug::Enabled)
124
        setActiveEngineClient(engineClient1);
Kai Koehne's avatar
Kai Koehne committed
125
    if (engineClient2->status() == QmlDebug::Enabled)
126 127 128
        setActiveEngineClient(engineClient2);

    DeclarativeToolsClient *toolsClient1 = new DeclarativeToolsClient(connection);
Kai Koehne's avatar
Kai Koehne committed
129
    connect(toolsClient1, SIGNAL(newStatus(QmlDebug::ClientStatus)),
Kai Koehne's avatar
Kai Koehne committed
130
            this, SLOT(clientStatusChanged(QmlDebug::ClientStatus)));
Kai Koehne's avatar
Kai Koehne committed
131
    connect(toolsClient1, SIGNAL(newStatus(QmlDebug::ClientStatus)),
Kai Koehne's avatar
Kai Koehne committed
132
            this, SLOT(toolsClientStatusChanged(QmlDebug::ClientStatus)));
133 134

    QmlToolsClient *toolsClient2 = new QmlToolsClient(connection);
Kai Koehne's avatar
Kai Koehne committed
135
    connect(toolsClient2, SIGNAL(newStatus(QmlDebug::ClientStatus)),
Kai Koehne's avatar
Kai Koehne committed
136
            this, SLOT(clientStatusChanged(QmlDebug::ClientStatus)));
Kai Koehne's avatar
Kai Koehne committed
137
    connect(toolsClient2, SIGNAL(newStatus(QmlDebug::ClientStatus)),
Kai Koehne's avatar
Kai Koehne committed
138
            this, SLOT(toolsClientStatusChanged(QmlDebug::ClientStatus)));
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

    // toolbar
    m_selectAction->setObjectName("QML Select Action");
    m_zoomAction->setObjectName("QML Zoom Action");
    m_selectAction->setCheckable(true);
    m_zoomAction->setCheckable(true);

    connect(m_selectAction, SIGNAL(triggered(bool)),
            SLOT(onSelectActionTriggered(bool)));
    connect(m_zoomAction, SIGNAL(triggered(bool)),
            SLOT(onZoomActionTriggered(bool)));
}

QmlInspectorAdapter::~QmlInspectorAdapter()
{
}

BaseEngineDebugClient *QmlInspectorAdapter::engineClient() const
{
    return m_engineClient;
}

BaseToolsClient *QmlInspectorAdapter::toolsClient() const
{
    return m_toolsClient;
}

QmlInspectorAgent *QmlInspectorAdapter::agent() const
{
    return m_agent;
}

int QmlInspectorAdapter::currentSelectedDebugId() const
{
    return m_currentSelectedDebugId;
}

QString QmlInspectorAdapter::currentSelectedDisplayName() const
{
    return m_currentSelectedDebugName;
}

Kai Koehne's avatar
Kai Koehne committed
181
void QmlInspectorAdapter::clientStatusChanged(QmlDebug::ClientStatus status)
182 183 184 185 186 187 188 189 190 191 192
{
    QString serviceName;
    float version = 0;
    if (QmlDebugClient *client = qobject_cast<QmlDebugClient*>(sender())) {
        serviceName = client->name();
        version = client->serviceVersion();
    }

    m_debugAdapter->logServiceStatusChange(serviceName, version, status);
}

Kai Koehne's avatar
Kai Koehne committed
193
void QmlInspectorAdapter::toolsClientStatusChanged(QmlDebug::ClientStatus status)
194 195 196
{
    Core::ActionManager *am = Core::ICore::actionManager();
    BaseToolsClient *client = qobject_cast<BaseToolsClient*>(sender());
Kai Koehne's avatar
Kai Koehne committed
197
    if (status == QmlDebug::Enabled) {
198 199 200 201 202 203
        m_toolsClient = client;

        connect(client, SIGNAL(currentObjectsChanged(QList<int>)),
                SLOT(selectObjectsFromToolsClient(QList<int>)));
        connect(client, SIGNAL(logActivity(QString,QString)),
                m_debugAdapter, SLOT(logServiceActivity(QString,QString)));
204
        connect(client, SIGNAL(reloaded()), SLOT(onReloaded()));
205
        connect(client, SIGNAL(destroyedObject()), SLOT(onDestroyedObject()));
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

        // only enable zoom action for Qt 4.x/old client
        // (zooming is integrated into selection tool in Qt 5).
        m_zoomAction->setEnabled(
                    qobject_cast<DeclarativeToolsClient*>(client) != 0);

        // register actions here
        // because there can be multiple QmlEngines
        // at the same time (but hopefully one one is connected)
        am->registerAction(m_selectAction,
                           Core::Id(Constants::QML_SELECTTOOL),
                           m_inspectorToolsContext);
        am->registerAction(m_zoomAction, Core::Id(Constants::QML_ZOOMTOOL),
                           m_inspectorToolsContext);

221
        Core::ICore::updateAdditionalContexts(Core::Context(),
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
                                       m_inspectorToolsContext);

        Utils::SavedAction *action = debuggerCore()->action(QmlUpdateOnSave);
        connect(action, SIGNAL(valueChanged(QVariant)),
                SLOT(onUpdateOnSaveChanged(QVariant)));

        action = debuggerCore()->action(ShowAppOnTop);
        connect(action, SIGNAL(valueChanged(QVariant)),
                SLOT(onShowAppOnTopChanged(QVariant)));
        if (action->isChecked())
            m_toolsClient->showAppOnTop(true);

        m_toolsClientConnected = true;
    } else if (m_toolsClientConnected
               && client == m_toolsClient) {
        disconnect(client, SIGNAL(currentObjectsChanged(QList<int>)),
                   this, SLOT(selectObjectsFromToolsClient(QList<int>)));
        disconnect(client, SIGNAL(logActivity(QString,QString)),
                   m_debugAdapter, SLOT(logServiceActivity(QString,QString)));

        am->unregisterAction(m_selectAction,
                             Core::Id(Constants::QML_SELECTTOOL));
        am->unregisterAction(m_zoomAction,
                             Core::Id(Constants::QML_ZOOMTOOL));

        m_selectAction->setChecked(false);
        m_zoomAction->setChecked(false);
249
        Core::ICore::updateAdditionalContexts(m_inspectorToolsContext,
250 251 252 253 254 255 256 257 258 259 260
                                       Core::Context());

        Utils::SavedAction *action = debuggerCore()->action(QmlUpdateOnSave);
        disconnect(action, 0, this, 0);
        action = debuggerCore()->action(ShowAppOnTop);
        disconnect(action, 0, this, 0);

        m_toolsClientConnected = false;
    }
}

Kai Koehne's avatar
Kai Koehne committed
261
void QmlInspectorAdapter::engineClientStatusChanged(QmlDebug::ClientStatus status)
262 263 264
{
    BaseEngineDebugClient *client
            = qobject_cast<BaseEngineDebugClient*>(sender());
265 266 267 268 269 270 271 272 273

    if (status == QmlDebug::Enabled) {
        QTC_ASSERT(client, return);
        setActiveEngineClient(client);
    } else if (m_engineClientConnected &&
               (client == m_engineClient)) {
        m_engineClientConnected = false;
        deletePreviews();
    }
274 275 276 277 278 279 280 281 282 283 284 285
}

void QmlInspectorAdapter::selectObjectsFromEditor(const QList<int> &debugIds)
{
    int debugId = debugIds.first();

    if (m_selectionCallbackExpected) {
        m_selectionCallbackExpected = false;
        return;
    }
    m_cursorPositionChangedExternally = true;

Kai Koehne's avatar
Kai Koehne committed
286
    ObjectReference clientRef
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
            = 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);
    }
}

void QmlInspectorAdapter::selectObjectsFromToolsClient(const QList<int> &debugIds)
{
    if (debugIds.isEmpty())
        return;

    int debugId = debugIds.first();

Kai Koehne's avatar
Kai Koehne committed
308
    ObjectReference clientRef
309 310 311 312 313 314 315 316 317 318 319
            = agent()->objectForId(debugId);

    if (clientRef.debugId() != debugId) {
        m_targetToSync = EditorTarget;
        m_debugIdToSelect = debugId;
        agent()->fetchObject(debugId);
    } else {
        selectObject(clientRef, EditorTarget);
    }
}

Kai Koehne's avatar
Kai Koehne committed
320
void QmlInspectorAdapter::onObjectFetched(const ObjectReference &ref)
321 322 323 324 325 326 327 328 329
{
    if (ref.debugId() == m_debugIdToSelect) {
        m_debugIdToSelect = -1;
        selectObject(ref, m_targetToSync);
    }
}

void QmlInspectorAdapter::createPreviewForEditor(Core::IEditor *newEditor)
{
330 331 332
    if (!m_engineClientConnected)
        return;

333 334 335 336 337 338 339 340 341
    if (newEditor && newEditor->id()
            != QmlJSEditor::Constants::C_QMLJSEDITOR_ID)
        return;

    QString filename = newEditor->document()->fileName();
    QmlJS::ModelManagerInterface *modelManager =
            QmlJS::ModelManagerInterface::instance();
    QmlJS::Document::Ptr doc = modelManager->snapshot().document(filename);
    if (!doc) {
342
        if (filename.endsWith(".qml") || filename.endsWith(".js")) {
343 344 345 346 347 348
            // add to list of docs that we have to update when
            // snapshot figures out that there's a new document
            m_pendingPreviewDocumentNames.append(filename);
        }
        return;
    }
349
    if (!doc->qmlProgram() && !filename.endsWith(".js"))
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        return;

    QmlJS::Document::Ptr initdoc = m_loadedSnapshot.document(filename);
    if (!initdoc)
        initdoc = doc;

    if (m_textPreviews.contains(filename)) {
        QmlLiveTextPreview *preview = m_textPreviews.value(filename);
        preview->associateEditor(newEditor);
    } else {
        QmlLiveTextPreview *preview
                = new QmlLiveTextPreview(doc, initdoc, this, this);
        connect(preview,
                SIGNAL(selectedItemsChanged(QList<int>)),
                SLOT(selectObjectsFromEditor(QList<int>)));
365

366 367
        preview->setApplyChangesToQmlInspector(
                    debuggerCore()->action(QmlUpdateOnSave)->isChecked());
368 369
        connect(preview, SIGNAL(reloadRequest()),
                this, SLOT(onReload()));
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

        m_textPreviews.insert(newEditor->document()->fileName(), preview);
        preview->associateEditor(newEditor);
        preview->updateDebugIds();
    }
}

void QmlInspectorAdapter::removePreviewForEditor(Core::IEditor *editor)
{
    if (QmlLiveTextPreview *preview
            = m_textPreviews.value(editor->document()->fileName())) {
        preview->unassociateEditor(editor);
    }
}

void QmlInspectorAdapter::updatePendingPreviewDocuments(QmlJS::Document::Ptr doc)
{
    int idx = -1;
    idx = m_pendingPreviewDocumentNames.indexOf(doc->fileName());

    if (idx == -1)
        return;

    Core::EditorManager *em = Core::EditorManager::instance();
    QList<Core::IEditor *> editors
            = em->editorsForFileName(doc->fileName());

    if (editors.isEmpty())
        return;

    m_pendingPreviewDocumentNames.removeAt(idx);

    Core::IEditor *editor = editors.takeFirst();
    createPreviewForEditor(editor);
    QmlLiveTextPreview *preview
            = m_textPreviews.value(editor->document()->fileName());
    foreach (Core::IEditor *editor, editors)
        preview->associateEditor(editor);
}

void QmlInspectorAdapter::onSelectActionTriggered(bool checked)
{
    if (checked) {
        toolsClient()->setDesignModeBehavior(true);
        toolsClient()->changeToSelectTool();
        m_zoomAction->setChecked(false);
    } else {
        toolsClient()->setDesignModeBehavior(false);
    }
}

void QmlInspectorAdapter::onZoomActionTriggered(bool checked)
{
    if (checked) {
        toolsClient()->setDesignModeBehavior(true);
        toolsClient()->changeToZoomTool();
        m_selectAction->setChecked(false);
    } else {
        toolsClient()->setDesignModeBehavior(false);
    }
}

void QmlInspectorAdapter::onShowAppOnTopChanged(const QVariant &value)
{
    bool showAppOnTop = value.toBool();
Kai Koehne's avatar
Kai Koehne committed
435
    if (m_toolsClient->status() == QmlDebug::Enabled)
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
        m_toolsClient->showAppOnTop(showAppOnTop);
}

void QmlInspectorAdapter::onUpdateOnSaveChanged(const QVariant &value)
{
    bool updateOnSave = value.toBool();
    for (QHash<QString, QmlLiveTextPreview *>::const_iterator it
         = m_textPreviews.constBegin();
         it != m_textPreviews.constEnd(); ++it) {
        it.value()->setApplyChangesToQmlInspector(updateOnSave);
    }
}

void QmlInspectorAdapter::setActiveEngineClient(BaseEngineDebugClient *client)
{
    if (m_engineClient == client)
        return;

    m_engineClient = client;
    m_agent->setEngineClient(m_engineClient);
456
    m_engineClientConnected = true;
457 458

    if (m_engineClient &&
Kai Koehne's avatar
Kai Koehne committed
459
            m_engineClient->status() == QmlDebug::Enabled) {
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
        QmlJS::ModelManagerInterface *modelManager
                = QmlJS::ModelManagerInterface::instance();
        QmlJS::Snapshot snapshot = modelManager->snapshot();
        for (QHash<QString, QmlLiveTextPreview *>::const_iterator it
             = m_textPreviews.constBegin();
             it != m_textPreviews.constEnd(); ++it) {
            QmlJS::Document::Ptr doc = snapshot.document(it.key());
            it.value()->resetInitialDoc(doc);
        }

        initializePreviews();
    }
}

void QmlInspectorAdapter::initializePreviews()
{
    Core::EditorManager *em = Core::EditorManager::instance();
    QmlJS::ModelManagerInterface *modelManager
            = QmlJS::ModelManagerInterface::instance();
    m_loadedSnapshot = modelManager->snapshot();

    if (!m_listeningToEditorManager) {
        m_listeningToEditorManager = true;
        connect(em, SIGNAL(editorAboutToClose(Core::IEditor*)),
                this, SLOT(removePreviewForEditor(Core::IEditor*)));
        connect(em, SIGNAL(editorOpened(Core::IEditor*)),
                this, SLOT(createPreviewForEditor(Core::IEditor*)));
        connect(modelManager,
                SIGNAL(documentChangedOnDisk(QmlJS::Document::Ptr)),
                this, SLOT(updatePendingPreviewDocuments(QmlJS::Document::Ptr)));
    }

    // initial update
    foreach (Core::IEditor *editor, em->openedEditors())
        createPreviewForEditor(editor);
}

void QmlInspectorAdapter::showConnectionStatusMessage(const QString &message)
{
    m_engine->showMessage(_("QML Inspector: ") + message, LogStatus);
}

void QmlInspectorAdapter::gotoObjectReferenceDefinition(
Kai Koehne's avatar
Kai Koehne committed
503
        const ObjectReference &obj)
504 505 506 507 508 509
{
    if (m_cursorPositionChangedExternally) {
        m_cursorPositionChangedExternally = false;
        return;
    }

Kai Koehne's avatar
Kai Koehne committed
510
    FileReference source = obj.source();
511 512 513 514 515 516 517 518 519 520 521 522 523

    const QString fileName = m_engine->toFileInProject(source.url());

    Core::EditorManager *editorManager = Core::EditorManager::instance();
    Core::IEditor *currentEditor = editorManager->currentEditor();
    Core::IEditor *editor = editorManager->openEditor(fileName);
    TextEditor::ITextEditor *textEditor
            = qobject_cast<TextEditor::ITextEditor*>(editor);

    if (currentEditor != editor)
        m_selectionCallbackExpected = true;

    if (textEditor) {
Kai Koehne's avatar
Kai Koehne committed
524
        ObjectReference ref = objectReferenceForLocation(fileName);
525 526 527 528 529 530 531 532 533
        if (ref.debugId() != obj.debugId()) {
            m_selectionCallbackExpected = true;
            editorManager->addCurrentPositionToNavigationHistory();
            textEditor->gotoLine(source.lineNumber());
            textEditor->widget()->setFocus();
        }
    }
}

Kai Koehne's avatar
Kai Koehne committed
534
ObjectReference QmlInspectorAdapter::objectReferenceForLocation(
535 536
        const QString &fileName, int cursorPosition) const
{
537
    Core::IEditor *editor = Core::EditorManager::openEditor(fileName);
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    TextEditor::ITextEditor *textEditor
            = qobject_cast<TextEditor::ITextEditor*>(editor);

    if (textEditor
            && textEditor->id() == QmlJSEditor::Constants::C_QMLJSEDITOR_ID) {
        if (cursorPosition == -1)
            cursorPosition = textEditor->position();
        TextEditor::BaseTextEditor *baseTextEditor =
                static_cast<TextEditor::BaseTextEditor*>(editor);
        QPlainTextEdit *editWidget
                = qobject_cast<QPlainTextEdit*>(baseTextEditor->widget());

        QmlJSTools::SemanticInfo semanticInfo = getSemanticInfo(editWidget);

        if (QmlJS::AST::Node *node
                = semanticInfo.declaringMemberNoProperties(cursorPosition)) {
            if (QmlJS::AST::UiObjectMember *objMember
                    = node->uiObjectMemberCast()) {
                return agent()->objectForLocation(
                            objMember->firstSourceLocation().startLine,
                            objMember->firstSourceLocation().startColumn);
            }
        }
    }
Kai Koehne's avatar
Kai Koehne committed
562
    return ObjectReference();
563 564
}

Kai Koehne's avatar
Kai Koehne committed
565
inline QString displayName(const ObjectReference &obj)
566 567 568
{
    // special! state names
    if (obj.className() == "State") {
Kai Koehne's avatar
Kai Koehne committed
569
        foreach (const PropertyReference &prop, obj.properties()) {
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
            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);
}

Kai Koehne's avatar
Kai Koehne committed
593
void QmlInspectorAdapter::selectObject(const ObjectReference &obj,
594 595 596 597
                                       SelectionTarget target)
{
    if (target == ToolTarget)
        m_toolsClient->setObjectIdList(
Kai Koehne's avatar
Kai Koehne committed
598
                    QList<ObjectReference>() << obj);
599 600 601 602 603 604 605 606 607 608 609

    if (target == EditorTarget)
        gotoObjectReferenceDefinition(obj);

    agent()->selectObjectInTree(obj.debugId());

    m_currentSelectedDebugId = obj.debugId();
    m_currentSelectedDebugName = displayName(obj);
    emit selectionChanged();
}

610 611 612 613 614 615
void QmlInspectorAdapter::deletePreviews()
{
    foreach (const QString &key, m_textPreviews.keys())
        delete m_textPreviews.take(key);
}

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
void QmlInspectorAdapter::onReload()
{
    QHash<QString, QByteArray> changesHash;
    for (QHash<QString, QmlLiveTextPreview *>::const_iterator it
         = m_textPreviews.constBegin();
         it != m_textPreviews.constEnd(); ++it) {
        if (it.value()->hasUnsynchronizableChange()) {
            QFileInfo info = QFileInfo(it.value()->fileName());
            QFile changedFile(it.value()->fileName());
            QByteArray fileContents;
            if (changedFile.open(QFile::ReadOnly))
                fileContents = changedFile.readAll();
            changedFile.close();
            changesHash.insert(info.fileName(),
                               fileContents);
        }
    }
    m_toolsClient->reload(changesHash);
}

void QmlInspectorAdapter::onReloaded()
{
    QmlJS::ModelManagerInterface *modelManager =
            QmlJS::ModelManagerInterface::instance();
    QmlJS::Snapshot snapshot = modelManager->snapshot();
    m_loadedSnapshot = snapshot;
    for (QHash<QString, QmlLiveTextPreview *>::const_iterator it
         = m_textPreviews.constBegin();
         it != m_textPreviews.constEnd(); ++it) {
        QmlJS::Document::Ptr doc = snapshot.document(it.key());
        it.value()->resetInitialDoc(doc);
    }
}

650 651 652 653 654
void QmlInspectorAdapter::onDestroyedObject()
{
    m_agent->refreshObjectTree();
}

655 656
} // namespace Internal
} // namespace Debugger