Okular

form.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Pino Toscano <pino@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "form.h"
8#include "form_p.h"
9
10// qt includes
11#include <QVariant>
12
13#include "action.h"
14
15using namespace Okular;
16
17FormFieldPrivate::FormFieldPrivate(FormField::FieldType type)
18 : m_type(type)
19 , m_activateAction(nullptr)
20 , q_ptr(nullptr)
21{
22}
23
24FormFieldPrivate::~FormFieldPrivate()
25{
26 delete m_activateAction;
27 qDeleteAll(m_additionalActions);
28 qDeleteAll(m_additionalAnnotActions);
29}
30
31void FormFieldPrivate::setDefault()
32{
33 m_default = value();
34}
35
36FormField::FormField(FormFieldPrivate &dd)
37 : d_ptr(&dd)
38{
39 d_ptr->q_ptr = this;
40}
41
42FormField::~FormField()
43{
44 delete d_ptr;
45}
46
48{
49 Q_D(const FormField);
50 return d->m_type;
51}
52
54{
55 return false;
56}
57
59{
60}
61
63{
64 return true;
65}
66
68{
69}
70
72{
73 return true;
74}
75
77{
78}
79
81{
82}
83
87
89{
90 return QVariant(QString());
91}
92
93Action *FormField::activationAction() const
94{
95 Q_D(const FormField);
96 return d->m_activateAction;
97}
98
99void FormField::setActivationAction(Action *action)
100{
101 Q_D(FormField);
102 delete d->m_activateAction;
103 d->m_activateAction = action;
104}
105
107{
108 Q_D(const FormField);
109 return d->m_additionalActions.value(type);
110}
111
112void FormField::setAdditionalAction(AdditionalActionType type, Action *action)
113{
114 Q_D(FormField);
115 delete d->m_additionalActions.value(type);
116 d->m_additionalActions[type] = action;
117}
118
120{
121 Q_D(const FormField);
122 return d->m_additionalAnnotActions.value(type);
123}
124
125void FormField::setAdditionalAction(Annotation::AdditionalActionType type, Action *action)
126{
127 Q_D(FormField);
128 delete d->m_additionalAnnotActions.value(type);
129 d->m_additionalAnnotActions[type] = action;
130}
131
133{
134 Q_D(const FormField);
135 // yes, calling values() is not great but it's a list of ~10 elements, we can live with that
136 return d->m_additionalAnnotActions.values() + d->m_additionalActions.values(); // clazy:exclude=container-anti-pattern
137}
138
140{
141 Q_D(const FormField);
142 return d->m_page;
143}
144
146{
147 Q_D(const FormField);
148 return d->m_committedValue;
149}
150
152{
153 Q_D(FormField);
154 d->m_committedValue = value;
155}
156
158{
159 Q_D(const FormField);
160 return d->m_committedFormattedValue;
161}
162
164{
165 Q_D(FormField);
166 d->m_committedFormattedValue = value;
167}
168
169class Okular::FormFieldButtonPrivate : public Okular::FormFieldPrivate
170{
171public:
172 FormFieldButtonPrivate()
173 : FormFieldPrivate(FormField::FormButton)
174 {
175 }
176
177 Q_DECLARE_PUBLIC(FormFieldButton)
178
179 void setValue(const QString &v) override
180 {
181 Q_Q(FormFieldButton);
182 q->setState(QVariant(v).toBool());
183 }
184
185 QString value() const override
186 {
187 Q_Q(const FormFieldButton);
188 return QVariant::fromValue<bool>(q->state()).toString();
189 }
190};
191
192FormFieldButton::FormFieldButton()
193 : FormField(*new FormFieldButtonPrivate())
194{
195}
196
197FormFieldButton::~FormFieldButton()
198{
199}
200
202{
203}
204
208
209class Okular::FormFieldTextPrivate : public Okular::FormFieldPrivate
210{
211public:
212 FormFieldTextPrivate()
213 : FormFieldPrivate(FormField::FormText)
214 {
215 }
216
217 Q_DECLARE_PUBLIC(FormFieldText)
218
219 void setValue(const QString &v) override
220 {
221 Q_Q(FormFieldText);
222 q->setText(v);
223 }
224
225 QString value() const override
226 {
227 Q_Q(const FormFieldText);
228 return q->text();
229 }
230};
231
232FormFieldText::FormFieldText()
233 : FormField(*new FormFieldTextPrivate())
234{
235}
236
237FormFieldText::~FormFieldText()
238{
239}
240
242{
243}
244
246{
247 return false;
248}
249
251{
252 return false;
253}
254
256{
257 return -1;
258}
259
264
266{
267 return false;
268}
269
271{
273}
274
279
281{
282 return QVariant(text());
283}
284
285class Okular::FormFieldChoicePrivate : public Okular::FormFieldPrivate
286{
287public:
288 FormFieldChoicePrivate()
289 : FormFieldPrivate(FormField::FormChoice)
290 {
291 }
292
293 Q_DECLARE_PUBLIC(FormFieldChoice)
294
295 void setValue(const QString &v) override
296 {
297 Q_Q(FormFieldChoice);
298 const QStringList choices = v.split(QLatin1Char(';'), Qt::SkipEmptyParts);
299 QList<int> newchoices;
300 for (const QString &str : choices) {
301 bool ok = true;
302 int val = str.toInt(&ok);
303 if (ok) {
304 newchoices.append(val);
305 }
306 }
307 if (!newchoices.isEmpty()) {
308 q->setCurrentChoices(newchoices);
309 }
310 }
311
312 QString value() const override
313 {
314 Q_Q(const FormFieldChoice);
315 QList<int> choices = q->currentChoices();
316 std::sort(choices.begin(), choices.end());
318 for (const int c : std::as_const(choices)) {
320 }
321 return list.join(QStringLiteral(";"));
322 }
323
324 QMap<QString, QString> exportValues;
325};
326
327FormFieldChoice::FormFieldChoice()
328 : FormField(*new FormFieldChoicePrivate())
329{
330}
331
332FormFieldChoice::~FormFieldChoice()
333{
334}
335
337{
338 return false;
339}
340
342{
343 return false;
344}
345
349
351{
352 return QString();
353}
354
358
363
365{
366 return false;
367}
368
370{
372 d->exportValues = values;
373}
374
376{
377 Q_D(const FormFieldChoice);
378 return d->exportValues.value(choice, choice);
379}
380
382{
383 if (choiceType() == ComboBox) {
384 const QStringList availableChoices = choices();
385 const QString valueToSet = value.toString();
386 for (int i = 0; i < availableChoices.size(); i++) {
387 if (valueToSet == availableChoices[i]) {
388 const QList oneChoiceList = {i};
389 setCurrentChoices(oneChoiceList);
390 return; // We expect to set only one element for a combo box
391 }
392 }
393 // If the value to be set is not present in the available choices
394 setEditChoice(valueToSet);
395 } else {
396 // TODO what to set for ListBox.
397 }
398}
399
401{
402 if (choiceType() == ComboBox) {
403 if (currentChoices().isEmpty()) {
404 return QVariant(editChoice());
405 }
406 return QVariant(choices().at(currentChoices().constFirst()));
407 } else {
408 // TODO what to return for ListBox. Temporarily return empty string.
409 return QVariant(QString());
410 }
411}
412
419
420class Okular::FormFieldSignaturePrivate : public Okular::FormFieldPrivate
421{
422public:
423 FormFieldSignaturePrivate()
424 : FormFieldPrivate(FormField::FormSignature)
425 {
426 }
427
428 Q_DECLARE_PUBLIC(FormFieldSignature)
429
430 void setValue(const QString &v) override
431 {
432 Q_UNUSED(v)
433 }
434
435 QString value() const override
436 {
437 return QString();
438 }
439};
440
441FormFieldSignature::FormFieldSignature()
442 : FormField(*new FormFieldSignaturePrivate())
443{
444}
445
446FormFieldSignature::~FormFieldSignature()
447{
448}
Encapsulates data that describes an action.
Definition action.h:41
AdditionalActionType
Describes the type of additional actions.
Interface of a button form field.
Definition form.h:247
virtual void setState(bool state)
Sets the state of the button to the new state .
Definition form.cpp:201
virtual void setIcon(Okular::FormField *field)
Sets the icon of the Button to the Icon of the field parameter.
Definition form.cpp:205
Interface of a choice form field.
Definition form.h:421
virtual void setCurrentChoices(const QList< int > &choices)
Sets the selected choices to choices .
Definition form.cpp:346
void setAppearanceValue(const QVariant &value) override
Sets the appearance value associated with the form field.
Definition form.cpp:413
QVariant value() const override
Returns the value associated with the choice form field.
Definition form.cpp:400
QString exportValueForChoice(const QString &choice) const
Returns the export value for a given choice.
Definition form.cpp:375
@ ComboBox
A combo box choice field.
Definition form.h:427
virtual void setEditChoice(const QString &text)
Sets the text entered into an editable combo box choice field.
Definition form.cpp:355
virtual QString editChoice() const
The text entered into an editable combo box choice field.
Definition form.cpp:350
virtual QList< int > currentChoices() const =0
The currently selected choices.
virtual Qt::Alignment textAlignment() const
The alignment of the text within the field.
Definition form.cpp:359
virtual bool multiSelect() const
Whether more than one choice of this ListBox can be selected at the same time.
Definition form.cpp:341
void setExportValues(const QMap< QString, QString > &values)
The possible choices of the choice field.
Definition form.cpp:369
virtual void setAppearanceChoiceText(const QString &text)=0
Sets the text which should be rendered by the PDF in the position of choice FormField.
virtual bool isEditable() const
Whether this ComboBox is editable, ie the user can type in a custom value.
Definition form.cpp:336
virtual ChoiceType choiceType() const =0
The particular type of the choice field.
virtual QStringList choices() const =0
The possible choices of the choice field.
virtual bool canBeSpellChecked() const
Whether the text inserted manually in the field (where possible) can be spell-checked.
Definition form.cpp:364
void setValue(const QVariant &value) override
Sets the value asssociated with the choice form field.
Definition form.cpp:381
Interface of a signature form field.
Definition form.h:560
Interface of a text form field.
Definition form.h:310
virtual void setAppearanceText(const QString &text)=0
Set the text which should be rendered by the PDF.
virtual bool isPassword() const
Whether this text field is a password input, eg its text must be replaced with asterisks.
Definition form.cpp:245
virtual Qt::Alignment textAlignment() const
The alignment of the text within the field.
Definition form.cpp:260
virtual bool isRichText() const
Whether this text field should allow rich text.
Definition form.cpp:250
QVariant value() const override
Returns the value associated with the text form field.
Definition form.cpp:280
void setAppearanceValue(const QVariant &value) override
Sets the appearance value associated with the text form field.
Definition form.cpp:275
virtual int maximumLength() const
The maximum length allowed for the text of text field, or -1 if there is no limitation for the text.
Definition form.cpp:255
virtual void setText(const QString &text)
Sets the new text in the text field.
Definition form.cpp:241
void setValue(const QVariant &value) override
Sets the value associated with the text form field.
Definition form.cpp:270
virtual QString text() const =0
The text of text field.
virtual bool canBeSpellChecked() const
Whether the text inserted manually in the field (where possible) can be spell-checked.
Definition form.cpp:265
The base interface of a form field.
Definition form.h:40
virtual bool isReadOnly() const
Whether the field is read-only.
Definition form.cpp:53
Action * additionalAction(AdditionalActionType type) const
Returns the additional action of the given type or nullptr if no action has been defined.
Definition form.cpp:106
AdditionalActionType
Describes the type of form additional action.
Definition form.h:160
virtual void setPrintable(bool value)
Set this field printable.
Definition form.cpp:76
void commitFormattedValue(const QString &value)
Updates the value that was last committed in this form field.
Definition form.cpp:163
virtual void setVisible(bool value)
Whether the field is visible.
Definition form.cpp:67
virtual bool isPrintable() const
Whether this field is printable.
Definition form.cpp:71
virtual void setValue(const QVariant &value)
Sets the value associated with the form field.
Definition form.cpp:80
FieldType
The types of form field.
Definition form.h:50
FieldType type() const
The type of the field.
Definition form.cpp:47
virtual void setAppearanceValue(const QVariant &value)
Sets the appearance value associated with the form field.
Definition form.cpp:84
virtual void setReadOnly(bool value)
Whether the field is read-only.
Definition form.cpp:58
QString committedValue() const
Returns the value that was last committed in this form field.
Definition form.cpp:145
void commitValue(const QString &value)
Updates the value that was last committed in this form field.
Definition form.cpp:151
QString committedFormattedValue() const
Returns the formatted value that was last committed in this form field.
Definition form.cpp:157
Page * page() const
Returns the page of this form field.
Definition form.cpp:139
virtual bool isVisible() const
Whether this form field is visible.
Definition form.cpp:62
QList< Action * > additionalActions() const
Returns all the additional actions for this form.
Definition form.cpp:132
virtual QVariant value() const
Returns the value associated wit the form field.
Definition form.cpp:88
Collector for all the data belonging to a page.
Definition page.h:48
Type type(const QSqlDatabase &db)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
global.h
Definition action.h:17
void append(QList< T > &&value)
iterator begin()
iterator end()
bool isEmpty() const const
qsizetype size() const const
QString number(double n, char format, int precision)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString join(QChar separator) const const
typedef Alignment
SkipEmptyParts
QVariant fromValue(T &&value)
QString toString() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.