Qt 4.8
qscriptdebuggerscriptsmodel.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 
43 #include "qscriptscriptdata_p.h"
44 
45 #include "private/qabstractitemmodel_p.h"
46 
47 #include <QtCore/qfileinfo.h>
48 #include <QtCore/qpair.h>
49 #include <QtCore/qdebug.h>
50 
52 
55 {
57 public:
58  struct Node {
59  Node(qint64 sid, const QScriptScriptData &dt)
60  : scriptId(sid), data(dt) {}
61 
66  };
67 
70 
72 
75 };
76 
78 {
79  nextNodeId = 0;
80 }
81 
83 {
85 }
86 
88 {
90  for (it = nodes.constBegin(); it != nodes.constEnd(); ++it) {
91  Node *n = it.value();
92  if (n->scriptId == scriptId)
93  return n;
94  }
95  return 0;
96 }
97 
100 {
101 }
102 
104 {
105 }
106 
108 {
111  for (it = d->nodes.begin(); it != d->nodes.end(); ++it) {
113  if (n->scriptId == id) {
114  d->nodes.erase(it);
115  delete n;
116  break;
117  }
118  }
119 }
120 
122 {
124  int id = d->nextNodeId;
125  ++d->nextNodeId;
126  d->nodes.insert(id, new QScriptDebuggerScriptsModelPrivate::Node(sid, data));
127 }
128 
130  qint64 sid, const QMap<QString, int> &functionsInfo,
132 {
134  QScriptDebuggerScriptsModelPrivate::Node *node = d->findScriptNode(sid);
135  if (!node)
136  return;
139  for (it = functionsInfo.constBegin(); it != functionsInfo.constEnd(); ++it)
140  lst.append(qMakePair(it.key(), it.value()));
141  node->functionsInfo = lst;
143 }
144 
146 {
148  layoutChanged();
149 }
150 
152 {
154  QScriptDebuggerScriptsModelPrivate::Node *node = d->findScriptNode(sid);
155  if (!node)
156  return QScriptScriptData();
157  return node->data;
158 }
159 
161 {
163  QScriptScriptMap result;
165  for (it = d->nodes.constBegin(); it != d->nodes.constEnd(); ++it) {
167  result.insert(n->scriptId, n->data);
168  }
169  return result;
170 }
171 
173 {
176  for (it = d->nodes.constBegin(); it != d->nodes.constEnd(); ++it) {
178  if (n->data.fileName() == fileName)
179  return n->scriptId;
180  }
181  return -1;
182 }
183 
185 {
187  QScriptDebuggerScriptsModelPrivate::Node *node = d->findScriptNode(scriptId);
188  if (!node)
189  return QSet<int>();
190  return node->executableLineNumbers;
191 }
192 
194 {
196  int row = 0;
198  for (it = d->nodes.constBegin(); it != d->nodes.constEnd(); ++it, ++row) {
200  if (n->scriptId == sid)
201  return createIndex(row, 0, it.key() << 12);
202  }
203  return QModelIndex();
204 }
205 
207 {
209  if (!index.isValid())
210  return -1;
211  int id = index.internalId();
212  if (id & 1)
213  return -1;
214  QScriptDebuggerScriptsModelPrivate::Node *n = d->nodes.value(id >> 12);
215  if (!n)
216  return -1;
217  return n->scriptId;
218 }
219 
221 {
223  QPair<QString, int> result;
224  if (!index.isValid())
225  return result;
226  int id = index.internalId();
227  if (!(id & 1))
228  return result;
229  QScriptDebuggerScriptsModelPrivate::Node *node = d->nodes.value(id >> 12);
230  if (!node)
231  return result;
232  int functionIndex = (id >> 1) & ((1 << 11) - 1);
233  result = node->functionsInfo.at(functionIndex);
234  return result;
235 }
236 
241 {
243  if (!parent.isValid()) {
244  if ((row < 0) || (row >= d->nodes.size()))
245  return QModelIndex();
246  if (column != 0)
247  return QModelIndex();
248  return createIndex(row, column, d->nodes.keys().at(row) << 12);
249  }
250  int id = parent.internalId();
251  if (id & 1)
252  return QModelIndex();
253  return createIndex(row, column, id | (row << 1) | 1);
254 }
255 
260 {
262  if (!index.isValid())
263  return QModelIndex();
264  int id = index.internalId();
265  if (!(id & 1))
266  return QModelIndex();
267  QScriptDebuggerScriptsModelPrivate::Node *n = d->nodes.value(id >> 12);
268  if (!n)
269  return QModelIndex();
270  return indexFromScriptId(n->scriptId);
271 }
272 
277 {
278  return 1;
279 }
280 
285 {
287  if (!parent.isValid())
288  return d->nodes.size();
289  int id = parent.internalId();
290  if (id & 1)
291  return 0;
292  QScriptDebuggerScriptsModelPrivate::Node *n = d->nodes.value(id >> 12);
293  if (!n)
294  return 0;
295  return n->functionsInfo.size();
296 }
297 
302 {
304  if (!index.isValid())
305  return QVariant();
306  int id = index.internalId();
307  QScriptDebuggerScriptsModelPrivate::Node *node = d->nodes.value(id >> 12);
308  if (!node)
309  return QVariant();
310  if (!(id & 1)) {
311  if (role == Qt::DisplayRole) {
312  QString fn = node->data.fileName();
313  if (fn.isEmpty())
314  fn = QString::fromLatin1("<anonymous script, id=%0>").arg(node->scriptId);
315  return fn;
316  } else if (role == Qt::ToolTipRole) {
317  QString fn = node->data.fileName();
318  if (QFileInfo(fn).fileName() != fn)
319  return fn;
320  } else if (role == Qt::UserRole) {
321  return node->scriptId;
322  } else if (role == Qt::UserRole+1) {
323  return node->data.baseLineNumber();
324  } else if (role == Qt::UserRole+2) {
325  return node->data.contents();
326  }
327  } else {
328  int functionIndex = (id >> 1) & ((1 << 11) - 1);
329  if (role == Qt::DisplayRole)
330  return node->functionsInfo[functionIndex].first;
331  }
332  return QVariant();
333 }
334 
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
Node * findScriptNode(qint64 scriptId) const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
#define it(className, varName)
QModelIndex indexFromScriptId(qint64 id) const
Node(qint64 sid, const QScriptScriptData &dt)
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. ...
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Reimplemented Function
const Key & key() const
Returns the current item&#39;s key.
Definition: qmap.h:324
QPair< QString, int > scriptFunctionInfoFromIndex(const QModelIndex &index) const
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
QScriptScriptData scriptData(qint64 id) const
#define Q_D(Class)
Definition: qglobal.h:2482
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qmap.h:251
The QScriptScriptData class holds data associated with a script.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qint64 internalId() const
Returns a qint64 used by the model to associate the index with the internal data structure.
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
void addExtraScriptInfo(qint64 id, const QMap< QString, int > &functionsInfo, const QSet< int > &executableLineNumbers)
void addScript(qint64 id, const QScriptScriptData &data)
qint64 resolveScript(const QString &fileName) const
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
__int64 qint64
Definition: qglobal.h:942
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
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.
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap.
Definition: qmap.h:233
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
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
qint64 scriptIdFromIndex(const QModelIndex &index) const
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
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
The QModelIndex class is used to locate data in a data model.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
const T & value() const
Returns the current item&#39;s value.
Definition: qmap.h:325
QObject * parent
Definition: qobject.h:92
QSet< int > executableLineNumbers(qint64 scriptId) const
QString contents() const
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static QString fileName(const QString &fileUrl)
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
QString fileName() const
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
The QList class is a template class that provides lists.
Definition: qdatastream.h:62