Changeset View
Changeset View
Standalone View
Standalone View
core/script/kjs_ocg.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by João Netto <joaonetto901@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | ***************************************************************************/ | ||||
9 | | ||||
10 | #include "kjs_ocg_p.h" | ||||
11 | | ||||
12 | #include <kjs/kjsobject.h> | ||||
13 | #include <kjs/kjsprototype.h> | ||||
14 | #include <kjs/kjsinterpreter.h> | ||||
15 | | ||||
16 | #include <QString> | ||||
17 | #include <QAbstractItemModel> | ||||
18 | #include <QDebug> | ||||
19 | #include <QPair> | ||||
20 | | ||||
21 | #include <memory> | ||||
22 | | ||||
23 | using namespace Okular; | ||||
24 | | ||||
25 | std::unique_ptr < KJSPrototype > g_OCGProto; | ||||
26 | | ||||
27 | typedef QHash< QPair< int, int > *, QAbstractItemModel* > OCGCache; | ||||
28 | Q_GLOBAL_STATIC( OCGCache, g_OCGCache ) | ||||
29 | | ||||
30 | // OCG.state (getter) | ||||
31 | static KJSObject OCGGetState( KJSContext *, void *object ) | ||||
32 | { | ||||
33 | QPair< int, int > *pair = reinterpret_cast< QPair< int, int >* > ( object ); | ||||
34 | QAbstractItemModel *model = g_OCGCache->value( pair ); | ||||
35 | | ||||
36 | QModelIndex index = model->index( pair->first, pair->second ); | ||||
37 | | ||||
38 | bool state = model->data( index, Qt::CheckStateRole ).toBool(); | ||||
39 | | ||||
40 | return KJSBoolean( state ); | ||||
41 | } | ||||
42 | | ||||
43 | // OCG.state (setter) | ||||
44 | static void OCGSetState( KJSContext* ctx, void* object, | ||||
45 | KJSObject value ) | ||||
46 | { | ||||
47 | QPair< int, int > *pair = reinterpret_cast< QPair< int, int >* > ( object ); | ||||
48 | QAbstractItemModel *model = g_OCGCache->value( pair ); | ||||
49 | | ||||
50 | QModelIndex index = model->index( pair->first, pair->second ); | ||||
51 | | ||||
52 | bool state = value.toBoolean( ctx ); | ||||
53 | | ||||
54 | model->setData( index, QVariant( state ? Qt::Checked : Qt::Unchecked ), Qt::CheckStateRole ); | ||||
55 | } | ||||
56 | | ||||
57 | | ||||
58 | void JSOCG::initType( KJSContext *ctx ) | ||||
59 | { | ||||
60 | if ( g_OCGProto ) | ||||
61 | return; | ||||
62 | | ||||
63 | g_OCGProto.reset(new KJSPrototype); | ||||
64 | | ||||
65 | g_OCGProto->defineProperty( ctx, QStringLiteral("state"), OCGGetState, OCGSetState ); | ||||
66 | } | ||||
67 | | ||||
68 | KJSObject JSOCG::object( KJSContext *ctx ) | ||||
69 | { | ||||
70 | return g_OCGProto->constructObject( ctx, nullptr ); | ||||
71 | } | ||||
72 | | ||||
73 | KJSObject JSOCG::wrapOCGObject( KJSContext *ctx, QAbstractItemModel *model, int i, int j ) | ||||
74 | { | ||||
75 | QPair< int, int > *pair = new QPair< int ,int >( i, j ); | ||||
76 | g_OCGCache->insert( pair, model ); | ||||
77 | return g_OCGProto->constructObject( ctx, pair ); | ||||
78 | } | ||||
79 | | ||||
80 | void JSOCG::clearCachedFields() | ||||
81 | { | ||||
82 | if ( g_OCGCache.exists() ) | ||||
83 | { | ||||
84 | g_OCGCache->clear(); | ||||
85 | } | ||||
86 | } | ||||
87 | No newline at end of file |