source: trunk/src/scripttools/debugging/qscriptbreakpointswidget.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 13.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtSCriptTools module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qscriptbreakpointswidget_p.h"
43#include "qscriptbreakpointswidgetinterface_p_p.h"
44#include "qscriptbreakpointsmodel_p.h"
45#include "qscriptdebuggerscriptsmodel_p.h"
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
61QT_BEGIN_NAMESPACE
62
63class QScriptNewBreakpointWidget : public QWidget
64{
65 Q_OBJECT
66public:
67 QScriptNewBreakpointWidget(QWidget *parent = 0)
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(QObject::tr("Close"));
82 hboxLayout->addWidget(toolClose);
83
84 fileNameEdit = new QLineEdit();
85 setFocusProxy(fileNameEdit);
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()));
99 QObject::connect(fileNameEdit, SIGNAL(textChanged(QString)),
100 this, SLOT(onTextChanged()));
101 QObject::connect(fileNameEdit, SIGNAL(returnPressed()),
102 this, SLOT(onOkClicked()));
103 }
104
105 void setCompleter(QCompleter *comp)
106 { fileNameEdit->setCompleter(comp); }
107
108Q_SIGNALS:
109 void newBreakpointRequest(const QString &fileName, int lineNumber);
110
111protected:
112 void keyPressEvent(QKeyEvent *e)
113 {
114 if (e->key() == Qt::Key_Escape)
115 hide();
116 else
117 QWidget::keyPressEvent(e);
118 }
119
120private Q_SLOTS:
121 void onOkClicked()
122 {
123 QString location = fileNameEdit->text();
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
130 void onTextChanged()
131 {
132 toolOk->setEnabled(fileNameEdit->hasAcceptableInput());
133 }
134
135private:
136 QLineEdit *fileNameEdit;
137 QToolButton *toolClose;
138 QToolButton *toolOk;
139};
140
141
142
143class QScriptBreakpointsWidgetPrivate
144 : public QScriptBreakpointsWidgetInterfacePrivate
145{
146 Q_DECLARE_PUBLIC(QScriptBreakpointsWidget)
147public:
148 QScriptBreakpointsWidgetPrivate();
149 ~QScriptBreakpointsWidgetPrivate();
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
162 QTreeView *view;
163 QScriptNewBreakpointWidget *newBreakpointWidget;
164 QAction *deleteBreakpointAction;
165 QScriptDebuggerScriptsModel *scriptsModel;
166};
167
168QScriptBreakpointsWidgetPrivate::QScriptBreakpointsWidgetPrivate()
169{
170}
171
172QScriptBreakpointsWidgetPrivate::~QScriptBreakpointsWidgetPrivate()
173{
174}
175
176void QScriptBreakpointsWidgetPrivate::_q_newBreakpoint()
177{
178 newBreakpointWidget->show();
179 newBreakpointWidget->setFocus(Qt::OtherFocusReason);
180}
181
182void QScriptBreakpointsWidgetPrivate::_q_deleteBreakpoint()
183{
184 Q_Q(QScriptBreakpointsWidget);
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
192void QScriptBreakpointsWidgetPrivate::_q_onCurrentChanged(const QModelIndex &index)
193{
194 deleteBreakpointAction->setEnabled(index.isValid());
195}
196
197void QScriptBreakpointsWidgetPrivate::_q_onNewBreakpointRequest(const QString &fileName, int lineNumber)
198{
199 QScriptBreakpointData data(fileName, lineNumber);
200 q_func()->breakpointsModel()->setBreakpoint(data);
201}
202
203class QScriptBreakpointsItemDelegate : public QStyledItemDelegate
204{
205 Q_OBJECT
206public:
207 QScriptBreakpointsItemDelegate(QObject *parent = 0)
208 : QStyledItemDelegate(parent) {}
209
210 QWidget *createEditor(QWidget *parent,
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 QT_VERSION >= 0x040500
229 if (QLineEdit *le = qobject_cast<QLineEdit*>(editor)) {
230 if (event->type() == QEvent::KeyPress) {
231 int key = static_cast<QKeyEvent*>(event)->key();
232 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
233 if (QScriptEngine::checkSyntax(le->text()).state() != QScriptSyntaxCheckResult::Valid) {
234 // ignore when script contains syntax error
235 return true;
236 }
237 }
238 }
239 }
240#endif
241 return QStyledItemDelegate::eventFilter(editor, event);
242 }
243
244 void setModelData(QWidget *editor, QAbstractItemModel *model,
245 const QModelIndex &index) const
246 {
247#if QT_VERSION >= 0x040500
248 if (index.column() == 2) {
249 // check that the syntax is OK
250 QString condition = qobject_cast<QLineEdit*>(editor)->text();
251 if (QScriptEngine::checkSyntax(condition).state() != QScriptSyntaxCheckResult::Valid)
252 return;
253 }
254#endif
255 QStyledItemDelegate::setModelData(editor, model, index);
256 }
257
258private Q_SLOTS:
259 void validateInput(const QString &text)
260 {
261 QWidget *editor = qobject_cast<QWidget*>(sender());
262 QPalette pal = editor->palette();
263 QColor col;
264#if QT_VERSION >= 0x040500
265 bool ok = (QScriptEngine::checkSyntax(text).state() == QScriptSyntaxCheckResult::Valid);
266#else
267 bool ok = true;
268#endif
269 if (ok) {
270 col = Qt::white;
271 } else {
272 QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(
273 text + QLatin1Char('\n'));
274 if (result.state() == QScriptSyntaxCheckResult::Intermediate)
275 col = QColor(255, 240, 192);
276 else
277 col = QColor(255, 102, 102);
278 }
279 pal.setColor(QPalette::Active, QPalette::Base, col);
280 editor->setPalette(pal);
281 }
282};
283
284QScriptBreakpointsWidget::QScriptBreakpointsWidget(QWidget *parent)
285 : QScriptBreakpointsWidgetInterface(*new QScriptBreakpointsWidgetPrivate, parent, 0)
286{
287 Q_D(QScriptBreakpointsWidget);
288 d->view = new QTreeView();
289// d->view->setEditTriggers(QAbstractItemView::NoEditTriggers);
290 d->view->setEditTriggers(QAbstractItemView::AllEditTriggers);
291// d->view->setAlternatingRowColors(true);
292 d->view->setRootIsDecorated(false);
293 d->view->setSelectionBehavior(QAbstractItemView::SelectRows);
294// d->view->header()->hide();
295// d->view->header()->setDefaultAlignment(Qt::AlignLeft);
296// d->view->header()->setResizeMode(QHeaderView::ResizeToContents);
297 d->view->setItemDelegate(new QScriptBreakpointsItemDelegate(this));
298
299 d->newBreakpointWidget = new QScriptNewBreakpointWidget();
300 d->newBreakpointWidget->hide();
301 QObject::connect(d->newBreakpointWidget, SIGNAL(newBreakpointRequest(QString,int)),
302 this, SLOT(_q_onNewBreakpointRequest(QString,int)));
303
304 QIcon newBreakpointIcon;
305 newBreakpointIcon.addPixmap(d->pixmap(QString::fromLatin1("new.png")), QIcon::Normal);
306 QAction *newBreakpointAction = new QAction(newBreakpointIcon, QObject::tr("New"), this);
307 QObject::connect(newBreakpointAction, SIGNAL(triggered()),
308 this, SLOT(_q_newBreakpoint()));
309
310 QIcon deleteBreakpointIcon;
311 deleteBreakpointIcon.addPixmap(d->pixmap(QString::fromLatin1("delete.png")), QIcon::Normal);
312 d->deleteBreakpointAction = new QAction(deleteBreakpointIcon, QObject::tr("Delete"), this);
313 d->deleteBreakpointAction->setEnabled(false);
314 QObject::connect(d->deleteBreakpointAction, SIGNAL(triggered()),
315 this, SLOT(_q_deleteBreakpoint()));
316
317 QToolBar *toolBar = new QToolBar();
318 toolBar->addAction(newBreakpointAction);
319 toolBar->addAction(d->deleteBreakpointAction);
320
321 QVBoxLayout *vbox = new QVBoxLayout(this);
322 vbox->setMargin(0);
323 vbox->addWidget(toolBar);
324 vbox->addWidget(d->newBreakpointWidget);
325 vbox->addWidget(d->view);
326}
327
328QScriptBreakpointsWidget::~QScriptBreakpointsWidget()
329{
330}
331
332/*!
333 \reimp
334*/
335QScriptBreakpointsModel *QScriptBreakpointsWidget::breakpointsModel() const
336{
337 Q_D(const QScriptBreakpointsWidget);
338 return qobject_cast<QScriptBreakpointsModel*>(d->view->model());
339}
340
341/*!
342 \reimp
343*/
344void QScriptBreakpointsWidget::setBreakpointsModel(QScriptBreakpointsModel *model)
345{
346 Q_D(QScriptBreakpointsWidget);
347 d->view->setModel(model);
348 d->view->header()->resizeSection(0, 50);
349 QObject::connect(d->view->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
350 this, SLOT(_q_onCurrentChanged(QModelIndex)));
351}
352
353/*!
354 \reimp
355*/
356QScriptDebuggerScriptsModel *QScriptBreakpointsWidget::scriptsModel() const
357{
358 Q_D(const QScriptBreakpointsWidget);
359 return d->scriptsModel;
360}
361
362/*!
363 \reimp
364*/
365void QScriptBreakpointsWidget::setScriptsModel(QScriptDebuggerScriptsModel *model)
366{
367 Q_D(QScriptBreakpointsWidget);
368 d->scriptsModel = model;
369 QCompleter *completer = new QCompleter(model, this);
370 completer->setCompletionRole(Qt::DisplayRole);
371 d->newBreakpointWidget->setCompleter(completer);
372}
373
374/*!
375 \reimp
376*/
377void QScriptBreakpointsWidget::keyPressEvent(QKeyEvent *e)
378{
379 Q_D(QScriptBreakpointsWidget);
380 if (e->key() == Qt::Key_Delete) {
381 QModelIndex index = d->view->currentIndex();
382 if (!index.isValid())
383 return;
384 int id = breakpointsModel()->breakpointIdAt(index.row());
385 breakpointsModel()->deleteBreakpoint(id);
386 }
387}
388
389QT_END_NAMESPACE
390
391#include "qscriptbreakpointswidget.moc"
392
393#include "moc_qscriptbreakpointswidget_p.cpp"
Note: See TracBrowser for help on using the repository browser.