Changeset View
Changeset View
Standalone View
Standalone View
core/script/kjs_document.cpp
Show All 18 Lines | |||||
19 | #include <QDebug> | 19 | #include <QDebug> | ||
20 | #include <assert.h> | 20 | #include <assert.h> | ||
21 | 21 | | |||
22 | #include "../document_p.h" | 22 | #include "../document_p.h" | ||
23 | #include "../page.h" | 23 | #include "../page.h" | ||
24 | #include "../form.h" | 24 | #include "../form.h" | ||
25 | #include "kjs_data_p.h" | 25 | #include "kjs_data_p.h" | ||
26 | #include "kjs_field_p.h" | 26 | #include "kjs_field_p.h" | ||
27 | #include "kjs_ocg_p.h" | ||||
27 | 28 | | |||
28 | using namespace Okular; | 29 | using namespace Okular; | ||
29 | 30 | | |||
30 | static KJSPrototype *g_docProto; | 31 | static KJSPrototype *g_docProto; | ||
31 | 32 | | |||
32 | // Document.numPages | 33 | // Document.numPages | ||
33 | static KJSObject docGetNumPages( KJSContext *, void *object ) | 34 | static KJSObject docGetNumPages( KJSContext *, void *object ) | ||
34 | { | 35 | { | ||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Line(s) | 123 | { | |||
124 | QWidget *widget = doc->m_widget; | 125 | QWidget *widget = doc->m_widget; | ||
125 | 126 | | |||
126 | const bool isShell = ( widget | 127 | const bool isShell = ( widget | ||
127 | && widget->parentWidget() | 128 | && widget->parentWidget() | ||
128 | && widget->parentWidget()->objectName().startsWith( QLatin1String( "okular::Shell" ) ) ); | 129 | && widget->parentWidget()->objectName().startsWith( QLatin1String( "okular::Shell" ) ) ); | ||
129 | return KJSBoolean( !isShell ); | 130 | return KJSBoolean( !isShell ); | ||
130 | } | 131 | } | ||
131 | 132 | | |||
133 | // Document.numFields | ||||
134 | static KJSObject docGetNumFields( KJSContext *, void *object ) | ||||
135 | { | ||||
136 | DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object ); | ||||
137 | | ||||
138 | unsigned int numFields = 0; | ||||
aacid: you don't need this anymore? | |||||
139 | | ||||
140 | for ( const Page * pIt : qAsConst(doc->m_pagesVector) ) | ||||
As you were using const iterators before: Shouldn't this be qAsConst(doc->m_pagesVector) to avoid unnecessary detaching? sander: As you were using const iterators before: Shouldn't this be `qAsConst(doc->m_pagesVector)` to… | |||||
141 | { | ||||
142 | numFields += pIt->formFields().size(); | ||||
143 | } | ||||
sander: Why not simply `numFields += pIt->formFields()` ? | |||||
144 | | ||||
145 | return KJSNumber( numFields ); | ||||
146 | } | ||||
147 | | ||||
132 | 148 | | |||
133 | static KJSObject docGetInfo( KJSContext *ctx, void *object ) | 149 | static KJSObject docGetInfo( KJSContext *ctx, void *object ) | ||
134 | { | 150 | { | ||
135 | DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object ); | 151 | DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object ); | ||
136 | 152 | | |||
137 | KJSObject obj; | 153 | KJSObject obj; | ||
138 | QSet<DocumentInfo::Key> keys; | 154 | QSet<DocumentInfo::Key> keys; | ||
139 | keys << DocumentInfo::Title | 155 | keys << DocumentInfo::Title | ||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Line(s) | |||||
243 | 259 | | |||
244 | // Document.syncAnnotScan() | 260 | // Document.syncAnnotScan() | ||
245 | static KJSObject docSyncAnnotScan( KJSContext *, void *, | 261 | static KJSObject docSyncAnnotScan( KJSContext *, void *, | ||
246 | const KJSArguments & ) | 262 | const KJSArguments & ) | ||
247 | { | 263 | { | ||
248 | return KJSUndefined(); | 264 | return KJSUndefined(); | ||
249 | } | 265 | } | ||
250 | 266 | | |||
267 | // Document.getNthFieldName | ||||
268 | static KJSObject docGetNthFieldName( KJSContext *ctx, void *object, | ||||
269 | const KJSArguments &arguments ) | ||||
270 | { | ||||
271 | DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object ); | ||||
272 | | ||||
273 | int numField = arguments.at( 0 ).toInt32( ctx ); | ||||
274 | | ||||
275 | for ( const Page * pIt : qAsConst(doc->m_pagesVector) ) | ||||
276 | { | ||||
sander: Please use a range-based for loop. | |||||
277 | const QLinkedList< Okular::FormField * > pageFields = pIt->formFields(); | ||||
278 | | ||||
279 | if(numField < pageFields.size()) | ||||
280 | { | ||||
281 | auto ffIt = pageFields.begin(); | ||||
282 | ffIt += numField; | ||||
283 | | ||||
284 | return KJSString( (*ffIt)->name() ); | ||||
285 | } | ||||
286 | | ||||
This convoluted for loop basically get the i-th element of a QLinkedList, which is not designed for index accessing (and that is why there is no at(int) method).
pino: This convoluted for loop basically get the i-th element of a QLinkedList, which is not designed… | |||||
I still have to check for all the pages. But my new implementation looks more like with what you described joaonetto: I still have to check for all the pages.
But my new implementation looks more like with what… | |||||
287 | numField -= pageFields.size(); | ||||
288 | } | ||||
289 | | ||||
290 | return KJSUndefined(); | ||||
291 | } | ||||
292 | | ||||
293 | static KJSObject docGetOCGs( KJSContext *ctx, void *object, | ||||
294 | const KJSArguments &arguments ) | ||||
295 | { | ||||
296 | DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object ); | ||||
297 | | ||||
298 | QAbstractItemModel * model = doc->m_parent->layersModel(); | ||||
299 | | ||||
300 | KJSArray array( ctx, model->rowCount() ); | ||||
301 | | ||||
302 | for(int i = 0;i < model->rowCount();++i){ | ||||
303 | for(int j = 0;j < model->columnCount();++j){ | ||||
304 | QModelIndex index = model->index( i, j ); | ||||
305 | | ||||
306 | KJSObject item = JSOCG::wrapOCGObject( ctx, model, i, j ); | ||||
307 | item.setProperty( ctx, QStringLiteral("name"), model->data( index , Qt::DisplayRole ).toString() ); | ||||
308 | item.setProperty( ctx, QStringLiteral("initState"), model->data( index , Qt::CheckStateRole ).toBool() ); | ||||
309 | | ||||
310 | array.setProperty( ctx, QString::number( i ), item ); | ||||
311 | } | ||||
312 | } | ||||
313 | | ||||
314 | return array; | ||||
315 | } | ||||
316 | | ||||
251 | void JSDocument::initType( KJSContext *ctx ) | 317 | void JSDocument::initType( KJSContext *ctx ) | ||
252 | { | 318 | { | ||
253 | assert( g_docProto ); | 319 | assert( g_docProto ); | ||
254 | 320 | | |||
255 | static bool initialized = false; | 321 | static bool initialized = false; | ||
256 | if ( initialized ) | 322 | if ( initialized ) | ||
257 | return; | 323 | return; | ||
258 | initialized = true; | 324 | initialized = true; | ||
259 | 325 | | |||
260 | g_docProto->defineProperty( ctx, QStringLiteral("numPages"), docGetNumPages ); | 326 | g_docProto->defineProperty( ctx, QStringLiteral("numPages"), docGetNumPages ); | ||
261 | g_docProto->defineProperty( ctx, QStringLiteral("pageNum"), docGetPageNum, docSetPageNum ); | 327 | g_docProto->defineProperty( ctx, QStringLiteral("pageNum"), docGetPageNum, docSetPageNum ); | ||
262 | g_docProto->defineProperty( ctx, QStringLiteral("documentFileName"), docGetDocumentFileName ); | 328 | g_docProto->defineProperty( ctx, QStringLiteral("documentFileName"), docGetDocumentFileName ); | ||
263 | g_docProto->defineProperty( ctx, QStringLiteral("filesize"), docGetFilesize ); | 329 | g_docProto->defineProperty( ctx, QStringLiteral("filesize"), docGetFilesize ); | ||
264 | g_docProto->defineProperty( ctx, QStringLiteral("path"), docGetPath ); | 330 | g_docProto->defineProperty( ctx, QStringLiteral("path"), docGetPath ); | ||
265 | g_docProto->defineProperty( ctx, QStringLiteral("URL"), docGetURL ); | 331 | g_docProto->defineProperty( ctx, QStringLiteral("URL"), docGetURL ); | ||
266 | g_docProto->defineProperty( ctx, QStringLiteral("permStatusReady"), docGetPermStatusReady ); | 332 | g_docProto->defineProperty( ctx, QStringLiteral("permStatusReady"), docGetPermStatusReady ); | ||
267 | g_docProto->defineProperty( ctx, QStringLiteral("dataObjects"), docGetDataObjects ); | 333 | g_docProto->defineProperty( ctx, QStringLiteral("dataObjects"), docGetDataObjects ); | ||
268 | g_docProto->defineProperty( ctx, QStringLiteral("external"), docGetExternal ); | 334 | g_docProto->defineProperty( ctx, QStringLiteral("external"), docGetExternal ); | ||
335 | g_docProto->defineProperty( ctx, QStringLiteral("numFields"), docGetNumFields ); | ||||
269 | 336 | | |||
270 | // info properties | 337 | // info properties | ||
271 | g_docProto->defineProperty( ctx, QStringLiteral("info"), docGetInfo ); | 338 | g_docProto->defineProperty( ctx, QStringLiteral("info"), docGetInfo ); | ||
272 | g_docProto->defineProperty( ctx, QStringLiteral("author"), docGetAuthor ); | 339 | g_docProto->defineProperty( ctx, QStringLiteral("author"), docGetAuthor ); | ||
273 | g_docProto->defineProperty( ctx, QStringLiteral("creator"), docGetCreator ); | 340 | g_docProto->defineProperty( ctx, QStringLiteral("creator"), docGetCreator ); | ||
274 | g_docProto->defineProperty( ctx, QStringLiteral("keywords"), docGetKeywords ); | 341 | g_docProto->defineProperty( ctx, QStringLiteral("keywords"), docGetKeywords ); | ||
275 | g_docProto->defineProperty( ctx, QStringLiteral("producer"), docGetProducer ); | 342 | g_docProto->defineProperty( ctx, QStringLiteral("producer"), docGetProducer ); | ||
276 | g_docProto->defineProperty( ctx, QStringLiteral("title"), docGetTitle ); | 343 | g_docProto->defineProperty( ctx, QStringLiteral("title"), docGetTitle ); | ||
277 | g_docProto->defineProperty( ctx, QStringLiteral("subject"), docGetSubject ); | 344 | g_docProto->defineProperty( ctx, QStringLiteral("subject"), docGetSubject ); | ||
278 | 345 | | |||
279 | g_docProto->defineFunction( ctx, QStringLiteral("getField"), docGetField ); | 346 | g_docProto->defineFunction( ctx, QStringLiteral("getField"), docGetField ); | ||
280 | g_docProto->defineFunction( ctx, QStringLiteral("getPageLabel"), docGetPageLabel ); | 347 | g_docProto->defineFunction( ctx, QStringLiteral("getPageLabel"), docGetPageLabel ); | ||
281 | g_docProto->defineFunction( ctx, QStringLiteral("getPageRotation"), docGetPageRotation ); | 348 | g_docProto->defineFunction( ctx, QStringLiteral("getPageRotation"), docGetPageRotation ); | ||
282 | g_docProto->defineFunction( ctx, QStringLiteral("gotoNamedDest"), docGotoNamedDest ); | 349 | g_docProto->defineFunction( ctx, QStringLiteral("gotoNamedDest"), docGotoNamedDest ); | ||
283 | g_docProto->defineFunction( ctx, QStringLiteral("syncAnnotScan"), docSyncAnnotScan ); | 350 | g_docProto->defineFunction( ctx, QStringLiteral("syncAnnotScan"), docSyncAnnotScan ); | ||
351 | g_docProto->defineFunction( ctx, QStringLiteral("getNthFieldName"), docGetNthFieldName ); | ||||
352 | g_docProto->defineFunction( ctx, QStringLiteral("getOCGs"), docGetOCGs ); | ||||
284 | } | 353 | } | ||
285 | 354 | | |||
286 | KJSGlobalObject JSDocument::wrapDocument( DocumentPrivate *doc ) | 355 | KJSGlobalObject JSDocument::wrapDocument( DocumentPrivate *doc ) | ||
287 | { | 356 | { | ||
288 | if ( !g_docProto ) | 357 | if ( !g_docProto ) | ||
289 | g_docProto = new KJSPrototype(); | 358 | g_docProto = new KJSPrototype(); | ||
290 | return g_docProto->constructGlobalObject( doc ); | 359 | return g_docProto->constructGlobalObject( doc ); | ||
291 | } | 360 | } | ||
361 | No newline at end of file |
you don't need this anymore?