OSDN Git Service

Debugger: Change BreakpointOnSignalHandler to BreakpointOnQMLSignalHandler
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / qml / qmlengine.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32
33 #include "qmlengine.h"
34 #include "qmladapter.h"
35
36 #include "debuggerstartparameters.h"
37 #include "debuggeractions.h"
38 #include "debuggerconstants.h"
39 #include "debuggercore.h"
40 #include "debuggerdialogs.h"
41 #include "debuggermainwindow.h"
42 #include "debuggerrunner.h"
43 #include "debuggerstringutils.h"
44 #include "debuggertooltipmanager.h"
45
46 #include "breakhandler.h"
47 #include "moduleshandler.h"
48 #include "registerhandler.h"
49 #include "stackhandler.h"
50 #include "watchhandler.h"
51 #include "watchutils.h"
52
53 #include <extensionsystem/pluginmanager.h>
54 #include <projectexplorer/applicationlauncher.h>
55
56 #include <utils/environment.h>
57 #include <utils/qtcassert.h>
58 #include <utils/fileinprojectfinder.h>
59
60 #include <coreplugin/icore.h>
61 #include <coreplugin/helpmanager.h>
62
63 #include <QtCore/QDateTime>
64 #include <QtCore/QDebug>
65 #include <QtCore/QDir>
66 #include <QtCore/QFileInfo>
67 #include <QtCore/QTimer>
68
69 #include <QtGui/QAction>
70 #include <QtGui/QApplication>
71 #include <QtGui/QMainWindow>
72 #include <QtGui/QMessageBox>
73 #include <QtGui/QToolTip>
74 #include <QtGui/QTextDocument>
75
76 #include <QtNetwork/QTcpSocket>
77 #include <QtNetwork/QHostAddress>
78
79 #define DEBUG_QML 1
80 #if DEBUG_QML
81 #   define SDEBUG(s) qDebug() << s
82 #else
83 #   define SDEBUG(s)
84 #endif
85 # define XSDEBUG(s) qDebug() << s
86
87 using namespace ProjectExplorer;
88
89 namespace Debugger {
90 namespace Internal {
91
92 class QmlEnginePrivate
93 {
94 public:
95     explicit QmlEnginePrivate(QmlEngine *q);
96
97 private:
98     friend class QmlEngine;
99     QmlAdapter m_adapter;
100     ApplicationLauncher m_applicationLauncher;
101     Utils::FileInProjectFinder fileFinder;
102     QTimer m_noDebugOutputTimer;
103 };
104
105 QmlEnginePrivate::QmlEnginePrivate(QmlEngine *q)
106     : m_adapter(q)
107 {}
108
109
110 ///////////////////////////////////////////////////////////////////////
111 //
112 // QmlEngine
113 //
114 ///////////////////////////////////////////////////////////////////////
115
116 QmlEngine::QmlEngine(const DebuggerStartParameters &startParameters,
117         DebuggerEngine *masterEngine)
118   : DebuggerEngine(startParameters, masterEngine),
119     d(new QmlEnginePrivate(this))
120 {
121     setObjectName(QLatin1String("QmlEngine"));
122
123     ExtensionSystem::PluginManager *pluginManager =
124         ExtensionSystem::PluginManager::instance();
125     pluginManager->addObject(this);
126
127     connect(&d->m_adapter, SIGNAL(connectionError(QAbstractSocket::SocketError)),
128         SLOT(connectionError(QAbstractSocket::SocketError)));
129     connect(&d->m_adapter, SIGNAL(serviceConnectionError(QString)),
130         SLOT(serviceConnectionError(QString)));
131     connect(&d->m_adapter, SIGNAL(connected()),
132         SLOT(connectionEstablished()));
133     connect(&d->m_adapter, SIGNAL(connectionStartupFailed()),
134         SLOT(connectionStartupFailed()));
135
136     connect(&d->m_applicationLauncher,
137         SIGNAL(processExited(int)),
138         SLOT(disconnected()));
139     connect(&d->m_applicationLauncher,
140         SIGNAL(appendMessage(QString, Utils::OutputFormat)),
141         SLOT(appendMessage(QString, Utils::OutputFormat)));
142     connect(&d->m_applicationLauncher,
143             SIGNAL(processStarted()),
144             &d->m_noDebugOutputTimer,
145             SLOT(start()));
146
147     // Only wait 8 seconds for the 'Waiting for connection' on application ouput, then just try to connect
148     // (application output might be redirected / blocked)
149     d->m_noDebugOutputTimer.setSingleShot(true);
150     d->m_noDebugOutputTimer.setInterval(8000);
151     connect(&d->m_noDebugOutputTimer, SIGNAL(timeout()), this, SLOT(beginConnection()));
152 }
153
154 QmlEngine::~QmlEngine()
155 {
156     ExtensionSystem::PluginManager *pluginManager =
157         ExtensionSystem::PluginManager::instance();
158
159     if (pluginManager->allObjects().contains(this)) {
160         pluginManager->removeObject(this);
161     }
162
163     delete d;
164 }
165
166 void QmlEngine::setupInferior()
167 {
168     QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
169
170     if (startParameters().startMode == AttachToRemoteServer) {
171         emit requestRemoteSetup();
172         if (startParameters().qmlServerPort != quint16(-1))
173             notifyInferiorSetupOk();
174     } if (startParameters().startMode == AttachToQmlPort) {
175             notifyInferiorSetupOk();
176
177     } else {
178         d->m_applicationLauncher.setEnvironment(startParameters().environment);
179         d->m_applicationLauncher.setWorkingDirectory(startParameters().workingDirectory);
180
181         notifyInferiorSetupOk();
182     }
183 }
184
185 void QmlEngine::appendMessage(const QString &msg, Utils::OutputFormat /* format */)
186 {
187     showMessage(msg, AppOutput); // FIXME: Redirect to RunControl
188 }
189
190 void QmlEngine::connectionEstablished()
191 {
192     attemptBreakpointSynchronization();
193
194     showMessage(tr("QML Debugger connected."), StatusBar);
195
196     if (!watchHandler()->watcherNames().isEmpty()) {
197         synchronizeWatchers();
198     }
199     connect(watchersModel(),SIGNAL(layoutChanged()),this,SLOT(synchronizeWatchers()));
200
201     notifyEngineRunAndInferiorRunOk();
202 }
203
204 void QmlEngine::beginConnection()
205 {
206     d->m_noDebugOutputTimer.stop();
207     showMessage(tr("QML Debugger connecting..."), StatusBar);
208     d->m_adapter.beginConnection();
209 }
210
211 void QmlEngine::connectionStartupFailed()
212 {
213     Core::ICore * const core = Core::ICore::instance();
214     QMessageBox *infoBox = new QMessageBox(core->mainWindow());
215     infoBox->setIcon(QMessageBox::Critical);
216     infoBox->setWindowTitle(tr("Qt Creator"));
217     infoBox->setText(tr("Could not connect to the in-process QML debugger.\n"
218                         "Do you want to retry?"));
219     infoBox->setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel | QMessageBox::Help);
220     infoBox->setDefaultButton(QMessageBox::Retry);
221     infoBox->setModal(true);
222
223     connect(infoBox, SIGNAL(finished(int)),
224             this, SLOT(retryMessageBoxFinished(int)));
225
226     infoBox->show();
227 }
228
229 void QmlEngine::retryMessageBoxFinished(int result)
230 {
231     switch (result) {
232     case QMessageBox::Retry: {
233         beginConnection();
234         break;
235     }
236     case QMessageBox::Help: {
237         Core::HelpManager *helpManager = Core::HelpManager::instance();
238         helpManager->handleHelpRequest("qthelp://com.nokia.qtcreator/doc/creator-debugging-qml.html");
239         // fall through
240     }
241     default:
242         notifyEngineRunFailed();
243         break;
244     }
245 }
246
247 void QmlEngine::connectionError(QAbstractSocket::SocketError socketError)
248 {
249     if (socketError == QAbstractSocket::RemoteHostClosedError)
250         showMessage(tr("QML Debugger: Remote host closed connection."), StatusBar);
251
252     notifyInferiorSpontaneousStop();
253     notifyInferiorIll();
254 }
255
256 void QmlEngine::serviceConnectionError(const QString &serviceName)
257 {
258     showMessage(tr("QML Debugger: Could not connect to service '%1'.")
259         .arg(serviceName), StatusBar);
260 }
261
262 bool QmlEngine::canDisplayTooltip() const
263 {
264     return state() == InferiorRunOk || state() == InferiorStopOk;
265 }
266
267 void QmlEngine::filterApplicationMessage(const QString &msg, int /*channel*/)
268 {
269     static const QString qddserver = QLatin1String("QDeclarativeDebugServer: ");
270     static const QString cannotRetrieveDebuggingOutput = ApplicationLauncher::msgWinCannotRetrieveDebuggingOutput();
271
272     const int index = msg.indexOf(qddserver);
273     if (index != -1) {
274         // we're actually getting debug output
275         d->m_noDebugOutputTimer.stop();
276
277         QString status = msg;
278         status.remove(0, index + qddserver.length()); // chop of 'QDeclarativeDebugServer: '
279
280         static QString waitingForConnection = QLatin1String("Waiting for connection ");
281         static QString unableToListen = QLatin1String("Unable to listen ");
282         static QString debuggingNotEnabled = QLatin1String("Ignoring \"-qmljsdebugger=");
283         static QString debuggingNotEnabled2 = QLatin1String("Ignoring\"-qmljsdebugger="); // There is (was?) a bug in one of the error strings - safest to handle both
284         static QString connectionEstablished = QLatin1String("Connection established");
285
286         QString errorMessage;
287         if (status.startsWith(waitingForConnection)) {
288             beginConnection();
289         } else if (status.startsWith(unableToListen)) {
290             //: Error message shown after 'Could not connect ... debugger:"
291             errorMessage = tr("The port seems to be in use.");
292         } else if (status.startsWith(debuggingNotEnabled) || status.startsWith(debuggingNotEnabled2)) {
293             //: Error message shown after 'Could not connect ... debugger:"
294             errorMessage = tr("The application is not set up for QML/JS debugging.");
295         } else if (status.startsWith(connectionEstablished)) {
296             // nothing to do
297         } else {
298             qWarning() << "Unknown QDeclarativeDebugServer status message: " << status;
299         }
300
301         if (!errorMessage.isEmpty()) {
302             notifyEngineRunFailed();
303
304             Core::ICore * const core = Core::ICore::instance();
305             QMessageBox *infoBox = new QMessageBox(core->mainWindow());
306             infoBox->setIcon(QMessageBox::Critical);
307             infoBox->setWindowTitle(tr("Qt Creator"));
308             //: %1 is detailed error message
309             infoBox->setText(tr("Could not connect to the in-process QML debugger:\n%1")
310                              .arg(errorMessage));
311             infoBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Help);
312             infoBox->setDefaultButton(QMessageBox::Ok);
313             infoBox->setModal(true);
314
315             connect(infoBox, SIGNAL(finished(int)),
316                     this, SLOT(wrongSetupMessageBoxFinished(int)));
317
318             infoBox->show();
319         }
320     } else if (msg.contains(cannotRetrieveDebuggingOutput)) {
321         // we won't get debugging output, so just try to connect ...
322         beginConnection();
323     }
324 }
325
326 void QmlEngine::showMessage(const QString &msg, int channel, int timeout) const
327 {
328     if (channel == AppOutput || channel == AppError) {
329         const_cast<QmlEngine*>(this)->filterApplicationMessage(msg, channel);
330     }
331     DebuggerEngine::showMessage(msg, channel, timeout);
332 }
333
334 void QmlEngine::closeConnection()
335 {
336     disconnect(watchersModel(),SIGNAL(layoutChanged()),this,SLOT(synchronizeWatchers()));
337     d->m_adapter.closeConnection();
338 }
339
340 void QmlEngine::runEngine()
341 {
342     QTC_ASSERT(state() == EngineRunRequested, qDebug() << state());
343
344     if (!isSlaveEngine() && startParameters().startMode != AttachToRemoteServer
345             && startParameters().startMode != AttachToQmlPort)
346         startApplicationLauncher();
347
348     if (startParameters().startMode == AttachToQmlPort)
349         beginConnection();
350 }
351
352 void QmlEngine::startApplicationLauncher()
353 {
354     if (!d->m_applicationLauncher.isRunning()) {
355         appendMessage(tr("Starting %1 %2").arg(
356                           QDir::toNativeSeparators(startParameters().executable),
357                           startParameters().processArgs)
358                       + QLatin1Char('\n')
359                      , Utils::NormalMessageFormat);
360         d->m_applicationLauncher.start(ApplicationLauncher::Gui,
361                                     startParameters().executable,
362                                     startParameters().processArgs);
363     }
364 }
365
366 void QmlEngine::stopApplicationLauncher()
367 {
368     if (d->m_applicationLauncher.isRunning()) {
369         disconnect(&d->m_applicationLauncher, SIGNAL(processExited(int)), this, SLOT(disconnected()));
370         d->m_applicationLauncher.stop();
371     }
372 }
373
374 void QmlEngine::handleRemoteSetupDone(int gdbServerPort, int qmlPort)
375 {
376     Q_UNUSED(gdbServerPort);
377     if (qmlPort != -1)
378         startParameters().qmlServerPort = qmlPort;
379     notifyInferiorSetupOk();
380 }
381
382 void QmlEngine::handleRemoteSetupFailed(const QString &message)
383 {
384     QMessageBox::critical(0,tr("Failed to start application"),
385         tr("Application startup failed: %1").arg(message));
386     notifyInferiorSetupFailed();
387 }
388
389 void QmlEngine::shutdownInferior()
390 {
391     d->m_noDebugOutputTimer.stop();
392
393     if (d->m_adapter.activeDebuggerClient())
394         d->m_adapter.activeDebuggerClient()->endSession();
395
396     if (isSlaveEngine()) {
397         resetLocation();
398     }
399     stopApplicationLauncher();
400
401     notifyInferiorShutdownOk();
402 }
403
404 void QmlEngine::shutdownEngine()
405 {
406     closeConnection();
407
408     // double check (ill engine?):
409     stopApplicationLauncher();
410
411     notifyEngineShutdownOk();
412     if (!isSlaveEngine())
413         showMessage(QString(), StatusBar);
414 }
415
416 void QmlEngine::setupEngine()
417 {
418     connect(&d->m_applicationLauncher, SIGNAL(bringToForegroundRequested(qint64)),
419             runControl(), SLOT(bringApplicationToForeground(qint64)),
420             Qt::UniqueConnection);
421
422     notifyEngineSetupOk();
423 }
424
425 void QmlEngine::continueInferior()
426 {
427     QTC_ASSERT(state() == InferiorStopOk, qDebug() << state());
428     if (d->m_adapter.activeDebuggerClient()) {
429         logMessage(LogSend, "CONTINUE");
430         d->m_adapter.activeDebuggerClient()->continueInferior();
431     }
432     resetLocation();
433     notifyInferiorRunRequested();
434     notifyInferiorRunOk();
435 }
436
437 void QmlEngine::interruptInferior()
438 {
439     if (d->m_adapter.activeDebuggerClient()) {
440         logMessage(LogSend, "INTERRUPT");
441         d->m_adapter.activeDebuggerClient()->interruptInferior();
442     }
443     notifyInferiorStopOk();
444 }
445
446 void QmlEngine::executeStep()
447 {
448     if (d->m_adapter.activeDebuggerClient()) {
449         logMessage(LogSend, "STEPINTO");
450         d->m_adapter.activeDebuggerClient()->executeStep();
451     }
452     resetLocation();
453     notifyInferiorRunRequested();
454     notifyInferiorRunOk();
455 }
456
457 void QmlEngine::executeStepI()
458 {
459     if (d->m_adapter.activeDebuggerClient()) {
460         logMessage(LogSend, "STEPINTO");
461         d->m_adapter.activeDebuggerClient()->executeStepI();
462     }
463     resetLocation();
464     notifyInferiorRunRequested();
465     notifyInferiorRunOk();
466 }
467
468 void QmlEngine::executeStepOut()
469 {
470     if (d->m_adapter.activeDebuggerClient()) {
471         logMessage(LogSend, "STEPOUT");
472         d->m_adapter.activeDebuggerClient()->executeStepOut();
473     }
474     resetLocation();
475     notifyInferiorRunRequested();
476     notifyInferiorRunOk();
477 }
478
479 void QmlEngine::executeNext()
480 {
481     if (d->m_adapter.activeDebuggerClient()) {
482         logMessage(LogSend, "STEPOVER");
483         d->m_adapter.activeDebuggerClient()->executeNext();
484     }
485     resetLocation();
486     notifyInferiorRunRequested();
487     notifyInferiorRunOk();
488 }
489
490 void QmlEngine::executeNextI()
491 {
492     SDEBUG("QmlEngine::executeNextI()");
493 }
494
495 void QmlEngine::executeRunToLine(const ContextData &data)
496 {
497     Q_UNUSED(data)
498     SDEBUG("FIXME:  QmlEngine::executeRunToLine()");
499 }
500
501 void QmlEngine::executeRunToFunction(const QString &functionName)
502 {
503     Q_UNUSED(functionName)
504     XSDEBUG("FIXME:  QmlEngine::executeRunToFunction()");
505 }
506
507 void QmlEngine::executeJumpToLine(const ContextData &data)
508 {
509     Q_UNUSED(data)
510     XSDEBUG("FIXME:  QmlEngine::executeJumpToLine()");
511 }
512
513 void QmlEngine::activateFrame(int index)
514 {
515     if (d->m_adapter.activeDebuggerClient()) {
516         logMessage(LogSend, QString("%1 %2").arg(QString("ACTIVATE_FRAME"), QString::number(index)));
517         d->m_adapter.activeDebuggerClient()->activateFrame(index);
518     }
519     gotoLocation(stackHandler()->frames().value(index));
520 }
521
522 void QmlEngine::selectThread(int index)
523 {
524     Q_UNUSED(index)
525 }
526
527 void QmlEngine::insertBreakpoint(BreakpointModelId id)
528 {
529     BreakHandler *handler = breakHandler();
530     BreakpointState state = handler->state(id);
531     QTC_ASSERT(state == BreakpointInsertRequested, qDebug() << id << this << state);
532     handler->notifyBreakpointInsertProceeding(id);
533
534     if (d->m_adapter.activeDebuggerClient()) {
535         d->m_adapter.activeDebuggerClient()->insertBreakpoint(id);
536     } else {
537         foreach (QmlDebuggerClient *client, d->m_adapter.debuggerClients()) {
538             client->insertBreakpoint(id);
539         }
540     }
541 }
542
543 void QmlEngine::removeBreakpoint(BreakpointModelId id)
544 {
545     BreakHandler *handler = breakHandler();
546     BreakpointState state = handler->state(id);
547     QTC_ASSERT(state == BreakpointRemoveRequested, qDebug() << id << this << state);
548     handler->notifyBreakpointRemoveProceeding(id);
549
550     if (d->m_adapter.activeDebuggerClient()) {
551         d->m_adapter.activeDebuggerClient()->removeBreakpoint(id);
552     } else {
553         foreach (QmlDebuggerClient *client, d->m_adapter.debuggerClients()) {
554             client->removeBreakpoint(id);
555         }
556     }
557
558     if (handler->state(id) == BreakpointRemoveProceeding) {
559         handler->notifyBreakpointRemoveOk(id);
560     }
561 }
562
563 void QmlEngine::changeBreakpoint(BreakpointModelId id)
564 {
565     BreakHandler *handler = breakHandler();
566     BreakpointState state = handler->state(id);
567     QTC_ASSERT(state == BreakpointChangeRequested, qDebug() << id << this << state);
568     handler->notifyBreakpointChangeProceeding(id);
569
570     if (d->m_adapter.activeDebuggerClient()) {
571         d->m_adapter.activeDebuggerClient()->changeBreakpoint(id);
572     } else {
573         foreach (QmlDebuggerClient *client, d->m_adapter.debuggerClients()) {
574             client->changeBreakpoint(id);
575         }
576     }
577
578     if (handler->state(id) == BreakpointChangeProceeding) {
579         handler->notifyBreakpointChangeOk(id);
580     }
581 }
582
583 void QmlEngine::attemptBreakpointSynchronization()
584 {
585     if (!stateAcceptsBreakpointChanges()) {
586         showMessage(_("BREAKPOINT SYNCHRONIZATION NOT POSSIBLE IN CURRENT STATE"));
587         return;
588     }
589
590     BreakHandler *handler = breakHandler();
591
592     foreach (BreakpointModelId id, handler->unclaimedBreakpointIds()) {
593         // Take ownership of the breakpoint. Requests insertion.
594         if (acceptsBreakpoint(id))
595             handler->setEngine(id, this);
596     }
597
598     foreach (BreakpointModelId id, handler->engineBreakpointIds(this)) {
599         switch (handler->state(id)) {
600         case BreakpointNew:
601             // Should not happen once claimed.
602             QTC_CHECK(false);
603             continue;
604         case BreakpointInsertRequested:
605             insertBreakpoint(id);
606             continue;
607         case BreakpointChangeRequested:
608             changeBreakpoint(id);
609             continue;
610         case BreakpointRemoveRequested:
611             removeBreakpoint(id);
612             continue;
613         case BreakpointChangeProceeding:
614         case BreakpointInsertProceeding:
615         case BreakpointRemoveProceeding:
616         case BreakpointInserted:
617         case BreakpointDead:
618             continue;
619         }
620         QTC_ASSERT(false, qDebug() << "UNKNOWN STATE"  << id << state());
621     }
622
623     DebuggerEngine::attemptBreakpointSynchronization();
624
625     if (d->m_adapter.activeDebuggerClient()) {
626         d->m_adapter.activeDebuggerClient()->updateBreakpoints();
627     } else {
628         foreach (QmlDebuggerClient *client, d->m_adapter.debuggerClients()) {
629             client->updateBreakpoints();
630         }
631     }
632 }
633
634 bool QmlEngine::acceptsBreakpoint(BreakpointModelId id) const
635 {
636     if (!DebuggerEngine::isCppBreakpoint(breakHandler()->breakpointData(id)))
637             return true;
638
639     //If it is a Cpp Breakpoint query if the type can be also handled by the debugger client
640     //TODO: enable setting of breakpoints before start of debug session
641     //For now, the event breakpoint can be set after the activeDebuggerClient is known
642     //This is because the older client does not support BreakpointOnQmlSignalHandler
643     bool acceptBreakpoint = false;
644     if (d->m_adapter.activeDebuggerClient()) {
645         acceptBreakpoint = d->m_adapter.activeDebuggerClient()->acceptsBreakpoint(id);
646     }
647     return acceptBreakpoint;
648 }
649
650 void QmlEngine::loadSymbols(const QString &moduleName)
651 {
652     Q_UNUSED(moduleName)
653 }
654
655 void QmlEngine::loadAllSymbols()
656 {
657 }
658
659 void QmlEngine::reloadModules()
660 {
661 }
662
663 void QmlEngine::requestModuleSymbols(const QString &moduleName)
664 {
665     Q_UNUSED(moduleName)
666 }
667
668 //////////////////////////////////////////////////////////////////////
669 //
670 // Tooltip specific stuff
671 //
672 //////////////////////////////////////////////////////////////////////
673
674 bool QmlEngine::setToolTipExpression(const QPoint &mousePos,
675     TextEditor::ITextEditor *editor, const DebuggerToolTipContext &ctx)
676 {
677     // This is processed by QML inspector, which has dependencies to 
678     // the qml js editor. Makes life easier.
679     emit tooltipRequested(mousePos, editor, ctx.position);
680     return true;
681 }
682
683 //////////////////////////////////////////////////////////////////////
684 //
685 // Watch specific stuff
686 //
687 //////////////////////////////////////////////////////////////////////
688
689 void QmlEngine::assignValueInDebugger(const WatchData *data,
690     const QString &expression, const QVariant &valueV)
691 {
692     quint64 objectId =  data->id;
693     if (objectId > 0 && !expression.isEmpty() && d->m_adapter.activeDebuggerClient()) {
694         logMessage(LogSend, QString("%1 %2 %3 %4 %5").arg(
695                        QString("SET_PROPERTY"), QString::number(objectId), QString(expression),
696                        valueV.toString()));
697         d->m_adapter.activeDebuggerClient()->assignValueInDebugger(expression.toUtf8(), objectId, expression, valueV.toString());
698     }
699 }
700
701 void QmlEngine::updateWatchData(const WatchData &data,
702     const WatchUpdateFlags &)
703 {
704 //    qDebug() << "UPDATE WATCH DATA" << data.toString();
705     //watchHandler()->rebuildModel();
706     showStatusMessage(tr("Stopped."), 5000);
707
708     if (!data.name.isEmpty() && d->m_adapter.activeDebuggerClient()) {
709         if (data.isValueNeeded()) {
710             logMessage(LogSend, QString("%1 %2 %3").arg(QString("EXEC"), QString(data.iname),
711                                                         QString(data.name)));
712             d->m_adapter.activeDebuggerClient()->updateWatchData(&data);
713         }
714         if (data.isChildrenNeeded()
715                 && watchHandler()->isExpandedIName(data.iname)) {
716             d->m_adapter.activeDebuggerClient()->expandObject(data.iname, data.id);
717         }
718     }
719
720     synchronizeWatchers();
721
722     if (!data.isSomethingNeeded())
723         watchHandler()->insertData(data);
724 }
725
726 void QmlEngine::synchronizeWatchers()
727 {
728     QStringList watchedExpressions = watchHandler()->watchedExpressions();
729     // send watchers list
730     logMessage(LogSend, QString("%1 %2").arg(
731                    QString("WATCH_EXPRESSIONS"), watchedExpressions.join(", ")));
732     if (d->m_adapter.activeDebuggerClient()) {
733         d->m_adapter.activeDebuggerClient()->synchronizeWatchers(watchedExpressions);
734     } else {
735         foreach (QmlDebuggerClient *client, d->m_adapter.debuggerClients())
736             client->synchronizeWatchers(watchedExpressions);
737     }
738 }
739
740 unsigned QmlEngine::debuggerCapabilities() const
741 {
742     return AddWatcherCapability|AddWatcherWhileRunningCapability;
743     /*ReverseSteppingCapability | SnapshotCapability
744         | AutoDerefPointersCapability | DisassemblerCapability
745         | RegisterCapability | ShowMemoryCapability
746         | JumpToLineCapability | ReloadModuleCapability
747         | ReloadModuleSymbolsCapability | BreakOnThrowAndCatchCapability
748         | ReturnFromFunctionCapability
749         | CreateFullBacktraceCapability
750         | WatchpointCapability
751         | AddWatcherCapability;*/
752 }
753
754 QString QmlEngine::toFileInProject(const QUrl &fileUrl)
755 {
756     if (startParameters().startMode != AttachToQmlPort) {
757         if (d->fileFinder.projectDirectory().isEmpty()) {
758             d->fileFinder.setProjectDirectory(startParameters().projectSourceDirectory);
759             d->fileFinder.setProjectFiles(startParameters().projectSourceFiles);
760         }
761     }
762
763     return d->fileFinder.findFile(fileUrl);
764 }
765
766 void QmlEngine::inferiorSpontaneousStop()
767 {
768     if (state() == InferiorRunOk)
769         notifyInferiorSpontaneousStop();
770 }
771
772 void QmlEngine::disconnected()
773 {
774     showMessage(tr("QML Debugger disconnected."), StatusBar);
775     notifyInferiorExited();
776 }
777
778 void QmlEngine::wrongSetupMessageBoxFinished(int result)
779 {
780     if (result == QMessageBox::Help) {
781         Core::HelpManager *helpManager = Core::HelpManager::instance();
782         helpManager->handleHelpRequest(
783                     QLatin1String("qthelp://com.nokia.qtcreator/doc/creator-debugging-qml.html"));
784     }
785 }
786
787 void QmlEngine::executeDebuggerCommand(const QString& command)
788 {
789     if (d->m_adapter.activeDebuggerClient()) {
790         logMessage(LogSend, QString("%1 %2 %3").arg(QString("EXEC"), QString("console"),
791                                                           QString(command)));
792         d->m_adapter.activeDebuggerClient()->executeDebuggerCommand(command);
793     }
794 }
795
796
797 QString QmlEngine::qmlImportPath() const
798 {
799     return startParameters().environment.value("QML_IMPORT_PATH");
800 }
801
802 void QmlEngine::logMessage(LogDirection direction, const QString &message)
803 {
804     QString msg = "QmlDebugger";
805     if (direction == LogSend) {
806         msg += " sending ";
807     } else {
808         msg += " receiving ";
809     }
810     msg += message;
811     showMessage(msg, LogDebug);
812 }
813
814 QmlEngine *createQmlEngine(const DebuggerStartParameters &sp,
815     DebuggerEngine *masterEngine)
816 {
817     return new QmlEngine(sp, masterEngine);
818 }
819
820 } // namespace Internal
821 } // namespace Debugger
822