View Issue Details

IDProjectCategoryView StatusLast Update
0017051ScribusScripterpublic2023-11-18 07:01
Reporterale Assigned To 
PrioritynormalSeverityfeatureReproducibilityN/A
Status newResolutionopen 
Product Version1.7.0.svn 
Summary0017051: [PATCH] scripter: add the itemDialog() api function
Descriptionsimilar to valueDialog() but triggering getItem:

https://doc.qt.io/qt-6/qinputdialog.html#getItem

offers a dropdown box for choosing among a set of options and returns the choice as a string.

the implementation is heavily based on valueDialog().
TagsNo tags attached.
PatchYes

Activities

ale

2023-11-17 23:27

manager  

item-dialog.diff (4,182 bytes)   
diff --git a/scribus/plugins/scriptplugin/cmddialog.cpp b/scribus/plugins/scriptplugin/cmddialog.cpp
index 263188fd087f6bf68e43bdd1392ab112a9aee185..f13efac127b4a93a998abe3497f246a8786c1b12 100644
--- a/scribus/plugins/scriptplugin/cmddialog.cpp
+++ b/scribus/plugins/scriptplugin/cmddialog.cpp
@@ -123,6 +123,47 @@ PyObject *scribus_valuedialog(PyObject* /* self */, PyObject* args)
 	return PyUnicode_FromString(txt.toUtf8());
 }
 
