scriptingwidget.cpp 4.83 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2015, all rights reserved,
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
// 
// ================================================================================================

#include "scriptingwidget.h"

27
28
#include <QCompleter>
#include <QDirModel>
29
30
#include <QKeyEvent>

31
32
33
34
namespace campvis {

    ScriptingWidget::ScriptingWidget(QWidget* parent)
        : QWidget(parent)
35
36
37
38
39
        , _consoleDisplay(nullptr)
        , _editCommand(nullptr)
        , _btnExecute(nullptr)
        , _btnClear(nullptr)
        , _currentPosition(-1)
40
41
    {
        setupGUI();
42
        addCat("", true, cgt::LuaInfo);
43
44
45
46
47
    }

    ScriptingWidget::~ScriptingWidget() {
    }

48
49
50
51
52
53
54
55
    void ScriptingWidget::init() {
        LogMgr.addLog(this);
    }

    void ScriptingWidget::deinit() {
        LogMgr.removeLog(this);
    }

56
57
58
    void ScriptingWidget::setupGUI() {
        setWindowTitle(tr("Scripting Console"));

59
60
61
62
63
        QHBoxLayout* mainLayout = new QHBoxLayout(this);

        QVBoxLayout* leftColumnLayout = new QVBoxLayout();
        mainLayout->addLayout(leftColumnLayout);

64
        QHBoxLayout* controlsLayout = new QHBoxLayout();
65
        leftColumnLayout->addLayout(controlsLayout);
66
67
68

        _consoleDisplay = new QTextEdit(this);
        _consoleDisplay->setReadOnly(true);
69
        leftColumnLayout->addWidget(_consoleDisplay);
70
71
72
73
74
75
76

        // Use the system's default monospace font at the default size in the log viewer
        QFont monoFont = QFont("Monospace");
        monoFont.setStyleHint(QFont::TypeWriter);
        monoFont.setPointSize(QFont().pointSize() + 1);

        _consoleDisplay->document()->setDefaultFont(monoFont);
77
        _editCommand = new CompletingLuaLineEdit(nullptr, this);
78
        _editCommand->setPlaceholderText(tr("Enter Lua commands here..."));
79
        _editCommand->installEventFilter(this);
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        controlsLayout->addWidget(_editCommand);

        _btnExecute = new QPushButton(tr("&Execute"), this);
        controlsLayout->addWidget(_btnExecute);

        _btnClear = new QPushButton(tr("&Clear"), this);
        controlsLayout->addWidget(_btnClear);


        connect(_btnClear, SIGNAL(clicked()), this, SLOT(clearLog()));
        connect(_btnExecute, SIGNAL(clicked()), this, SLOT(execute()));
        connect(_editCommand, SIGNAL(returnPressed()), this, SLOT(execute()));
    }

    void ScriptingWidget::appendMessage(const QString& message) {
95
        _consoleDisplay->append(message);
96
97
98
99
100
101
102
103
    }

    void ScriptingWidget::clearLog() {
        _consoleDisplay->clear();
    }

    void ScriptingWidget::execute() {
        QString command = _editCommand->text();
104
105
        appendMessage(tr("> ") + command);

106
        emit s_commandExecuted(command);
107

108
109
        _history.push_front(command);
        _currentPosition = -1;
110
111
112
        _editCommand->clear();
    }

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    bool ScriptingWidget::eventFilter(QObject* obj, QEvent* event) {
        if (obj == _editCommand && event->type() == QEvent::KeyPress) {
            QKeyEvent* e = static_cast<QKeyEvent*>(event);
            if (e->key() == Qt::Key_Up && _currentPosition < static_cast<int>(_history.size())-1) {
                ++_currentPosition;
                _editCommand->setText(_history[_currentPosition]);
                return true;
            }
            if (e->key() == Qt::Key_Down && _currentPosition >= 0) {
                --_currentPosition;
                if (_currentPosition >= 0)
                    _editCommand->setText(_history[_currentPosition]);
                else
                    _editCommand->setText(tr(""));

                return true;
            }
        }

        return QObject::eventFilter(obj, event);
    }

135
136
    void ScriptingWidget::logFiltered(const std::string &cat, cgt::LogLevel level, const std::string& msg, const std::string& extendedInfo/*=""*/) {
        if (level == cgt::LuaInfo || level == cgt::LuaError) {
137
138
139
140
141
            appendMessage(QString::fromStdString(msg));
        }
    }


142
143


144
}