Qt 4.8
qscriptbreakpointswidget.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 <QtCore/qdebug.h>
48 #include <QtGui/qaction.h>
49 #include <QtGui/qcompleter.h>
50 #include <QtGui/qheaderview.h>
51 #include <QtGui/qlineedit.h>
52 #include <QtGui/qmessagebox.h>
53 #include <QtGui/qtoolbar.h>
54 #include <QtGui/qtoolbutton.h>
55 #include <QtGui/qtreeview.h>
56 #include <QtGui/qboxlayout.h>
57 #include <QtGui/qstyleditemdelegate.h>
58 #include <QtGui/qevent.h>
59 #include <QtScript/qscriptengine.h>
60 
62 
64 {
65  Q_OBJECT
66 public:
68  : QWidget(parent) {
69  QString system = QLatin1String("win");
70  QHBoxLayout *hboxLayout = new QHBoxLayout(this);
71 #ifdef Q_OS_MAC
72  system = QLatin1String("mac");
73 #else
74  hboxLayout->setSpacing(6);
75  hboxLayout->setMargin(0);
76 #endif
77 
78  toolClose = new QToolButton(this);
79  toolClose->setIcon(QIcon(QString::fromUtf8(":/qt/scripttools/debugging/images/%1/closetab.png").arg(system)));
80  toolClose->setAutoRaise(true);
81  toolClose->setText(tr("Close"));
82  hboxLayout->addWidget(toolClose);
83 
84  fileNameEdit = new QLineEdit();
86  QRegExp locationRegExp(QString::fromLatin1(".+:[0-9]+"));
87  QRegExpValidator *validator = new QRegExpValidator(locationRegExp, fileNameEdit);
88  fileNameEdit->setValidator(validator);
89  hboxLayout->addWidget(fileNameEdit);
90 
91  toolOk = new QToolButton(this);
92  toolOk->setIcon(QIcon(QString::fromUtf8(":/qt/scripttools/debugging/images/%1/plus.png").arg(system)));
93  toolOk->setAutoRaise(true);
94  toolOk->setEnabled(false);
95  hboxLayout->addWidget(toolOk);
96 
97  QObject::connect(toolClose, SIGNAL(clicked()), this, SLOT(hide()));
98  QObject::connect(toolOk, SIGNAL(clicked()), this, SLOT(onOkClicked()));
100  this, SLOT(onTextChanged()));
101  QObject::connect(fileNameEdit, SIGNAL(returnPressed()),
102  this, SLOT(onOkClicked()));
103  }
104 
106  { fileNameEdit->setCompleter(comp); }
107 
108 Q_SIGNALS:
109  void newBreakpointRequest(const QString &fileName, int lineNumber);
110 
111 protected:
113  {
114  if (e->key() == Qt::Key_Escape)
115  hide();
116  else
118  }
119 
120 private Q_SLOTS:
121  void onOkClicked()
122  {
124  fileNameEdit->clear();
125  QString fileName = location.left(location.lastIndexOf(QLatin1Char(':')));
126  int lineNumber = location.mid(fileName.length()+1).toInt();
127  emit newBreakpointRequest(fileName, lineNumber);
128  }
129 
131  {
133  }
134 
135 private:
139 };
140 
141 
142 
145 {
147 public:
150 
151  void _q_newBreakpoint();
152  void _q_deleteBreakpoint();
153  void _q_onCurrentChanged(const QModelIndex &index);
154  void _q_onNewBreakpointRequest(const QString &fileName, int lineNumber);
155 
156  static QPixmap pixmap(const QString &path)
157  {
158  static QString prefix = QString::fromLatin1(":/qt/scripttools/debugging/images/");
159  return QPixmap(prefix + path);
160  }
161 
166 };
167 
169 {
170 }
171 
173 {
174 }
175 
177 {
178  newBreakpointWidget->show();
179  newBreakpointWidget->setFocus(Qt::OtherFocusReason);
180 }
181 
183 {
185  QModelIndex index = view->currentIndex();
186  if (index.isValid()) {
187  int id = q->breakpointsModel()->breakpointIdAt(index.row());
188  q->breakpointsModel()->deleteBreakpoint(id);
189  }
190 }
191 
193 {
194  deleteBreakpointAction->setEnabled(index.isValid());
195 }
196 
198 {
199  QScriptBreakpointData data(fileName, lineNumber);
200  q_func()->breakpointsModel()->setBreakpoint(data);
201 }
202 
204 {
205  Q_OBJECT
206 public:
209 
211  const QStyleOptionViewItem &option,
212  const QModelIndex &index) const
213  {
214  QWidget *editor = QStyledItemDelegate::createEditor(parent, option, index);
215  if (index.column() == 2) {
216  // condition
217  QLineEdit *le = qobject_cast<QLineEdit*>(editor);
218  if (le) {
219  QObject::connect(le, SIGNAL(textEdited(QString)),
220  this, SLOT(validateInput(QString)));
221  }
222  }
223  return editor;
224  }
225 
226  bool eventFilter(QObject *editor, QEvent *event)
227  {
228  if (QLineEdit *le = qobject_cast<QLineEdit*>(editor)) {
229  if (event->type() == QEvent::KeyPress) {
230  int key = static_cast<QKeyEvent*>(event)->key();
231  if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
232  if (QScriptEngine::checkSyntax(le->text()).state() != QScriptSyntaxCheckResult::Valid) {
233  // ignore when script contains syntax error
234  return true;
235  }
236  }
237  }
238  }
239  return QStyledItemDelegate::eventFilter(editor, event);
240  }
241 
243  const QModelIndex &index) const
244  {
245  if (index.column() == 2) {
246  // check that the syntax is OK
247  QString condition = qobject_cast<QLineEdit*>(editor)->text();
249  return;
250  }
251  QStyledItemDelegate::setModelData(editor, model, index);
252  }
253 
254 private Q_SLOTS:
256  {
257  QWidget *editor = qobject_cast<QWidget*>(sender());
258  QPalette pal = editor->palette();
259  QColor col;
261  if (ok) {
262  col = Qt::white;
263  } else {
265  text + QLatin1Char('\n'));
267  col = QColor(255, 240, 192);
268  else
269  col = QColor(255, 102, 102);
270  }
271  pal.setColor(QPalette::Active, QPalette::Base, col);
272  editor->setPalette(pal);
273  }
274 };
275 
278 {
280  d->view = new QTreeView();
281 // d->view->setEditTriggers(QAbstractItemView::NoEditTriggers);
282  d->view->setEditTriggers(QAbstractItemView::AllEditTriggers);
283 // d->view->setAlternatingRowColors(true);
284  d->view->setRootIsDecorated(false);
285  d->view->setSelectionBehavior(QAbstractItemView::SelectRows);
286 // d->view->header()->hide();
287 // d->view->header()->setDefaultAlignment(Qt::AlignLeft);
288 // d->view->header()->setResizeMode(QHeaderView::ResizeToContents);
289  d->view->setItemDelegate(new QScriptBreakpointsItemDelegate(this));
290 
291  d->newBreakpointWidget = new QScriptNewBreakpointWidget();
292  d->newBreakpointWidget->hide();
293  QObject::connect(d->newBreakpointWidget, SIGNAL(newBreakpointRequest(QString,int)),
294  this, SLOT(_q_onNewBreakpointRequest(QString,int)));
295 
296  QIcon newBreakpointIcon;
297  newBreakpointIcon.addPixmap(d->pixmap(QString::fromLatin1("new.png")), QIcon::Normal);
298  QAction *newBreakpointAction = new QAction(newBreakpointIcon, tr("New"), this);
299  QObject::connect(newBreakpointAction, SIGNAL(triggered()),
300  this, SLOT(_q_newBreakpoint()));
301 
302  QIcon deleteBreakpointIcon;
303  deleteBreakpointIcon.addPixmap(d->pixmap(QString::fromLatin1("delete.png")), QIcon::Normal);
304  d->deleteBreakpointAction = new QAction(deleteBreakpointIcon, tr("Delete"), this);
305  d->deleteBreakpointAction->setEnabled(false);
306  QObject::connect(d->deleteBreakpointAction, SIGNAL(triggered()),
307  this, SLOT(_q_deleteBreakpoint()));
308 
309 #ifndef QT_NO_TOOLBAR
310  QToolBar *toolBar = new QToolBar();
311  toolBar->addAction(newBreakpointAction);
312  toolBar->addAction(d->deleteBreakpointAction);
313 #endif
314 
315  QVBoxLayout *vbox = new QVBoxLayout(this);
316  vbox->setMargin(0);
317 #ifndef QT_NO_TOOLBAR
318  vbox->addWidget(toolBar);
319 #endif
320  vbox->addWidget(d->newBreakpointWidget);
321  vbox->addWidget(d->view);
322 }
323 
325 {
326 }
327 
332 {
334  return qobject_cast<QScriptBreakpointsModel*>(d->view->model());
335 }
336 
341 {
343  d->view->setModel(model);
344  d->view->header()->resizeSection(0, 50);
345  QObject::connect(d->view->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
346  this, SLOT(_q_onCurrentChanged(QModelIndex)));
347 }
348 
353 {
355  return d->scriptsModel;
356 }
357 
362 {
364  d->scriptsModel = model;
365  QCompleter *completer = new QCompleter(model, this);
367  d->newBreakpointWidget->setCompleter(completer);
368 }
369 
374 {
376  if (e->key() == Qt::Key_Delete) {
377  QModelIndex index = d->view->currentIndex();
378  if (!index.isValid())
379  return;
380  int id = breakpointsModel()->breakpointIdAt(index.row());
382  }
383 }
384 
386 
387 #include "qscriptbreakpointswidget.moc"
388 
389 #include "moc_qscriptbreakpointswidget_p.cpp"
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
QPalette palette
the widget&#39;s palette
Definition: qwidget.h:180
void setScriptsModel(QScriptDebuggerScriptsModel *model)
Reimplemented Function
The QRegExpValidator class is used to check a string against a regular expression.
Definition: qvalidator.h:182
The QKeyEvent class describes a key event.
Definition: qevent.h:224
void addPixmap(const QPixmap &pixmap, Mode mode=Normal, State state=Off)
Adds pixmap to the icon, as a specialization for mode and state.
Definition: qicon.cpp:814
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setText(const QString &text)
int breakpointIdAt(int row) const
Returns the id of the breakpoint at the given row.
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Gets data from the editor widget and stores it in the specified model at the item index...
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
void deleteBreakpoint(int id)
Deletes the breakpoint with the given id.
QScriptDebuggerScriptsModel * scriptsModel() const
Reimplemented Function
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
void setCompleter(QCompleter *completer)
Sets this line edit to provide auto completions from the completer, c.
Definition: qlineedit.cpp:654
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void _q_onNewBreakpointRequest(const QString &fileName, int lineNumber)
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
void keyPressEvent(QKeyEvent *e)
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static QPixmap pixmap(const QString &path)
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QScriptNewBreakpointWidget(QWidget *parent=0)
#define Q_D(Class)
Definition: qglobal.h:2482
#define Q_SIGNALS
Definition: qobjectdefs.h:72
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...
The QStyledItemDelegate class provides display and editing facilities for data items from a model...
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition: qobject.cpp:2327
#define Q_Q(Class)
Definition: qglobal.h:2483
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
#define SIGNAL(a)
Definition: qobjectdefs.h:227
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Returns the widget used to edit the item specified by index for editing.
QScriptBreakpointsWidget(QWidget *parent=0)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void _q_onCurrentChanged(const QModelIndex &index)
friend class QPixmap
Definition: qwidget.h:748
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
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
The QTreeView class provides a default model/view implementation of a tree view.
Definition: qtreeview.h:58
The QToolBar class provides a movable panel that contains a set of controls.
Definition: qtoolbar.h:62
QString text
the line edit&#39;s text
Definition: qlineedit.h:72
int row() const
Returns the row this model index refers to.
QScriptBreakpointsModel * breakpointsModel() const
Reimplemented Function
#define emit
Definition: qobjectdefs.h:76
static int toInt(const QByteArray &str)
Definition: generator.cpp:167
QWidgetData * data
Definition: qwidget.h:815
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
void setValidator(const QValidator *)
Sets this line edit to only accept input that the validator, v, will accept.
Definition: qlineedit.cpp:627
The QScriptSyntaxCheckResult class provides the result of a script syntax check.
Definition: qscriptengine.h:75
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Gets data from the editor widget and stores it in the specified model at the item index...
QScriptDebuggerScriptsModel * scriptsModel
The QScriptBreakpointData class contains data associated with a breakpoint.
virtual void keyPressEvent(QKeyEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
Definition: qwidget.cpp:9375
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
void setEnabled(bool)
Definition: qwidget.cpp:3447
#define Q_OBJECT
Definition: qobjectdefs.h:157
void hide()
Hides the widget.
Definition: qwidget.h:501
The QAbstractItemModel class provides the abstract interface for item model classes.
QScriptNewBreakpointWidget * newBreakpointWidget
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
void setFocusProxy(QWidget *)
Sets the widget&#39;s focus proxy to widget w.
Definition: qwidget.cpp:6537
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
void clear()
Clears the contents of the line edit.
Definition: qlineedit.cpp:1443
int lastIndexOf(QChar c, int from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:3000
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
void setSpacing(int spacing)
Reimplements QLayout::setSpacing().
Definition: qboxlayout.cpp:667
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
static QTestResult::TestLocation location
Definition: qtestresult.cpp:63
int key
The QModelIndex class is used to locate data in a data model.
void setAutoRaise(bool enable)
void newBreakpointRequest(const QString &fileName, int lineNumber)
The QHBoxLayout class lines up widgets horizontally.
Definition: qboxlayout.h:129
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:66
quint16 index
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
static QScriptSyntaxCheckResult checkSyntax(const QString &program)
Checks the syntax of the given program.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Returns the widget used to edit the item specified by index for editing.
bool eventFilter(QObject *editor, QEvent *event)
Returns true if the given editor is a valid QWidget and the given event is handled; otherwise returns...
void setCompletionRole(int role)
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:149
The QStyleOptionViewItem class is used to describe the parameters used to draw an item in a view widg...
Definition: qstyleoption.h:539
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
The QToolButton class provides a quick-access button to commands or options, usually used inside a QT...
Definition: qtoolbutton.h:59
QAction * addAction(const QString &text)
Creates a new action with the given text.
Definition: qtoolbar.cpp:868
void setMargin(int)
Definition: qlayout.cpp:464
void setIcon(const QIcon &icon)
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
void keyPressEvent(QKeyEvent *e)
Reimplemented Function
static QString fileName(const QString &fileUrl)
bool hasAcceptableInput() const
Definition: qlineedit.cpp:1246
bool eventFilter(QObject *object, QEvent *event)
Returns true if the given editor is a valid QWidget and the given event is handled; otherwise returns...
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void setBreakpointsModel(QScriptBreakpointsModel *model)
Reimplemented Function
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
void setPalette(const QPalette &)
Use the single-argument overload instead.
Definition: qwidget.cpp:4858
#define text
Definition: qobjectdefs.h:80
int column() const
Returns the column this model index refers to.
State state() const
Returns the state of this QScriptSyntaxCheckResult.
The QPalette class contains color groups for each widget state.
Definition: qpalette.h:61
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60