View Issue Details

IDProjectCategoryView StatusLast Update
0014858ScribusPDFpublic2017-06-08 19:38
Reporterdockattt Assigned To 
PrioritynormalSeverityfeatureReproducibilityalways
Status newResolutionopen 
Product Version1.5.4.svn 
Summary0014858: add the writing of external javascripts to pdfs
Descriptionthe attached patch does the following:
adds a tabbed widget to the javascripts dialog that contains the original widgets in one pane. The 2nd pane contains a new list and 2 buttons. The list will be filled with the paths to external files that should contain javascript in them. The first button opens a file dialog and lets the user select a file. The 2nd button deletes the selected path in the list. These paths are stored in the scribus/.sla file and persist form session to session. The 3rd thing it does is alter the pdf writing so that the external files data is added to the pdf file. Testing has shown positive results. An external file with javascript was created and accessed by the patched scribus. 2 pdf buttons accessed the functions from the external javascript and it worked. The external functions were written in the pdf file along with the internal javascript functions that were in the .sla file.

I thought this might be a good idea when I was doing something with some javascript that was easier to work on externally than internally.
Steps To ReproduceRun with patch and try to use javascript from files that is not kept in the .sla file.
TagsNo tags attached.
PatchYes

Activities

dockattt

2017-06-08 19:38

reporter  

