| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 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. | 
|---|
| 15 | ** | 
|---|
| 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. | 
|---|
| 23 | ** | 
|---|
| 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. | 
|---|
| 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 have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qscriptdebuggerscriptsmodel_p.h" | 
|---|
| 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 |  | 
|---|
| 51 | QT_BEGIN_NAMESPACE | 
|---|
| 52 |  | 
|---|
| 53 | class QScriptDebuggerScriptsModelPrivate | 
|---|
| 54 | : public QAbstractItemModelPrivate | 
|---|
| 55 | { | 
|---|
| 56 | Q_DECLARE_PUBLIC(QScriptDebuggerScriptsModel) | 
|---|
| 57 | public: | 
|---|
| 58 | struct Node { | 
|---|
| 59 | Node(qint64 sid, const QScriptScriptData &dt) | 
|---|
| 60 | : scriptId(sid), data(dt) {} | 
|---|
| 61 |  | 
|---|
| 62 | qint64 scriptId; | 
|---|
| 63 | QScriptScriptData data; | 
|---|
| 64 | QList<QPair<QString, int> > functionsInfo; | 
|---|
| 65 | QSet<int> executableLineNumbers; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | QScriptDebuggerScriptsModelPrivate(); | 
|---|
| 69 | ~QScriptDebuggerScriptsModelPrivate(); | 
|---|
| 70 |  | 
|---|
| 71 | Node *findScriptNode(qint64 scriptId) const; | 
|---|
| 72 |  | 
|---|
| 73 | int nextNodeId; | 
|---|
| 74 | QMap<int, Node*> nodes; | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | QScriptDebuggerScriptsModelPrivate::QScriptDebuggerScriptsModelPrivate() | 
|---|
| 78 | { | 
|---|
| 79 | nextNodeId = 0; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | QScriptDebuggerScriptsModelPrivate::~QScriptDebuggerScriptsModelPrivate() | 
|---|
| 83 | { | 
|---|
| 84 | qDeleteAll(nodes); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | QScriptDebuggerScriptsModelPrivate::Node *QScriptDebuggerScriptsModelPrivate::findScriptNode(qint64 scriptId) const | 
|---|
| 88 | { | 
|---|
| 89 | QMap<int, Node*>::const_iterator it; | 
|---|
| 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 |  | 
|---|
| 98 | QScriptDebuggerScriptsModel::QScriptDebuggerScriptsModel(QObject *parent) | 
|---|
| 99 | : QAbstractItemModel(*new QScriptDebuggerScriptsModelPrivate, parent) | 
|---|
| 100 | { | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | QScriptDebuggerScriptsModel::~QScriptDebuggerScriptsModel() | 
|---|
| 104 | { | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | void QScriptDebuggerScriptsModel::removeScript(qint64 id) | 
|---|
| 108 | { | 
|---|
| 109 | Q_D(QScriptDebuggerScriptsModel); | 
|---|
| 110 | QMap<int, QScriptDebuggerScriptsModelPrivate::Node*>::iterator it; | 
|---|
| 111 | for (it = d->nodes.begin(); it != d->nodes.end(); ++it) { | 
|---|
| 112 | QScriptDebuggerScriptsModelPrivate::Node *n = it.value(); | 
|---|
| 113 | if (n->scriptId == id) { | 
|---|
| 114 | d->nodes.erase(it); | 
|---|
| 115 | delete n; | 
|---|
| 116 | break; | 
|---|
| 117 | } | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | void QScriptDebuggerScriptsModel::addScript(qint64 sid, const QScriptScriptData &data) | 
|---|
| 122 | { | 
|---|
| 123 | Q_D(QScriptDebuggerScriptsModel); | 
|---|
| 124 | int id = d->nextNodeId; | 
|---|
| 125 | ++d->nextNodeId; | 
|---|
| 126 | d->nodes.insert(id, new QScriptDebuggerScriptsModelPrivate::Node(sid, data)); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | void QScriptDebuggerScriptsModel::addExtraScriptInfo( | 
|---|
| 130 | qint64 sid, const QMap<QString, int> &functionsInfo, | 
|---|
| 131 | const QSet<int> &executableLineNumbers) | 
|---|
| 132 | { | 
|---|
| 133 | Q_D(QScriptDebuggerScriptsModel); | 
|---|
| 134 | QScriptDebuggerScriptsModelPrivate::Node *node = d->findScriptNode(sid); | 
|---|
| 135 | if (!node) | 
|---|
| 136 | return; | 
|---|
| 137 | QList<QPair<QString, int> > lst; | 
|---|
| 138 | QMap<QString, int>::const_iterator it; | 
|---|
| 139 | for (it = functionsInfo.constBegin(); it != functionsInfo.constEnd(); ++it) | 
|---|
| 140 | lst.append(qMakePair(it.key(), it.value())); | 
|---|
| 141 | node->functionsInfo = lst; | 
|---|
| 142 | node->executableLineNumbers = executableLineNumbers; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | void QScriptDebuggerScriptsModel::commit() | 
|---|
| 146 | { | 
|---|
| 147 | layoutAboutToBeChanged(); | 
|---|
| 148 | layoutChanged(); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | QScriptScriptData QScriptDebuggerScriptsModel::scriptData(qint64 sid) const | 
|---|
| 152 | { | 
|---|
| 153 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 154 | QScriptDebuggerScriptsModelPrivate::Node *node = d->findScriptNode(sid); | 
|---|
| 155 | if (!node) | 
|---|
| 156 | return QScriptScriptData(); | 
|---|
| 157 | return node->data; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | QScriptScriptMap QScriptDebuggerScriptsModel::scripts() const | 
|---|
| 161 | { | 
|---|
| 162 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 163 | QScriptScriptMap result; | 
|---|
| 164 | QMap<int, QScriptDebuggerScriptsModelPrivate::Node*>::const_iterator it; | 
|---|
| 165 | for (it = d->nodes.constBegin(); it != d->nodes.constEnd(); ++it) { | 
|---|
| 166 | QScriptDebuggerScriptsModelPrivate::Node *n = it.value(); | 
|---|
| 167 | result.insert(n->scriptId, n->data); | 
|---|
| 168 | } | 
|---|
| 169 | return result; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | qint64 QScriptDebuggerScriptsModel::resolveScript(const QString &fileName) const | 
|---|
| 173 | { | 
|---|
| 174 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 175 | QMap<int, QScriptDebuggerScriptsModelPrivate::Node*>::const_iterator it; | 
|---|
| 176 | for (it = d->nodes.constBegin(); it != d->nodes.constEnd(); ++it) { | 
|---|
| 177 | QScriptDebuggerScriptsModelPrivate::Node *n = it.value(); | 
|---|
| 178 | if (n->data.fileName() == fileName) | 
|---|
| 179 | return n->scriptId; | 
|---|
| 180 | } | 
|---|
| 181 | return -1; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | QSet<int> QScriptDebuggerScriptsModel::executableLineNumbers(qint64 scriptId) const | 
|---|
| 185 | { | 
|---|
| 186 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 187 | QScriptDebuggerScriptsModelPrivate::Node *node = d->findScriptNode(scriptId); | 
|---|
| 188 | if (!node) | 
|---|
| 189 | return QSet<int>(); | 
|---|
| 190 | return node->executableLineNumbers; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | QModelIndex QScriptDebuggerScriptsModel::indexFromScriptId(qint64 sid) const | 
|---|
| 194 | { | 
|---|
| 195 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 196 | int row = 0; | 
|---|
| 197 | QMap<int, QScriptDebuggerScriptsModelPrivate::Node*>::const_iterator it; | 
|---|
| 198 | for (it = d->nodes.constBegin(); it != d->nodes.constEnd(); ++it, ++row) { | 
|---|
| 199 | QScriptDebuggerScriptsModelPrivate::Node *n = it.value(); | 
|---|
| 200 | if (n->scriptId == sid) | 
|---|
| 201 | return createIndex(row, 0, it.key() << 12); | 
|---|
| 202 | } | 
|---|
| 203 | return QModelIndex(); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | qint64 QScriptDebuggerScriptsModel::scriptIdFromIndex(const QModelIndex &index) const | 
|---|
| 207 | { | 
|---|
| 208 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 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 |  | 
|---|
| 220 | QPair<QString, int> QScriptDebuggerScriptsModel::scriptFunctionInfoFromIndex(const QModelIndex &index) const | 
|---|
| 221 | { | 
|---|
| 222 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 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 |  | 
|---|
| 237 | /*! | 
|---|
| 238 | \reimp | 
|---|
| 239 | */ | 
|---|
| 240 | QModelIndex QScriptDebuggerScriptsModel::index(int row, int column, const QModelIndex &parent) const | 
|---|
| 241 | { | 
|---|
| 242 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 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 |  | 
|---|
| 256 | /*! | 
|---|
| 257 | \reimp | 
|---|
| 258 | */ | 
|---|
| 259 | QModelIndex QScriptDebuggerScriptsModel::parent(const QModelIndex &index) const | 
|---|
| 260 | { | 
|---|
| 261 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 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 |  | 
|---|
| 273 | /*! | 
|---|
| 274 | \reimp | 
|---|
| 275 | */ | 
|---|
| 276 | int QScriptDebuggerScriptsModel::columnCount(const QModelIndex &) const | 
|---|
| 277 | { | 
|---|
| 278 | return 1; | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | /*! | 
|---|
| 282 | \reimp | 
|---|
| 283 | */ | 
|---|
| 284 | int QScriptDebuggerScriptsModel::rowCount(const QModelIndex &parent) const | 
|---|
| 285 | { | 
|---|
| 286 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 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 |  | 
|---|
| 298 | /*! | 
|---|
| 299 | \reimp | 
|---|
| 300 | */ | 
|---|
| 301 | QVariant QScriptDebuggerScriptsModel::data(const QModelIndex &index, int role) const | 
|---|
| 302 | { | 
|---|
| 303 | Q_D(const QScriptDebuggerScriptsModel); | 
|---|
| 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 |  | 
|---|
| 335 | QT_END_NAMESPACE | 
|---|