List box implementation

This commit is contained in:
Jakub Melka 2020-05-16 17:34:23 +02:00
parent 0f4eeeacb7
commit a51aae3836
7 changed files with 1206 additions and 343 deletions

View File

@ -140,6 +140,7 @@ public:
_PDFReal,
_PDFObjectReference,
_PDFObject,
_PDFIntegerVector,
_QString,
_QPointF,
_QRectF,

View File

@ -2918,21 +2918,16 @@ void PDFWidgetAnnotation::draw(AnnotationDrawParameters& parameters) const
const PDFFormFieldWidgetEditor* editor = parameters.formManager->getEditor(formField);
if (editor && editor->isEditorDrawEnabled())
{
editor->draw(parameters);
editor->draw(parameters, true);
}
else
{
switch (formField->getFieldType())
{
case PDFFormField::FieldType::Text:
case PDFFormField::FieldType::Choice:
{
if (const PDFFormFieldTextBoxEditor* textBoxEditor = qobject_cast<const PDFFormFieldTextBoxEditor*>(editor))
{
PDFTextEditPseudowidget pseudowidget(formField->getFlags());
textBoxEditor->initializeTextEdit(&pseudowidget);
pseudowidget.draw(parameters, false);
}
editor->draw(parameters, false);
break;
}
@ -3039,9 +3034,6 @@ void PDFWidgetAnnotation::draw(AnnotationDrawParameters& parameters) const
break;
}
case PDFFormField::FieldType::Choice:
// TODO: Draw choice field
case PDFFormField::FieldType::Invalid:
case PDFFormField::FieldType::Signature:
break;

View File

@ -3210,6 +3210,36 @@ void PDFDocumentBuilder::setDocumentTitle(QString title)
}
void PDFDocumentBuilder::setFormFieldChoiceIndices(PDFObjectReference formField,
PDFIntegerVector indices)
{
PDFObjectFactory objectBuilder;
objectBuilder.beginDictionary();
objectBuilder.beginDictionaryItem("I");
objectBuilder << indices;
objectBuilder.endDictionaryItem();
objectBuilder.endDictionary();
PDFObject formFieldObject = objectBuilder.takeObject();
mergeTo(formField, formFieldObject);
}
void PDFDocumentBuilder::setFormFieldChoiceTopIndex(PDFObjectReference formField,
PDFInteger topIndex)
{
PDFObjectFactory objectBuilder;
objectBuilder.beginDictionary();
objectBuilder.beginDictionaryItem("TI");
objectBuilder << topIndex;
objectBuilder.endDictionaryItem();
objectBuilder.endDictionary();
PDFObject formFieldObject = objectBuilder.takeObject();
mergeTo(formField, formFieldObject);
}
void PDFDocumentBuilder::setFormFieldValue(PDFObjectReference formField,
PDFObject value)
{

View File

@ -27,6 +27,8 @@ class QPdfWriter;
namespace pdf
{
using PDFIntegerVector = std::vector<PDFInteger>;
struct WrapName
{
WrapName(const char* name) :
@ -928,6 +930,20 @@ public:
void setDocumentTitle(QString title);
/// Sets form field list box indices.
/// \param formField Form field
/// \param indices Sorted list of selected indices
void setFormFieldChoiceIndices(PDFObjectReference formField,
PDFIntegerVector indices);
/// Sets form field list box top index. Top index is zero-based index of first item visible in the list box.
/// \param formField Form field
/// \param topIndex Zero-based index of first visible item
void setFormFieldChoiceTopIndex(PDFObjectReference formField,
PDFInteger topIndex);
/// Sets form field value. Value must be correct for this form field, no checking is performed. Also, if
/// you use this function, annotation widgets, which are attached to this form field, should also be
/// updated (for example, appearance state and sometimes appearance streams).

File diff suppressed because it is too large Load Diff

View File

@ -198,7 +198,7 @@ public:
void fillWidgetToFormFieldMapping(PDFWidgetToFormFieldMapping& mapping);
/// Reloads value from object storage. Actually stored value is lost.
void reloadValue(const PDFObjectStorage* storage, PDFObject parentValue);
virtual void reloadValue(const PDFObjectStorage* storage, PDFObject parentValue);
/// Applies function to this form field and all its descendants,
/// in pre-order (first application is to the parent, following
@ -237,6 +237,10 @@ public:
PDFDocumentModifier* modifier = nullptr;
PDFFormManager* formManager = nullptr;
Scope scope = Scope::User;
// Choice list box field only
PDFInteger listboxTopIndex= 0;
std::vector<PDFInteger> listboxChoices;
};
/// Sets value to the form field. If value has been correctly
@ -340,6 +344,8 @@ private:
class PDFFormFieldChoice : public PDFFormField
{
using BaseClass = PDFFormField;
public:
explicit inline PDFFormFieldChoice() = default;
@ -359,6 +365,9 @@ public:
PDFInteger getTopIndex() const { return m_topIndex; }
const PDFObject& getSelection() const { return m_selection; }
virtual bool setValue(const SetValueParameters& parameters) override;
virtual void reloadValue(const PDFObjectStorage* storage, PDFObject parentValue) override;
private:
friend static PDFFormFieldPointer PDFFormField::parse(const PDFObjectStorage* storage, PDFObjectReference reference, PDFFormField* parentField);
@ -412,6 +421,8 @@ public:
const std::optional<PDFInteger>& getQuadding() const { return m_quadding; }
const PDFObject& getXFA() const { return m_xfa; }
Qt::Alignment getDefaultAlignment() const;
bool isAcroForm() const { return getFormType() == PDFForm::FormType::AcroForm; }
bool isXFAForm() const { return getFormType() == PDFForm::FormType::XFAForm; }
@ -446,182 +457,12 @@ private:
PDFWidgetToFormFieldMapping m_widgetToFormField;
};
/// "Pseudo" widget, which is emulating text editor, which can be single line, or multiline.
/// Passwords can also be edited and editor can be read only.
class PDFTextEditPseudowidget
{
public:
explicit inline PDFTextEditPseudowidget(PDFFormField::FieldFlags flags);
void shortcutOverrideEvent(QWidget* widget, QKeyEvent* event);
void keyPressEvent(QWidget* widget, QKeyEvent* event);
inline bool isReadonly() const { return m_flags.testFlag(PDFFormField::ReadOnly); }
inline bool isMultiline() const { return m_flags.testFlag(PDFFormField::Multiline); }
inline bool isPassword() const { return m_flags.testFlag(PDFFormField::Password); }
inline bool isFileSelect() const { return m_flags.testFlag(PDFFormField::FileSelect); }
inline bool isComb() const { return m_flags.testFlag(PDFFormField::Comb); }
inline bool isEmpty() const { return m_editText.isEmpty(); }
inline bool isTextSelected() const { return !isEmpty() && getSelectionLength() > 0; }
inline bool isWholeTextSelected() const { return !isEmpty() && getSelectionLength() == m_editText.length(); }
inline int getTextLength() const { return m_editText.length(); }
inline int getSelectionLength() const { return m_selectionEnd - m_selectionStart; }
inline int getPositionCursor() const { return m_positionCursor; }
inline int getPositionStart() const { return 0; }
inline int getPositionEnd() const { return getTextLength(); }
inline void clearSelection() { m_selectionStart = m_selectionEnd = 0; }
inline const QString& getText() const { return m_editText; }
inline QString getSelectedText() const { return m_editText.mid(m_selectionStart, getSelectionLength()); }
/// Sets (updates) text selection
/// \param startPosition From where we are selecting text
/// \param selectionLength Selection length (positive - to the right, negative - to the left)
void setSelection(int startPosition, int selectionLength);
/// Moves cursor position. It behaves as usual in text boxes,
/// when user moves the cursor. If \p select is true, then
/// selection is updated.
/// \param position New position of the cursor
/// \param select Select text when moving the cursor?
void setCursorPosition(int position, bool select);
/// Sets text content of the widget. This functions sets the text,
/// even if widget is readonly.
/// \param text Text to be set
void setText(const QString& text);
/// Sets widget appearance, such as font, font size, color, text alignment,
/// and rectangle, in which widget resides on page (in page coordinates)
/// \param appearance Appearance
/// \param textAlignment Text alignment
/// \param rect Widget rectangle in page coordinates
/// \param maxTextLength Maximal text length
void setAppearance(const PDFAnnotationDefaultAppearance& appearance,
Qt::Alignment textAlignment,
QRectF rect,
int maxTextLength);
void performCut();
void performCopy();
void performPaste();
void performClear();
void performSelectAll();
void performBackspace();
void performDelete();
void performRemoveSelectedText();
void performInsertText(const QString& text);
/// Draw text edit using given parameters
/// \param parameters Parameters
void draw(AnnotationDrawParameters& parameters, bool edit) const;
/// Returns valid cursor position retrieved from position in the widget.
/// \param point Point in page coordinate space
/// \param edit Are we using edit transformations?
/// \returns Cursor position
int getCursorPositionFromWidgetPosition(const QPointF& point, bool edit) const;
inline int getCursorForward(QTextLayout::CursorMode mode) const { return getNextPrevCursorPosition(getSingleStepForward(), mode); }
inline int getCursorBackward(QTextLayout::CursorMode mode) const { return getNextPrevCursorPosition(getSingleStepBackward(), mode); }
inline int getCursorCharacterForward() const { return getCursorForward(QTextLayout::SkipCharacters); }
inline int getCursorCharacterBackward() const { return getCursorBackward(QTextLayout::SkipCharacters); }
inline int getCursorWordForward() const { return getCursorForward(QTextLayout::SkipWords); }
inline int getCursorWordBackward() const { return getCursorBackward(QTextLayout::SkipWords); }
inline int getCursorDocumentStart() const { return (getSingleStepForward() > 0) ? getPositionStart() : getPositionEnd(); }
inline int getCursorDocumentEnd() const { return (getSingleStepForward() > 0) ? getPositionEnd() : getPositionStart(); }
inline int getCursorLineStart() const { return (getSingleStepForward() > 0) ? getCurrentLineTextStart() : getCurrentLineTextEnd(); }
inline int getCursorLineEnd() const { return (getSingleStepForward() > 0) ? getCurrentLineTextEnd() : getCurrentLineTextStart(); }
inline int getCursorNextLine() const { return getCurrentLineTextEnd(); }
inline int getCursorPreviousLine() const { return getNextPrevCursorPosition(getCurrentLineTextStart(), -1, QTextLayout::SkipCharacters); }
private:
/// This function does following things:
/// 1) Clamps edit text to fit maximum length
/// 2) Creates display string from edit string
/// 3) Updates text layout
void updateTextLayout();
/// Returns single step forward, which is determined
/// by cursor move style and layout direction.
int getSingleStepForward() const;
/// Returns single step backward, which is determined
/// by cursor move style and layout direction.
int getSingleStepBackward() const { return -getSingleStepForward(); }
/// Returns next/previous position, by number of steps,
/// using given cursor mode (skipping characters or whole words).
/// \param steps Number of steps to proceed (can be negative number)
/// \param mode Skip mode - letters or words?
int getNextPrevCursorPosition(int steps, QTextLayout::CursorMode mode) const { return getNextPrevCursorPosition(m_positionCursor, steps, mode); }
/// Returns next/previous position from reference cursor position, by number of steps,
/// using given cursor mode (skipping characters or whole words).
/// \param referencePosition Reference cursor position
/// \param steps Number of steps to proceed (can be negative number)
/// \param mode Skip mode - letters or words?
int getNextPrevCursorPosition(int referencePosition, int steps, QTextLayout::CursorMode mode) const;
/// Returns current line text start position
int getCurrentLineTextStart() const;
/// Returns current line text end position
int getCurrentLineTextEnd() const;
/// Creates text box transform matrix, which transforms from
/// widget space to page space.
/// \param edit Create matrix for text editing?
QMatrix createTextBoxTransformMatrix(bool edit) const;
/// Returns vector of cursor positions (which may be not equal
/// to edit string length, because edit string can contain surrogates,
/// or graphemes, which are single glyphs, but represented by more
/// 16-bit unicode codes).
std::vector<int> getCursorPositions() const;
int getCursorLineUp() const;
int getCursorLineDown() const;
PDFFormField::FieldFlags m_flags;
/// Text edited by the user
QString m_editText;
/// Text, which is displayed. It can differ from text
/// edited by user, in case password is being entered.
QString m_displayText;
/// Text layout
QTextLayout m_textLayout;
/// Character for displaying passwords
QChar m_passwordReplacementCharacter;
int m_selectionStart;
int m_selectionEnd;
int m_positionCursor;
int m_maxTextLength;
QRectF m_widgetRect;
QColor m_textColor;
};
/// Base class for editors of form fields. It enables editation
/// of form fields, such as entering text, clicking on check box etc.
class PDFFormFieldWidgetEditor : public QObject
class PDFFormFieldWidgetEditor
{
Q_OBJECT
private:
using BaseClass = QObject;
public:
explicit PDFFormFieldWidgetEditor(PDFFormManager* formManager, PDFFormWidget formWidget, QObject* parent);
explicit PDFFormFieldWidgetEditor(PDFFormManager* formManager, PDFFormWidget formWidget);
virtual ~PDFFormFieldWidgetEditor() = default;
virtual void shortcutOverrideEvent(QWidget* widget, QKeyEvent* event);
@ -640,9 +481,13 @@ public:
void setFocus(bool hasFocus);
/// Draw form field widget using given parameters
/// Draw form field widget using given parameters. It is used, when
/// we want to draw editor contents on the painter using parameters.
/// Parameter \p edit decides, if editor is drawn, or static contents
/// based on field value is drawn.
/// \param parameters Parameters
virtual void draw(AnnotationDrawParameters& parameters) const;
/// \param edit Draw editor or static contents
virtual void draw(AnnotationDrawParameters& parameters, bool edit) const;
protected:
/// This function is called every time, the focus state changes
@ -656,118 +501,6 @@ protected:
bool m_hasFocus;
};
/// Editor for button-like editors
class PDFFormFieldAbstractButtonEditor : public PDFFormFieldWidgetEditor
{
Q_OBJECT
private:
using BaseClass = PDFFormFieldWidgetEditor;
public:
explicit PDFFormFieldAbstractButtonEditor(PDFFormManager* formManager, PDFFormWidget formWidget, QObject* parent);
virtual ~PDFFormFieldAbstractButtonEditor() = default;
virtual void shortcutOverrideEvent(QWidget* widget, QKeyEvent* event) override;
virtual void keyPressEvent(QWidget* widget, QKeyEvent* event) override;
virtual void keyReleaseEvent(QWidget* widget, QKeyEvent* event) override;
virtual void mousePressEvent(QWidget* widget, QMouseEvent* event, const QPointF& mousePagePosition) override;
protected:
virtual void click() = 0;
};
/// Editor for push buttons
class PDFFormFieldPushButtonEditor : public PDFFormFieldAbstractButtonEditor
{
Q_OBJECT
private:
using BaseClass = PDFFormFieldAbstractButtonEditor;
public:
explicit PDFFormFieldPushButtonEditor(PDFFormManager* formManager, PDFFormWidget formWidget, QObject* parent);
virtual ~PDFFormFieldPushButtonEditor() = default;
protected:
virtual void click() override;
};
/// Editor for check boxes or radio buttons
class PDFFormFieldCheckableButtonEditor : public PDFFormFieldAbstractButtonEditor
{
Q_OBJECT
private:
using BaseClass = PDFFormFieldAbstractButtonEditor;
public:
explicit PDFFormFieldCheckableButtonEditor(PDFFormManager* formManager, PDFFormWidget formWidget, QObject* parent);
virtual ~PDFFormFieldCheckableButtonEditor() = default;
protected:
virtual void click() override;
};
/// Editor for text fields
class PDFFormFieldTextBoxEditor : public PDFFormFieldWidgetEditor
{
Q_OBJECT
private:
using BaseClass = PDFFormFieldWidgetEditor;
public:
explicit PDFFormFieldTextBoxEditor(PDFFormManager* formManager, PDFFormWidget formWidget, QObject* parent);
virtual ~PDFFormFieldTextBoxEditor() = default;
virtual void shortcutOverrideEvent(QWidget* widget, QKeyEvent* event);
virtual void keyPressEvent(QWidget* widget, QKeyEvent* event) override;
virtual void mousePressEvent(QWidget* widget, QMouseEvent* event, const QPointF& mousePagePosition);
virtual void mouseDoubleClickEvent(QWidget* widget, QMouseEvent* event, const QPointF& mousePagePosition);
virtual void mouseMoveEvent(QWidget* widget, QMouseEvent* event, const QPointF& mousePagePosition);
virtual void reloadValue() override;
virtual bool isEditorDrawEnabled() const override { return m_hasFocus; }
virtual void draw(AnnotationDrawParameters& parameters) const override;
/// Initializes text edit using actual form field value,
/// font, color for text edit appearance.
/// \param textEdit Text editor
void initializeTextEdit(PDFTextEditPseudowidget* textEdit) const;
protected:
virtual void setFocusImpl(bool focused);
private:
PDFTextEditPseudowidget m_textEdit;
};
/// Editor for combo boxes
class PDFFormFieldComboBoxEditor : public PDFFormFieldWidgetEditor
{
Q_OBJECT
private:
using BaseClass = PDFFormFieldWidgetEditor;
public:
explicit PDFFormFieldComboBoxEditor(PDFFormManager* formManager, PDFFormWidget formWidget, QObject* parent);
virtual ~PDFFormFieldComboBoxEditor() = default;
};
/// Editor for list boxes
class PDFFormFieldListBoxEditor : public PDFFormFieldWidgetEditor
{
Q_OBJECT
private:
using BaseClass = PDFFormFieldWidgetEditor;
public:
explicit PDFFormFieldListBoxEditor(PDFFormManager* formManager, PDFFormWidget formWidget, QObject* parent);
virtual ~PDFFormFieldListBoxEditor() = default;
};
/// Form manager. Manages all form widgets functionality - triggers actions,
/// edits fields, updates annotation appearances, etc. Valid pointer to annotation
/// manager is requirement.
@ -940,6 +673,9 @@ private:
/// \param event Mouse event
void ungrabMouse(const MouseEventInfo& info, QMouseEvent* event);
/// Releases all form widget editors
void clearEditors();
PDFDrawWidgetProxy* m_proxy;
PDFAnnotationManager* m_annotationManager;
const PDFDocument* m_document;

View File

@ -6918,6 +6918,134 @@ return annotationObject;</property>
<property name="functionDescription">Set document title.</property>
<property name="returnType">_void</property>
</QObject>
<QObject class="codegen::GeneratedFunction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">formField</property>
<property name="parameterType">_PDFObjectReference</property>
<property name="parameterDescription">Form field</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">indices</property>
<property name="parameterType">_PDFIntegerVector</property>
<property name="parameterDescription">Sorted list of selected indices</property>
</QObject>
</property>
<property name="actionType">Parameters</property>
<property name="variableName"></property>
<property name="variableType">_void</property>
<property name="code"></property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">I</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">indices</property>
</QObject>
</property>
<property name="dictionaryItemName"></property>
<property name="objectType">Dictionary</property>
<property name="value"></property>
</QObject>
</property>
<property name="actionType">CreateObject</property>
<property name="variableName">formFieldObject</property>
<property name="variableType">_PDFObject</property>
<property name="code"></property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items"/>
<property name="actionType">Code</property>
<property name="variableName"></property>
<property name="variableType">_void</property>
<property name="code">mergeTo(formField, formFieldObject);</property>
</QObject>
</property>
<property name="functionType">Forms</property>
<property name="functionName">setFormFieldChoiceIndices</property>
<property name="functionDescription">Sets form field list box indices.</property>
<property name="returnType">_void</property>
</QObject>
<QObject class="codegen::GeneratedFunction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">formField</property>
<property name="parameterType">_PDFObjectReference</property>
<property name="parameterDescription">Form field</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">topIndex</property>
<property name="parameterType">_PDFInteger</property>
<property name="parameterDescription">Zero-based index of first visible item</property>
</QObject>
</property>
<property name="actionType">Parameters</property>
<property name="variableName"></property>
<property name="variableType">_void</property>
<property name="code"></property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">TI</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">topIndex</property>
</QObject>
</property>
<property name="dictionaryItemName"></property>
<property name="objectType">Dictionary</property>
<property name="value"></property>
</QObject>
</property>
<property name="actionType">CreateObject</property>
<property name="variableName">formFieldObject</property>
<property name="variableType">_PDFObject</property>
<property name="code"></property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items"/>
<property name="actionType">Code</property>
<property name="variableName"></property>
<property name="variableType">_void</property>
<property name="code">mergeTo(formField, formFieldObject);</property>
</QObject>
</property>
<property name="functionType">Forms</property>
<property name="functionName">setFormFieldChoiceTopIndex</property>
<property name="functionDescription">Sets form field list box top index. Top index is zero-based index of first item visible in the list box.</property>
<property name="returnType">_void</property>
</QObject>
<QObject class="codegen::GeneratedFunction">
<property name="objectName"></property>
<property name="items">