~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/poppler/qt4/src/poppler-form.cc

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* poppler-form.h: qt4 interface to poppler
 
2
 * Copyright (C) 2007-2008, Pino Toscano <pino@kde.org>
 
3
 * Copyright (C) 2008, Albert Astals Cid <aacid@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, or (at your option)
 
8
 * any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "poppler-qt4.h"
 
21
 
 
22
#include <QtCore/QSizeF>
 
23
 
 
24
#include <Form.h>
 
25
#include <Object.h>
 
26
#include <Link.h>
 
27
 
 
28
#include "poppler-form.h"
 
29
#include "poppler-page-private.h"
 
30
#include "poppler-private.h"
 
31
#include "poppler-annotation-helper.h"
 
32
 
 
33
#include <math.h>
 
34
 
 
35
namespace Poppler {
 
36
 
 
37
FormField::FormField(FormFieldData &dd)
 
38
  : m_formData(&dd)
 
39
{
 
40
  const int rotation = m_formData->page->getRotate();
 
41
  // reading the coords
 
42
  double left, top, right, bottom;
 
43
  m_formData->fm->getRect(&left, &bottom, &right, &top);
 
44
  // build a normalized transform matrix for this page at 100% scale
 
45
  GfxState gfxState( 72.0, 72.0, m_formData->page->getCropBox(), rotation, gTrue );
 
46
  double * gfxCTM = gfxState.getCTM();
 
47
  double MTX[6];
 
48
  double pageWidth = m_formData->page->getCropWidth();
 
49
  double pageHeight = m_formData->page->getCropHeight();
 
50
  // landscape and seascape page rotation: be sure to use the correct (== rotated) page size
 
51
  if (((rotation / 90) % 2) == 1)
 
52
    qSwap(pageWidth, pageHeight);
 
53
  for ( int i = 0; i < 6; i+=2 )
 
54
  {
 
55
    MTX[i] = gfxCTM[i] / pageWidth;
 
56
    MTX[i+1] = gfxCTM[i+1] / pageHeight;
 
57
  }
 
58
  QPointF topLeft;
 
59
  XPDFReader::transform( MTX, qMin( left, right ), qMax( top, bottom ), topLeft );
 
60
  QPointF bottomRight;
 
61
  XPDFReader::transform( MTX, qMax( left, right ), qMin( top, bottom ), bottomRight );
 
62
  m_formData->box = QRectF(topLeft, QSizeF(bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y()));
 
63
 
 
64
  Object *obj = m_formData->fm->getObj();
 
65
  Object tmp;
 
66
 
 
67
  // reading the flags
 
68
  if (obj->isDict() && obj->dictLookup("Ff", &tmp)->isInt())
 
69
  {
 
70
    m_formData->flags = tmp.getInt();
 
71
  }
 
72
  tmp.free();
 
73
  // reading the widget annotation flags
 
74
  if (obj->isDict() && obj->dictLookup("F", &tmp)->isInt())
 
75
  {
 
76
    m_formData->annoflags = tmp.getInt();
 
77
  }
 
78
  tmp.free();
 
79
}
 
80
 
 
81
FormField::~FormField()
 
82
{
 
83
  delete m_formData;
 
84
  m_formData = 0;
 
85
}
 
86
 
 
87
QRectF FormField::rect() const
 
88
{
 
89
  return m_formData->box;
 
90
}
 
91
 
 
92
int FormField::id() const
 
93
{
 
94
  return m_formData->fm->getID();
 
95
}
 
96
 
 
97
QString FormField::name() const
 
98
{
 
99
  Object tmp;
 
100
  Object *obj = m_formData->fm->getObj();
 
101
  QString name;
 
102
  if (obj->dictLookup("T", &tmp)->isString())
 
103
  {
 
104
    GooString *goo = tmp.getString();
 
105
    if (goo)
 
106
      name = goo->getCString();
 
107
  }
 
108
  tmp.free();
 
109
  return name;
 
110
}
 
111
 
 
112
QString FormField::uiName() const
 
113
{
 
114
  Object tmp;
 
115
  Object *obj = m_formData->fm->getObj();
 
116
  QString name;
 
117
  if (obj->dictLookup("TU", &tmp)->isString())
 
118
  {
 
119
    GooString *goo = tmp.getString();
 
120
    if (goo)
 
121
      name = goo->getCString();
 
122
  }
 
123
  tmp.free();
 
124
  return name;
 
125
}
 
126
 
 
127
bool FormField::isReadOnly() const
 
128
{
 
129
  return m_formData->fm->isReadOnly();
 
130
}
 
131
 
 
132
bool FormField::isVisible() const
 
133
{
 
134
  return !(m_formData->annoflags & (1 << 1));
 
135
}
 
136
 
 
137
Link* FormField::activationAction() const
 
138
{
 
139
  Object tmp;
 
140
  Object *obj = m_formData->fm->getObj();
 
141
  Link* action = 0;
 
142
  if (obj->dictLookup("A", &tmp)->isDict())
 
143
  {
 
144
    ::LinkAction *act = ::LinkAction::parseAction(&tmp, m_formData->doc->doc->getCatalog()->getBaseURI());
 
145
    if (act)
 
146
    {
 
147
      action = PageData::convertLinkActionToLink(act, m_formData->doc, QRectF());
 
148
      delete act;
 
149
    }
 
150
  }
 
151
  tmp.free();
 
152
  return action;
 
153
}
 
154
 
 
155
 
 
156
FormFieldButton::FormFieldButton(DocumentData *doc, ::Page *p, ::FormWidgetButton *w)
 
157
  : FormField(*new FormFieldData(doc, p, w))
 
158
{
 
159
}
 
160
 
 
161
FormFieldButton::~FormFieldButton()
 
162
{
 
163
}
 
164
 
 
165
FormFieldButton::FormType FormFieldButton::type() const
 
166
{
 
167
  return FormField::FormButton;
 
168
}
 
169
 
 
170
FormFieldButton::ButtonType FormFieldButton::buttonType() const
 
171
{
 
172
  FormWidgetButton* fwb = static_cast<FormWidgetButton*>(m_formData->fm);
 
173
  switch (fwb->getButtonType())
 
174
  {
 
175
    case formButtonCheck:
 
176
      return FormFieldButton::CheckBox;
 
177
      break;
 
178
    case formButtonPush:
 
179
      return FormFieldButton::Push;
 
180
      break;
 
181
    case formButtonRadio:
 
182
      return FormFieldButton::Radio;
 
183
      break;
 
184
  }
 
185
  return FormFieldButton::CheckBox;
 
186
}
 
187
 
 
188
QString FormFieldButton::caption() const
 
189
{
 
190
  FormWidgetButton* fwb = static_cast<FormWidgetButton*>(m_formData->fm);
 
191
  // HACK push buttons seems to have a null GooString for the caption
 
192
  if (fwb->getButtonType() == formButtonPush)
 
193
    return QString();
 
194
 
 
195
  return fwb->getOnStr() ? QString::fromUtf8(fwb->getOnStr()) : QString();
 
196
}
 
197
 
 
198
bool FormFieldButton::state() const
 
199
{
 
200
  FormWidgetButton* fwb = static_cast<FormWidgetButton*>(m_formData->fm);
 
201
  return fwb->getState();
 
202
}
 
203
 
 
204
void FormFieldButton::setState( bool state )
 
205
{
 
206
  FormWidgetButton* fwb = static_cast<FormWidgetButton*>(m_formData->fm);
 
207
  fwb->setState((GBool)state);
 
208
}
 
209
 
 
210
QList<int> FormFieldButton::siblings() const
 
211
{
 
212
  FormWidgetButton* fwb = static_cast<FormWidgetButton*>(m_formData->fm);
 
213
  if (fwb->getButtonType() == formButtonPush)
 
214
    return QList<int>();
 
215
 
 
216
  QList<int> ret;
 
217
  unsigned *sibls = fwb->getSiblingsID();
 
218
  for (int i = 0; i < fwb->getNumSiblingsID(); ++i)
 
219
    ret.append(sibls[i]);
 
220
 
 
221
  return ret;
 
222
}
 
223
 
 
224
 
 
225
FormFieldText::FormFieldText(DocumentData *doc, ::Page *p, ::FormWidgetText *w)
 
226
  : FormField(*new FormFieldData(doc, p, w))
 
227
{
 
228
}
 
229
 
 
230
FormFieldText::~FormFieldText()
 
231
{
 
232
}
 
233
 
 
234
FormField::FormType FormFieldText::type() const
 
235
{
 
236
  return FormField::FormText;
 
237
}
 
238
 
 
239
FormFieldText::TextType FormFieldText::textType() const
 
240
{
 
241
  FormWidgetText* fwt = static_cast<FormWidgetText*>(m_formData->fm);
 
242
  if (fwt->isFileSelect())
 
243
    return FormFieldText::FileSelect;
 
244
  else if (fwt->isMultiline())
 
245
    return FormFieldText::Multiline;
 
246
  return FormFieldText::Normal;
 
247
}
 
248
 
 
249
QString FormFieldText::text() const
 
250
{
 
251
  GooString *goo = static_cast<FormWidgetText*>(m_formData->fm)->getContent();
 
252
  return UnicodeParsedString(goo);
 
253
}
 
254
 
 
255
void FormFieldText::setText( const QString& text )
 
256
{
 
257
  FormWidgetText* fwt = static_cast<FormWidgetText*>(m_formData->fm);
 
258
  GooString * goo = QStringToUnicodeGooString( text );
 
259
  fwt->setContent( goo );
 
260
  delete goo;
 
261
}
 
262
 
 
263
bool FormFieldText::isPassword() const
 
264
{
 
265
  FormWidgetText* fwt = static_cast<FormWidgetText*>(m_formData->fm);
 
266
  return fwt->isPassword();
 
267
}
 
268
 
 
269
bool FormFieldText::isRichText() const
 
270
{
 
271
  FormWidgetText* fwt = static_cast<FormWidgetText*>(m_formData->fm);
 
272
  return fwt->isRichText();
 
273
}
 
274
 
 
275
int FormFieldText::maximumLength() const
 
276
{
 
277
  FormWidgetText* fwt = static_cast<FormWidgetText*>(m_formData->fm);
 
278
  const int maxlen = fwt->getMaxLen();
 
279
  return maxlen > 0 ? maxlen : -1;
 
280
}
 
281
 
 
282
Qt::Alignment FormFieldText::textAlignment() const
 
283
{
 
284
  return m_formData->textAlignment(m_formData->fm->getObj());
 
285
}
 
286
 
 
287
bool FormFieldText::canBeSpellChecked() const
 
288
{
 
289
  FormWidgetText* fwt = static_cast<FormWidgetText*>(m_formData->fm);
 
290
  return !fwt->noSpellCheck();
 
291
}
 
292
 
 
293
 
 
294
FormFieldChoice::FormFieldChoice(DocumentData *doc, ::Page *p, ::FormWidgetChoice *w)
 
295
  : FormField(*new FormFieldData(doc, p, w))
 
296
{
 
297
}
 
298
 
 
299
FormFieldChoice::~FormFieldChoice()
 
300
{
 
301
}
 
302
 
 
303
FormFieldChoice::FormType FormFieldChoice::type() const
 
304
{
 
305
  return FormField::FormChoice;
 
306
}
 
307
 
 
308
FormFieldChoice::ChoiceType FormFieldChoice::choiceType() const
 
309
{
 
310
  FormWidgetChoice* fwc = static_cast<FormWidgetChoice*>(m_formData->fm);
 
311
  if (fwc->isCombo())
 
312
    return FormFieldChoice::ComboBox;
 
313
  return FormFieldChoice::ListBox;
 
314
}
 
315
 
 
316
QStringList FormFieldChoice::choices() const
 
317
{
 
318
  FormWidgetChoice* fwc = static_cast<FormWidgetChoice*>(m_formData->fm);
 
319
  QStringList ret;
 
320
  int num = fwc->getNumChoices();
 
321
  for (int i = 0; i < num; ++i)
 
322
  {
 
323
    ret.append(UnicodeParsedString(fwc->getChoice(i)));
 
324
  }
 
325
  return ret;
 
326
}
 
327
 
 
328
bool FormFieldChoice::isEditable() const
 
329
{
 
330
  FormWidgetChoice* fwc = static_cast<FormWidgetChoice*>(m_formData->fm);
 
331
  return fwc->isCombo() ? fwc->hasEdit() : false;
 
332
}
 
333
 
 
334
bool FormFieldChoice::multiSelect() const
 
335
{
 
336
//  return m_formData->flags & (1 << 21);
 
337
  FormWidgetChoice* fwc = static_cast<FormWidgetChoice*>(m_formData->fm);
 
338
  return !fwc->isCombo() ? fwc->isMultiSelect() : false;
 
339
}
 
340
 
 
341
QList<int> FormFieldChoice::currentChoices() const
 
342
{
 
343
  FormWidgetChoice* fwc = static_cast<FormWidgetChoice*>(m_formData->fm);
 
344
  int num = fwc->getNumChoices();
 
345
  QList<int> choices;
 
346
  for ( int i = 0; i < num; ++i )
 
347
    if ( fwc->isSelected( i ) )
 
348
      choices.append( i );
 
349
  return choices;
 
350
}
 
351
 
 
352
void FormFieldChoice::setCurrentChoices( const QList<int> &choice )
 
353
{
 
354
  FormWidgetChoice* fwc = static_cast<FormWidgetChoice*>(m_formData->fm);
 
355
  fwc->deselectAll();
 
356
  for ( int i = 0; i < choice.count(); ++i )
 
357
    fwc->select( choice.at( i ) );
 
358
}
 
359
 
 
360
Qt::Alignment FormFieldChoice::textAlignment() const
 
361
{
 
362
  return m_formData->textAlignment(m_formData->fm->getObj());
 
363
}
 
364
 
 
365
bool FormFieldChoice::canBeSpellChecked() const
 
366
{
 
367
  FormWidgetChoice* fwc = static_cast<FormWidgetChoice*>(m_formData->fm);
 
368
  return !fwc->noSpellCheck();
 
369
}
 
370
 
 
371
}