1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 #include "cmddialog.h"
8 #include "cmdutil.h"
9 #include "scribuscore.h"
10 #include "ui/customfdialog.h"
11 #include "ui/scmessagebox.h"
12 #include "ui/stylemanager.h"
13 
14 #include <QMessageBox>
15 #include <QCursor>
16 #include <QInputDialog>
17 
18 
scribus_newdocdialog(PyObject *)19 PyObject *scribus_newdocdialog(PyObject* /* self */)
20 {
21 	QApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
22 	bool ret = ScCore->primaryMainWindow()->slotFileNew();
23 	QApplication::changeOverrideCursor(Qt::ArrowCursor);
24 	return PyLong_FromLong(static_cast<long>(ret));
25 }
26 
scribus_filedialog(PyObject *,PyObject * args,PyObject * kw)27 PyObject *scribus_filedialog(PyObject* /* self */, PyObject* args, PyObject* kw)
28 {
29 	char *caption = const_cast<char*>("");
30 	char *filter = const_cast<char*>("");
31 	char *defName = const_cast<char*>("");
32 	int haspreview = 0;
33 	int issave = 0;
34 	int isdir = 0;
35 	// FIXME: parsing named params failure. e.g. fileDialog(caption="foo", issave=True)
36 	// FIXME: it's a bug in Python. I'm monitoring it
37 	// https://sourceforge.net/tracker/index.php?func=detail&aid=893549&group_id=5470&atid=105470
38 	char* kwargs[] = {const_cast<char*>("caption"), const_cast<char*>("filter"),
39 						const_cast<char*>("defaultname"), const_cast<char*>("haspreview"),
40 						const_cast<char*>("issave"), const_cast<char*>("isdir"),
41 						nullptr};
42 	if (!PyArg_ParseTupleAndKeywords(args, kw, "es|esesiii", kwargs,
43 									 "utf-8", &caption, "utf-8", &filter, "utf-8", &defName,
44 									 &haspreview, &issave, &isdir))
45 	{
46 		return nullptr;
47 	}
48 	QApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
49 	/* nobool = Nothing doing boolean for CFileDialog last attrs.
50 	Due the 'isdir' parameter. CFileDialog needs the last 2 pointers
51 	initialized. */
52 	bool nobool = false;
53 	int optionFlags = 0;
54 	if (haspreview)
55 		optionFlags |= fdShowPreview;
56 	if (issave)
57 		optionFlags |= fdExistingFiles;
58 	if (isdir)
59 		optionFlags |= fdDirectoriesOnly;
60 	QString fName = ScCore->primaryMainWindow()->CFileDialog(".",
61 										 QString::fromUtf8(caption),
62 										 QString::fromUtf8(filter),
63 										 QString::fromUtf8(defName),
64 										 optionFlags,
65 										 &nobool,
66 										 &nobool,
67 										 &nobool
68 										);
69 //	QApplication::restoreOverrideCursor();
70 	// FIXME: filename return unicode OK?
71 	return PyUnicode_FromString(fName.toUtf8());
72 }
73 
scribus_messagebox(PyObject *,PyObject * args,PyObject * kw)74 PyObject *scribus_messagebox(PyObject* /* self */, PyObject* args, PyObject* kw)
75 {
76 	char *caption = const_cast<char*>("");
77 	char *message = const_cast<char*>("");
78 	uint result;
79 	QMessageBox::Icon ico = QMessageBox::NoIcon;
80 	int butt[3] = { QMessageBox::Ok|QMessageBox::Default, QMessageBox::NoButton, QMessageBox::NoButton };
81 	QMessageBox::StandardButtons buttons;
82 	enum QMessageBox::StandardButton defaultButton = QMessageBox::NoButton;
83 	char* kwargs[] = {const_cast<char*>("caption"), const_cast<char*>("message"),
84 						const_cast<char*>("icon"), const_cast<char*>("button1"),
85 						const_cast<char*>("button2"), const_cast<char*>("button3"), nullptr};
86 	if (!PyArg_ParseTupleAndKeywords(args, kw, "eses|iiii", kwargs, "utf-8", &caption, "utf-8", &message, &ico, &butt[0], &butt[1], &butt[2]))
87 		return nullptr;
88 	QApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
89 	// ScMessageBox mb(ico, butt1, butt2, butt3, ScCore->primaryMainWindow());
90 	for (int bi = 0; bi < 3; bi++) {
91 		enum QMessageBox::StandardButton b = static_cast<QMessageBox::StandardButton>(butt[bi]);
92 		if (b != QMessageBox::NoButton) {
93 			if ((b & QMessageBox::Default) != 0) {
94 				b = QMessageBox::StandardButton(b & ~QMessageBox::Default);
95 				defaultButton = b;
96 			}
97 			buttons |= b;
98 		}
99 	}
100 	ScMessageBox mb(ico, QString::fromUtf8(caption), QString::fromUtf8(message), buttons, ScCore->primaryMainWindow());
101 	if (defaultButton != QMessageBox::NoButton) {
102 		mb.setDefaultButton(defaultButton);
103 	}
104 	result = mb.exec();
105 //	QApplication::restoreOverrideCursor();
106 	return PyLong_FromLong(static_cast<long>(result));
107 }
108 
scribus_valuedialog(PyObject *,PyObject * args)109 PyObject *scribus_valuedialog(PyObject* /* self */, PyObject* args)
110 {
111 	char *caption = const_cast<char*>("");
112 	char *message = const_cast<char*>("");
113 	char *value = const_cast<char*>("");
114 	if (!PyArg_ParseTuple(args, "eses|es", "utf-8", &caption, "utf-8", &message, "utf-8", &value))
115 		return nullptr;
116 	QApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
117 	QString txt = QInputDialog::getText(ScCore->primaryMainWindow(),
118 										QString::fromUtf8(caption),
119 										QString::fromUtf8(message),
120 										QLineEdit::Normal,
121 										QString::fromUtf8(value));
122 //	QApplication::restoreOverrideCursor();
123 	return PyUnicode_FromString(txt.toUtf8());
124 }
125 
scribus_newstyledialog(PyObject *,PyObject * args)126 PyObject *scribus_newstyledialog(PyObject*, PyObject* args)
127 {
128 	if (!checkHaveDocument())
129 		return nullptr;
130 
131 	ScribusDoc *d = ScCore->primaryMainWindow()->doc;
132 	bool ok;
133 	QString s = QInputDialog::getText(ScCore->primaryMainWindow(), "New Paragraph Style",
134 			"Enter name of the new paragraph style:", QLineEdit::Normal,
135 			QString(), &ok);
136 
137 	if (ok && !s.isEmpty())
138 	{
139 		StyleSet<ParagraphStyle> st;
140 		st.redefine(d->paragraphStyles(), true);
141 		ParagraphStyle p;
142 		p.setName(s);
143 		st.create(p);
144 		d->redefineStyles(st, false);
145 		ScCore->primaryMainWindow()->styleMgr()->setDoc(d);
146 		return PyUnicode_FromString(s.toUtf8());
147 	}
148 	Py_RETURN_NONE;
149 }
150 
151 /*! HACK: this removes "warning: 'blash' defined but not used" compiler warnings
152 with header files structure untouched (docstrings are kept near declarations)
153 PV */
cmddialogdocwarnings()154 void cmddialogdocwarnings()
155 {
156 	QStringList s;
157 	s << scribus_filedialog__doc__
158 	  << scribus_messagebox__doc__
159 	  << scribus_newdocdialog__doc__
160 	  << scribus_newstyledialog__doc__
161 	  << scribus_valuedialog__doc__ ;
162 }
163