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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/***************************************************************************
* Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
* Copyright (C) 2008 by Harri Porten <porten@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "kjs_console_p.h"
#include <kjs/kjsobject.h>
#include <kjs/kjsprototype.h>
#include <kjs/kjsarguments.h>
#include <kdebug.h>
#include "../debug_p.h"
using namespace Okular;
static KJSPrototype *g_consoleProto;
#ifdef OKULAR_JS_CONSOLE
#include <qlayout.h>
#include <qplaintextedit.h>
#include <kdialog.h>
#include <kstandardguiitem.h>
K_GLOBAL_STATIC( KDialog, g_jsConsoleWindow )
static QPlainTextEdit *g_jsConsoleLog = 0;
static void createConsoleWindow()
{
if ( g_jsConsoleWindow.exists() )
return;
g_jsConsoleWindow->setButtons( KDialog::Close | KDialog::User1 );
g_jsConsoleWindow->setButtonGuiItem( KDialog::User1, KStandardGuiItem::clear() );
QVBoxLayout *mainLay = new QVBoxLayout( g_jsConsoleWindow->mainWidget() );
mainLay->setMargin( 0 );
g_jsConsoleLog = new QPlainTextEdit( g_jsConsoleWindow->mainWidget() );
g_jsConsoleLog->setReadOnly( true );
mainLay->addWidget( g_jsConsoleLog );
QObject::connect( g_jsConsoleWindow, SIGNAL(closeClicked()),
g_jsConsoleWindow, SLOT(close()) );
QObject::connect( g_jsConsoleWindow, SIGNAL(user1Clicked()),
g_jsConsoleLog, SLOT(clear()) );
}
static void showConsole()
{
createConsoleWindow();
g_jsConsoleWindow->show();
}
static void hideConsole()
{
if ( !g_jsConsoleWindow.exists() )
return;
g_jsConsoleWindow->hide();
}
static void clearConsole()
{
if ( !g_jsConsoleWindow.exists() )
return;
g_jsConsoleLog->clear();
}
static void outputToConsole( const QString &message )
{
showConsole();
g_jsConsoleLog->appendPlainText( message );
}
#else /* OKULAR_JS_CONSOLE */
static void showConsole()
{
}
static void hideConsole()
{
}
static void clearConsole()
{
}
static void outputToConsole( const QString &cMessage )
{
kDebug(OkularDebug) << "CONSOLE:" << cMessage;
}
#endif /* OKULAR_JS_CONSOLE */
static KJSObject consoleClear( KJSContext *, void *, const KJSArguments & )
{
clearConsole();
return KJSUndefined();
}
static KJSObject consoleHide( KJSContext *, void *, const KJSArguments & )
{
hideConsole();
return KJSUndefined();
}
static KJSObject consolePrintln( KJSContext *ctx, void *,
const KJSArguments &arguments )
{
QString cMessage = arguments.at( 0 ).toString( ctx );
outputToConsole( cMessage );
return KJSUndefined();
}
static KJSObject consoleShow( KJSContext *, void *, const KJSArguments & )
{
showConsole();
return KJSUndefined();
}
void JSConsole::initType( KJSContext *ctx )
{
static bool initialized = false;
if ( initialized )
return;
initialized = true;
g_consoleProto = new KJSPrototype();
g_consoleProto->defineFunction( ctx, "clear", consoleClear );
g_consoleProto->defineFunction( ctx, "hide", consoleHide );
g_consoleProto->defineFunction( ctx, "println", consolePrintln );
g_consoleProto->defineFunction( ctx, "hide", consoleShow );
}
KJSObject JSConsole::object( KJSContext *ctx )
{
return g_consoleProto->constructObject( ctx, 0 );
}
|