• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegraphics API Reference
  • KDE Home
  • Contact Us
 

okular

  • sources
  • kde-4.12
  • kdegraphics
  • okular
  • core
  • script
kjs_field.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
3  * Copyright (C) 2008 by Harri Porten <porten@kde.org> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ***************************************************************************/
10 
11 #include "kjs_field_p.h"
12 
13 #include <kjs/kjsinterpreter.h>
14 #include <kjs/kjsprototype.h>
15 #include <kjs/kjsarguments.h>
16 
17 #include <qhash.h>
18 
19 #include <kdebug.h>
20 #include <kglobal.h>
21 
22 #include "../debug_p.h"
23 #include "../document_p.h"
24 #include "../form.h"
25 #include "../page.h"
26 
27 using namespace Okular;
28 
29 static KJSPrototype *g_fieldProto;
30 
31 typedef QHash< FormField *, KJSObject > FormCache;
32 K_GLOBAL_STATIC( FormCache, g_fieldCache )
33 
34 // Field.doc
35 static KJSObject fieldGetDoc( KJSContext *context, void * )
36 {
37  return context->interpreter().globalObject();
38 }
39 
40 // Field.name
41 static KJSObject fieldGetName( KJSContext *, void *object )
42 {
43  const FormField *field = reinterpret_cast< FormField * >( object );
44  return KJSString( field->name() );
45 }
46 
47 // Field.readonly (getter)
48 static KJSObject fieldGetReadOnly( KJSContext *, void *object )
49 {
50  const FormField *field = reinterpret_cast< FormField * >( object );
51  return KJSBoolean( field->isReadOnly() );
52 }
53 
54 // Field.readonly (setter)
55 static void fieldSetReadOnly( KJSContext *context, void *object, KJSObject value )
56 {
57 #if 0
58  FormField *field = reinterpret_cast< FormField * >( object );
59  bool b = value.toBoolean( context );
60  field->setReadOnly( b );
61 #else
62  Q_UNUSED( context );
63  Q_UNUSED( object );
64  Q_UNUSED( value );
65  kDebug(OkularDebug) << "Not implemented: setting readonly property";
66 #endif
67 }
68 
69 static QString fieldGetTypeHelper( const FormField *field )
70 {
71  switch ( field->type() )
72  {
73  case FormField::FormButton:
74  {
75  const FormFieldButton *button = static_cast< const FormFieldButton * >( field );
76  switch ( button->buttonType() )
77  {
78  case FormFieldButton::Push:
79  return "button";
80  case FormFieldButton::CheckBox:
81  return "checkbox";
82  case FormFieldButton::Radio:
83  return "radiobutton";
84  }
85  break;
86  }
87  case FormField::FormText:
88  return "text";
89  case FormField::FormChoice:
90  {
91  const FormFieldChoice *choice = static_cast< const FormFieldChoice * >( field );
92  switch ( choice->choiceType() )
93  {
94  case FormFieldChoice::ComboBox:
95  return "combobox";
96  case FormFieldChoice::ListBox:
97  return "listbox";
98  }
99  break;
100  }
101  case FormField::FormSignature:
102  return "signature";
103  }
104  return QString();
105 }
106 
107 // Field.type
108 static KJSObject fieldGetType( KJSContext *, void *object )
109 {
110  const FormField *field = reinterpret_cast< FormField * >( object );
111 
112  return KJSString( fieldGetTypeHelper( field ) );
113 }
114 
115 // Field.value (getter)
116 static KJSObject fieldGetValue( KJSContext *context, void *object )
117 {
118  FormField *field = reinterpret_cast< FormField * >( object );
119  if ( field->isReadOnly() )
120  {
121  KJSObject value = g_fieldCache->value( field );
122  if ( g_fieldCache.exists() && g_fieldCache->contains( field ) )
123  value = g_fieldCache->value( field );
124  else
125  value = KJSString("");
126  kDebug(OkularDebug) << "Getting the value of a readonly field" << field->name() << ":" << value.toString( context );
127  return value;
128  }
129 
130  switch ( field->type() )
131  {
132  case FormField::FormButton:
133  {
134  const FormFieldButton *button = static_cast< const FormFieldButton * >( field );
135  Q_UNUSED( button ); // ###
136  break;
137  }
138  case FormField::FormText:
139  {
140  const FormFieldText *text = static_cast< const FormFieldText * >( field );
141  return KJSString( text->text() );
142  }
143  case FormField::FormChoice:
144  {
145  const FormFieldChoice *choice = static_cast< const FormFieldChoice * >( field );
146  Q_UNUSED( choice ); // ###
147  break;
148  }
149  case FormField::FormSignature:
150  {
151  break;
152  }
153  }
154 
155  return KJSUndefined();
156 }
157 
158 // Field.value (setter)
159 static void fieldSetValue( KJSContext *context, void *object, KJSObject value )
160 {
161  FormField *field = reinterpret_cast< FormField * >( object );
162 
163  if ( field->isReadOnly() )
164  {
165  // ### throw exception?
166  kDebug(OkularDebug) << "Trying to change the readonly field" << field->name() << "to" << value.toString( context );
167  g_fieldCache->insert( field, value );
168  return;
169  }
170 
171  switch ( field->type() )
172  {
173  case FormField::FormButton:
174  {
175  FormFieldButton *button = static_cast< FormFieldButton * >( field );
176  Q_UNUSED( button ); // ###
177  break;
178  }
179  case FormField::FormText:
180  {
181  FormFieldText *text = static_cast< FormFieldText * >( field );
182  text->setText( value.toString( context ) );
183  break;
184  }
185  case FormField::FormChoice:
186  {
187  FormFieldChoice *choice = static_cast< FormFieldChoice * >( field );
188  Q_UNUSED( choice ); // ###
189  break;
190  }
191  case FormField::FormSignature:
192  {
193  break;
194  }
195  }
196 }
197 
198 void JSField::initType( KJSContext *ctx )
199 {
200  static bool initialized = false;
201  if ( initialized )
202  return;
203  initialized = true;
204 
205  if ( !g_fieldProto )
206  g_fieldProto = new KJSPrototype();
207 
208  g_fieldProto->defineProperty( ctx, "doc", fieldGetDoc );
209  g_fieldProto->defineProperty( ctx, "name", fieldGetName );
210  g_fieldProto->defineProperty( ctx, "readonly",
211  fieldGetReadOnly, fieldSetReadOnly );
212  g_fieldProto->defineProperty( ctx, "type", fieldGetType );
213  g_fieldProto->defineProperty( ctx, "value", fieldGetValue, fieldSetValue );
214 }
215 
216 KJSObject JSField::wrapField( KJSContext *ctx, FormField *field, Page *page )
217 {
218  // ### cache unique wrapper
219  KJSObject f = g_fieldProto->constructObject( ctx, field );
220  f.setProperty( ctx, "page", page->number() );
221  return f;
222 }
223 
224 void JSField::clearCachedFields()
225 {
226  if ( g_fieldCache.exists() )
227  {
228  g_fieldCache->clear();
229  }
230 }
fieldGetDoc
static KJSObject fieldGetDoc(KJSContext *context, void *)
Definition: kjs_field.cpp:35
Okular::FormFieldText::text
virtual QString text() const =0
The text of text field.
Okular::FormFieldChoice::choiceType
virtual ChoiceType choiceType() const =0
The particular type of the choice field.
Okular::FormField::FormChoice
A choice field. See FormFieldChoice::ChoiceType.
Definition: form.h:51
Okular::FormField::name
virtual QString name() const =0
The internal name of the field, to be used when referring to the field in eg scripts.
fieldGetTypeHelper
static QString fieldGetTypeHelper(const FormField *field)
Definition: kjs_field.cpp:69
Okular::Page::number
int number() const
Returns the number of the page in the document.
Definition: page.cpp:160
Okular::FormFieldButton::CheckBox
A check box.
Definition: form.h:127
Okular::FormField::FormText
A field of variable text. See FormFieldText::TextType.
Definition: form.h:50
Okular::FormFieldChoice::ComboBox
A combo box choice field.
Definition: form.h:266
Okular::FormField::FormButton
A "button". See FormFieldButton::ButtonType.
Definition: form.h:49
kjs_field_p.h
Okular::FormFieldChoice::ListBox
A list box choice field.
Definition: form.h:267
Okular::FormFieldText
Interface of a text form field.
Definition: form.h:176
fieldSetReadOnly
static void fieldSetReadOnly(KJSContext *context, void *object, KJSObject value)
Definition: kjs_field.cpp:55
Okular::FormFieldChoice
Interface of a choice form field.
Definition: form.h:258
FormCache
QHash< FormField *, KJSObject > FormCache
Definition: kjs_field.cpp:31
fieldGetType
static KJSObject fieldGetType(KJSContext *, void *object)
Definition: kjs_field.cpp:108
OkularDebug
#define OkularDebug
Definition: debug_p.h:13
Okular::Page
Collector for all the data belonging to a page.
Definition: page.h:49
fieldGetValue
static KJSObject fieldGetValue(KJSContext *context, void *object)
Definition: kjs_field.cpp:116
Okular::FormFieldButton::Push
A simple push button.
Definition: form.h:126
fieldGetReadOnly
static KJSObject fieldGetReadOnly(KJSContext *, void *object)
Definition: kjs_field.cpp:48
Okular::JSField::wrapField
static KJSObject wrapField(KJSContext *ctx, FormField *field, Page *page)
Definition: kjs_field.cpp:216
Okular::FormField::type
FieldType type() const
The type of the field.
Definition: form.cpp:47
Okular::FormField::isReadOnly
virtual bool isReadOnly() const
Whether the field is read-only.
Definition: form.cpp:53
Okular::JSField::clearCachedFields
static void clearCachedFields()
Definition: kjs_field.cpp:224
Okular::FormFieldText::setText
virtual void setText(const QString &text)
Sets the new text in the text field.
Definition: form.cpp:148
fieldGetName
static KJSObject fieldGetName(KJSContext *, void *object)
Definition: kjs_field.cpp:41
Okular::FormFieldButton::Radio
A radio button.
Definition: form.h:128
g_fieldProto
static KJSPrototype * g_fieldProto
Definition: kjs_field.cpp:29
Okular::JSField::initType
static void initType(KJSContext *ctx)
Definition: kjs_field.cpp:198
fieldSetValue
static void fieldSetValue(KJSContext *context, void *object, KJSObject value)
Definition: kjs_field.cpp:159
Okular::FormFieldButton::buttonType
virtual ButtonType buttonType() const =0
The particular type of the button field.
Okular::FormField::FormSignature
A signature.
Definition: form.h:52
Okular::FormFieldButton
Interface of a button form field.
Definition: form.h:118
Okular::FormField
The base interface of a form field.
Definition: form.h:36
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:45:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okular

Skip menu "okular"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegraphics API Reference

Skip menu "kdegraphics API Reference"
  •     libkdcraw
  •     libkexiv2
  •     libkipi
  •     libksane
  • okular

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal