From 57c9cae257c7db47c5862d7d9a36b8e983139d19 Mon Sep 17 00:00:00 2001 From: Fabio D'Urso Date: Sun, 11 May 2014 15:06:27 +0200 Subject: [PATCH] Don't save form contents to docdata/ But existing saved form from previous Okular versions are preserved --- core/document.cpp | 23 +++++++++++++++++------ core/document_p.h | 1 + core/documentcommands.cpp | 8 ++++++++ core/observer.h | 2 +- core/page.cpp | 16 +++++++++++++++- core/page_p.h | 7 ++++++- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/core/document.cpp b/core/document.cpp index e78dcf17e..dd67e4afb 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -1198,12 +1198,13 @@ void DocumentPrivate::saveDocumentInfo() const // 2.1. Save page attributes (bookmark state, annotations, ... ) to DOM QDomElement pageList = doc.createElement( "pageList" ); root.appendChild( pageList ); - // OriginalAnnotationPageItems tells to store the same unmodified - // annotation list that we read when we opened the file and ignore any - // change made by the user. Since we don't store annotations in - // docdata/ any more, this is necessary to preserve annotations that - // previous Okular version had stored there. - const PageItems saveWhat = AllPageItems | OriginalAnnotationPageItems; + // OriginalAnnotationPageItems and OriginalFormFieldPageItems tell to + // store the same unmodified annotation list and form contents that we + // read when we opened the file and ignore any change made by the user. + // Since we don't store annotations and forms docdata/ any more, this is + // necessary to preserve annotations/forms that previous Okular version + // had stored there. + const PageItems saveWhat = AllPageItems | OriginalAnnotationPageItems | OriginalFormFieldPageItems; // .... save pages that hold data QVector< Page * >::const_iterator pIt = m_pagesVector.constBegin(), pEnd = m_pagesVector.constEnd(); for ( ; pIt != pEnd; ++pIt ) @@ -2921,6 +2922,16 @@ void DocumentPrivate::notifyAnnotationChanges( int page ) foreachObserverD( notifyPageChanged( page, flags ) ); } +void DocumentPrivate::notifyFormChanges( int page ) +{ + // Unless we're still loading initial form contents from metadata, the user + // now needs to save or form changes will be lost + if ( !m_metadataLoadingCompleted ) + return; + + foreachObserverD( notifyPageChanged( page, DocumentObserver::NeedSaveAs ) ); +} + void Document::addPageAnnotation( int page, Annotation * annotation ) { // Transform annotation's base boundary rectangle into unrotated coordinates diff --git a/core/document_p.h b/core/document_p.h index 3c539cf42..deff05a61 100644 --- a/core/document_p.h +++ b/core/document_p.h @@ -133,6 +133,7 @@ class DocumentPrivate bool savePageDocumentInfo( KTemporaryFile *infoFile, int what ) const; DocumentViewport nextDocumentViewport() const; void notifyAnnotationChanges( int page ); + void notifyFormChanges( int page ); bool canAddAnnotationsNatively() const; bool canModifyExternalAnnotations() const; bool canRemoveExternalAnnotations() const; diff --git a/core/documentcommands.cpp b/core/documentcommands.cpp index 95aded51d..b106f0457 100644 --- a/core/documentcommands.cpp +++ b/core/documentcommands.cpp @@ -368,6 +368,7 @@ void EditFormTextCommand::undo() moveViewportIfBoundingRectNotFullyVisible( m_form->rect(), m_docPriv, m_pageNumber ); m_form->setText( m_prevContents ); emit m_docPriv->m_parent->formTextChangedByUndoRedo( m_pageNumber, m_form, m_prevContents, m_prevCursorPos, m_prevAnchorPos ); + m_docPriv->notifyFormChanges( m_pageNumber ); } void EditFormTextCommand::redo() @@ -375,6 +376,7 @@ void EditFormTextCommand::redo() moveViewportIfBoundingRectNotFullyVisible( m_form->rect(), m_docPriv, m_pageNumber ); m_form->setText( m_newContents ); emit m_docPriv->m_parent->formTextChangedByUndoRedo( m_pageNumber, m_form, m_newContents, m_newCursorPos, m_newCursorPos ); + m_docPriv->notifyFormChanges( m_pageNumber ); } int EditFormTextCommand::id() const @@ -415,6 +417,7 @@ void EditFormListCommand::undo() moveViewportIfBoundingRectNotFullyVisible( m_form->rect(), m_docPriv, m_pageNumber ); m_form->setCurrentChoices( m_prevChoices ); emit m_docPriv->m_parent->formListChangedByUndoRedo( m_pageNumber, m_form, m_prevChoices ); + m_docPriv->notifyFormChanges( m_pageNumber ); } void EditFormListCommand::redo() @@ -422,6 +425,7 @@ void EditFormListCommand::redo() moveViewportIfBoundingRectNotFullyVisible( m_form->rect(), m_docPriv, m_pageNumber ); m_form->setCurrentChoices( m_newChoices ); emit m_docPriv->m_parent->formListChangedByUndoRedo( m_pageNumber, m_form, m_newChoices ); + m_docPriv->notifyFormChanges( m_pageNumber ); } EditFormComboCommand::EditFormComboCommand( Okular::DocumentPrivate* docPriv, @@ -468,6 +472,7 @@ void EditFormComboCommand::undo() } moveViewportIfBoundingRectNotFullyVisible( m_form->rect(), m_docPriv, m_pageNumber ); emit m_docPriv->m_parent->formComboChangedByUndoRedo( m_pageNumber, m_form, m_prevContents, m_prevCursorPos, m_prevAnchorPos ); + m_docPriv->notifyFormChanges( m_pageNumber ); } void EditFormComboCommand::redo() @@ -482,6 +487,7 @@ void EditFormComboCommand::redo() } moveViewportIfBoundingRectNotFullyVisible( m_form->rect(), m_docPriv, m_pageNumber ); emit m_docPriv->m_parent->formComboChangedByUndoRedo( m_pageNumber, m_form, m_newContents, m_newCursorPos, m_newCursorPos ); + m_docPriv->notifyFormChanges( m_pageNumber ); } int EditFormComboCommand::id() const @@ -538,6 +544,7 @@ void EditFormButtonsCommand::undo() Okular::NormalizedRect boundingRect = buildBoundingRectangleForButtons( m_formButtons ); moveViewportIfBoundingRectNotFullyVisible( boundingRect, m_docPriv, m_pageNumber ); emit m_docPriv->m_parent->formButtonsChangedByUndoRedo( m_pageNumber, m_formButtons ); + m_docPriv->notifyFormChanges( m_pageNumber ); } void EditFormButtonsCommand::redo() @@ -553,6 +560,7 @@ void EditFormButtonsCommand::redo() Okular::NormalizedRect boundingRect = buildBoundingRectangleForButtons( m_formButtons ); moveViewportIfBoundingRectNotFullyVisible( boundingRect, m_docPriv, m_pageNumber ); emit m_docPriv->m_parent->formButtonsChangedByUndoRedo( m_pageNumber, m_formButtons ); + m_docPriv->notifyFormChanges( m_pageNumber ); } void EditFormButtonsCommand::clearFormButtonStates() diff --git a/core/observer.h b/core/observer.h index 4d3d9e0e4..bda930672 100644 --- a/core/observer.h +++ b/core/observer.h @@ -45,7 +45,7 @@ class OKULAR_EXPORT DocumentObserver TextSelection = 8, ///< Text selection has been changed Annotations = 16, ///< Annotations have been changed BoundingBox = 32, ///< Bounding boxes have been changed - NeedSaveAs = 64 ///< Set along with Annotations when Save As is needed or annotation changes will be lost @since 0.15 (KDE 4.9) + NeedSaveAs = 64 ///< Set when "Save" is needed or annotation/form changes will be lost @since 0.15 (KDE 4.9) }; /** diff --git a/core/page.cpp b/core/page.cpp index b48f21067..5b988a2cc 100644 --- a/core/page.cpp +++ b/core/page.cpp @@ -832,6 +832,10 @@ void PagePrivate::restoreLocalContents( const QDomNode & pageNode ) // parse formList child element else if ( childElement.tagName() == "forms" ) { + // Clone forms as root node in restoredFormFieldList + const QDomNode clonedNode = restoredFormFieldList.importNode( childElement, true ); + restoredFormFieldList.appendChild( clonedNode ); + if ( formfields.isEmpty() ) continue; @@ -927,7 +931,17 @@ void PagePrivate::saveLocalContents( QDomNode & parentNode, QDomDocument & docum } // add forms info if has got any - if ( ( what & FormFieldPageItems ) && !formfields.isEmpty() ) + if ( ( what & FormFieldPageItems ) && ( what & OriginalFormFieldPageItems ) ) + { + const QDomElement savedDocRoot = restoredFormFieldList.documentElement(); + if ( !savedDocRoot.isNull() ) + { + // Import and append node in target document + const QDomNode importedNode = document.importNode( savedDocRoot, true ); + pageElement.appendChild( importedNode ); + } + } + else if ( ( what & FormFieldPageItems ) && !formfields.isEmpty() ) { // create the formList QDomElement formListElement = document.createElement( "forms" ); diff --git a/core/page_p.h b/core/page_p.h index 69a2c66c2..8be2da08e 100644 --- a/core/page_p.h +++ b/core/page_p.h @@ -48,7 +48,11 @@ enum PageItem /* If set along with AnnotationPageItems, tells saveLocalContents to save * the original annotations (if any) instead of the modified ones */ - OriginalAnnotationPageItems = 0x100 + OriginalAnnotationPageItems = 0x100, + + /* If set along with FormFieldPageItems, tells saveLocalContents to save + * the original form contents (if any) instead of the modified one */ + OriginalFormFieldPageItems = 0x200 }; Q_DECLARE_FLAGS(PageItems, PageItem) @@ -142,6 +146,7 @@ class PagePrivate bool m_isBoundingBoxKnown : 1; QDomDocument restoredLocalAnnotationList; // ... + QDomDocument restoredFormFieldList; // ... }; }