Okular

kjs_console.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Pino Toscano <[email protected]>
3  SPDX-FileCopyrightText: 2008 Harri Porten <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "kjs_console_p.h"
9 
10 #include <kjs/kjsarguments.h>
11 #include <kjs/kjsobject.h>
12 #include <kjs/kjsprototype.h>
13 
14 #include <QDebug>
15 
16 #include "../debug_p.h"
17 
18 using namespace Okular;
19 
20 static KJSPrototype *g_consoleProto;
21 
22 #ifdef OKULAR_JS_CONSOLE
23 
24 #include <QLayout>
25 #include <QPlainTextEdit>
26 
27 #include <KDialog>
28 #include <KStandardGuiItem>
29 
30 K_GLOBAL_STATIC(KDialog, g_jsConsoleWindow)
31 static QPlainTextEdit *g_jsConsoleLog = 0;
32 
33 static void createConsoleWindow()
34 {
35  if (g_jsConsoleWindow.exists())
36  return;
37 
38  g_jsConsoleWindow->setButtons(KDialog::Close | KDialog::User1);
39  g_jsConsoleWindow->setButtonGuiItem(KDialog::User1, KStandardGuiItem::clear());
40 
41  QVBoxLayout *mainLay = new QVBoxLayout(g_jsConsoleWindow->mainWidget());
42  mainLay->setContentsMargins(0, 0, 0, 0);
43  g_jsConsoleLog = new QPlainTextEdit(g_jsConsoleWindow->mainWidget());
44  g_jsConsoleLog->setReadOnly(true);
45  mainLay->addWidget(g_jsConsoleLog);
46 
47  QObject::connect(g_jsConsoleWindow, SIGNAL(closeClicked()), g_jsConsoleWindow, SLOT(close()));
48  QObject::connect(g_jsConsoleWindow, SIGNAL(user1Clicked()), g_jsConsoleLog, SLOT(clear()));
49 }
50 
51 static void showConsole()
52 {
53  createConsoleWindow();
54  g_jsConsoleWindow->show();
55 }
56 
57 static void hideConsole()
58 {
59  if (!g_jsConsoleWindow.exists())
60  return;
61 
62  g_jsConsoleWindow->hide();
63 }
64 
65 static void clearConsole()
66 {
67  if (!g_jsConsoleWindow.exists())
68  return;
69 
70  g_jsConsoleLog->clear();
71 }
72 
73 static void outputToConsole(const QString &message)
74 {
75  showConsole();
76  g_jsConsoleLog->appendPlainText(message);
77 }
78 
79 #else /* OKULAR_JS_CONSOLE */
80 
81 static void showConsole()
82 {
83 }
84 
85 static void hideConsole()
86 {
87 }
88 
89 static void clearConsole()
90 {
91 }
92 
93 static void outputToConsole(const QString &cMessage)
94 {
95  qCDebug(OkularCoreDebug) << "CONSOLE:" << cMessage;
96 }
97 
98 #endif /* OKULAR_JS_CONSOLE */
99 
100 static KJSObject consoleClear(KJSContext *, void *, const KJSArguments &)
101 {
102  clearConsole();
103  return KJSUndefined();
104 }
105 
106 static KJSObject consoleHide(KJSContext *, void *, const KJSArguments &)
107 {
108  hideConsole();
109  return KJSUndefined();
110 }
111 
112 static KJSObject consolePrintln(KJSContext *ctx, void *, const KJSArguments &arguments)
113 {
114  QString cMessage = arguments.at(0).toString(ctx);
115  outputToConsole(cMessage);
116 
117  return KJSUndefined();
118 }
119 
120 static KJSObject consoleShow(KJSContext *, void *, const KJSArguments &)
121 {
122  showConsole();
123  return KJSUndefined();
124 }
125 
126 void JSConsole::initType(KJSContext *ctx)
127 {
128  static bool initialized = false;
129  if (initialized) {
130  return;
131  }
132  initialized = true;
133 
134  g_consoleProto = new KJSPrototype();
135 
136  g_consoleProto->defineFunction(ctx, QStringLiteral("clear"), consoleClear);
137  g_consoleProto->defineFunction(ctx, QStringLiteral("hide"), consoleHide);
138  g_consoleProto->defineFunction(ctx, QStringLiteral("println"), consolePrintln);
139  g_consoleProto->defineFunction(ctx, QStringLiteral("hide"), consoleShow);
140 }
141 
142 KJSObject JSConsole::object(KJSContext *ctx)
143 {
144  return g_consoleProto->constructObject(ctx, nullptr);
145 }
The documentation to the global Okular namespace.
Definition: action.h:16
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setReadOnly(bool ro)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void appendPlainText(const QString &text)
KGuiItem clear()
KJSObject at(int idx) const
QAction * close(const QObject *recvr, const char *slot, QObject *parent)
void defineFunction(KJSContext *ctx, const QString &name, FunctionCall callback)
QAction * clear(const QObject *recvr, const char *slot, QObject *parent)
KJSObject constructObject(KJSContext *ctx, void *internalValue=nullptr)
void setContentsMargins(int left, int top, int right, int bottom)
QString toString(KJSContext *ctx)
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Mar 23 2023 04:04:24 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.