compile
[kdegraphics.git] / okular / core / script / kjs_console.cpp
blob01a36534469976fb9addaed271a9cc0b9b18049e
1 /***************************************************************************
2 * Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
3 * Copyright (C) 2008 by Harri Porten <porten@kde.org> *
4 * *
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>
17 #include <kdebug.h>
19 #include "../debug_p.h"
21 using namespace Okular;
23 static KJSPrototype *g_consoleProto;
25 #ifdef OKULAR_JS_CONSOLE
27 #include <qlayout.h>
28 #include <qplaintextedit.h>
30 #include <kdialog.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() )
39 return;
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() )
65 return;
67 g_jsConsoleWindow->hide();
70 static void clearConsole()
72 if ( !g_jsConsoleWindow.exists() )
73 return;
75 g_jsConsoleLog->clear();
78 static void outputToConsole( const QString &message )
80 showConsole();
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 & )
107 clearConsole();
108 return KJSUndefined();
111 static KJSObject consoleHide( KJSContext *, void *, const KJSArguments & )
113 hideConsole();
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 & )
128 showConsole();
129 return KJSUndefined();
132 void JSConsole::initType( KJSContext *ctx )
134 static bool initialized = false;
135 if ( initialized )
136 return;
137 initialized = true;
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 );