Commit ba412ee5 authored by Aurindam Jana's avatar Aurindam Jana

ScriptConsole: Use QTreeView

Use QTreeView instead of modified QPlainTextEdit to show the
console. Using a QTreeView gives more control over output
display as well as separates out the model from the view.

Change-Id: I436b13ed042c00d09d6de627b442bfd8d10d2236
Reviewed-by: default avatarKai Koehne <kai.koehne@nokia.com>
parent bb4f25b7
......@@ -30,5 +30,11 @@
<file>images/location_16.png</file>
<file>images/location_24.png</file>
<file>images/pin.xpm</file>
<file>images/collapse.png</file>
<file>images/error.png</file>
<file>images/expand.png</file>
<file>images/log.png</file>
<file>images/prompt.png</file>
<file>images/warning.png</file>
</qresource>
</RCC>
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "consolebackend.h"
#include "qmlengine.h"
#include "qmladapter.h"
#include "debuggerstringutils.h"
#include <utils/qtcassert.h>
namespace Debugger {
namespace Internal {
///////////////////////////////////////////////////////////////////////
//
// ConsoleBackend
//
///////////////////////////////////////////////////////////////////////
ConsoleBackend::ConsoleBackend(QObject *parent) :
QObject(parent)
{
}
///////////////////////////////////////////////////////////////////////
//
// QmlJSConsoleBackend
//
///////////////////////////////////////////////////////////////////////
QmlJSConsoleBackend::QmlJSConsoleBackend(QObject *parent) :
ConsoleBackend(parent),
m_engine(0),
m_inferiorStopped(false),
m_isValidContext(false),
m_Error(false)
{
}
void QmlJSConsoleBackend::setInferiorStopped(bool isInferiorStopped)
{
m_inferiorStopped = isInferiorStopped;
}
bool QmlJSConsoleBackend::inferiorStopped() const
{
return m_inferiorStopped;
}
void QmlJSConsoleBackend::setEngine(QmlEngine *engine)
{
m_engine = engine;
}
QmlEngine *QmlJSConsoleBackend::engine() const
{
return m_engine;
}
void QmlJSConsoleBackend::setIsValidContext(bool isValidContext)
{
m_isValidContext = isValidContext;
}
bool QmlJSConsoleBackend::isValidContext() const
{
return m_isValidContext;
}
void QmlJSConsoleBackend::onDebugQueryStateChanged(
QmlJsDebugClient::QDeclarativeDebugQuery::State state)
{
QmlJsDebugClient::QDeclarativeDebugExpressionQuery *query =
qobject_cast<QmlJsDebugClient::QDeclarativeDebugExpressionQuery *>(
sender());
if (query && state != QmlJsDebugClient::QDeclarativeDebugQuery::Error)
emit message(ConsoleItemModel::UndefinedType,
query->result().toString());
else
emit message(ConsoleItemModel::ErrorType,
_("Error evaluating expression."));
delete query;
}
void QmlJSConsoleBackend::evaluate(const QString &script,
bool *returnKeyConsumed)
{
*returnKeyConsumed = true;
m_Error = false;
//Check if string is only white spaces
if (!script.trimmed().isEmpty()) {
//Check for a valid context
if (m_isValidContext) {
//check if it can be evaluated
if (canEvaluateScript(script)) {
//Evaluate expression based on engine state
//When engine->state() == InferiorStopOk, the expression
//is sent to V8DebugService. In all other cases, the
//expression is evaluated by QDeclarativeEngine.
if (!m_inferiorStopped) {
QmlAdapter *adapter = m_engine->adapter();
QTC_ASSERT(adapter, return);
QDeclarativeEngineDebug *engineDebug =
adapter->engineDebugClient();
int id = adapter->currentSelectedDebugId();
if (engineDebug && id != -1) {
QDeclarativeDebugExpressionQuery *query =
engineDebug->queryExpressionResult(id, script);
connect(query,
SIGNAL(stateChanged(
QmlJsDebugClient::QDeclarativeDebugQuery
::State)),
this,
SLOT(onDebugQueryStateChanged(
QmlJsDebugClient::QDeclarativeDebugQuery
::State)));
}
} else {
emit evaluateExpression(script);
}
} else {
*returnKeyConsumed = false;
}
} else {
//Incase of invalid context, append the expression to history
//and show Error message
m_Error = true;
}
}
}
void QmlJSConsoleBackend::emitErrorMessage()
{
if (m_Error)
emit message(
ConsoleItemModel::ErrorType,
_("Cannot evaluate without a valid QML/JS Context"));
}
bool QmlJSConsoleBackend::canEvaluateScript(const QString &script)
{
m_interpreter.clearText();
m_interpreter.appendText(script);
return m_interpreter.canEvaluate();
}
}
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#ifndef CONSOLEBACKEND_H
#define CONSOLEBACKEND_H
#include "consoleitemmodel.h"
#include "interactiveinterpreter.h"
#include <qmljsdebugclient/qdeclarativeenginedebug.h>
#include <QtCore/QObject>
namespace Debugger {
namespace Internal {
class ConsoleBackend : public QObject
{
Q_OBJECT
public:
explicit ConsoleBackend(QObject *parent = 0);
virtual void emitErrorMessage() = 0;
virtual void evaluate(const QString &, bool *returnKeyConsumed) = 0;
signals:
void message(ConsoleItemModel::ItemType itemType, const QString &msg);
};
class QmlEngine;
class QmlJSConsoleBackend : public ConsoleBackend
{
Q_OBJECT
public:
explicit QmlJSConsoleBackend(QObject *parent = 0);
void setInferiorStopped(bool inferiorStopped);
bool inferiorStopped() const;
void setEngine(QmlEngine *engine);
QmlEngine *engine() const;
void setIsValidContext(bool isValidContext);
bool isValidContext() const;
virtual void evaluate(const QString &, bool *returnKeyConsumed);
void emitErrorMessage();
private slots:
void onDebugQueryStateChanged(
QmlJsDebugClient::QDeclarativeDebugQuery::State state);
signals:
void evaluateExpression(const QString &);
private:
bool canEvaluateScript(const QString &script);
private:
QmlEngine *m_engine;
InteractiveInterpreter m_interpreter;
bool m_inferiorStopped;
bool m_isValidContext;
bool m_Error;
};
} //Internal
} //Debugger
#endif // CONSOLEBACKEND_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "consoleeditor.h"
#include "consoleitemmodel.h"
#include "consoleitemdelegate.h"
#include "consolebackend.h"
#include "debuggerstringutils.h"
#include <utils/qtcassert.h>
#include <QtGui/QMenu>
#include <QtGui/QKeyEvent>
namespace Debugger {
namespace Internal {
///////////////////////////////////////////////////////////////////////
//
// ConsoleEditor
//
///////////////////////////////////////////////////////////////////////
ConsoleEditor::ConsoleEditor(const QModelIndex &index,
ConsoleBackend *backend,
QWidget *parent) :
QTextEdit(parent),
m_consoleBackend(backend),
m_historyIndex(index),
m_prompt(QLatin1String(":/debugger/images/prompt.png")),
m_startOfEditableArea(0)
{
setFrameStyle(QFrame::NoFrame);
setUndoRedoEnabled(false);
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
document()->addResource(QTextDocument::ImageResource,
QUrl(QLatin1String("prompt")), m_prompt);
QTextImageFormat format;
format.setName(QLatin1String("prompt"));
format.setHeight(9);
format.setWidth(9);
textCursor().insertImage(format);
textCursor().insertText(QLatin1String(" "));
m_startOfEditableArea = textCursor().position();
ensureCursorVisible();
setTextInteractionFlags(Qt::TextEditorInteraction);
}
void ConsoleEditor::keyPressEvent(QKeyEvent *e)
{
bool keyConsumed = false;
switch (e->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
if (m_consoleBackend) {
m_consoleBackend->evaluate(getCurrentScript(), &keyConsumed);
if (keyConsumed) {
emit editingFinished();
emit appendEditableRow();
//emit error message if there is an error
m_consoleBackend->emitErrorMessage();
}
} else {
emit editingFinished();
emit appendEditableRow();
keyConsumed = true;
}
break;
case Qt::Key_Backspace:
if (textCursor().selectionStart() <= m_startOfEditableArea)
keyConsumed = true;
break;
case Qt::Key_Delete:
if (textCursor().selectionStart() < m_startOfEditableArea)
keyConsumed = true;
break;
case Qt::Key_Home:
{
QTextCursor c(textCursor());
c.setPosition(m_startOfEditableArea);
setTextCursor(c);
keyConsumed = true;
}
break;
case Qt::Key_Up:
handleUpKey();
keyConsumed = true;
break;
case Qt::Key_Down:
handleDownKey();
keyConsumed = true;
break;
// Ctrl+Left: Moves the cursor one word to the left.
// Left: Moves the cursor one character to the left.
case Qt::Key_Left:
if (textCursor().position() <= m_startOfEditableArea
|| e->modifiers() & Qt::ControlModifier) {
QTextCursor c(textCursor());
c.setPosition(m_startOfEditableArea);
setTextCursor(c);
keyConsumed = true;
}
break;
// Ctrl+Right: Moves the cursor one word to the right.
// Right: Moves the cursor one character to the right.
case Qt::Key_Right:
if ( !(e->modifiers() & Qt::ControlModifier)
&& textCursor().position() <= m_startOfEditableArea) {
QTextCursor c(textCursor());
c.setPosition(m_startOfEditableArea);
setTextCursor(c);
keyConsumed = true;
}
break;
// Ctrl+C, Ctrl+Insert: Allow to Copy the selected text to the clipboard.
case Qt::Key_C:
case Qt::Key_Insert:
if (textCursor().selectionStart() < m_startOfEditableArea &&
!(e->modifiers() & Qt::ControlModifier))
keyConsumed = true;
break;
default:
// Disallow any other keys in the prompt area
if (textCursor().selectionStart() < m_startOfEditableArea)
keyConsumed = true;
break;
}
if (!keyConsumed)
QTextEdit::keyPressEvent(e);
}
void ConsoleEditor::contextMenuEvent(QContextMenuEvent *event)
{
QTextCursor cursor = textCursor();
bool editable = cursor.position() > m_startOfEditableArea;
QMenu *menu = new QMenu();
QAction *a;
a = menu->addAction(tr("Cu&t"), this, SLOT(cut()));
a->setEnabled(cursor.hasSelection() && editable);
a = menu->addAction(tr("&Copy"), this, SLOT(copy()));
a->setEnabled(cursor.hasSelection());
a = menu->addAction(tr("&Paste"), this, SLOT(paste()));
a->setEnabled(canPaste() && editable);
menu->addSeparator();
a = menu->addAction(tr("Select &All"), this, SLOT(selectAll()));
a->setEnabled(!document()->isEmpty());
menu->addSeparator();
menu->addAction(tr("C&lear"), this, SLOT(clear()));
menu->exec(event->globalPos());
delete menu;
}
void ConsoleEditor::focusOutEvent(QFocusEvent * /*e*/)
{
emit editingFinished();
}
void ConsoleEditor::handleUpKey()
{
QTC_ASSERT(m_historyIndex.isValid(), return);
int currentRow = m_historyIndex.row();
const QAbstractItemModel *model = m_historyIndex.model();
if (currentRow == model->rowCount() - 1)
m_cachedScript = getCurrentScript();
while (currentRow) {
currentRow--;
if (model->hasIndex(currentRow, 0)) {
QModelIndex index = model->index(currentRow, 0);
if (ConsoleItemModel::InputType ==
(ConsoleItemModel::ItemType)model->data(
index, ConsoleItemModel::TypeRole).toInt()) {
m_historyIndex = index;
replaceCurrentScript(model->data(
index, Qt::DisplayRole).
toString());
break;
}
}
}
}
void ConsoleEditor::handleDownKey()
{
QTC_ASSERT(m_historyIndex.isValid(), return);
int currentRow = m_historyIndex.row();
const QAbstractItemModel *model = m_historyIndex.model();
while (currentRow < model->rowCount() - 1) {
currentRow++;
if (model->hasIndex(currentRow, 0)) {
QModelIndex index = model->index(currentRow, 0);
if (ConsoleItemModel::InputType ==
(ConsoleItemModel::ItemType)model->data(
index, ConsoleItemModel::TypeRole).toInt()) {
m_historyIndex = index;
if (currentRow == model->rowCount() - 1)
replaceCurrentScript(m_cachedScript);
else
replaceCurrentScript(model->data(
index, Qt::DisplayRole).
toString());
break;
}
}
}
}
QString ConsoleEditor::getCurrentScript() const
{
QTextCursor cursor = textCursor();
cursor.setPosition(m_startOfEditableArea);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
QString script = cursor.selectedText();
//remove WS
return script.trimmed();
}
void ConsoleEditor::replaceCurrentScript(const QString &script)
{
QTextCursor cursor = textCursor();
cursor.setPosition(m_startOfEditableArea);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(script);
setTextCursor(cursor);
}
} //Internal
} //Debugger
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#ifndef CONSOLEEDITOR_H
#define CONSOLEEDITOR_H
#include "consoleitemmodel.h"
#include <QtGui/QTextEdit>
#include <QtCore/QModelIndex>
namespace Debugger {
namespace Internal {
class ConsoleBackend;
class ConsoleEditor : public QTextEdit
{
Q_OBJECT
public:
explicit ConsoleEditor(const QModelIndex &index,
ConsoleBackend *backend = 0,
QWidget *parent = 0);
QString getCurrentScript() const;
protected:
void keyPressEvent(QKeyEvent *e);
void contextMenuEvent(QContextMenuEvent *event);
void focusOutEvent(QFocusEvent *e);
signals:
void editingFinished();
void appendEditableRow();
protected:
void handleUpKey();
void handleDownKey();
void replaceCurrentScript(const QString &script);
private:
ConsoleBackend *m_consoleBackend;
QModelIndex m_historyIndex;
QString m_cachedScript;
QImage m_prompt;
int m_startOfEditableArea;
};
} //Internal
} //Debugger
#endif // CONSOLEEDITOR_H
This diff is collapsed.
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**