Debian Codegraph @

src/scripts/console.js

raw symbols SCIP
1 // Shamelessly stole from Amarok.
2
3 function globalScopeEval( inputCode )
4 {
5 try
6 {
7 result = eval( inputCode ) + " ";
8 }
9 catch( error )
10 {
11 result = error + " ";
12 }
13 return result;
14 }
15
16 function ScriptConsoleMainWindow()
17 {
18 QMainWindow.call( this, null );
19
20 var mainWidget = new QWidget( this );
21 this.historyList = new QListWidget( mainWidget );
22 this.historyList.sizeHint = new QSize(900, 600);
23 this.historyList.size = new QSize(900, 600);
24 this.historyList.verticalScrollMode = QAbstractItemView.ScrollPerPixel;
25 this.inputLine = new QTextEdit( mainWidget );
26 this.executeButton = new QPushButton( mainWidget );
27 this.commandArray = [];
28 this.commandPos = -1;
29 this.executeButton.clicked.connect( this, this.executeLine );
30 this.executeButton.text = qsTr("Execute Code");
31 this.windowTitle = qsTr("QTerm Script Console");
32 //the following line doesn't work for some reason:
33 //executeButton.shortcut = new QKeySequence( "CTRL+Return" );
34
35 var executeShortcut = new QShortcut( this );
36 executeShortcut.key = new QKeySequence( "CTRL+Return" );
37 executeShortcut.activated.connect( this, this.executeLine );
38
39 var backHistoryShortcut = new QShortcut( this );
40 backHistoryShortcut.key = new QKeySequence( QKeySequence.StandardKey( QKeySequence.MoveToPreviousPage ) );
41 backHistoryShortcut.activated.connect( this, this.backHistory );
42
43 var forwardHistoryShortcut = new QShortcut( this );
44 forwardHistoryShortcut.key = new QKeySequence( QKeySequence.StandardKey( QKeySequence.MoveToNextPage ) );
45 forwardHistoryShortcut.activated.connect( this, this.forwardHistory );
46
47 var explanationItem = new QListWidgetItem(qsTr(
48 "The QTerm Script Console allows you to easily execute JavaScript with access to\nall functions and methods you would have in an QTerm script.Information on\nscripting for QTerm can be found in the doc directory of the QTerm source code.\nExecute code: CTRL-Enter\nBack in code history: Page Up\nForward in code history: Page Down"));
49 //explanationItem.setForeground( new QBrush( new QColor(Qt.darkGray) ) );
50 explanationItem.setFlags( !Qt.ItemIsSelectable );
51 this.historyList.addItem( explanationItem );
52
53 var layout = new QGridLayout( mainWidget );
54 layout.addWidget( this.historyList, 0, 0, 1, 2 );
55 layout.addWidget( this.inputLine, 1, 0, 3, 2 );
56 layout.addWidget( this.executeButton, 4, 1 );
57 mainWidget.setLayout( layout );
58 this.setCentralWidget( mainWidget );
59 this.resize(750, 400);
60 this.hide();
61 }
62
63 ScriptConsoleMainWindow.prototype = new QMainWindow();
64
65 ScriptConsoleMainWindow.prototype.executeLine = function()
66 {
67 command = new QListWidgetItem( this.inputLine.plainText );
68 this.commandArray.unshift( this.inputLine.plainText );
69 boldFont = new QFont();
70 boldFont.setBold( true );
71 command.setFont( boldFont );
72 result = globalScopeEval.call( null, this.inputLine.plainText );
73 resultsRow = new QListWidgetItem( result );
74 this.historyList.addItem( command );
75 this.historyList.addItem( resultsRow );
76 this.historyList.setCurrentItem( resultsRow );
77 this.inputLine.plainText = "";
78 this.commandPos = -1;
79 }
80
81 ScriptConsoleMainWindow.prototype.backHistory = function()
82 {
83 if( this.commandArray.length > ( this.commandPos + 1 ) )
84 {
85 this.commandPos++;
86 }
87 if( this.commandArray.length > 0 )
88 {
89 this.inputLine.plainText = this.commandArray[ this.commandPos ];
90 }
91 }
92
93 ScriptConsoleMainWindow.prototype.forwardHistory = function()
94 {
95 if( this.commandArray.length > 0 )
96 {
97 if( this.commandPos > 0 )
98 {
99 this.commandPos--;
100 }
101 this.inputLine.plainText = this.commandArray[ this.commandPos ];
102 }
103 }
104
105 scriptConsoleMainWindow = new ScriptConsoleMainWindow();
106 if (QTerm.addPopupMenu( "scriptConsole", qsTr("Script Console...") ) ) {
107 QTerm.scriptConsole.triggered.connect(scriptConsoleMainWindow.show);
108 }
109