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 
8 // include the python header first because on OSX that uses the name "slots"
9 #include "cmdvar.h"
10 
11 #include <QColorDialog>
12 #include <QFileDialog>
13 #include <QPalette>
14 
15 #include "prefs_scripter.h"
16 #include "scriptercore.h"
17 #include "prefsfile.h"
18 #include "prefscontext.h"
19 #include "prefsmanager.h"
20 #include "prefsstructs.h"
21 
Prefs_Scripter(QWidget * parent)22 Prefs_Scripter::Prefs_Scripter(QWidget* parent)
23 	: Prefs_Pane(parent)
24 {
25 	setupUi(this);
26 	languageChange();
27 
28 	m_caption = tr("Scripter");
29 	m_icon = "python_16.png";
30 
31 	setupSyntaxColors();
32 
33 	// Set the state of the ext script enable checkbox
34 	extensionScriptsChk->setChecked(scripterCore->extensionsEnabled());
35 	// The startup script box should be disabled  if ext scripts are off
36 	startupScriptEdit->setEnabled(extensionScriptsChk->isChecked());
37 	startupScriptEdit->setText(scripterCore->startupScript());
38 	// signals and slots connections
39 	connect(extensionScriptsChk, SIGNAL(toggled(bool)), startupScriptEdit, SLOT(setEnabled(bool)));
40 	// colors
41 	connect(textButton, SIGNAL(clicked()), this, SLOT(setColor()));
42 	connect(commentButton, SIGNAL(clicked()), this, SLOT(setColor()));
43 	connect(keywordButton, SIGNAL(clicked()), this, SLOT(setColor()));
44 	connect(errorButton, SIGNAL(clicked()), this, SLOT(setColor()));
45 	connect(signButton, SIGNAL(clicked()), this, SLOT(setColor()));
46 	connect(stringButton, SIGNAL(clicked()), this, SLOT(setColor()));
47 	connect(numberButton, SIGNAL(clicked()), this, SLOT(setColor()));
48 	connect(extensionScriptsChk, SIGNAL(toggled(bool)), startupScriptChangeButton, SLOT(setEnabled(bool)));
49 	connect(startupScriptChangeButton, SIGNAL(clicked()), this, SLOT(changeStartupScript()));
50 }
51 
52 Prefs_Scripter::~Prefs_Scripter() = default;
53 
languageChange()54 void Prefs_Scripter::languageChange()
55 {
56 }
57 
restoreDefaults(struct ApplicationPrefs * prefsData)58 void Prefs_Scripter::restoreDefaults(struct ApplicationPrefs *prefsData)
59 {
60 }
61 
saveGuiToPrefs(struct ApplicationPrefs * prefsData) const62 void Prefs_Scripter::saveGuiToPrefs(struct ApplicationPrefs *prefsData) const
63 {
64 }
65 
66 // Apply changes to prefs. Auto connected.
apply()67 void Prefs_Scripter::apply()
68 {
69 	scripterCore->setExtensionsEnabled(extensionScriptsChk->isChecked());
70 	scripterCore->setStartupScript(startupScriptEdit->text());
71 	// colors
72 	PrefsContext* prefs = PrefsManager::instance().prefsFile->getPluginContext("scriptplugin");
73 	if (prefs)
74 	{
75 		prefs->set("syntaxerror", errorColor.name());
76 		prefs->set("syntaxcomment", commentColor.name());
77 		prefs->set("syntaxkeyword", keywordColor.name());
78 		prefs->set("syntaxsign", signColor.name());
79 		prefs->set("syntaxnumber", numberColor.name());
80 		prefs->set("syntaxstring", stringColor.name());
81 		prefs->set("syntaxtext", textColor.name());
82 
83 		emit prefsChanged();
84 	}
85 }
86 
setColor()87 void Prefs_Scripter::setColor()
88 {
89 	QPushButton* button = (QPushButton*) sender();
90 
91 	QColor oldColor;
92 	if (button == textButton)    oldColor = textColor;
93 	if (button == commentButton) oldColor = commentColor;
94  	if (button == keywordButton) oldColor = keywordColor;
95  	if (button == errorButton)   oldColor = errorColor;
96  	if (button == signButton)    oldColor = signColor;
97  	if (button == stringButton)  oldColor = stringColor;
98  	if (button == numberButton)  oldColor = numberColor;
99 
100 	QColor color = QColorDialog::getColor(oldColor, this);
101 	if (color.isValid() && button)
102 	{
103 		QPixmap pm(54, 14);
104 		pm.fill(color);
105 		button->setIcon(pm);
106 		if (button==textButton)
107 			textColor=color;
108 		if (button==commentButton)
109 			commentColor=color;
110 		if (button==keywordButton)
111 			keywordColor=color;
112 		if (button==errorButton)
113 			errorColor=color;
114 		if (button==signButton)
115 			signColor=color;
116 		if (button==stringButton)
117 			stringColor=color;
118 		if (button==numberButton)
119 			numberColor=color;
120 	}
121 }
122 
setupSyntaxColors()123 void Prefs_Scripter::setupSyntaxColors()
124 {
125 	SyntaxColors *syntax = new SyntaxColors();
126 	textColor=syntax->textColor;
127 	commentColor=syntax->commentColor;
128 	keywordColor=syntax->keywordColor;
129 	errorColor=syntax->errorColor;
130 	signColor=syntax->signColor;
131 	stringColor=syntax->stringColor;
132 	numberColor=syntax->numberColor;
133 
134 	QPixmap pm(54, 14);
135 	pm.fill(textColor);
136 	textButton->setIcon(pm);
137 	pm.fill(commentColor);
138 	commentButton->setIcon(pm);
139 	pm.fill(keywordColor);
140 	keywordButton->setIcon(pm);
141 	pm.fill(errorColor);
142 	errorButton->setIcon(pm);
143 	pm.fill(signColor);
144 	signButton->setIcon(pm);
145 	pm.fill(stringColor);
146 	stringButton->setIcon(pm);
147 	pm.fill(numberColor);
148 	numberButton->setIcon(pm);
149 
150 	delete(syntax);
151 }
152 
changeStartupScript()153 void Prefs_Scripter::changeStartupScript()
154 {
155 	QString currentScript=startupScriptEdit->text();
156 	QFileInfo fi(startupScriptEdit->text());
157 	if (!fi.exists())
158 		currentScript = QDir::homePath();
159 
160 	QString s = QFileDialog::getOpenFileName(this, tr("Locate Startup Script"), currentScript, "Python Scripts (*.py *.PY)");
161 	if (!s.isEmpty())
162 		startupScriptEdit->setText(s);
163 }
164 
165