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
This commit is contained in:
Pino Toscano 2007-07-04 11:41:45 +00:00
parent 55d3e7f353
commit fa6b376626
4 changed files with 178 additions and 10 deletions

View file

@ -7,25 +7,33 @@
* (at your option) any later version. *
***************************************************************************/
// local includes
#include "form.h"
#include "form_p.h"
// qt includes
#include <QtCore/QVariant>
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<int> 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<int> choices = q->currentChoices();
qSort( choices );
QStringList list;
foreach ( int c, choices )
{
list.append( QString::number( c ) );
}
return list.join( QLatin1String( ";" ) );
}
};

View file

@ -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.

41
core/form_p.h Normal file
View file

@ -0,0 +1,41 @@
/***************************************************************************
* Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
* *
* 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 <QtCore/QString>
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

View file

@ -10,8 +10,10 @@
#include "page_p.h"
// qt/kde includes
#include <QtCore/QHash>
#include <QtCore/QSet>
#include <QtCore/QString>
#include <QtCore/QVariant>
#include <QtGui/QPixmap>
#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
@ -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<int, FormField*> 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 );