Okular

kjs_ocg.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 João Netto <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "kjs_ocg_p.h"
8 
9 #include <kjs/kjsinterpreter.h>
10 #include <kjs/kjsobject.h>
11 #include <kjs/kjsprototype.h>
12 
13 #include <QAbstractItemModel>
14 #include <QDebug>
15 #include <QPair>
16 #include <QString>
17 
18 using namespace Okular;
19 
20 static KJSPrototype *g_OCGProto;
21 
23 Q_GLOBAL_STATIC(OCGCache, g_OCGCache)
24 
25 // OCG.state (getter)
26 static KJSObject OCGGetState(KJSContext *, void *object)
27 {
28  QPair<int, int> *pair = reinterpret_cast<QPair<int, int> *>(object);
29  QAbstractItemModel *model = g_OCGCache->value(pair);
30 
31  const QModelIndex index = model->index(pair->first, pair->second);
32 
33  const bool state = model->data(index, Qt::CheckStateRole).toBool();
34 
35  return KJSBoolean(state);
36 }
37 
38 // OCG.state (setter)
39 static void OCGSetState(KJSContext *ctx, void *object, KJSObject value)
40 {
41  QPair<int, int> *pair = reinterpret_cast<QPair<int, int> *>(object);
42  QAbstractItemModel *model = g_OCGCache->value(pair);
43 
44  const QModelIndex index = model->index(pair->first, pair->second);
45 
46  const bool state = value.toBoolean(ctx);
47 
48  model->setData(index, QVariant(state ? Qt::Checked : Qt::Unchecked), Qt::CheckStateRole);
49 }
50 
51 void JSOCG::initType(KJSContext *ctx)
52 {
53  static bool initialized = false;
54  if (initialized) {
55  return;
56  }
57  initialized = true;
58 
59  g_OCGProto = new KJSPrototype();
60 
61  g_OCGProto->defineProperty(ctx, QStringLiteral("state"), OCGGetState, OCGSetState);
62 }
63 
64 KJSObject JSOCG::object(KJSContext *ctx)
65 {
66  return g_OCGProto->constructObject(ctx, nullptr);
67 }
68 
69 KJSObject JSOCG::wrapOCGObject(KJSContext *ctx, QAbstractItemModel *model, const int i, const int j)
70 {
71  QPair<int, int> *pair = new QPair<int, int>(i, j);
72  g_OCGCache->insert(pair, model);
73  return g_OCGProto->constructObject(ctx, pair);
74 }
75 
76 void JSOCG::clearCachedFields()
77 {
78  if (g_OCGCache.exists()) {
79  g_OCGCache->clear();
80  }
81 }
bool toBoolean(KJSContext *ctx)
CheckStateRole
The documentation to the global Okular namespace.
Definition: action.h:16
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
void defineProperty(KJSContext *ctx, const QString &name, PropertyGetter getter, PropertySetter setter=nullptr)
KJSObject constructObject(KJSContext *ctx, void *internalValue=nullptr)
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.