Fixes a crash in QDoubleSpinBox
[qt-netbsd.git] / src / scripttools / debugging / qscriptbreakpointsmodel.cpp
blob3da236fb0eab2171c4da8356e62ed12e8721c294
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial Usage
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.
38 ** $QT_END_LICENSE$
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>
53 QT_BEGIN_NAMESPACE
55 /*!
56 \since 4.5
57 \class QScriptBreakpointsModel
58 \internal
61 class QScriptBreakpointsModelPrivate
62 : public QAbstractItemModelPrivate
64 Q_DECLARE_PUBLIC(QScriptBreakpointsModel)
65 public:
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,
85 QObject *parent)
86 : QAbstractItemModel(*new QScriptBreakpointsModelPrivate, parent)
88 Q_D(QScriptBreakpointsModel);
89 d->jobScheduler = jobScheduler;
90 d->commandScheduler = commandScheduler;
93 QScriptBreakpointsModel::~QScriptBreakpointsModel()
97 namespace
100 class SetBreakpointJob : public QScriptDebuggerCommandSchedulerJob
102 public:
103 SetBreakpointJob(const QScriptBreakpointData &data,
104 QScriptDebuggerCommandSchedulerInterface *scheduler)
105 : QScriptDebuggerCommandSchedulerJob(scheduler),
106 m_data(data)
109 void start()
111 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
112 frontend.scheduleSetBreakpoint(m_data);
115 void handleResponse(const QScriptDebuggerResponse &, int)
117 finish();
120 private:
121 QScriptBreakpointData m_data;
124 } // namespace
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
129 successfully set.
131 void QScriptBreakpointsModel::setBreakpoint(const QScriptBreakpointData &data)
133 Q_D(QScriptBreakpointsModel);
134 QScriptDebuggerJob *job = new SetBreakpointJob(data, d->commandScheduler);
135 d->jobScheduler->scheduleJob(job);
138 namespace
141 class SetBreakpointDataJob : public QScriptDebuggerCommandSchedulerJob
143 public:
144 SetBreakpointDataJob(int id, const QScriptBreakpointData &data,
145 QScriptDebuggerCommandSchedulerInterface *scheduler)
146 : QScriptDebuggerCommandSchedulerJob(scheduler),
147 m_id(id), m_data(data)
150 void start()
152 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
153 frontend.scheduleSetBreakpointData(m_id, m_data);
156 void handleResponse(const QScriptDebuggerResponse &, int)
158 finish();
161 private:
162 int m_id;
163 QScriptBreakpointData m_data;
166 } // namespace
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);
180 namespace
183 class DeleteBreakpointJob : public QScriptDebuggerCommandSchedulerJob
185 public:
186 DeleteBreakpointJob(int id, QScriptDebuggerCommandSchedulerInterface *scheduler)
187 : QScriptDebuggerCommandSchedulerJob(scheduler),
188 m_id(id)
191 void start()
193 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
194 frontend.scheduleDeleteBreakpoint(m_id);
197 void handleResponse(const QScriptDebuggerResponse &, int)
199 finish();
202 private:
203 int m_id;
206 } // namespace
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));
230 endInsertRows();
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));
243 break;
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
251 with the debugger).
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);
260 endRemoveRows();
261 break;
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;
308 return -1;
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;
320 return -1;
324 \reimp
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);
339 \reimp
341 QModelIndex QScriptBreakpointsModel::parent(const QModelIndex &) const
343 return QModelIndex();
347 \reimp
349 int QScriptBreakpointsModel::columnCount(const QModelIndex &parent) const
351 if (!parent.isValid())
352 return 6;
353 return 0;
357 \reimp
359 int QScriptBreakpointsModel::rowCount(const QModelIndex &parent) const
361 Q_D(const QScriptBreakpointsModel);
362 if (!parent.isValid())
363 return d->breakpoints.size();
364 return 0;
368 \reimp
370 QVariant QScriptBreakpointsModel::data(const QModelIndex &index, int role) const
372 Q_D(const QScriptBreakpointsModel);
373 if (!index.isValid() || (index.row() >= d->breakpoints.size()))
374 return QVariant();
375 const QPair<int, QScriptBreakpointData> &item = d->breakpoints.at(index.row());
376 if (role == Qt::DisplayRole) {
377 if (index.column() == 0)
378 return item.first;
379 else if (index.column() == 1) {
380 QString loc = item.second.fileName();
381 if (loc.isEmpty())
382 loc = QString::fromLatin1("<anonymous script, id=%0>").arg(item.second.scriptId());
383 loc.append(QString::fromLatin1(":%0").arg(item.second.lineNumber()));
384 return loc;
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();
406 return QVariant();
410 \reimp
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()))
416 return false;
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;
423 if (col == 0)
424 modifiedData.setEnabled(value.toInt() == Qt::Checked);
425 else
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())
440 return false;
441 QScriptDebuggerJob *job = new SetBreakpointDataJob(item.first, modifiedData, d->commandScheduler);
442 d->jobScheduler->scheduleJob(job);
443 return true;
447 \reimp
449 QVariant QScriptBreakpointsModel::headerData(int section, Qt::Orientation orient, int role) const
451 if (orient == Qt::Horizontal) {
452 if (role == Qt::DisplayRole) {
453 if (section == 0)
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");
467 return QVariant();
471 \reimp
473 Qt::ItemFlags QScriptBreakpointsModel::flags(const QModelIndex &index) const
475 if (!index.isValid())
476 return 0;
477 Qt::ItemFlags ret = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
478 switch (index.column()) {
479 case 0:
480 ret |= Qt::ItemIsUserCheckable;
481 break;
482 case 1:
483 break;
484 case 2:
485 ret |= Qt::ItemIsEditable;
486 break;
487 case 3:
488 ret |= Qt::ItemIsEditable;
489 break;
490 case 4:
491 ret |= Qt::ItemIsUserCheckable;
492 break;
494 return ret;
497 QT_END_NAMESPACE