Qt 4.8
qscriptbreakpointsmodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
46 
47 #include "private/qabstractitemmodel_p.h"
48 
49 #include <QtCore/qpair.h>
50 #include <QtCore/qcoreapplication.h>
51 #include <QtGui/qicon.h>
52 #include <QtCore/qdebug.h>
53 
55 
64 {
66 public:
69 
73 };
74 
76 {
77 }
78 
80 {
81 }
82 
86  QObject *parent)
88 {
90  d->jobScheduler = jobScheduler;
91  d->commandScheduler = commandScheduler;
92 }
93 
95 {
96 }
97 
98 namespace
99 {
100 
101 class SetBreakpointJob : public QScriptDebuggerCommandSchedulerJob
102 {
103 public:
104  SetBreakpointJob(const QScriptBreakpointData &data,
107  m_data(data)
108  { }
109 
110  void start()
111  {
112  QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
113  frontend.scheduleSetBreakpoint(m_data);
114  }
115 
116  void handleResponse(const QScriptDebuggerResponse &, int)
117  {
118  finish();
119  }
120 
121 private:
122  QScriptBreakpointData m_data;
123 };
124 
125 } // namespace
126 
133 {
135  QScriptDebuggerJob *job = new SetBreakpointJob(data, d->commandScheduler);
136  d->jobScheduler->scheduleJob(job);
137 }
138 
139 namespace
140 {
141 
142 class SetBreakpointDataJob : public QScriptDebuggerCommandSchedulerJob
143 {
144 public:
145  SetBreakpointDataJob(int id, const QScriptBreakpointData &data,
148  m_id(id), m_data(data)
149  { }
150 
151  void start()
152  {
153  QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
154  frontend.scheduleSetBreakpointData(m_id, m_data);
155  }
156 
157  void handleResponse(const QScriptDebuggerResponse &, int)
158  {
159  finish();
160  }
161 
162 private:
163  int m_id;
164  QScriptBreakpointData m_data;
165 };
166 
167 } // namespace
168 
175 {
177  QScriptDebuggerJob *job = new SetBreakpointDataJob(id, data, d->commandScheduler);
178  d->jobScheduler->scheduleJob(job);
179 }
180 
181 namespace
182 {
183 
184 class DeleteBreakpointJob : public QScriptDebuggerCommandSchedulerJob
185 {
186 public:
187  DeleteBreakpointJob(int id, QScriptDebuggerCommandSchedulerInterface *scheduler)
189  m_id(id)
190  { }
191 
192  void start()
193  {
194  QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
195  frontend.scheduleDeleteBreakpoint(m_id);
196  }
197 
198  void handleResponse(const QScriptDebuggerResponse &, int)
199  {
200  finish();
201  }
202 
203 private:
204  int m_id;
205 };
206 
207 } // namespace
208 
215 {
217  QScriptDebuggerJob *job = new DeleteBreakpointJob(id, d->commandScheduler);
218  d->jobScheduler->scheduleJob(job);
219 }
220 
226 {
228  int rowIndex = d->breakpoints.size();
229  beginInsertRows(QModelIndex(), rowIndex, rowIndex);
230  d->breakpoints.append(qMakePair(id, data));
231  endInsertRows();
232 }
233 
238 {
240  for (int i = 0; i < d->breakpoints.size(); ++i) {
241  if (d->breakpoints.at(i).first == id) {
242  d->breakpoints[i] = qMakePair(id, data);
244  break;
245  }
246  }
247 }
248 
255 {
257  for (int i = 0; i < d->breakpoints.size(); ++i) {
258  if (d->breakpoints.at(i).first == id) {
259  beginRemoveRows(QModelIndex(), i, i);
260  d->breakpoints.removeAt(i);
261  endRemoveRows();
262  break;
263  }
264  }
265 }
266 
271 {
273  return d->breakpoints.at(row).first;
274 }
275 
280 {
282  return d->breakpoints.at(row).second;
283 }
284 
286 {
288  for (int i = 0; i < d->breakpoints.size(); ++i) {
289  if (d->breakpoints.at(i).first == id)
290  return d->breakpoints.at(i).second;
291  }
292  return QScriptBreakpointData();
293 }
294 
300 int QScriptBreakpointsModel::resolveBreakpoint(qint64 scriptId, int lineNumber) const
301 {
303  for (int i = 0; i < d->breakpoints.size(); ++i) {
304  if ((d->breakpoints.at(i).second.scriptId() == scriptId)
305  && (d->breakpoints.at(i).second.lineNumber() == lineNumber)) {
306  return d->breakpoints.at(i).first;
307  }
308  }
309  return -1;
310 }
311 
313 {
315  for (int i = 0; i < d->breakpoints.size(); ++i) {
316  if ((d->breakpoints.at(i).second.fileName() == fileName)
317  && (d->breakpoints.at(i).second.lineNumber() == lineNumber)) {
318  return d->breakpoints.at(i).first;
319  }
320  }
321  return -1;
322 }
323 
328 {
330  if (parent.isValid())
331  return QModelIndex();
332  if ((row < 0) || (row >= d->breakpoints.size()))
333  return QModelIndex();
334  if ((column < 0) || (column >= columnCount()))
335  return QModelIndex();
336  return createIndex(row, column);
337 }
338 
343 {
344  return QModelIndex();
345 }
346 
351 {
352  if (!parent.isValid())
353  return 6;
354  return 0;
355 }
356 
361 {
363  if (!parent.isValid())
364  return d->breakpoints.size();
365  return 0;
366 }
367 
372 {
374  if (!index.isValid() || (index.row() >= d->breakpoints.size()))
375  return QVariant();
376  const QPair<int, QScriptBreakpointData> &item = d->breakpoints.at(index.row());
377  if (role == Qt::DisplayRole) {
378  if (index.column() == 0)
379  return item.first;
380  else if (index.column() == 1) {
381  QString loc = item.second.fileName();
382  if (loc.isEmpty())
383  loc = QString::fromLatin1("<anonymous script, id=%0>").arg(item.second.scriptId());
384  loc.append(QString::fromLatin1(":%0").arg(item.second.lineNumber()));
385  return loc;
386  } else if (index.column() == 2) {
387  if (!item.second.condition().isEmpty())
388  return item.second.condition();
389  } else if (index.column() == 3) {
390  if (item.second.ignoreCount() != 0)
391  return item.second.ignoreCount();
392  } else if (index.column() == 5) {
393  return item.second.hitCount();
394  }
395  } else if (role == Qt::CheckStateRole) {
396  if (index.column() == 0) {
397  return item.second.isEnabled() ? Qt::Checked : Qt::Unchecked;
398  } else if (index.column() == 4) {
399  return item.second.isSingleShot() ? Qt::Checked : Qt::Unchecked;
400  }
401  } else if (role == Qt::EditRole) {
402  if (index.column() == 2)
403  return item.second.condition();
404  else if (index.column() == 3)
405  return item.second.ignoreCount();
406  }
407  return QVariant();
408 }
409 
413 bool QScriptBreakpointsModel::setData(const QModelIndex &index, const QVariant &value, int role)
414 {
416  if (!index.isValid() || (index.row() >= d->breakpoints.size()))
417  return false;
418  const QPair<int, QScriptBreakpointData> &item = d->breakpoints.at(index.row());
419  QScriptBreakpointData modifiedData;
420  int col = index.column();
421  if ((col == 0) || (col == 4)) {
422  if (role == Qt::CheckStateRole) {
423  modifiedData = item.second;
424  if (col == 0)
425  modifiedData.setEnabled(value.toInt() == Qt::Checked);
426  else
427  modifiedData.setSingleShot(value.toInt() == Qt::Checked);
428  }
429  } else if (col == 2) {
430  if (role == Qt::EditRole) {
431  modifiedData = item.second;
432  modifiedData.setCondition(value.toString());
433  }
434  } else if (col == 3) {
435  if (role == Qt::EditRole) {
436  modifiedData = item.second;
437  modifiedData.setIgnoreCount(value.toInt());
438  }
439  }
440  if (!modifiedData.isValid())
441  return false;
442  QScriptDebuggerJob *job = new SetBreakpointDataJob(item.first, modifiedData, d->commandScheduler);
443  d->jobScheduler->scheduleJob(job);
444  return true;
445 }
446 
451 {
452  if (orient == Qt::Horizontal) {
453  if (role == Qt::DisplayRole) {
454  if (section == 0)
455  return QCoreApplication::translate("QScriptBreakpointsModel", "ID");
456  else if (section == 1)
457  return QCoreApplication::translate("QScriptBreakpointsModel", "Location");
458  else if (section == 2)
459  return QCoreApplication::translate("QScriptBreakpointsModel", "Condition");
460  else if (section == 3)
461  return QCoreApplication::translate("QScriptBreakpointsModel", "Ignore-count");
462  else if (section == 4)
463  return QCoreApplication::translate("QScriptBreakpointsModel", "Single-shot");
464  else if (section == 5)
465  return QCoreApplication::translate("QScriptBreakpointsModel", "Hit-count");
466  }
467  }
468  return QVariant();
469 }
470 
475 {
476  if (!index.isValid())
477  return 0;
478  Qt::ItemFlags ret = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
479  switch (index.column()) {
480  case 0:
482  break;
483  case 1:
484  break;
485  case 2:
486  ret |= Qt::ItemIsEditable;
487  break;
488  case 3:
489  ret |= Qt::ItemIsEditable;
490  break;
491  case 4:
493  break;
494  }
495  return ret;
496 }
497 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
bool isSingleShot() const
Returns true if the breakpoint is single-shot, false otherwise.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int breakpointIdAt(int row) const
Returns the id of the breakpoint at the given row.
QScriptBreakpointData breakpointDataAt(int row) const
Returns the data for the breakpoint at the given row.
void deleteBreakpoint(int id)
Deletes the breakpoint with the given id.
QScriptDebuggerCommandSchedulerInterface * commandScheduler
The QScriptDebuggerResponse class represents a front-end&#39;s response to a QScriptDebuggerCommand.
int scheduleSetBreakpoint(const QString &fileName, int lineNumber)
T1 first
Definition: qpair.h:65
QScriptBreakpointsModel(QScriptDebuggerJobSchedulerInterface *jobScheduler, QScriptDebuggerCommandSchedulerInterface *commandScheduler, QObject *parent=0)
T2 second
Definition: qpair.h:66
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
int ignoreCount() const
Returns the ignore count of the breakpoint.
QModelIndex createIndex(int row, int column, void *data=0) const
Creates a model index for the given row and column with the internal pointer ptr. ...
void endInsertRows()
Ends a row insertion operation.
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
void setSingleShot(bool singleShot)
Sets the singleShot state of the breakpoint.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
int row() const
Returns the row this model index refers to.
void setIgnoreCount(int count)
Sets the ignore count of the breakpoint.
void modifyBreakpoint(int id, const QScriptBreakpointData &data)
Modify the data of breakpoint id.
#define emit
Definition: qobjectdefs.h:76
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Reimplemented Function
void setEnabled(bool enabled)
Sets the enabled state of the breakpoint.
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void endRemoveRows()
Ends a row removal operation.
__int64 qint64
Definition: qglobal.h:942
void setCondition(const QString &condition)
Sets the condition of the breakpoint.
The QScriptBreakpointData class contains data associated with a breakpoint.
QScriptBreakpointData breakpointData(int id) const
void setBreakpointData(int id, const QScriptBreakpointData &data)
Sets the data associated with the breakpoint identified by id.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QAbstractItemModel class provides the abstract interface for item model classes.
QList< QPair< int, QScriptBreakpointData > > breakpoints
QScriptDebuggerJobSchedulerInterface * jobScheduler
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
void setBreakpoint(const QScriptBreakpointData &data)
Sets a breakpoint defined by the given data.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
void addBreakpoint(int id, const QScriptBreakpointData &data)
Adds a breakpoint to the model.
QString & append(QChar c)
Definition: qstring.cpp:1777
bool isEnabled() const
Returns true if the breakpoint is enabled, false otherwise.
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
Definition: qpair.h:102
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
The QModelIndex class is used to locate data in a data model.
void removeBreakpoint(int id)
Remove the breakpoint identified by id from the model.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
if(void) toggleToolbarShown
QObject * parent
Definition: qobject.h:92
QVariant headerData(int section, Qt::Orientation, int role=Qt::DisplayRole) const
Reimplemented Function
QString condition() const
Returns the condition of the breakpoint.
int hitCount() const
Returns the hit count of the breakpoint (the number of times the breakpoint has been triggered)...
int scheduleSetBreakpointData(int id, const QScriptBreakpointData &data)
int resolveBreakpoint(qint64 scriptId, int lineNumber) const
Tries to find a breakpoint with the given scriptId and lineNumber.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Reimplemented Function
Orientation
Definition: qnamespace.h:174
int lineNumber() const
Returns the breakpoint line number.
static QString fileName(const QString &fileUrl)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
int column() const
Returns the column this model index refers to.
Qt::ItemFlags flags(const QModelIndex &index) const
Reimplemented Function
The QList class is a template class that provides lists.
Definition: qdatastream.h:62