summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Heinecke <[email protected]>2018-02-21 23:56:43 +0100
committerAlbert Astals Cid <[email protected]>2018-02-22 00:12:34 +0100
commitd50c06df25902b63aaab29963db81f275d8df51b (patch)
tree06fcdc8dac60ec23209f48dd98959dee8f0316c9
parentd3a549ca258e04058fa04d2993e2926e7136f390 (diff)
Add refresh widgets if underlying field changes
Summary: If a field is updated because of a calculate form action / a script execution, not only refresh the rendered pixmap but also the corresponding formWidget. Test Plan: Unittest in separate revision. Tested it manually, too. Reviewers: #okular Subscribers: aacid Tags: #okular Maniphest Tasks: T7805 Differential Revision: https://phabricator.kde.org/D10048
-rw-r--r--core/document.cpp33
-rw-r--r--core/document.h8
-rw-r--r--core/script/kjs_field.cpp1
-rw-r--r--ui/formwidgets.cpp29
-rw-r--r--ui/formwidgets.h4
5 files changed, 75 insertions, 0 deletions
diff --git a/core/document.cpp b/core/document.cpp
index 5f5069c..33ac210 100644
--- a/core/document.cpp
+++ b/core/document.cpp
@@ -16,6 +16,7 @@
#include "documentcommands_p.h"
#include <limits.h>
+#include <memory>
#ifdef Q_OS_WIN
#define _WIN32_WINNT 0x0500
#include <windows.h>
@@ -80,6 +81,7 @@
#include "page_p.h"
#include "pagecontroller_p.h"
#include "scripter.h"
+#include "script/event_p.h"
#include "settings_core.h"
#include "sourcereference.h"
#include "sourcereference_p.h"
@@ -1118,13 +1120,40 @@ void DocumentPrivate::recalculateForms()
const Page *p = m_parent->page( pageIdx );
if (p)
{
+ bool pageNeedsRefresh = false;
foreach( FormField *form, p->formFields() )
{
if ( form->id() == formId ) {
Action *action = form->additionalAction( FormField::CalculateField );
if (action)
{
+ FormFieldText *fft = dynamic_cast< FormFieldText * >( form );
+ std::shared_ptr<Event> event;
+ QString oldVal;
+ if ( fft )
+ {
+ // Pepare text calculate event
+ event = Event::createFormCalculateEvent( fft, m_pagesVector[pageIdx] );
+ if ( !m_scripter )
+ m_scripter = new Scripter( this );
+ m_scripter->setEvent( event.get() );
+ // The value maybe changed in javascript so save it first.
+ oldVal = fft->text();
+ }
+
m_parent->processAction( action );
+ if ( event && fft )
+ {
+ // Update text field from calculate
+ m_scripter->setEvent( nullptr );
+ const QString newVal = event->value().toString();
+ if ( newVal != oldVal )
+ {
+ fft->setText( newVal );
+ emit m_parent->refreshFormWidget( fft );
+ pageNeedsRefresh = true;
+ }
+ }
}
else
{
@@ -1132,6 +1161,10 @@ void DocumentPrivate::recalculateForms()
}
}
}
+ if ( pageNeedsRefresh )
+ {
+ refreshPixmaps( p->number() );
+ }
}
}
}
diff --git a/core/document.h b/core/document.h
index f996f41..3354e55 100644
--- a/core/document.h
+++ b/core/document.h
@@ -48,6 +48,7 @@ class DocumentViewport;
class EmbeddedFile;
class ExportFormat;
class FontInfo;
+class FormField;
class FormFieldText;
class FormFieldButton;
class FormFieldChoice;
@@ -1192,6 +1193,13 @@ class OKULARCORE_EXPORT Document : public QObject
* @since 0.17 (KDE 4.11)
*/
void formButtonsChangedByUndoRedo( int page, const QList< Okular::FormFieldButton* > & formButtons );
+
+ /**
+ * This signal is emmitted whenever a FormField was changed programatically and the
+ * according widget should be updated.
+ * @since 1.4
+ */
+ void refreshFormWidget( Okular::FormField *field );
private:
/// @cond PRIVATE
friend class DocumentPrivate;
diff --git a/core/script/kjs_field.cpp b/core/script/kjs_field.cpp
index 1e77913..b3798eb 100644
--- a/core/script/kjs_field.cpp
+++ b/core/script/kjs_field.cpp
@@ -171,6 +171,7 @@ static void fieldSetValue( KJSContext *context, void *object, KJSObject value )
{
Document *doc = PagePrivate::get( page )->m_doc->m_parent;
QMetaObject::invokeMethod( doc, "refreshPixmaps", Qt::QueuedConnection, Q_ARG( int, page->number() ) );
+ emit doc->refreshFormWidget( field );
}
else
{
diff --git a/ui/formwidgets.cpp b/ui/formwidgets.cpp
index 7f3d188..d39bd26 100644
--- a/ui/formwidgets.cpp
+++ b/ui/formwidgets.cpp
@@ -70,6 +70,10 @@ FormWidgetsController::FormWidgetsController( Okular::Document *doc )
this, &FormWidgetsController::canUndoChanged );
connect( doc, &Okular::Document::canRedoChanged,
this, &FormWidgetsController::canRedoChanged );
+
+ // Connect the generic formWidget refresh signal
+ connect( doc, &Okular::Document::refreshFormWidget,
+ this, &FormWidgetsController::refreshFormWidget );
}
FormWidgetsController::~FormWidgetsController()
@@ -419,6 +423,8 @@ void FormLineEdit::setFormWidgetsController(FormWidgetsController* controller)
FormWidgetIface::setFormWidgetsController(controller);
connect( m_controller, &FormWidgetsController::formTextChangedByUndoRedo,
this, &FormLineEdit::slotHandleTextChangedByUndoRedo );
+ connect( m_controller, &FormWidgetsController::refreshFormWidget,
+ this, &FormLineEdit::slotRefresh );
}
bool FormLineEdit::event( QEvent* e )
@@ -515,6 +521,17 @@ void FormLineEdit::slotHandleTextChangedByUndoRedo( int pageNumber,
setFocus();
}
+void FormLineEdit::slotRefresh( Okular::FormField *form )
+{
+ if (form != m_ff)
+ {
+ return;
+ }
+ Okular::FormFieldText *text = static_cast<Okular::FormFieldText *> ( form );
+
+ setText( text->text() );
+}
+
TextAreaEdit::TextAreaEdit( Okular::FormFieldText * text, QWidget * parent )
: KTextEdit( parent ), FormWidgetIface( this, text, true )
{
@@ -582,6 +599,8 @@ void TextAreaEdit::setFormWidgetsController( FormWidgetsController* controller )
FormWidgetIface::setFormWidgetsController( controller );
connect( m_controller, &FormWidgetsController::formTextChangedByUndoRedo,
this, &TextAreaEdit::slotHandleTextChangedByUndoRedo );
+ connect( m_controller, &FormWidgetsController::refreshFormWidget,
+ this, &TextAreaEdit::slotRefresh );
}
void TextAreaEdit::slotHandleTextChangedByUndoRedo( int pageNumber,
@@ -623,6 +642,16 @@ void TextAreaEdit::slotChanged()
m_prevAnchorPos = textCursor().anchor();
}
+void TextAreaEdit::slotRefresh( Okular::FormField *form )
+{
+ if (form != m_ff)
+ {
+ return;
+ }
+ Okular::FormFieldText *text = static_cast<Okular::FormFieldText *> ( form );
+
+ setPlainText( text->text() );
+}
FileEdit::FileEdit( Okular::FormFieldText * text, QWidget * parent )
: KUrlRequester( parent ), FormWidgetIface( this, text, !text->isReadOnly() )
diff --git a/ui/formwidgets.h b/ui/formwidgets.h
index d8ba961..e0b1f03 100644
--- a/ui/formwidgets.h
+++ b/ui/formwidgets.h
@@ -113,6 +113,8 @@ class FormWidgetsController : public QObject
void action( Okular::Action *action );
+ void refreshFormWidget( Okular::FormField * form );
+
private Q_SLOTS:
void slotButtonClicked( QAbstractButton *button );
void slotFormButtonsChangedByUndoRedo( int pageNumber,
@@ -223,6 +225,7 @@ class FormLineEdit : public QLineEdit, public FormWidgetIface
int anchorPos );
private Q_SLOTS:
void slotChanged();
+ void slotRefresh(Okular::FormField* form);
private:
int m_prevCursorPos;
@@ -249,6 +252,7 @@ class TextAreaEdit : public KTextEdit, public FormWidgetIface
private Q_SLOTS:
void slotChanged();
+ void slotRefresh(Okular::FormField* form);
private:
int m_prevCursorPos;