From fa6b376626afa585b7d9542dc61d66533d2dfdef Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Wed, 4 Jul 2007 11:41:45 +0000 Subject: [PATCH] Add the possibility to remember the contents of form fields when closing a document, and to restore them when reopening the document. For now it's disabled, as I want to investigate a bit more with poppler wrt text fields, and to verify if that is an acceptable behaviour. svn path=/trunk/KDE/kdegraphics/okular/; revision=683212 --- core/form.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++------- core/form.h | 5 ++++ core/form_p.h | 41 +++++++++++++++++++++++++++++ core/page.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 178 insertions(+), 10 deletions(-) create mode 100644 core/form_p.h diff --git a/core/form.cpp b/core/form.cpp index 2777dface..052e97923 100644 --- a/core/form.cpp +++ b/core/form.cpp @@ -7,25 +7,33 @@ * (at your option) any later version. * ***************************************************************************/ -// local includes #include "form.h" +#include "form_p.h" + +// qt includes +#include using namespace Okular; -class Okular::FormFieldPrivate +FormFieldPrivate::FormFieldPrivate( FormField::FieldType type ) + : m_type( type ) { - public: - FormFieldPrivate( FormField::FieldType type ) - : m_type( type ) - { - } +} + +FormFieldPrivate::~FormFieldPrivate() +{ +} + +void FormFieldPrivate::setDefault() +{ + m_default = value(); +} - FormField::FieldType m_type; -}; FormField::FormField( FormFieldPrivate &dd ) : d_ptr( &dd ) { + d_ptr->q_ptr = this; } FormField::~FormField() @@ -57,6 +65,20 @@ class Okular::FormFieldTextPrivate : public Okular::FormFieldPrivate : FormFieldPrivate( FormField::FormText ) { } + + Q_DECLARE_PUBLIC( FormFieldText ) + + void setValue( const QString& v ) + { + Q_Q( FormFieldText ); + q->setText( v ); + } + + QString value() const + { + Q_Q( const FormFieldText ); + return q->text(); + } }; @@ -106,6 +128,37 @@ class Okular::FormFieldChoicePrivate : public Okular::FormFieldPrivate : FormFieldPrivate( FormField::FormChoice ) { } + + Q_DECLARE_PUBLIC( FormFieldChoice ) + + void setValue( const QString& v ) + { + Q_Q( FormFieldChoice ); + QStringList choices = v.split( ';', QString::SkipEmptyParts ); + QList newchoices; + foreach ( const QString& str, choices ) + { + bool ok = true; + int val = str.toInt( &ok ); + if ( ok ) + newchoices.append( val ); + } + if ( !newchoices.isEmpty() ) + q->setCurrentChoices( newchoices ); + } + + QString value() const + { + Q_Q( const FormFieldChoice ); + QList choices = q->currentChoices(); + qSort( choices ); + QStringList list; + foreach ( int c, choices ) + { + list.append( QString::number( c ) ); + } + return list.join( QLatin1String( ";" ) ); + } }; diff --git a/core/form.h b/core/form.h index 9ae53bae6..8eceb280a 100644 --- a/core/form.h +++ b/core/form.h @@ -17,6 +17,8 @@ namespace Okular { +class Page; +class PagePrivate; class FormFieldPrivate; class FormFieldTextPrivate; class FormFieldChoicePrivate; @@ -31,6 +33,9 @@ class FormFieldChoicePrivate; */ class OKULAR_EXPORT FormField { + friend class Page; + friend class PagePrivate; + public: /** * The types of form field. diff --git a/core/form_p.h b/core/form_p.h new file mode 100644 index 000000000..50f573e58 --- /dev/null +++ b/core/form_p.h @@ -0,0 +1,41 @@ +/*************************************************************************** + * Copyright (C) 2007 by Pino Toscano * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#ifndef OKULAR_FORM_P_H +#define OKULAR_FORM_P_H + +#include "form.h" + +#include + +namespace Okular { + +class FormField; + +class FormFieldPrivate +{ + public: + FormFieldPrivate( FormField::FieldType type ); + virtual ~FormFieldPrivate(); + + void setDefault(); + + virtual void setValue( const QString& ) = 0; + virtual QString value() const = 0; + + FormField::FieldType m_type; + QString m_default; + + Q_DECLARE_PUBLIC( FormField ) + FormField *q_ptr; +}; + +} + +#endif diff --git a/core/page.cpp b/core/page.cpp index 6a779e1ef..f4ddbe497 100644 --- a/core/page.cpp +++ b/core/page.cpp @@ -10,8 +10,10 @@ #include "page_p.h" // qt/kde includes +#include #include #include +#include #include #include #include @@ -23,6 +25,7 @@ #include "annotations.h" #include "area.h" #include "form.h" +#include "form_p.h" #include "pagecontroller_p.h" #include "pagesize.h" #include "pagetransition.h" @@ -572,6 +575,11 @@ void Page::setFormFields( const QLinkedList< FormField * >& fields ) { qDeleteAll( d->formfields ); d->formfields = fields; + QLinkedList< FormField * >::const_iterator it = d->formfields.begin(), itEnd = d->formfields.end(); + for ( ; it != itEnd; ++it ) + { + (*it)->d_ptr->setDefault(); + } } void Page::deletePixmap( int id ) @@ -688,13 +696,43 @@ void PagePrivate::restoreLocalContents( const QDomNode & pageNode ) kDebug() << "annots: XML Load time: " << time.elapsed() << "ms" << endl; #endif } + // parse formList child element + else if ( childElement.tagName() == "forms" ) + { + QHash hashedforms; + QLinkedList< FormField * >::const_iterator fIt = formfields.begin(), fItEnd = formfields.end(); + for ( ; fIt != fItEnd; ++fItEnd ) + { + hashedforms[(*fIt)->id()] = (*fIt); + } + + // iterate over all forms + QDomNode formsNode = childElement.firstChild(); + while( formsNode.isElement() ) + { + // get annotation element and advance to next annot + QDomElement formElement = formsNode.toElement(); + formsNode = formsNode.nextSibling(); + + if ( formElement.tagName() != "form" ) + continue; + + bool ok = true; + int index = formElement.attribute( "id" ).toInt( &ok ); + if ( !ok ) + continue; + + QString value = formElement.attribute( "value" ); + hashedforms[index]->d_ptr->setValue( value ); + } + } } } void PagePrivate::saveLocalContents( QDomNode & parentNode, QDomDocument & document ) const { // only add a node if there is some stuff to write into - if ( m_page->m_annotations.isEmpty() ) + if ( m_page->m_annotations.isEmpty() && formfields.isEmpty() ) return; // create the page node and set the 'number' attribute @@ -742,6 +780,37 @@ void PagePrivate::saveLocalContents( QDomNode & parentNode, QDomDocument & docum pageElement.appendChild( annotListElement ); } + // add forms info if has got any + if ( !formfields.isEmpty() ) + { +#if 0 + // create the formList + QDomElement formListElement = document.createElement( "forms" ); + + // add every form data to the formList + QLinkedList< FormField * >::const_iterator fIt = formfields.begin(), fItEnd = formfields.end(); + for ( ; fIt != fItEnd; ++fIt ) + { + // get the form field + const FormField * f = *fIt; + + QString newvalue = f->d_ptr->value(); + if ( f->d_ptr->m_default == newvalue ) + continue; + + // append an filled-up element called 'annotation' to the list + QDomElement formElement = document.createElement( "form" ); + formElement.setAttribute( "id", f->id() ); + formElement.setAttribute( "value", newvalue ); + formListElement.appendChild( formElement ); + } + + // append the annotationList element if annotations have been set + if ( formListElement.hasChildNodes() ) + pageElement.appendChild( formListElement ); +#endif + } + // append the page element only if has children if ( pageElement.hasChildNodes() ) parentNode.appendChild( pageElement );