source: trunk/src/scripttools/debugging/qscriptdebuggerfrontend.cpp@ 160

Last change on this file since 160 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 7.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtSCriptTools module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qscriptdebuggerfrontend_p.h"
43#include "qscriptdebuggerfrontend_p_p.h"
44#include "qscriptdebuggercommand_p.h"
45#include "qscriptdebuggerevent_p.h"
46#include "qscriptdebuggerresponse_p.h"
47#include "qscriptdebuggereventhandlerinterface_p.h"
48#include "qscriptdebuggerresponsehandlerinterface_p.h"
49
50#include <QtCore/qcoreevent.h>
51#include <QtCore/qcoreapplication.h>
52
53QT_BEGIN_NAMESPACE
54
55/*!
56 \class QScriptDebuggerFrontend
57 \since 4.5
58 \internal
59
60 \brief The QScriptDebuggerFrontend class is the base class of debugger front-ends.
61*/
62
63// helper class that's used to handle our custom Qt events
64class QScriptDebuggerFrontendEventReceiver : public QObject
65{
66public:
67 QScriptDebuggerFrontendEventReceiver(QScriptDebuggerFrontendPrivate *frontend,
68 QObject *parent = 0);
69 ~QScriptDebuggerFrontendEventReceiver();
70
71 bool event(QEvent *);
72
73private:
74 QScriptDebuggerFrontendPrivate *m_frontend;
75};
76
77QScriptDebuggerFrontendEventReceiver::QScriptDebuggerFrontendEventReceiver(
78 QScriptDebuggerFrontendPrivate *frontend, QObject *parent)
79 : QObject(parent), m_frontend(frontend)
80{
81}
82
83QScriptDebuggerFrontendEventReceiver::~QScriptDebuggerFrontendEventReceiver()
84{
85}
86
87bool QScriptDebuggerFrontendEventReceiver::event(QEvent *e)
88{
89 return m_frontend->event(e);
90}
91
92
93QScriptDebuggerFrontendPrivate::QScriptDebuggerFrontendPrivate()
94{
95 eventHandler = 0;
96 nextCommandId = 0;
97 eventReceiver = new QScriptDebuggerFrontendEventReceiver(this);
98}
99
100QScriptDebuggerFrontendPrivate::~QScriptDebuggerFrontendPrivate()
101{
102 delete eventReceiver;
103}
104
105void QScriptDebuggerFrontendPrivate::postEvent(QEvent *e)
106{
107 QCoreApplication::postEvent(eventReceiver, e);
108}
109
110bool QScriptDebuggerFrontendPrivate::event(QEvent *e)
111{
112 Q_Q(QScriptDebuggerFrontend);
113 if (e->type() == QEvent::User+1) {
114 QScriptDebuggerEventEvent *de = static_cast<QScriptDebuggerEventEvent*>(e);
115 bool handled = q->notifyEvent(de->event());
116 if (handled) {
117 q->scheduleCommand(QScriptDebuggerCommand::resumeCommand(),
118 /*responseHandler=*/0);
119 }
120 return true;
121 } else if (e->type() == QEvent::User+2) {
122 processCommands();
123 return true;
124 }
125 return false;
126}
127
128void QScriptDebuggerFrontendPrivate::processCommands()
129{
130 Q_Q(QScriptDebuggerFrontend);
131 while (!pendingCommands.isEmpty()) {
132 QScriptDebuggerCommand command(pendingCommands.takeFirst());
133 int id = pendingCommandIds.takeFirst();
134 q->processCommand(id, command);
135 }
136}
137
138QScriptDebuggerFrontend::QScriptDebuggerFrontend()
139 : d_ptr(new QScriptDebuggerFrontendPrivate())
140{
141 d_ptr->q_ptr = this;
142}
143
144QScriptDebuggerFrontend::~QScriptDebuggerFrontend()
145{
146 delete d_ptr;
147}
148
149QScriptDebuggerFrontend::QScriptDebuggerFrontend(QScriptDebuggerFrontendPrivate &dd)
150 : d_ptr(&dd)
151{
152 d_ptr->q_ptr = this;
153}
154
155QScriptDebuggerEventHandlerInterface *QScriptDebuggerFrontend::eventHandler() const
156{
157 Q_D(const QScriptDebuggerFrontend);
158 return d->eventHandler;
159}
160
161void QScriptDebuggerFrontend::setEventHandler(QScriptDebuggerEventHandlerInterface *eventHandler)
162{
163 Q_D(QScriptDebuggerFrontend);
164 d->eventHandler = eventHandler;
165}
166
167/*!
168 Schedules the given \a command for execution by this front-end,
169 and returns a unique identifier associated with this command.
170
171 Subclasses can call this function to schedule custom commands.
172
173 \sa notifyCommandFinished()
174*/
175int QScriptDebuggerFrontend::scheduleCommand(
176 const QScriptDebuggerCommand &command,
177 QScriptDebuggerResponseHandlerInterface *responseHandler)
178{
179 Q_D(QScriptDebuggerFrontend);
180 int id = ++d->nextCommandId;
181 d->pendingCommands.append(command);
182 d->pendingCommandIds.append(id);
183 if (responseHandler)
184 d->responseHandlers.insert(id, responseHandler);
185 if (d->pendingCommands.size() == 1) {
186 QEvent *e = new QEvent(QEvent::Type(QEvent::User+2)); // ProcessCommands
187 d->postEvent(e);
188 }
189 return id;
190}
191
192/*!
193 Subclasses should call this function when the command identified by
194 the given \a id has finished and produced the given \a response.
195
196 \sa processCommand(), notifyEvent()
197*/
198void QScriptDebuggerFrontend::notifyCommandFinished(int id, const QScriptDebuggerResponse &response)
199{
200 Q_D(QScriptDebuggerFrontend);
201 if (d->responseHandlers.contains(id)) {