scriptconsole.cpp 7.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "scriptconsole.h"

#include <QtCore/QDebug>
#include <QtGui/QVBoxLayout>
#include <QtGui/QDockWidget>
#include <qmljseditor/qmljshighlighter.h>
#include <utils/styledbar.h>
#include <utils/filterlineedit.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>

namespace Debugger {
namespace Internal {

ScriptConsole::ScriptConsole(QWidget *parent)
    : QWidget(parent),
      m_textEdit(new QPlainTextEdit),
      m_lineEdit(0)
{

//    m_prompt = QLatin1String(">");

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(m_textEdit);
    m_textEdit->setFrameStyle(QFrame::NoFrame);

    //updateTitle();

    /*m_highlighter = new QmlJSEditor::Highlighter(m_textEdit->document());
    m_highlighter->setParent(m_textEdit->document());*/

    Utils::StyledBar *bar = new Utils::StyledBar;
    m_lineEdit = new Utils::FilterLineEdit;

    m_lineEdit->setPlaceholderText(tr("<Type expression to evaluate>"));
    m_lineEdit->setToolTip(tr("Write and evaluate QtScript expressions."));

    /*m_clearButton = new QToolButton();
    m_clearButton->setToolTip(tr("Clear Output"));
    m_clearButton->setIcon(QIcon(Core::Constants::ICON_CLEAN_PANE));
    connect(m_clearButton, SIGNAL(clicked()), this, SLOT(clearTextEditor()));*/
    //connect(m_lineEdit, SIGNAL(textChanged(QString)), SLOT(changeContextHelpId(QString)));

    connect(m_lineEdit, SIGNAL(returnPressed()), SLOT(executeExpression()));
    QHBoxLayout *hbox = new QHBoxLayout(bar);
    hbox->setMargin(1);
    hbox->setSpacing(1);
    hbox->addWidget(m_lineEdit);
    //hbox->addWidget(m_clearButton);
    layout->addWidget(bar);

    m_textEdit->setReadOnly(true);
    m_lineEdit->installEventFilter(this);

    setFontSettings();
}


void ScriptConsole::setFontSettings()
{
    const TextEditor::FontSettings &fs = TextEditor::TextEditorSettings::instance()->fontSettings();
    static QVector<QString> categories;
    if (categories.isEmpty()) {
        categories << QLatin1String(TextEditor::Constants::C_NUMBER)
                << QLatin1String(TextEditor::Constants::C_STRING)
                << QLatin1String(TextEditor::Constants::C_TYPE)
                << QLatin1String(TextEditor::Constants::C_KEYWORD)
                << QLatin1String(TextEditor::Constants::C_LABEL)
                << QLatin1String(TextEditor::Constants::C_COMMENT)
                << QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
    }

    const QVector<QTextCharFormat> formats = fs.toTextCharFormats(categories);
/*    m_highlighter->setFormats(formats);
    m_highlighter->rehighlight();*/
    m_textEdit->setFont(fs.font());
    m_lineEdit->setFont(fs.font());
}


void ScriptConsole::clear()
{
    clearTextEditor();

    if (m_lineEdit)
        m_lineEdit->clear();
//    appendPrompt();
}

void ScriptConsole::clearTextEditor()
{
    m_textEdit->clear();
    m_textEdit->appendPlainText(tr("Script Console\n"));
}


/*void ExpressionQueryWidget::updateTitle()
{
    if (m_currObject.debugId() < 0) {
        m_title = tr("Expression queries");
    } else {
        QString desc = QLatin1String("<")
            + m_currObject.className() + QLatin1String(": ")
            + (m_currObject.name().isEmpty() ? QLatin1String("<unnamed>") : m_currObject.name())
            + QLatin1String(">");
        m_title = tr("Expression queries (using context for %1)" , "Selected object").arg(desc);
    }
}*/
/*
void ExpressionQueryWidget::appendPrompt()
{
    m_textEdit->moveCursor(QTextCursor::End);

    if (m_mode == SeparateEntryMode) {
        m_textEdit->insertPlainText("\n");
    } else {
        m_textEdit->appendPlainText(m_prompt);
    }
}
*/



bool ScriptConsole::eventFilter(QObject* obj, QEvent* event)
{
    if (obj == m_textEdit) {
        switch (event->type()) {
            case QEvent::KeyPress:
            {
                QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
                int key = keyEvent->key();
                if (key == Qt::Key_Return || key == Qt::Key_Enter) {
                    executeExpression();
                    return true;
                } else if (key == Qt::Key_Backspace) {
                    // ensure m_expr doesn't contain backspace characters
                    QTextCursor cursor = m_textEdit->textCursor();
                    bool atLastLine = !(cursor.block().next().isValid());
                    if (!atLastLine)
                        return true;
                    if (cursor.positionInBlock() <= m_prompt.count())
                        return true;
                    cursor.deletePreviousChar();
                    m_expr = cursor.block().text().mid(m_prompt.count());
                    return true;
                } else {
                    m_textEdit->moveCursor(QTextCursor::End);
                    m_expr += keyEvent->text();
                }
                break;
            }
            case QEvent::FocusIn:
                //checkCurrentContext();
                m_textEdit->moveCursor(QTextCursor::End);
                break;
            default:
                break;
        }
    } else if (obj == m_lineEdit) {
        switch (event->type()) {
            case QEvent::KeyPress:
            {
                QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
                int key = keyEvent->key();
                if (key == Qt::Key_Up && m_lineEdit->text() != m_lastExpr) {
                    m_expr = m_lineEdit->text();
                    if (!m_lastExpr.isEmpty())
                        m_lineEdit->setText(m_lastExpr);
                } else if (key == Qt::Key_Down) {
                    m_lineEdit->setText(m_expr);
                }
                break;
            }
            case QEvent::FocusIn:
               // checkCurrentContext();
                break;
            default:
                break;
        }
    }
    return QWidget::eventFilter(obj, event);
}

void ScriptConsole::executeExpression()
{
    m_expr = m_lineEdit->text().trimmed();
    m_expr = m_expr.trimmed();
    if (!m_expr.isEmpty()) {
        emit expressionEntered(m_expr);
        m_lastExpr = m_expr;
        if (m_lineEdit)
            m_lineEdit->clear();
    }
}

void ScriptConsole::appendResult(const QString& result)
{
    m_textEdit->moveCursor(QTextCursor::End);
    m_textEdit->insertPlainText(m_expr + " : ");
    m_textEdit->insertPlainText(result);
    m_textEdit->insertPlainText("\n");
    m_expr.clear();
}

}
}