1 /***************************************************************************
2 * Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
3 * Copyright (C) 2008 by Harri Porten <porten@kde.org> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #include "kjs_console_p.h"
13 #include <kjs/kjsobject.h>
14 #include <kjs/kjsprototype.h>
15 #include <kjs/kjsarguments.h>
19 #include "../debug_p.h"
21 using namespace Okular
;
23 static KJSPrototype
*g_consoleProto
;
25 #ifdef OKULAR_JS_CONSOLE
28 #include <qplaintextedit.h>
31 #include <kstandardguiitem.h>
33 K_GLOBAL_STATIC( KDialog
, g_jsConsoleWindow
)
34 static QPlainTextEdit
*g_jsConsoleLog
= 0;
36 static void createConsoleWindow()
38 if ( g_jsConsoleWindow
.exists() )
41 g_jsConsoleWindow
->setButtons( KDialog::Close
| KDialog::User1
);
42 g_jsConsoleWindow
->setButtonGuiItem( KDialog::User1
, KStandardGuiItem::clear() );
44 QVBoxLayout
*mainLay
= new QVBoxLayout( g_jsConsoleWindow
->mainWidget() );
45 mainLay
->setMargin( 0 );
46 g_jsConsoleLog
= new QPlainTextEdit( g_jsConsoleWindow
->mainWidget() );
47 g_jsConsoleLog
->setReadOnly( true );
48 mainLay
->addWidget( g_jsConsoleLog
);
50 QObject::connect( g_jsConsoleWindow
, SIGNAL( closeClicked() ),
51 g_jsConsoleWindow
, SLOT( close() ) );
52 QObject::connect( g_jsConsoleWindow
, SIGNAL( user1Clicked() ),
53 g_jsConsoleLog
, SLOT( clear() ) );
56 static void showConsole()
58 createConsoleWindow();
59 g_jsConsoleWindow
->show();
62 static void hideConsole()
64 if ( !g_jsConsoleWindow
.exists() )
67 g_jsConsoleWindow
->hide();
70 static void clearConsole()
72 if ( !g_jsConsoleWindow
.exists() )
75 g_jsConsoleLog
->clear();
78 static void outputToConsole( const QString
&message
)
81 g_jsConsoleLog
->appendPlainText( message
);
84 #else /* OKULAR_JS_CONSOLE */
86 static void showConsole()
90 static void hideConsole()
94 static void clearConsole()
98 static void outputToConsole( const QString
&cMessage
)
100 kDebug(OkularDebug
) << "CONSOLE:" << cMessage
;
103 #endif /* OKULAR_JS_CONSOLE */
105 static KJSObject
consoleClear( KJSContext
*, void *, const KJSArguments
& )
108 return KJSUndefined();
111 static KJSObject
consoleHide( KJSContext
*, void *, const KJSArguments
& )
114 return KJSUndefined();
117 static KJSObject
consolePrintln( KJSContext
*ctx
, void *,
118 const KJSArguments
&arguments
)
120 QString cMessage
= arguments
.at( 0 ).toString( ctx
);
121 outputToConsole( cMessage
);
123 return KJSUndefined();
126 static KJSObject
consoleShow( KJSContext
*, void *, const KJSArguments
& )
129 return KJSUndefined();
132 void JSConsole::initType( KJSContext
*ctx
)
134 static bool initialized
= false;
139 g_consoleProto
= new KJSPrototype();
141 g_consoleProto
->defineFunction( ctx
, "clear", consoleClear
);
142 g_consoleProto
->defineFunction( ctx
, "hide", consoleHide
);
143 g_consoleProto
->defineFunction( ctx
, "println", consolePrintln
);
144 g_consoleProto
->defineFunction( ctx
, "hide", consoleShow
);
147 KJSObject
JSConsole::object( KJSContext
*ctx
)
149 return g_consoleProto
->constructObject( ctx
, 0 );