+PyObject *scribus_itemdialog(PyObject* /* self */, PyObject* args)
+{
+	char *caption = const_cast<char*>("");
+	char *message = const_cast<char*>("");
+	PyObject* listObject;
+	if (!PyArg_ParseTuple(args, "esesO", "utf-8", &caption, "utf-8", &message, &listObject))
+		return nullptr;
+
+	QStringList items;
+	PyObject* list = PySequence_List(listObject);
+	if (list == nullptr)
+	{
+		PyErr_SetString(PyExc_ValueError, QObject::tr("Expected a list of options", "python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+	int n = PyList_Size(list);
+	for (int i = 0; i < n; ++i)
+	{
+		PyObject* itemObject = PyList_GetItem(list, i);
+		char *item = const_cast<char*>("");
+		if (!PyArg_Parse(itemObject, "es", "utf-8", &item))
+		{
+			PyErr_SetString(PyExc_ValueError, QObject::tr("Items must be strings", "python error").toLocal8Bit().constData());
+			return nullptr;
+		}
+		items << QString::fromUtf8(item);
+	}
+
+	QApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
+	bool ok;
+	QString choice = QInputDialog::getItem(ScCore->primaryMainWindow(),
+										QString::fromUtf8(caption),
+										QString::fromUtf8(message),
+										items,
+										0,
+										false,
+										&ok);
+	return PyUnicode_FromString(choice.toUtf8());
+}
+
 PyObject *scribus_newstyledialog(PyObject*, PyObject* args)
 {
 	if (!checkHaveDocument())
@@ -155,6 +196,7 @@ void cmddialogdocwarnings()
 {
 	QStringList s;
 	s << scribus_filedialog__doc__
+	  << scribus_itemdialog__doc__
 	  << scribus_messagebox__doc__
 	  << scribus_newdocdialog__doc__ 
 	  << scribus_newstyledialog__doc__
diff --git a/scribus/plugins/scriptplugin/cmddialog.h b/scribus/plugins/scriptplugin/cmddialog.h
index 6940b730d5dda85105447c9e28fb274652e2eb93..284eed342269d0c3d1c08c6561dc857428c487bc 100644
--- a/scribus/plugins/scriptplugin/cmddialog.h
+++ b/scribus/plugins/scriptplugin/cmddialog.h
@@ -103,6 +103,16 @@ Example: valueDialog('title', 'text in the window', 'optional')\n\
 /* 09/24/2004 petr vanek */
 PyObject *scribus_valuedialog(PyObject * /*self*/, PyObject* args);
 
+/*! docstring */
+PyDoc_STRVAR(scribus_itemdialog__doc__,
+QT_TR_NOOP("itemDialog(caption, message, list_of_strings) -> string\n\
+\n\
+Shows the common 'Make a choice' dialog and returns the selected value as a string\n\
+Parameters: window title, text in the window and a list of string values.\n\
+\n\
+Example: itemDialog('title', 'text in the window', ['apples', 'pears', 'raisins'])\n\
+"));
+PyObject *scribus_itemdialog(PyObject * /*self*/, PyObject* args);
 
 PyDoc_STRVAR(scribus_newstyledialog__doc__,
 QT_TR_NOOP("newStyleDialog() -> string\n\
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index 2836dfb9672dd54a06ee5ff98f8084e54a9b9f7c..d7ade0199a4602af94a45906d8cf676d33493c0c 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -441,6 +441,7 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("isLocked"), scribus_islocked, METH_VARARGS, tr(scribus_islocked__doc__)},
 	{const_cast<char*>("isPDFBookmark"), scribus_ispdfbookmark, METH_VARARGS, tr(scribus_ispdfbookmark__doc__)},
 	{const_cast<char*>("isSpotColor"), scribus_isspotcolor, METH_VARARGS, tr(scribus_isspotcolor__doc__)},
+	{const_cast<char*>("itemDialog"), scribus_itemdialog, METH_VARARGS, tr(scribus_itemdialog__doc__)},
 	{const_cast<char*>("layoutText"), scribus_layouttext, METH_VARARGS, tr(scribus_layouttext__doc__)},
 	{const_cast<char*>("layoutTextChain"), scribus_layouttextchain, METH_VARARGS, tr(scribus_layouttextchain__doc__)},
 	{const_cast<char*>("linkTextFrames"), scribus_linktextframes, METH_VARARGS, tr(scribus_linktextframes__doc__)},
item-dialog.diff (4,182 bytes)   

ale

2023-11-17 23:57

manager   ~0050481

sorry, i noticed that the `editable` option is not as useless (in our context) as i thought...

new patch that optionally passes it to the dialog
item-dialog-2.diff (4,329 bytes)   
diff --git a/scribus/plugins/scriptplugin/cmddialog.cpp b/scribus/plugins/scriptplugin/cmddialog.cpp
index 263188fd087f6bf68e43bdd1392ab112a9aee185..d63ec41e072ceb72bae0e83e204520dadcd95367 100644
--- a/scribus/plugins/scriptplugin/cmddialog.cpp
+++ b/scribus/plugins/scriptplugin/cmddialog.cpp
@@ -123,6 +123,48 @@ PyObject *scribus_valuedialog(PyObject* /* self */, PyObject* args)
 	return PyUnicode_FromString(txt.toUtf8());
 }
 
+PyObject *scribus_itemdialog(PyObject* /* self */, PyObject* args)
+{
+	char *caption = const_cast<char*>("");
+	char *message = const_cast<char*>("");
+	PyObject* listObject;
+	unsigned int editable = 0;
+	if (!PyArg_ParseTuple(args, "esesO|p", "utf-8", &caption, "utf-8", &message, &listObject, &editable))
+		return nullptr;
+
+	QStringList items;
+	PyObject* list = PySequence_List(listObject);
+	if (list == nullptr)
+	{
+		PyErr_SetString(PyExc_ValueError, QObject::tr("Expected a list of options", "python error").toLocal8Bit().constData());
+		return nullptr;
+	}
+
+	int n = PyList_Size(list);
+	for (int i = 0; i < n; ++i)
+	{
+		PyObject* itemObject = PyList_GetItem(list, i);
+		char *item = const_cast<char*>("");
+		if (!PyArg_Parse(itemObject, "es", "utf-8", &item))
+		{
+			PyErr_SetString(PyExc_ValueError, QObject::tr("Items must be strings", "python error").toLocal8Bit().constData());
+			return nullptr;
+		}
+		items << QString::fromUtf8(item);
+	}
+
+	QApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
+	bool ok;
+	QString choice = QInputDialog::getItem(ScCore->primaryMainWindow(),
+										QString::fromUtf8(caption),
+										QString::fromUtf8(message),
+										items,
+										0,
+										editable == 1,
+										&ok);
+	return PyUnicode_FromString(choice.toUtf8());
+}
+
 PyObject *scribus_newstyledialog(PyObject*, PyObject* args)
 {
 	if (!checkHaveDocument())
@@ -155,6 +197,7 @@ void cmddialogdocwarnings()
 {
 	QStringList s;
 	s << scribus_filedialog__doc__
+	  << scribus_itemdialog__doc__
 	  << scribus_messagebox__doc__
 	  << scribus_newdocdialog__doc__ 
 	  << scribus_newstyledialog__doc__
diff --git a/scribus/plugins/scriptplugin/cmddialog.h b/scribus/plugins/scriptplugin/cmddialog.h
index 6940b730d5dda85105447c9e28fb274652e2eb93..0b2348b606d3525dd10495c25193361c20e3049e 100644
--- a/scribus/plugins/scriptplugin/cmddialog.h
+++ b/scribus/plugins/scriptplugin/cmddialog.h
@@ -103,6 +103,17 @@ Example: valueDialog('title', 'text in the window', 'optional')\n\
 /* 09/24/2004 petr vanek */
 PyObject *scribus_valuedialog(PyObject * /*self*/, PyObject* args);
 
+/*! docstring */
+PyDoc_STRVAR(scribus_itemdialog__doc__,
+QT_TR_NOOP("itemDialog(caption, message, list_of_strings[, editable]) -> string\n\
+\n\
+Shows the common 'Make a choice' dialog and returns the selected value as a string\n\
+Parameters: window title, text in the window and a list of string values.\n\
+Optionally, you can let the user edit the value by setting `editable` to `True`.\n\
+\n\
+Example: itemDialog('title', 'text in the window', ['apples', 'pears', 'raisins'])\n\
+"));
+PyObject *scribus_itemdialog(PyObject * /*self*/, PyObject* args);
 
 PyDoc_STRVAR(scribus_newstyledialog__doc__,
 QT_TR_NOOP("newStyleDialog() -> string\n\
diff --git a/scribus/plugins/scriptplugin/scriptplugin.cpp b/scribus/plugins/scriptplugin/scriptplugin.cpp
index 2836dfb9672dd54a06ee5ff98f8084e54a9b9f7c..d7ade0199a4602af94a45906d8cf676d33493c0c 100644
--- a/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ b/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -441,6 +441,7 @@ PyMethodDef scribus_methods[] = {
 	{const_cast<char*>("isLocked"), scribus_islocked, METH_VARARGS, tr(scribus_islocked__doc__)},
 	{const_cast<char*>("isPDFBookmark"), scribus_ispdfbookmark, METH_VARARGS, tr(scribus_ispdfbookmark__doc__)},
 	{const_cast<char*>("isSpotColor"), scribus_isspotcolor, METH_VARARGS, tr(scribus_isspotcolor__doc__)},
+	{const_cast<char*>("itemDialog"), scribus_itemdialog, METH_VARARGS, tr(scribus_itemdialog__doc__)},
 	{const_cast<char*>("layoutText"), scribus_layouttext, METH_VARARGS, tr(scribus_layouttext__doc__)},
 	{const_cast<char*>("layoutTextChain"), scribus_layouttextchain, METH_VARARGS, tr(scribus_layouttextchain__doc__)},
 	{const_cast<char*>("linkTextFrames"), scribus_linktextframes, METH_VARARGS, tr(scribus_linktextframes__doc__)},
item-dialog-2.diff (4,329 bytes)   

ale

2023-11-18 07:01

manager   ~0050482

Last edited: 2023-11-18 07:01

btw, this allows to easily write ( (no tk, no pyqt) a script like

def main():
    layers = scribus.getLayers()
    target_layer = scribus.itemDialog('Choose a layer', 'Choose one of the layers or type the name of the layer to be created:', layers, True)
    if target_layer == '':
        return
    if not target_layer in layers:
        scribus.createLayer(target_layer)
        scribus.setActiveLayer(current_layer)

pick-a-layer.png (15,543 bytes)   
pick-a-layer.png (15,543 bytes)   

Issue History

Date Modified Username Field Change
2023-11-17 23:27 ale New Issue
2023-11-17 23:27 ale File Added: item-dialog.diff
2023-11-17 23:57 ale Note Added: 0050481
2023-11-17 23:57 ale File Added: item-dialog-2.diff
2023-11-18 07:01 ale Note Added: 0050482
2023-11-18 07:01 ale File Added: pick-a-layer.png
2023-11-18 07:01 ale Note Edited: 0050482
2023-11-18 07:01 ale Note Edited: 0050482