externaljavascripts.patch (11,017 bytes)   
Index: Scribus/scribus/pdflib_core.cpp
===================================================================
--- Scribus/scribus/pdflib_core.cpp	(revision 22056)
+++ Scribus/scribus/pdflib_core.cpp	(working copy)
@@ -10984,15 +10984,47 @@
 void PDFLibCore::PDF_End_JavaScripts()
 {
 	PdfId jsNameTreeObj = 0;
-	if (doc.JavaScripts.count() != 0)
-	{
+
+	//load the external javascript files
+	QList<QString>* data = new QList<QString>();
+	QList<QString>::const_iterator exjs_it = doc.ExternalJavaScripts.begin();
+	for(;exjs_it!=doc.ExternalJavaScripts.end(); ++exjs_it){
+		QFile* file = new QFile(*exjs_it);
+    		if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text)){
+        		
+			QTextStream* qts = new QTextStream(file);
+			QString nwstring = qts->readAll();
+			data->append(nwstring);
+			delete qts;
+		}
+		file->close();
+		delete file;
+	}
+
+	//copy the JavaScripts map and add the external javascript text
+	QMap<QString,QString> jscopy = doc.JavaScripts;
+	QList<QString>::const_iterator loaded_it = data->begin();
+	int x = 0;
+	QString base = QString("ExternalFile");
+	QString fname = base + QString(x);
+	for(;loaded_it!=data->end(); ++loaded_it){
+		while(jscopy.contains(fname)){
+			x+=1;
+			fname = base + QString(x);
+		}
+		jscopy.insert(fname, *loaded_it);
+	}	
+
+
+	if (jscopy.count() != 0)
+	{ 
 		QList<PdfId> Fjav;
 		QMap<QString,QString>::Iterator itja0;
-		for (itja0 = doc.JavaScripts.begin(); itja0 != doc.JavaScripts.end(); ++itja0)
+		for (itja0 = jscopy.begin(); itja0 != jscopy.end(); ++itja0)
 			Fjav.append(WritePDFString(itja0.value()));
 		QMap<QString,QString>::Iterator itja;
 		int i = 0;
-		for (itja = doc.JavaScripts.begin(); itja != doc.JavaScripts.end(); ++itja)
+		for (itja = jscopy.begin(); itja != jscopy.end(); ++itja)
 		{
 			PdfId Fjav1 = writer.startObj();
 			Fjav.append(Fjav1);
@@ -11003,7 +11035,7 @@
 		jsNameTreeObj = writer.startObj();
 		PutDoc("<< /Names [ ");
 		QMap<QString,QString>::Iterator itja2;
-		for (itja2 = doc.JavaScripts.begin(); itja2 != doc.JavaScripts.end(); ++itja2)
+		for (itja2 = jscopy.begin(); itja2 != jscopy.end(); ++itja2)
 		{
 			PutDoc(EncString(Pdf::toPdfDocEncoding(itja2.key()), jsNameTreeObj) + " " + Pdf::toObjRef(Fjav[i]) + " ");
 			++i;
@@ -11014,7 +11046,7 @@
 
 	writer.startObj(writer.NamesObj);
 	PutDoc("<< ");
-	if (doc.JavaScripts.count() != 0)
+	if (jscopy.count() != 0)
 		PutDoc("/JavaScript "+Pdf::toPdf(jsNameTreeObj)+" 0 R");
 	PutDoc(" >>");
 	writer.endObj(writer.NamesObj);
Index: Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.cpp
===================================================================
--- Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.cpp	(revision 22056)
+++ Scribus/scribus/plugins/fileloader/scribus150format/scribus150format.cpp	(working copy)
@@ -1401,6 +1401,12 @@
 			if (!name.isEmpty())
 				m_Doc->JavaScripts[name] = attrs.valueAsString("SCRIPT");
 		}
+		if (tagName == "JAVAEXTERNAL")
+		{
+			QString path = attrs.valueAsString("PATH");
+			if (!path.isEmpty())
+				m_Doc->ExternalJavaScripts.append(path);
+		}
 		if (tagName == "LAYERS")
 		{
 			ScLayer newLayer;
Index: Scribus/scribus/plugins/fileloader/scribus150format/scribus150format_save.cpp
===================================================================
--- Scribus/scribus/plugins/fileloader/scribus150format/scribus150format_save.cpp	(revision 22056)
+++ Scribus/scribus/plugins/fileloader/scribus150format/scribus150format_save.cpp	(working copy)
@@ -543,6 +543,12 @@
 		docu.writeAttribute("NAME",itja.key());
 		docu.writeAttribute("SCRIPT",itja.value());
 	}
+	QList<QString>::Iterator iteja;
+	for (iteja = m_Doc->ExternalJavaScripts.begin(); iteja != m_Doc->ExternalJavaScripts.end(); ++iteja)
+	{
+		docu.writeEmptyElement("JAVAEXTERNAL");
+		docu.writeAttribute("PATH",*iteja);
+	}
 	
 }
 
Index: Scribus/scribus/scribusdoc.cpp
===================================================================
--- Scribus/scribus/scribusdoc.cpp	(revision 22056)
+++ Scribus/scribus/scribusdoc.cpp	(working copy)
@@ -259,6 +259,7 @@
 	GroupCounter(1),
 	colorEngine(ScCore->defaultEngine),
 	JavaScripts(),
+	ExternalJavaScripts(),
 	TotalItems(0),
 	RePos(false),
 	BookMarks(),
@@ -376,6 +377,7 @@
 	GroupCounter(1),
 	colorEngine(ScCore->defaultEngine),
 	JavaScripts(),
+	ExternalJavaScripts(),
 	TotalItems(0),
 	RePos(false),
 	BookMarks(),
Index: Scribus/scribus/scribusdoc.h
===================================================================
--- Scribus/scribus/scribusdoc.h	(revision 22056)
+++ Scribus/scribus/scribusdoc.h	(working copy)
@@ -1322,6 +1322,7 @@
 	eRenderIntent IntentImages;
 	bool HasCMS;
 	QMap<QString,QString> JavaScripts;
+	QList<QString> ExternalJavaScripts;
 	int TotalItems;
 	PrintOptions Print_Options;
 	bool RePos;
Index: Scribus/scribus/ui/javadocs.cpp
===================================================================
--- Scribus/scribus/ui/javadocs.cpp	(revision 22056)
+++ Scribus/scribus/ui/javadocs.cpp	(working copy)
@@ -15,6 +15,7 @@
 #include <QRegExp>
 #include <QTextEdit>
 #include <QToolTip>
+#include <QFileDialog>
 
 #include "commonstrings.h"
 #include "editor.h"
@@ -32,11 +33,45 @@
 	setWindowIcon(IconManager::instance()->loadIcon("AppIcon.png"));
 	m_Doc = doc;
 	m_View = vie;
-	JavaDocsLayout = new QHBoxLayout(this);
+
+	QVBoxLayout* baselayout = new QVBoxLayout(this);
+	QTabWidget* tabthing = new QTabWidget(this);
+	baselayout->addWidget(tabthing);
+	
+	QWidget* jspanel = new QWidget(tabthing);
+	tabthing->addTab(jspanel, QString("JavaScripts"));
+	QWidget* ejspanel = new QWidget(tabthing);
+	tabthing->addTab(ejspanel, QString("External JavaScripts"));
+
+	ExternalScripts = new QListWidget(ejspanel);
+	ExternalScripts->setMinimumSize(QSize(150,200));
+	QList<QString>::const_iterator eit = m_Doc->ExternalJavaScripts.begin();
+	for(;eit != m_Doc->ExternalJavaScripts.end(); ++eit){
+		ExternalScripts->addItem(*eit);
+	}
+
+	QWidget* fppanel = new QWidget(ejspanel);
+	GetScriptPath = new QPushButton(QString("Add File Path"), fppanel);
+	DeleteScriptPath = new QPushButton(QString("Delete File Path"), fppanel);
+
+	QHBoxLayout* ejslayout = new QHBoxLayout(ejspanel);
+	ejslayout->setMargin(10);
+	ejslayout->setSpacing(5);
+	ejslayout->addWidget(ExternalScripts);
+	ejslayout->addWidget(fppanel);
+
+	QVBoxLayout* ejsblayout = new QVBoxLayout;
+	ejsblayout->addWidget(GetScriptPath);
+	ejsblayout->addWidget(DeleteScriptPath);
+	QSpacerItem* filler = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
+	ejsblayout->addItem(filler);
+	fppanel->setLayout(ejsblayout);
+
+	JavaDocsLayout = new QHBoxLayout(jspanel);
 	JavaDocsLayout->setMargin(10);
 	JavaDocsLayout->setSpacing(5);
 
-	Scripts = new QListWidget( this );
+	Scripts = new QListWidget( jspanel );
 	Scripts->setMinimumSize( QSize( 150, 200 ) );
 	QMap<QString,QString>::Iterator it;
 	for (it = m_Doc->JavaScripts.begin(); it != m_Doc->JavaScripts.end(); ++it)
@@ -47,20 +82,29 @@
 	Layout1->setMargin(0);
 	Layout1->setSpacing(5);
 
-	EditScript = new QPushButton( tr( "&Edit..." ), this);
+	EditScript = new QPushButton( tr( "&Edit..." ), jspanel);
 	Layout1->addWidget( EditScript );
 
-	AddScript = new QPushButton( tr( "&Add..." ), this);
+	AddScript = new QPushButton( tr( "&Add..." ), jspanel);
 	Layout1->addWidget( AddScript );
 
-	DeleteScript = new QPushButton( tr( "&Delete" ), this);
+	DeleteScript = new QPushButton( tr( "&Delete" ), jspanel);
 	Layout1->addWidget( DeleteScript );
 	QSpacerItem* spacer = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
 	Layout1->addItem( spacer );
 
-	ExitDia = new QPushButton( tr( "&Close" ), this);
+
+	QWidget* closearea = new QWidget(this);
+	QHBoxLayout* cabl = new QHBoxLayout(closearea);
+	closearea->setLayout(cabl);
+	QSpacerItem* closespacer = new QSpacerItem( jspanel->sizeHint().width(), 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
+	cabl->addItem(closespacer);
+	ExitDia = new QPushButton( tr( "&Close" ), closearea);
 	ExitDia->setDefault( true );
-	Layout1->addWidget( ExitDia );
+	ExitDia->setMaximumSize(ExitDia->sizeHint());
+	cabl->addWidget(ExitDia);
+	baselayout->addWidget(closearea);
+
 	if (m_Doc->JavaScripts.count() == 0)
 	{
 		EditScript->setEnabled(false);
@@ -67,6 +111,7 @@
 		DeleteScript->setEnabled(false);
 	}
 	JavaDocsLayout->addLayout( Layout1 );
+	tabthing->setMinimumSize(tabthing->sizeHint());
 	connect(AddScript, SIGNAL(clicked()), this, SLOT(slotAdd()));
 	connect(EditScript, SIGNAL(clicked()), this, SLOT(slotEdit()));
 	connect(DeleteScript, SIGNAL(clicked()), this, SLOT(slotDelete()));
@@ -73,7 +118,10 @@
 	connect(ExitDia, SIGNAL(clicked()), this, SLOT(accept()));
 	connect(Scripts, SIGNAL(itemActivated (QListWidgetItem *)), this, SLOT(slotEdit()));
 	connect(Scripts, SIGNAL(itemSelectionChanged()), this, SLOT(slotSelectionChanged()));
+	connect(GetScriptPath, SIGNAL(clicked()), this, SLOT(slotAddFilePath()));
+	connect(DeleteScriptPath, SIGNAL(clicked()), this, SLOT(slotDeleteFilePath()));
 	AddScript->setToolTip( "<qt>" + tr( "Adds a new Script, predefines a function with the same name. If you want to use this script as an \"Open Action\" script be sure not to change the name of the function." ) + "</qt>" );
+
 }
 
 void JavaDocs::slotAdd()
@@ -149,3 +197,29 @@
 	EditScript->setEnabled(currentItem != 0);
 	DeleteScript->setEnabled(currentItem != 0);
 }
