Okular

js_document.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Pino Toscano <pino@kde.org>
3 SPDX-FileCopyrightText: 2008 Harri Porten <porten@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "js_document_p.h"
9
10#include <qwidget.h>
11
12#include <QDebug>
13#include <QJSEngine>
14#include <assert.h>
15
16#include "../document_p.h"
17#include "../form.h"
18#include "../page.h"
19#include "js_data_p.h"
20#include "js_field_p.h"
21#include "js_ocg_p.h"
22
23using namespace Okular;
24
25// Document.numPages
26int JSDocument::numPages() const
27{
28 return m_doc->m_pagesVector.count();
29}
30
31// Document.pageNum (getter)
32int JSDocument::pageNum() const
33{
34 return m_doc->m_parent->currentPage();
35}
36
37// Document.pageNum (setter)
38void JSDocument::setPageNum(int page)
39{
40 if (page == (int)m_doc->m_parent->currentPage()) {
41 return;
42 }
43
44 m_doc->m_parent->setViewportPage(page);
45}
46
47// Document.documentFileName
48QString JSDocument::documentFileName() const
49{
50 return m_doc->m_url.fileName();
51}
52
53// Document.filesize
54int JSDocument::filesize() const
55{
56 return m_doc->m_docSize;
57}
58
59// Document.path
60QString JSDocument::path() const
61{
62 return m_doc->m_url.toDisplayString(QUrl::PreferLocalFile);
63}
64
65// Document.URL
66QString JSDocument::URL() const
67{
68 return m_doc->m_url.toDisplayString();
69}
70
71// Document.permStatusReady
72bool JSDocument::permStatusReady() const
73{
74 return true;
75}
76
77// Document.dataObjects
78QJSValue JSDocument::dataObjects() const
79{
80 const QList<EmbeddedFile *> *files = m_doc->m_generator->embeddedFiles();
81
82 QJSValue dataObjects = qjsEngine(this)->newArray(files ? files->count() : 0);
83 if (files) {
84 QList<EmbeddedFile *>::ConstIterator it = files->begin(), itEnd = files->end();
85 for (int i = 0; it != itEnd; ++it, ++i) {
86 QJSValue newdata = qjsEngine(this)->newQObject(new JSData(*it));
87 dataObjects.setProperty(i, newdata);
88 }
89 }
90 return dataObjects;
91}
92
93// Document.external
94bool JSDocument::external() const
95{
96 QWidget *widget = m_doc->m_widget;
97
98 const bool isShell = (widget && widget->parentWidget() && widget->parentWidget()->objectName().startsWith(QLatin1String("okular::Shell")));
99 return !isShell;
100}
101
102// Document.numFields
103int JSDocument::numFields() const
104{
105 unsigned int numFields = 0;
106
107 for (const Page *pIt : std::as_const(m_doc->m_pagesVector)) {
108 numFields += pIt->formFields().size();
109 }
110
111 return numFields;
112}
113
114QJSValue JSDocument::info() const
115{
116 QJSValue obj = qjsEngine(this)->newObject();
119 const DocumentInfo docinfo = m_doc->m_parent->documentInfo(keys);
120#define KEY_GET(key, property) \
121 do { \
122 const QString data = docinfo.get(key); \
123 if (!data.isEmpty()) { \
124 obj.setProperty(QStringLiteral(property), data); \
125 obj.setProperty(QStringLiteral(property).toLower(), data); \
126 } \
127 } while (0);
128 KEY_GET(DocumentInfo::Title, "Title");
129 KEY_GET(DocumentInfo::Author, "Author");
130 KEY_GET(DocumentInfo::Subject, "Subject");
131 KEY_GET(DocumentInfo::Keywords, "Keywords");
132 KEY_GET(DocumentInfo::Creator, "Creator");
133 KEY_GET(DocumentInfo::Producer, "Producer");
134#undef KEY_GET
135 return obj;
136}
137
138#define DOCINFO_GET_METHOD(key, name) \
139 QString JSDocument::name() const \
140 { \
141 const DocumentInfo docinfo = m_doc->m_parent->documentInfo(QSet<DocumentInfo::Key>() << key); \
142 return docinfo.get(key); \
143 }
144
145DOCINFO_GET_METHOD(DocumentInfo::Author, author)
146DOCINFO_GET_METHOD(DocumentInfo::Creator, creator)
147DOCINFO_GET_METHOD(DocumentInfo::Keywords, keywords)
148DOCINFO_GET_METHOD(DocumentInfo::Producer, producer)
149DOCINFO_GET_METHOD(DocumentInfo::Title, title)
150DOCINFO_GET_METHOD(DocumentInfo::Subject, subject)
151
152#undef DOCINFO_GET_METHOD
153
154// Document.getField()
155QJSValue JSDocument::getField(const QString &cName) const
156{
157 QVector<Page *>::const_iterator pIt = m_doc->m_pagesVector.constBegin(), pEnd = m_doc->m_pagesVector.constEnd();
158 for (; pIt != pEnd; ++pIt) {
159 const QList<Okular::FormField *> pageFields = (*pIt)->formFields();
160 for (FormField *form : pageFields) {
161 if (form->fullyQualifiedName() == cName) {
162 return JSField::wrapField(qjsEngine(this), form, *pIt);
163 }
164 }
165 }
167}
168
169// Document.getPageLabel()
170QString JSDocument::getPageLabel(int nPage) const
171{
172 const Page *p = m_doc->m_pagesVector.value(nPage);
173 return p ? p->label() : QString();
174}
175
176// Document.getPageRotation()
177int JSDocument::getPageRotation(int nPage) const
178{
179 const Page *p = m_doc->m_pagesVector.value(nPage);
180 return p ? p->orientation() * 90 : 0;
181}
182
183// Document.gotoNamedDest()
184void JSDocument::gotoNamedDest(const QString &cName) const
185{
186 DocumentViewport viewport(m_doc->m_generator->metaData(QStringLiteral("NamedViewport"), cName).toString());
187 if (viewport.isValid()) {
188 m_doc->m_parent->setViewport(viewport);
189 }
190}
191
192// Document.syncAnnotScan()
193void JSDocument::syncAnnotScan() const
194{
195}
196
197// Document.getNthFieldName
198QJSValue JSDocument::getNthFieldName(int nIndex) const
199{
200 for (const Page *pIt : std::as_const(m_doc->m_pagesVector)) {
201 const QList<Okular::FormField *> pageFields = pIt->formFields();
202
203 if (nIndex < pageFields.size()) {
204 const Okular::FormField *form = pageFields[nIndex];
205
206 return form->fullyQualifiedName();
207 }
208
209 nIndex -= pageFields.size();
210 }
211
213}
214
215QJSValue JSDocument::getOCGs([[maybe_unused]] int nPage) const
216{
217 QAbstractItemModel *model = m_doc->m_parent->layersModel();
218
219 QJSValue array = qjsEngine(this)->newArray(model->rowCount());
220
221 for (int i = 0; i < model->rowCount(); ++i) {
222 for (int j = 0; j < model->columnCount(); ++j) {
223 const QModelIndex index = model->index(i, j);
224
225 QJSValue item = qjsEngine(this)->newQObject(new JSOCG(model, i, j));
226 item.setProperty(QStringLiteral("name"), model->data(index, Qt::DisplayRole).toString());
227 item.setProperty(QStringLiteral("initState"), model->data(index, Qt::CheckStateRole).toBool());
228
229 array.setProperty(i, item);
230 }
231 }
232
233 return array;
234}
235
236JSDocument::JSDocument(DocumentPrivate *doc, QObject *parent)
237 : QObject(parent)
238 , m_doc(doc)
239{
240}
241
242JSDocument::~JSDocument() = default;
The DocumentInfo structure can be filled in by generators to display metadata about the currently ope...
Definition document.h:76
@ Author
The author of the document.
Definition document.h:87
@ Subject
The subject of the document.
Definition document.h:85
@ Producer
The producer of the document (e.g. some software)
Definition document.h:89
@ Keywords
The keywords which describe the content of the document.
Definition document.h:96
@ Creator
The creator of the document (this can be different from the author)
Definition document.h:88
@ Title
The title of the document.
Definition document.h:84
A view on the document.
Definition document.h:1450
The base interface of a form field.
Definition form.h:40
virtual QString fullyQualifiedName() const =0
The fully qualified name of the field, is used in the JavaScript scripts.
char * toString(const EngineQuery &query)
global.h
Definition action.h:17
virtual int columnCount(const QModelIndex &parent) const const=0
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
void setProperty(const QString &name, const QJSValue &value)
iterator begin()
qsizetype count() const const
iterator end()
qsizetype size() const const
DisplayRole
PreferLocalFile
bool toBool() const const
QString toString() const const
QWidget * parentWidget() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:47:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.