1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
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.
36 ** If you have questions regarding the use of this file, please contact
37 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qscriptbreakpointsmodel_p.h"
43 #include "qscriptdebuggerjobschedulerinterface_p.h"
44 #include "qscriptdebuggercommandschedulerjob_p.h"
45 #include "qscriptdebuggercommandschedulerfrontend_p.h"
47 #include "private/qabstractitemmodel_p.h"
49 #include <QtCore/qpair.h>
50 #include <QtGui/qicon.h>
51 #include <QtCore/qdebug.h>
57 \class QScriptBreakpointsModel
61 class QScriptBreakpointsModelPrivate
62 : public QAbstractItemModelPrivate
64 Q_DECLARE_PUBLIC(QScriptBreakpointsModel
)
66 QScriptBreakpointsModelPrivate();
67 ~QScriptBreakpointsModelPrivate();
69 QScriptDebuggerJobSchedulerInterface
*jobScheduler
;
70 QScriptDebuggerCommandSchedulerInterface
*commandScheduler
;
71 QList
<QPair
<int, QScriptBreakpointData
> > breakpoints
;
74 QScriptBreakpointsModelPrivate::QScriptBreakpointsModelPrivate()
78 QScriptBreakpointsModelPrivate::~QScriptBreakpointsModelPrivate()
82 QScriptBreakpointsModel::QScriptBreakpointsModel(
83 QScriptDebuggerJobSchedulerInterface
*jobScheduler
,
84 QScriptDebuggerCommandSchedulerInterface
*commandScheduler
,
86 : QAbstractItemModel(*new QScriptBreakpointsModelPrivate
, parent
)
88 Q_D(QScriptBreakpointsModel
);
89 d
->jobScheduler
= jobScheduler
;
90 d
->commandScheduler
= commandScheduler
;
93 QScriptBreakpointsModel::~QScriptBreakpointsModel()
100 class SetBreakpointJob
: public QScriptDebuggerCommandSchedulerJob
103 SetBreakpointJob(const QScriptBreakpointData
&data
,
104 QScriptDebuggerCommandSchedulerInterface
*scheduler
)
105 : QScriptDebuggerCommandSchedulerJob(scheduler
),
111 QScriptDebuggerCommandSchedulerFrontend
frontend(commandScheduler(), this);
112 frontend
.scheduleSetBreakpoint(m_data
);
115 void handleResponse(const QScriptDebuggerResponse
&, int)
121 QScriptBreakpointData m_data
;
127 Sets a breakpoint defined by the given \a data.
128 A new row will be inserted into the model if the breakpoint could be
131 void QScriptBreakpointsModel::setBreakpoint(const QScriptBreakpointData
&data
)
133 Q_D(QScriptBreakpointsModel
);
134 QScriptDebuggerJob
*job
= new SetBreakpointJob(data
, d
->commandScheduler
);
135 d
->jobScheduler
->scheduleJob(job
);
141 class SetBreakpointDataJob
: public QScriptDebuggerCommandSchedulerJob
144 SetBreakpointDataJob(int id
, const QScriptBreakpointData
&data
,
145 QScriptDebuggerCommandSchedulerInterface
*scheduler
)
146 : QScriptDebuggerCommandSchedulerJob(scheduler
),
147 m_id(id
), m_data(data
)
152 QScriptDebuggerCommandSchedulerFrontend
frontend(commandScheduler(), this);
153 frontend
.scheduleSetBreakpointData(m_id
, m_data
);
156 void handleResponse(const QScriptDebuggerResponse
&, int)
163 QScriptBreakpointData m_data
;
169 Sets the \a data associated with the breakpoint identified by \a id.
170 A dataChanged() signal will be emitted if the breakpoint data could
171 be successfully changed.
173 void QScriptBreakpointsModel::setBreakpointData(int id
, const QScriptBreakpointData
&data
)
175 Q_D(QScriptBreakpointsModel
);
176 QScriptDebuggerJob
*job
= new SetBreakpointDataJob(id
, data
, d
->commandScheduler
);
177 d
->jobScheduler
->scheduleJob(job
);
183 class DeleteBreakpointJob
: public QScriptDebuggerCommandSchedulerJob
186 DeleteBreakpointJob(int id
, QScriptDebuggerCommandSchedulerInterface
*scheduler
)
187 : QScriptDebuggerCommandSchedulerJob(scheduler
),
193 QScriptDebuggerCommandSchedulerFrontend
frontend(commandScheduler(), this);
194 frontend
.scheduleDeleteBreakpoint(m_id
);
197 void handleResponse(const QScriptDebuggerResponse
&, int)
209 Deletes the breakpoint with the given \a id.
210 The corresponding row in the model will be removed if the breakpoint
211 was successfully deleted.
213 void QScriptBreakpointsModel::deleteBreakpoint(int id
)
215 Q_D(QScriptBreakpointsModel
);
216 QScriptDebuggerJob
*job
= new DeleteBreakpointJob(id
, d
->commandScheduler
);
217 d
->jobScheduler
->scheduleJob(job
);
221 Adds a breakpoint to the model. This function does not actually set
222 a breakpoint (i.e. it doesn't communicate with the debugger).
224 void QScriptBreakpointsModel::addBreakpoint(int id
, const QScriptBreakpointData
&data
)
226 Q_D(QScriptBreakpointsModel
);
227 int rowIndex
= d
->breakpoints
.size();
228 beginInsertRows(QModelIndex(), rowIndex
, rowIndex
);
229 d
->breakpoints
.append(qMakePair(id
, data
));
234 Modify the \a data of breakpoint \a id.
236 void QScriptBreakpointsModel::modifyBreakpoint(int id
, const QScriptBreakpointData
&data
)
238 Q_D(QScriptBreakpointsModel
);
239 for (int i
= 0; i
< d
->breakpoints
.size(); ++i
) {
240 if (d
->breakpoints
.at(i
).first
== id
) {
241 d
->breakpoints
[i
] = qMakePair(id
, data
);
242 emit
dataChanged(createIndex(i
, 0), createIndex(i
, columnCount()-1));
249 Remove the breakpoint identified by \a id from the model. This
250 function does not delete the breakpoint (i.e. it doesn't communicate
253 void QScriptBreakpointsModel::removeBreakpoint(int id
)
255 Q_D(QScriptBreakpointsModel
);
256 for (int i
= 0; i
< d
->breakpoints
.size(); ++i
) {
257 if (d
->breakpoints
.at(i
).first
== id
) {
258 beginRemoveRows(QModelIndex(), i
, i
);
259 d
->breakpoints
.removeAt(i
);
267 Returns the id of the breakpoint at the given \a row.
269 int QScriptBreakpointsModel::breakpointIdAt(int row
) const
271 Q_D(const QScriptBreakpointsModel
);
272 return d
->breakpoints
.at(row
).first
;
276 Returns the data for the breakpoint at the given \a row.
278 QScriptBreakpointData
QScriptBreakpointsModel::breakpointDataAt(int row
) const
280 Q_D(const QScriptBreakpointsModel
);
281 return d
->breakpoints
.at(row
).second
;
284 QScriptBreakpointData
QScriptBreakpointsModel::breakpointData(int id
) const
286 Q_D(const QScriptBreakpointsModel
);
287 for (int i
= 0; i
< d
->breakpoints
.size(); ++i
) {
288 if (d
->breakpoints
.at(i
).first
== id
)
289 return d
->breakpoints
.at(i
).second
;
291 return QScriptBreakpointData();
295 Tries to find a breakpoint with the given \a scriptId and \a
296 lineNumber. Returns the id of the first breakpoint that matches, or
297 -1 if no such breakpoint is found.
299 int QScriptBreakpointsModel::resolveBreakpoint(qint64 scriptId
, int lineNumber
) const
301 Q_D(const QScriptBreakpointsModel
);
302 for (int i
= 0; i
< d
->breakpoints
.size(); ++i
) {
303 if ((d
->breakpoints
.at(i
).second
.scriptId() == scriptId
)
304 && (d
->breakpoints
.at(i
).second
.lineNumber() == lineNumber
)) {
305 return d
->breakpoints
.at(i
).first
;
311 int QScriptBreakpointsModel::resolveBreakpoint(const QString
&fileName
, int lineNumber
) const
313 Q_D(const QScriptBreakpointsModel
);
314 for (int i
= 0; i
< d
->breakpoints
.size(); ++i
) {
315 if ((d
->breakpoints
.at(i
).second
.fileName() == fileName
)
316 && (d
->breakpoints
.at(i
).second
.lineNumber() == lineNumber
)) {
317 return d
->breakpoints
.at(i
).first
;
326 QModelIndex
QScriptBreakpointsModel::index(int row
, int column
, const QModelIndex
&parent
) const
328 Q_D(const QScriptBreakpointsModel
);
329 if (parent
.isValid())
330 return QModelIndex();
331 if ((row
< 0) || (row
>= d
->breakpoints
.size()))
332 return QModelIndex();
333 if ((column
< 0) || (column
>= columnCount()))
334 return QModelIndex();
335 return createIndex(row
, column
);
341 QModelIndex
QScriptBreakpointsModel::parent(const QModelIndex
&) const
343 return QModelIndex();
349 int QScriptBreakpointsModel::columnCount(const QModelIndex
&parent
) const
351 if (!parent
.isValid())
359 int QScriptBreakpointsModel::rowCount(const QModelIndex
&parent
) const
361 Q_D(const QScriptBreakpointsModel
);
362 if (!parent
.isValid())
363 return d
->breakpoints
.size();
370 QVariant
QScriptBreakpointsModel::data(const QModelIndex
&index
, int role
) const
372 Q_D(const QScriptBreakpointsModel
);
373 if (!index
.isValid() || (index
.row() >= d
->breakpoints
.size()))
375 const QPair
<int, QScriptBreakpointData
> &item
= d
->breakpoints
.at(index
.row());
376 if (role
== Qt::DisplayRole
) {
377 if (index
.column() == 0)
379 else if (index
.column() == 1) {
380 QString loc
= item
.second
.fileName();
382 loc
= QString::fromLatin1("<anonymous script, id=%0>").arg(item
.second
.scriptId());
383 loc
.append(QString::fromLatin1(":%0").arg(item
.second
.lineNumber()));
385 } else if (index
.column() == 2) {
386 if (!item
.second
.condition().isEmpty())
387 return item
.second
.condition();
388 } else if (index
.column() == 3) {
389 if (item
.second
.ignoreCount() != 0)
390 return item
.second
.ignoreCount();
391 } else if (index
.column() == 5) {
392 return item
.second
.hitCount();
394 } else if (role
== Qt::CheckStateRole
) {
395 if (index
.column() == 0) {
396 return item
.second
.isEnabled() ? Qt::Checked
: Qt::Unchecked
;
397 } else if (index
.column() == 4) {
398 return item
.second
.isSingleShot() ? Qt::Checked
: Qt::Unchecked
;
400 } else if (role
== Qt::EditRole
) {
401 if (index
.column() == 2)
402 return item
.second
.condition();
403 else if (index
.column() == 3)
404 return item
.second
.ignoreCount();
412 bool QScriptBreakpointsModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
414 Q_D(QScriptBreakpointsModel
);
415 if (!index
.isValid() || (index
.row() >= d
->breakpoints
.size()))
417 const QPair
<int, QScriptBreakpointData
> &item
= d
->breakpoints
.at(index
.row());
418 QScriptBreakpointData modifiedData
;
419 int col
= index
.column();
420 if ((col
== 0) || (col
== 4)) {
421 if (role
== Qt::CheckStateRole
) {
422 modifiedData
= item
.second
;
424 modifiedData
.setEnabled(value
.toInt() == Qt::Checked
);
426 modifiedData
.setSingleShot(value
.toInt() == Qt::Checked
);
428 } else if (col
== 2) {
429 if (role
== Qt::EditRole
) {
430 modifiedData
= item
.second
;
431 modifiedData
.setCondition(value
.toString());
433 } else if (col
== 3) {
434 if (role
== Qt::EditRole
) {
435 modifiedData
= item
.second
;
436 modifiedData
.setIgnoreCount(value
.toInt());
439 if (!modifiedData
.isValid())
441 QScriptDebuggerJob
*job
= new SetBreakpointDataJob(item
.first
, modifiedData
, d
->commandScheduler
);
442 d
->jobScheduler
->scheduleJob(job
);
449 QVariant
QScriptBreakpointsModel::headerData(int section
, Qt::Orientation orient
, int role
) const
451 if (orient
== Qt::Horizontal
) {
452 if (role
== Qt::DisplayRole
) {
454 return QObject::tr("ID");
455 else if (section
== 1)
456 return QObject::tr("Location");
457 else if (section
== 2)
458 return QObject::tr("Condition");
459 else if (section
== 3)
460 return QObject::tr("Ignore-count");
461 else if (section
== 4)
462 return QObject::tr("Single-shot");
463 else if (section
== 5)
464 return QObject::tr("Hit-count");
473 Qt::ItemFlags
QScriptBreakpointsModel::flags(const QModelIndex
&index
) const
475 if (!index
.isValid())
477 Qt::ItemFlags ret
= Qt::ItemIsEnabled
| Qt::ItemIsSelectable
;
478 switch (index
.column()) {
480 ret
|= Qt::ItemIsUserCheckable
;
485 ret
|= Qt::ItemIsEditable
;
488 ret
|= Qt::ItemIsEditable
;
491 ret
|= Qt::ItemIsUserCheckable
;