View Issue Details

IDProjectCategoryView StatusLast Update
0016273ScribusScripterpublic2020-12-02 14:52
Reporterale Assigned To 
PrioritynormalSeverityfeatureReproducibilityN/A
Status newResolutionopen 
Product Version1.5.6.svn 
Target VersionFixed in Version 
Summary0016273: [PATCH] scripter api: add commands for embedding / extract images
Descriptionadd two commands to the scripter API for embedding images in the .sla and extracting embedded.
TagsNo tags attached.
PatchYes

Activities

ale

2020-10-22 16:19

manager   ~0048180

i'm working on it...

ale

2020-10-27 13:06

manager   ~0048228

refactor ScribusMainWindow::toogleInlineState() into extractImageFromFrame() and embedImageInFrame() and add the two scripter commands that use the new functions.

ale

2020-10-27 13:25

manager   ~0048229

sorry, new version with a check if the image is (not) already embedded...

extract-embed-image-02.diff (9,588 bytes)
diff --git a/scribus/plugins/scriptplugin/cmdmani.cpp b/scribus/plugins/scriptplugin/cmdmani.cpp
index db30970e3..115388e79 100644
--- a/scribus/plugins/scriptplugin/cmdmani.cpp
+++ b/scribus/plugins/scriptplugin/cmdmani.cpp
@@ -649,6 +649,68 @@ PyObject *scribus_combinepolygons(PyObject * /* self */)
 	Py_RETURN_NONE;
 }
 
