r16728 by jghali - #10141 : Console syntax highlighting options do not work

scribus-commit scribus-commit at lists.scribus.net
Thu Jul 21 22:06:06 UTC 2011


Author: jghali
Date: Thu Jul 21 22:06:06 2011
New Revision: 16728

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=16728
Log:
#10141 : Console syntax highlighting options do not work

Modified:
    trunk/Scribus/scribus/plugins/scriptplugin/pconsole.cpp
    trunk/Scribus/scribus/plugins/scriptplugin/pconsole.h
    trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.cpp
    trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.h
    trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.cpp
    trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.h
    trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.cpp
    trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.h
    trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp

Modified: trunk/Scribus/scribus/plugins/scriptplugin/pconsole.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=16728&path=/trunk/Scribus/scribus/plugins/scriptplugin/pconsole.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/pconsole.cpp (original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/pconsole.cpp Thu Jul 21 22:06:06 2011
@@ -49,7 +49,6 @@
 	commandEdit->setTabStopWidth(qRound(commandEdit->fontPointSize() * 4));
 
 	// install syntax highlighter.
-	//SyntaxHighlighter *sxHigh =
 	new SyntaxHighlighter(commandEdit);
 
 	languageChange();
@@ -83,6 +82,11 @@
 {
 }
 
+void PythonConsole::updateSyntaxHighlighter()
+{
+	new SyntaxHighlighter(commandEdit);
+}
+
 void PythonConsole::setFonts()
 {
 	QFont font = QFont("Fixed");
@@ -313,6 +317,9 @@
 
 void SyntaxHighlighter::highlightBlock(const QString &text)
 {
+	// Apply default text color
+	setFormat(0, text.length(), colors.textColor);
+
 	foreach (HighlightingRule rule, highlightingRules)
 	{
 		QRegExp expression(rule.pattern);
@@ -375,8 +382,19 @@
 	}
 }
 
-SyntaxColors::~SyntaxColors()
-{
+void SyntaxColors::saveToPrefs()
+{
+	PrefsContext* prefs = PrefsManager::instance()->prefsFile->getPluginContext("scriptplugin");
+	if (prefs)
+	{
+		prefs->set("syntaxerror", qcolor2named(errorColor));
+		prefs->set("syntaxcomment", qcolor2named(commentColor));
+		prefs->set("syntaxkeyword", qcolor2named(keywordColor));
+		prefs->set("syntaxsign", qcolor2named(signColor));
+		prefs->set("syntaxnumber", qcolor2named(numberColor));
+		prefs->set("syntaxstring", qcolor2named(stringColor));
+		prefs->set("syntaxtext", qcolor2named(textColor));
+	}
 }
 
 QString SyntaxColors::qcolor2named(QColor color)

Modified: trunk/Scribus/scribus/plugins/scriptplugin/pconsole.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=16728&path=/trunk/Scribus/scribus/plugins/scriptplugin/pconsole.h
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/pconsole.h (original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/pconsole.h Thu Jul 21 22:06:06 2011
@@ -43,6 +43,8 @@
 		//! \brief Close event for turning the action off
 		void closeEvent(QCloseEvent *);
 
+		void updateSyntaxHighlighter();
+
 	public slots:
 		//! menu operations
 		virtual void slot_runScript();
@@ -86,7 +88,7 @@
 {
 	public:
 		SyntaxColors();
-		~SyntaxColors();
+
 		QColor errorColor;
 		QColor commentColor;
 		QColor keywordColor;
@@ -94,6 +96,9 @@
 		QColor numberColor;
 		QColor stringColor;
 		QColor textColor;
+
+		void saveToPrefs();
+
 	private:
 		/*! \brief Converts QColor into #rrggbb string.
 		\param color a QColor to convert. */
@@ -113,7 +118,7 @@
 		SyntaxHighlighter(QTextEdit *textEdit);
 
 	protected:
-		void highlightBlock(const QString &text);
+		virtual void highlightBlock(const QString &text);
 
 		struct HighlightingRule
 		{

Modified: trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=16728&path=/trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.cpp (original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.cpp Thu Jul 21 22:06:06 2011
@@ -78,13 +78,25 @@
 		prefs->set("syntaxnumber", numberColor.name());
 		prefs->set("syntaxstring", stringColor.name());
 		prefs->set("syntaxtext", textColor.name());
+
+		emit prefsChanged();
 	}
 }
 
 void Prefs_Scripter::setColor()
 {
-	QPushButton* button = (QPushButton*)sender();
-	QColor color = QColorDialog::getColor(button->palette().color(QPalette::Window), this);
+	QPushButton* button = (QPushButton*) sender();
+
+	QColor oldColor;
+	if (button == textButton)    oldColor = textColor;
+	if (button == commentButton) oldColor = commentColor;
+ 	if (button == keywordButton) oldColor = keywordColor;
+ 	if (button == errorButton)   oldColor = errorColor;
+ 	if (button == signButton)    oldColor = signColor;
+ 	if (button == stringButton)  oldColor = stringColor;
+ 	if (button == numberButton)  oldColor = numberColor;
+
+	QColor color = QColorDialog::getColor(oldColor, this);
 	if (color.isValid() && button)
 	{
 		QPixmap pm(54, 14);

Modified: trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=16728&path=/trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.h
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.h (original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/prefs_scripter.h Thu Jul 21 22:06:06 2011
@@ -44,6 +44,9 @@
 		\warning I'm trying to handle multiple signals via single slot here. sender() Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise the return value is undefined. This function will return something apparently correct in other cases as well. However, its value may change during any function call, depending on what signal-slot connections are activated during that call. In Qt 3.0 the value will change more often than in 2.x. This function violates the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot. The sender is undefined if the slot is called as a normal C++ function. */
 		void setColor();
 		void changeStartupScript();
+
+	signals:
+		void prefsChanged();
 };
 
 #endif // PREFS_SCRIPTER_H

Modified: trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=16728&path=/trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.cpp (original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.cpp Thu Jul 21 22:06:06 2011
@@ -576,6 +576,11 @@
 	m_enableExtPython = enable;
 }
 
+void ScripterCore::updateSyntaxHighlighter()
+{
+	pcon->updateSyntaxHighlighter();
+}
+
 const QString & ScripterCore::startupScript() const
 {
 	return m_startupScript;

Modified: trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=16728&path=/trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.h
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.h (original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/scriptercore.h Thu Jul 21 22:06:06 2011
@@ -56,6 +56,7 @@
 	bool extensionsEnabled() const;
 	void setStartupScript(const QString& newScript);
 	void setExtensionsEnabled(bool enable);
+	void updateSyntaxHighlighter();
 
 protected:
 	// Private helper functions

Modified: trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=16728&path=/trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.cpp (original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.cpp Thu Jul 21 22:06:06 2011
@@ -20,6 +20,8 @@
 	: PrefsPanel(parent)
 {
 	setupUi(this);
+
+	syntaxColors = new SyntaxColors();
 	
 	languageChange();
 	setupSyntaxColors();
@@ -34,13 +36,13 @@
 	// signals and slots connections
 	connect(extensionScriptsChk, SIGNAL(toggled(bool)), startupScriptEdit, SLOT(setEnabled(bool)));
 	// colors
-	connect(textButton, SIGNAL(clicked()), this, SLOT(setColor()));
+	connect(textButton   , SIGNAL(clicked()), this, SLOT(setColor()));
 	connect(commentButton, SIGNAL(clicked()), this, SLOT(setColor()));
 	connect(keywordButton, SIGNAL(clicked()), this, SLOT(setColor()));
-	connect(errorButton, SIGNAL(clicked()), this, SLOT(setColor()));
-	connect(signButton, SIGNAL(clicked()), this, SLOT(setColor()));
-	connect(stringButton, SIGNAL(clicked()), this, SLOT(setColor()));
-	connect(numberButton, SIGNAL(clicked()), this, SLOT(setColor()));
+	connect(errorButton  , SIGNAL(clicked()), this, SLOT(setColor()));
+	connect(signButton   , SIGNAL(clicked()), this, SLOT(setColor()));
+	connect(stringButton , SIGNAL(clicked()), this, SLOT(setColor()));
+	connect(numberButton , SIGNAL(clicked()), this, SLOT(setColor()));
 	connect(startupScriptChangeButton, SIGNAL(clicked()), this, SLOT(changeStartupScript()));
 }
 
@@ -49,6 +51,7 @@
  */
 ScripterPrefsGui::~ScripterPrefsGui()
 {
+	delete syntaxColors;
 }
 
 /*
@@ -74,49 +77,63 @@
 {
 	scripterCore->setExtensionsEnabled(extensionScriptsChk->isChecked());
 	scripterCore->setStartupScript(startupScriptEdit->text());
-	// colors
-	SyntaxColors *syntax = new SyntaxColors();
-	syntax->textColor = textButton->palette().color(QPalette::Window);
-	syntax->commentColor = commentButton->palette().color(QPalette::Window);
-	syntax->keywordColor = keywordButton->palette().color(QPalette::Window);
-	syntax->errorColor = errorButton->palette().color(QPalette::Window);
-	syntax->signColor = signButton->palette().color(QPalette::Window);
-	syntax->stringColor = stringButton->palette().color(QPalette::Window);
-	syntax->numberColor = numberButton->palette().color(QPalette::Window);
-	delete(syntax);
+	syntaxColors->saveToPrefs();
+
+	// Necessary to update console syntax highlighter
+	emit prefsChanged();
 }
 
 void ScripterPrefsGui::setColor()
 {
-	QPalette palette;
-	QPushButton* button = (QPushButton*)sender();
-	QColor color = QColorDialog::getColor(button->palette().color(QPalette::Window), this);
+	QPushButton* button = (QPushButton*) sender();
+
+	QColor oldColor;
+	if (button == textButton)    oldColor = syntaxColors->textColor;
+	if (button == commentButton) oldColor = syntaxColors->commentColor;
+ 	if (button == keywordButton) oldColor = syntaxColors->keywordColor;
+ 	if (button == errorButton)   oldColor = syntaxColors->errorColor;
+ 	if (button == signButton)    oldColor = syntaxColors->signColor;
+ 	if (button == stringButton)  oldColor = syntaxColors->stringColor;
+ 	if (button == numberButton)  oldColor = syntaxColors->numberColor;
+
+	QColor color = QColorDialog::getColor(color, this);
 	if (color.isValid())
 	{
-		palette.setColor(button->backgroundRole(), color);
- 		button->setPalette(palette);
+		setButtonIcon(button, color);
+
+		if (button == textButton)    syntaxColors->textColor = color;
+		if (button == commentButton) syntaxColors->commentColor = color;
+ 		if (button == keywordButton) syntaxColors->keywordColor = color;
+ 		if (button == errorButton)   syntaxColors->errorColor = color;
+ 		if (button == signButton)    syntaxColors->signColor = color;
+ 		if (button == stringButton)  syntaxColors->stringColor = color;
+ 		if (button == numberButton)  syntaxColors->numberColor = color;
 	}
+}
+
+void ScripterPrefsGui::setButtonIcon(QPushButton* button, QColor color)
+{
+	QSize  iconSize   = button->iconSize();
+	double iconWidth  = qMax(iconSize.width() , button->width() / 3);
+	double iconHeight = qMin(iconSize.height(), button->height() / 3);
+	QSize  newIconSize(iconWidth, iconHeight);
+	if (iconSize != newIconSize)
+		button->setIconSize(newIconSize);
+	QPixmap icon(button->iconSize());
+	icon.fill(color);
+	button->setIcon(icon);
 }
 
 void ScripterPrefsGui::setupSyntaxColors()
 {
-	QPalette palette;
-	SyntaxColors *syntax = new SyntaxColors();
-	palette.setColor(textButton->backgroundRole(), syntax->textColor);
- 	textButton->setPalette(palette);
-	palette.setColor(commentButton->backgroundRole(), syntax->commentColor);
- 	commentButton->setPalette(palette);
-	palette.setColor(keywordButton->backgroundRole(), syntax->keywordColor);
- 	keywordButton->setPalette(palette);
-	palette.setColor(errorButton->backgroundRole(), syntax->errorColor);
- 	errorButton->setPalette(palette);
-	palette.setColor(signButton->backgroundRole(), syntax->signColor);
- 	signButton->setPalette(palette);
-	palette.setColor(stringButton->backgroundRole(), syntax->stringColor);
- 	stringButton->setPalette(palette);
-	palette.setColor(numberButton->backgroundRole(), syntax->numberColor);
- 	numberButton->setPalette(palette);
-	delete(syntax);
+	SyntaxColors syntax;
+	setButtonIcon(textButton   , syntax.textColor);
+	setButtonIcon(commentButton, syntax.commentColor);
+ 	setButtonIcon(keywordButton, syntax.keywordColor);
+ 	setButtonIcon(errorButton  , syntax.errorColor);
+ 	setButtonIcon(signButton   , syntax.signColor);
+ 	setButtonIcon(stringButton ,syntax.stringColor);
+ 	setButtonIcon(numberButton , syntax.numberColor);
 }
 
 void ScripterPrefsGui::changeStartupScript()

Modified: trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=16728&path=/trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.h
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.h (original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/scripterprefsgui.h Thu Jul 21 22:06:06 2011
@@ -10,6 +10,7 @@
 #include "ui_scripterprefsgui.h"
 #include "ui/prefspanel.h"
 
+class SyntaxColors;
 
 /*! \brief Subclass of PrefsPanel that's supplied to the prefs
 dialog for use when showing plugin preferences. */
@@ -26,6 +27,9 @@
 
 	protected:
 		void setupSyntaxColors();
+		void setButtonIcon(QPushButton* button, QColor color);
+
+		SyntaxColors* syntaxColors;
 
 	protected slots:
 		void languageChange();
@@ -33,7 +37,11 @@
 		\author Petr Vanek
 		\warning I'm trying to handle multiple signals via single slot here. sender() Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise the return value is undefined. This function will return something apparently correct in other cases as well. However, its value may change during any function call, depending on what signal-slot connections are activated during that call. In Qt 3.0 the value will change more often than in 2.x. This function violates the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot. The sender is undefined if the slot is called as a normal C++ function. */
 		void setColor();
+
 		void changeStartupScript();
+
+	signals:
+		void prefsChanged();
 };
 
 #endif

Modified: trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=16728&path=/trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp (original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp Thu Jul 21 22:06:06 2011
@@ -202,6 +202,7 @@
 {
 	panel = new ScripterPrefsGui(parent);
 	Q_CHECK_PTR(panel);
+	connect(panel, SIGNAL(prefsChanged()), scripterCore, SLOT(updateSyntaxHighlighter()));
 	caption = tr("Scripter");
 	icon = loadIcon("python.png");
 	return true;
@@ -212,6 +213,7 @@
 {
 	panel = new Prefs_Scripter(parent);
 	Q_CHECK_PTR(panel);
+	connect(panel, SIGNAL(prefsChanged()), scripterCore, SLOT(updateSyntaxHighlighter()));
 	caption = tr("Scripter");
 	icon = loadIcon("python_16.png");
 	return true;




More information about the scribus-commit mailing list