Qt 4.8
qscriptdebuggercodewidget.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 
48 
49 #include <QtCore/qdebug.h>
50 #include <QtGui/qboxlayout.h>
51 #include <QtGui/qstackedwidget.h>
52 
54 
57 {
59 public:
62 
64 
65  // private slots
66  void _q_onBreakpointToggleRequest(int lineNumber, bool on);
67  void _q_onBreakpointEnableRequest(int lineNumber, bool enable);
68  void _q_onBreakpointsAboutToBeRemoved(const QModelIndex&, int, int);
69  void _q_onBreakpointsInserted(const QModelIndex&, int, int);
71  void _q_onScriptsChanged();
72  void _q_onToolTipRequest(const QPoint &pos, int lineNumber, const QStringList &path);
73 
79 };
80 
82 {
83  scriptsModel = 0;
84  breakpointsModel = 0;
85  toolTipProvider = 0;
86 }
87 
89 {
90 }
91 
93 {
94  if (!view)
95  return -1;
96  return viewHash.key(view);
97 }
98 
100 {
102  qint64 sid = scriptId(view);
103  Q_ASSERT(sid != -1);
104  if (on) {
105  QScriptBreakpointData data(sid, lineNumber);
108  } else {
109  int bpid = breakpointsModel->resolveBreakpoint(sid, lineNumber);
110  if (bpid == -1)
112  Q_ASSERT(bpid != -1);
114  }
115 }
116 
118 {
120  qint64 sid = scriptId(view);
121  int bpid = breakpointsModel->resolveBreakpoint(sid, lineNumber);
122  if (bpid == -1)
124  Q_ASSERT(bpid != -1);
126  data.setEnabled(enable);
127  breakpointsModel->setBreakpointData(bpid, data);
128 }
129 
131  const QModelIndex &, int first, int last)
132 {
133  for (int i = first; i <= last; ++i) {
135  qint64 scriptId = data.scriptId();
136  if (scriptId == -1) {
137  scriptId = scriptsModel->resolveScript(data.fileName());
138  if (scriptId == -1)
139  continue;
140  }
142  if (!view)
143  continue;
144  view->deleteBreakpoint(data.lineNumber());
145  }
146 }
147 
149  const QModelIndex &, int first, int last)
150 {
151  for (int i = first; i <= last; ++i) {
153  qint64 scriptId = data.scriptId();
154  if (scriptId == -1) {
155  scriptId = scriptsModel->resolveScript(data.fileName());
156  if (scriptId == -1)
157  continue;
158  }
160  if (!view)
161  continue;
162  view->setBreakpoint(data.lineNumber());
163  }
164 }
165 
167  const QModelIndex &tl, const QModelIndex &br)
168 {
169  for (int i = tl.row(); i <= br.row(); ++i) {
171  qint64 scriptId = data.scriptId();
172  if (scriptId == -1) {
173  scriptId = scriptsModel->resolveScript(data.fileName());
174  if (scriptId == -1)
175  continue;
176  }
178  if (!view)
179  continue;
180  view->setBreakpointEnabled(data.lineNumber(), data.isEnabled());
181  }
182 }
183 
185 {
186  // kill editors for scripts that have been removed
188  for (it = viewHash.begin(); it != viewHash.end(); ) {
189  if (!scriptsModel->scriptData(it.key()).isValid()) {
190  it = viewHash.erase(it);
191  } else
192  ++it;
193  }
194 }
195 
197  const QPoint &pos, int lineNumber, const QStringList &path)
198 {
199  toolTipProvider->showToolTip(pos, /*frameIndex=*/-1, lineNumber, path);
200 }
201 
204 {
206  QVBoxLayout *vbox = new QVBoxLayout(this);
207  vbox->setMargin(0);
208  d->viewStack = new QStackedWidget();
209  vbox->addWidget(d->viewStack);
210 }
211 
213 {
214 }
215 
217 {
219  return d->scriptsModel;
220 }
221 
223 {
225  d->scriptsModel = model;
226  QObject::connect(model, SIGNAL(layoutChanged()),
227  this, SLOT(_q_onScriptsChanged()));
228 }
229 
231 {
233  return d->scriptId(currentView());
234 }
235 
237 {
239  if (scriptId == -1) {
240  // ### show "native script"
241  return;
242  }
243  QScriptDebuggerCodeViewInterface *view = d->viewHash.value(scriptId);
244  if (!view) {
245  Q_ASSERT(d->scriptsModel != 0);
246  QScriptScriptData data = d->scriptsModel->scriptData(scriptId);
247  if (!data.isValid())
248  return;
249  view = new QScriptDebuggerCodeView(); // ### use factory, so user can provide his own view
250  view->setBaseLineNumber(data.baseLineNumber());
251  view->setText(data.contents());
252  view->setExecutableLineNumbers(d->scriptsModel->executableLineNumbers(scriptId));
253  QObject::connect(view, SIGNAL(breakpointToggleRequest(int,bool)),
254  this, SLOT(_q_onBreakpointToggleRequest(int,bool)));
255  QObject::connect(view, SIGNAL(breakpointEnableRequest(int,bool)),
256  this, SLOT(_q_onBreakpointEnableRequest(int,bool)));
257  QObject::connect(view, SIGNAL(toolTipRequest(QPoint,int,QStringList)),
258  this, SLOT(_q_onToolTipRequest(QPoint,int,QStringList)));
259  d->viewStack->addWidget(view);
260  d->viewHash.insert(scriptId, view);
261  }
262  d->viewStack->setCurrentWidget(view);
263 }
264 
266 {
269  for (it = d->viewHash.constBegin(); it != d->viewHash.constEnd(); ++it)
270  it.value()->setExecutionLineNumber(-1, /*error=*/false);
271 }
272 
274 {
276  return d->breakpointsModel;
277 }
278 
280 {
282  d->breakpointsModel = model;
283  QObject::connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
284  this, SLOT(_q_onBreakpointsAboutToBeRemoved(QModelIndex,int,int)));
285  QObject::connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
286  this, SLOT(_q_onBreakpointsInserted(QModelIndex,int,int)));
287  QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
288  this, SLOT(_q_onBreakpointsDataChanged(QModelIndex,QModelIndex)));
289 }
290 
292 {
294  d->toolTipProvider = toolTipProvider;
295 }
296 
298 {
300  return qobject_cast<QScriptDebuggerCodeViewInterface*>(d->viewStack->currentWidget());
301 }
302 
304 
305 #include "moc_qscriptdebuggercodewidget_p.cpp"
T qobject_cast(QObject *object)
Definition: qobject.h:375
double d
Definition: qnumeric_p.h:62
QHash< qint64, QScriptDebuggerCodeViewInterface * > viewHash
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void _q_onBreakpointsInserted(const QModelIndex &, int, int)
QScriptDebuggerScriptsModel * scriptsModel() const
QScriptBreakpointData breakpointDataAt(int row) const
Returns the data for the breakpoint at the given row.
QScriptToolTipProviderInterface * toolTipProvider
void deleteBreakpoint(int id)
Deletes the breakpoint with the given id.
#define it(className, varName)
void _q_onToolTipRequest(const QPoint &pos, int lineNumber, const QStringList &path)
void setFileName(const QString &fileName)
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
virtual void setExecutableLineNumbers(const QSet< int > &lineNumbers)=0
QScriptDebuggerScriptsModel * scriptsModel
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QScriptScriptData scriptData(qint64 id) const
virtual void setBaseLineNumber(int lineNumber)=0
#define Q_D(Class)
Definition: qglobal.h:2482
void _q_onBreakpointEnableRequest(int lineNumber, bool enable)
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=0)
Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment...
virtual void deleteBreakpoint(int lineNumber)=0
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
virtual void setBreakpointEnabled(int lineNumber, bool enable)=0
QWidgetData data
Definition: qwidget_p.h:755
The QScriptScriptData class holds data associated with a script.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setBreakpointsModel(QScriptBreakpointsModel *model)
qint64 scriptId(QScriptDebuggerCodeViewInterface *view) const
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
int row() const
Returns the row this model index refers to.
virtual void showToolTip(const QPoint &pos, int frameIndex, int lineNumber, const QStringList &path)=0
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QWidgetData * data
Definition: qwidget.h:815
const T & value() const
Returns the current item&#39;s value.
Definition: qhash.h:420
qint64 resolveScript(const QString &fileName) const
void setEnabled(bool enabled)
Sets the enabled state of the breakpoint.
void setScriptsModel(QScriptDebuggerScriptsModel *model)
void _q_onBreakpointsDataChanged(const QModelIndex &, const QModelIndex &)
__int64 qint64
Definition: qglobal.h:942
virtual void setBreakpoint(int lineNumber)=0
const Key & key() const
Returns the current item&#39;s key as a const reference.
Definition: qhash.h:347
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.
void _q_onBreakpointToggleRequest(int lineNumber, bool on)
QScriptDebuggerCodeViewInterface * currentView() const
void setBreakpoint(const QScriptBreakpointData &data)
Sets a breakpoint defined by the given data.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
bool isEnabled() const
Returns true if the breakpoint is enabled, false otherwise.
const Key key(const T &value) const
Returns the first key mapped to value.
Definition: qhash.h:674
void _q_onBreakpointsAboutToBeRemoved(const QModelIndex &, int, int)
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
The QModelIndex class is used to locate data in a data model.
iterator begin()
Returns an STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:464
QObject * parent
Definition: qobject.h:92
QScriptBreakpointsModel * breakpointsModel() const
QString contents() const
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:149
int resolveBreakpoint(qint64 scriptId, int lineNumber) const
Tries to find a breakpoint with the given scriptId and lineNumber.
virtual void setText(const QString &text)=0
void setMargin(int)
Definition: qlayout.cpp:464
void setToolTipProvider(QScriptToolTipProviderInterface *toolTipProvider)
The QStackedWidget class provides a stack of widgets where only one widget is visible at a time...
int lineNumber() const
Returns the breakpoint line number.
iterator erase(iterator it)
Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator...
Definition: qhash.h:827
QString fileName() const