2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

scriptingwidget.cpp 4.65 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
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
//      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 <QKeyEvent>

29
30
31
32
namespace campvis {

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

    ScriptingWidget::~ScriptingWidget() {
    }

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

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

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    void ScriptingWidget::setupGUI() {
        setWindowTitle(tr("Scripting Console"));

        QVBoxLayout* mainLayout = new QVBoxLayout(this);

        QHBoxLayout* controlsLayout = new QHBoxLayout();
        mainLayout->addLayout(controlsLayout);

        _consoleDisplay = new QTextEdit(this);
        _consoleDisplay->setReadOnly(true);
        mainLayout->addWidget(_consoleDisplay);

        // 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);
        _editCommand = new QLineEdit(this);
        _editCommand->setPlaceholderText(tr("Enter Lua commands here..."));
74
        _editCommand->installEventFilter(this);
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        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) {
90
        _consoleDisplay->append(message);
91
92
93
94
95
96
97
98
    }

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

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

101
        emit s_commandExecuted(command);
102

103
104
        _history.push_front(command);
        _currentPosition = -1;
105
106
107
        _editCommand->clear();
    }

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    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);
    }

130
131
132
133
134
135
136
    void ScriptingWidget::logFiltered(const std::string &cat, tgt::LogLevel level, const std::string& msg, const std::string& extendedInfo/*=""*/) {
        if (level == tgt::LuaInfo || level == tgt::LuaError) {
            appendMessage(QString::fromStdString(msg));
        }
    }


137
138


139
}