+
+void JavaDocs::slotAddFilePath(){
+
+	QString file = QFileDialog::getOpenFileName(this, tr("Select A Script File"), "", tr("All Files (*)"));
+	if(file==NULL)
+		return;
+	m_Doc->ExternalJavaScripts.append(file);
+	ExternalScripts->addItem(file);
+	ExternalScripts->setCurrentRow(ExternalScripts->count() - 1);
+	
+
+
+}
+void JavaDocs::slotDeleteFilePath(){
+	QListWidgetItem* currentItem = ExternalScripts->currentItem();
+	int currentRow = ExternalScripts->currentRow();
+	if(!currentItem)
+		return;
+	m_Doc->ExternalJavaScripts.removeAt(currentRow);
+	ExternalScripts->clear();
+	QList<QString>::Iterator it;
+	for (it = m_Doc->ExternalJavaScripts.begin(); it != m_Doc->ExternalJavaScripts.end(); ++it)
+		ExternalScripts->addItem(*it);
+
+}
+
Index: Scribus/scribus/ui/javadocs.h
===================================================================
--- Scribus/scribus/ui/javadocs.h	(revision 22056)
+++ Scribus/scribus/ui/javadocs.h	(working copy)
@@ -14,6 +14,7 @@
 class QPushButton;
 class QVBoxLayout;
 class QHBoxLayout;
+class QTabWidget;
 class ScribusDoc;
 class ScribusView;
 
@@ -25,6 +26,10 @@
 	JavaDocs( QWidget* parent, ScribusDoc *doc, ScribusView* vie);
 	~JavaDocs() {};
 
+	QListWidget* ExternalScripts;
+	QPushButton* GetScriptPath;
+	QPushButton* DeleteScriptPath;
+
 	QListWidget* Scripts;
 	QPushButton* EditScript;
 	QPushButton* AddScript;
@@ -36,6 +41,8 @@
 	void slotEdit();
 	void slotDelete();
 	void slotSelectionChanged();
+	void slotAddFilePath();
+	void slotDeleteFilePath();
 
 signals:
 	void docChanged(bool);
externaljavascripts.patch (11,017 bytes)   

Issue History

Date Modified Username Field Change
2017-06-08 19:38 dockattt New Issue
2017-06-08 19:38 dockattt File Added: externaljavascripts.patch