+PyObject *scribus_embedimage(PyObject* /* self */, PyObject* args)
+{
+	if (!checkHaveDocument())
+		return nullptr;
+	char *name = const_cast<char*>("");
+	if (!PyArg_ParseTuple(args, "|es", "utf-8", &name))
+		return nullptr;
+	PageItem *item = GetUniqueItem(QString::fromUtf8(name));
+	if (item == nullptr)
+		return nullptr;
+
+	if (!item->isImageFrame())
+	{
+		PyErr_SetString(WrongFrameTypeError, QObject::tr("Specified item not an image frame.","python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+	if (item->isImageInline())
+	{
+		PyErr_SetString(ScribusException, QObject::tr("The image is already embedded ","python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+	ScCore->primaryMainWindow()->embedImageInFrame(item);
+
+	Py_RETURN_NONE;
+}
+
+PyObject *scribus_extractimage(PyObject* /* self */, PyObject* args)
+{
+	char *itemname = const_cast<char*>("");
+	char *filename = const_cast<char*>("");
+	if (!PyArg_ParseTuple(args, "es|es", "utf-8", &filename, "utf-8", &itemname))
+		return nullptr;
+	if (!checkHaveDocument())
+		return nullptr;
+	PageItem *item = GetUniqueItem(QString::fromUtf8(itemname));
+	if (item == nullptr)
+		return nullptr;
+	if (!item->isImageFrame())
+	{
+		PyErr_SetString(ScribusException, QObject::tr("Specified item not an image frame.","python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+	if (!item->isImageInline())
+	{
+		PyErr_SetString(ScribusException, QObject::tr("The image is not embedded ","python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+	if (filename == "")
+	{
+		PyErr_SetString(ScribusException, QObject::tr("File name cannot be empty.","python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+	ScCore->primaryMainWindow()->extractImageFromFrame(item, filename);
+
+	Py_RETURN_NONE;
+}
+
 /*! HACK: this removes "warning: 'blah' defined but not used" compiler warnings
 with header files structure untouched (docstrings are kept near declarations)
 PV */
@@ -657,6 +719,8 @@ void cmdmanidocwarnings()
 	QStringList s;
 	s << scribus_combinepolygons__doc__
 	  << scribus_deselect__doc__
+	  << scribus_embedimage__doc__
+	  << scribus_extractimage__doc__
 	  << scribus_flipobject__doc__
 	  << scribus_getselobjnam__doc__
 	  << scribus_groupobj__doc__
diff --git a/scribus/plugins/scriptplugin/cmdmani.h b/scribus/plugins/scriptplugin/cmdmani.h
index d8b8ecca9..0276bdc2e 100644
--- a/scribus/plugins/scriptplugin/cmdmani.h
+++ b/scribus/plugins/scriptplugin/cmdmani.h
@@ -278,4 +278,22 @@ Combine two or more selected Polygons\n\
 "));
 PyObject *scribus_combinepolygons(PyObject * /* self */);
 
+PyDoc_STRVAR(scribus_embedimage__doc__,
+QT_TR_NOOP("embedImage(name:str = None )\n\
+\n\
+Embed the linked image for the image frame \"name\".\n\
+If \"name\" is not given the currently selected item is used.\n\
+"));
+/*! embed the linked image in the frame */
+PyObject *scribus_embedimage(PyObject * /*self*/, PyObject* args);
+
+PyDoc_STRVAR(scribus_extractimage__doc__,
+QT_TR_NOOP("extractImage(filename:str, name:str = None )\n\
+\n\
+Extract the image embedded in the image frame \"name\" and store it in the \"filename\" file.\n\
+If \"name\" is not given the currently selected item is used.\n\
+"));
+/*! extract the embedded image from the frame */
+PyObject *scribus_extractimage(PyObject * /*self*/, PyObject* args);
+
 #endif
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index ced8c5e50..7a71994d5 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -327,6 +327,8 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("deselectAll"), (PyCFunction)scribus_deselect, METH_NOARGS, tr(scribus_deselect__doc__)},
 	{const_cast<char*>("docChanged"), scribus_docchanged, METH_VARARGS, tr(scribus_docchanged__doc__)},
 	{const_cast<char*>("editMasterPage"), scribus_editmasterpage, METH_VARARGS, tr(scribus_editmasterpage__doc__)},
+	{const_cast<char*>("embedImage"), scribus_embedimage, METH_VARARGS, tr(scribus_embedimage__doc__)},
+	{const_cast<char*>("extractImage"), scribus_extractimage, METH_VARARGS, tr(scribus_extractimage__doc__)},
 	{const_cast<char*>("fileDialog"), (PyCFunction)scribus_filedialog, METH_VARARGS|METH_KEYWORDS, tr(scribus_filedialog__doc__)},
 	{const_cast<char*>("fileQuit"), scribus_filequit, METH_VARARGS, tr(scribus_filequit__doc__)},
 	{const_cast<char*>("flipObject"), scribus_flipobject, METH_VARARGS, tr(scribus_flipobject__doc__)},
diff --git a/scribus/scribus.cpp b/scribus/scribus.cpp
index 792763a73..529fda0c9 100644
--- a/scribus/scribus.cpp
+++ b/scribus/scribus.cpp
@@ -3903,56 +3903,70 @@ void ScribusMainWindow::toogleInlineState()
 	if (!currItem->imageIsAvailable)
 		return;
 	if (currItem->isImageInline())
-	{
-		QFileInfo fiB(currItem->Pfile);
+		extractImageFromFrame(currItem);
+	else
+		embedImageInFrame(currItem);
+	scrActions["itemToggleInlineImage"]->setChecked(currItem->isImageInline());
+}
 
-		PrefsContext* docContext = m_prefsManager.prefsFile->getContext("docdirs", false);
-		QString wdir = ".";
-		if (doc->hasName)
-		{
-			QFileInfo fi(doc->documentFileName());
-			wdir = QDir::fromNativeSeparators( fi.path() );
-		}
-		else
-		{
-			QString prefsDocDir = m_prefsManager.documentDir();
-			if (!prefsDocDir.isEmpty())
-				wdir = docContext->get("place_as", prefsDocDir);
-			else
-				wdir = docContext->get("place_as", ".");
-			wdir = QDir::fromNativeSeparators( wdir );
-		}
-		QString fileName = CFileDialog(wdir, tr("Filename and Path for Image"), tr("All Files (*)"), fiB.fileName(), fdHidePreviewCheckBox);
-		if (!fileName.isEmpty())
-		{
-			if (ScCore->fileWatcher->files().contains(currItem->Pfile) != 0)
-				ScCore->fileWatcher->removeFile(currItem->Pfile);
-			docContext->set("place_as", fileName.left(fileName.lastIndexOf("/")));
-			if (overwrite(this, fileName))
-			{
-				currItem->makeImageExternal(fileName);
-				ScCore->fileWatcher->addFile(currItem->Pfile);
-				bool fho = currItem->imageFlippedH();
-				bool fvo = currItem->imageFlippedV();
-				doc->loadPict(currItem->Pfile, currItem, true);
-				currItem->setImageFlippedH(fho);
-				currItem->setImageFlippedV(fvo);
-			}
-		}
+void ScribusMainWindow::embedImageInFrame(PageItem* currItem)
+{
+	if (ScCore->fileWatcher->files().contains(currItem->Pfile) != 0)
+		ScCore->fileWatcher->removeFile(currItem->Pfile);
+	currItem->makeImageInline();
+	ScCore->fileWatcher->addFile(currItem->Pfile);
+	bool fho = currItem->imageFlippedH();
+	bool fvo = currItem->imageFlippedV();
+	doc->loadPict(currItem->Pfile, currItem, true);
+	currItem->setImageFlippedH(fho);
+	currItem->setImageFlippedV(fvo);
+}
+
+void ScribusMainWindow::extractImageFromFrame(PageItem* currItem)
+{
+	QFileInfo fiB(currItem->Pfile);
+
+	PrefsContext* docContext = m_prefsManager.prefsFile->getContext("docdirs", false);
+	QString wdir = ".";
+	if (doc->hasName)
+	{
+		QFileInfo fi(doc->documentFileName());
+		wdir = QDir::fromNativeSeparators( fi.path() );
 	}
 	else
 	{
-		if (ScCore->fileWatcher->files().contains(currItem->Pfile) != 0)
-			ScCore->fileWatcher->removeFile(currItem->Pfile);
-		currItem->makeImageInline();
-		ScCore->fileWatcher->addFile(currItem->Pfile);
-		bool fho = currItem->imageFlippedH();
-		bool fvo = currItem->imageFlippedV();
-		doc->loadPict(currItem->Pfile, currItem, true);
-		currItem->setImageFlippedH(fho);
-		currItem->setImageFlippedV(fvo);
+		QString prefsDocDir = m_prefsManager.documentDir();
+		if (!prefsDocDir.isEmpty())
+			wdir = docContext->get("place_as", prefsDocDir);
+		else
+			wdir = docContext->get("place_as", ".");
+		wdir = QDir::fromNativeSeparators( wdir );
 	}
-	scrActions["itemToggleInlineImage"]->setChecked(currItem->isImageInline());
+	QString fileName = CFileDialog(wdir, tr("Filename and Path for Image"), tr("All Files (*)"), fiB.fileName(), fdHidePreviewCheckBox);
+
+	if (fileName.isEmpty())
+		return;
+
+	docContext->set("place_as", fileName.left(fileName.lastIndexOf("/")));
+
+	if (!overwrite(this, fileName))
+		return;
+
+	extractImageFromFrame(currItem, fileName);
+}
+
+void ScribusMainWindow::extractImageFromFrame(PageItem* currItem, const QString& filename)
+{
+	if (ScCore->fileWatcher->files().contains(currItem->Pfile) != 0)
+		ScCore->fileWatcher->removeFile(currItem->Pfile);
+
+	currItem->makeImageExternal(filename);
+	ScCore->fileWatcher->addFile(currItem->Pfile);
+	bool fho = currItem->imageFlippedH();
+	bool fvo = currItem->imageFlippedV();
+	doc->loadPict(currItem->Pfile, currItem, true);
+	currItem->setImageFlippedH(fho);
+	currItem->setImageFlippedV(fvo);
 }
 
 void ScribusMainWindow::slotFileAppend()
diff --git a/scribus/scribus.h b/scribus/scribus.h
index 91fc4c7df..0c66451f2 100644
--- a/scribus/scribus.h
+++ b/scribus/scribus.h
@@ -336,6 +336,9 @@ public slots:
 	void slotGetContent2(); // kk2006
 	void slotGetClipboardImage();
 	void toogleInlineState();
+	void embedImageInFrame(PageItem* currItem);
+	void extractImageFromFrame(PageItem* currItem);
+	void extractImageFromFrame(PageItem* currItem, const QString& filename);
 	/*!
 	\author Franz Schmid
 	\brief Appends a Textfile to the Text in the selected Textframe at the Cursorposition

digirew

2020-12-02 14:52

reporter   ~0048516

embedImageInFrame() command is exactly what im looking for!

Im just curious if there is an "easy" way to test (and other patches) with existing scribus deployments ? or does it all have to be recompiled ?

Issue History

Date Modified Username Field Change
2020-10-21 07:09 ale New Issue
2020-10-21 12:22 jghali Description Updated View Revisions
2020-10-21 12:23 jghali Summary scripter api: add command for embedding / extract images => scripter api: add commands for embedding / extract images
2020-10-21 12:23 jghali Description Updated View Revisions
2020-10-22 16:19 ale Note Added: 0048180
2020-10-27 13:06 ale File Added: extract-embed-image.diff
2020-10-27 13:06 ale Note Added: 0048228
2020-10-27 13:25 ale File Added: extract-embed-image-02.diff
2020-10-27 13:25 ale Note Added: 0048229
2020-10-27 13:25 ale File Deleted: extract-embed-image.diff
2020-10-27 13:27 ale Summary scripter api: add commands for embedding / extract images => [PATCH] scripter api: add commands for embedding / extract images
2020-10-27 13:27 ale Patch No => Yes
2020-12-02 14:52 digirew Note Added: 0048516