r14626 by cbradney - Enable shortwords plugin prefs to work in new prefs area

scribus-commit scribus-commit at lists.scribus.net
Tue Feb 2 22:30:24 CET 2010


Revision: 14626
Author: cbradney
Date: 2010-02-02T21:22:32.484969Z
Commit message: Enable shortwords plugin prefs to work in new prefs area

Changeset: 
M  /trunk/Scribus/scribus/plugins/short-words/prefs_shortwords.cpp
M  /trunk/Scribus/scribus/plugins/short-words/prefs_shortwords.h
M  /trunk/Scribus/scribus/plugins/short-words/prefs_shortwordsbase.ui

Diffs:
Index: scribus/plugins/short-words/prefs_shortwordsbase.ui
===================================================================
--- scribus/plugins/short-words/prefs_shortwordsbase.ui	(revision 14625)
+++ scribus/plugins/short-words/prefs_shortwordsbase.ui	(revision 14626)
@@ -36,11 +36,18 @@
     </widget>
    </item>
    <item>
-    <widget class="QTextEdit" name="textEdit"/>
+    <widget class="QTextEdit" name="cfgEdit"/>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
+      <widget class="QLabel" name="messageLabel">
+       <property name="text">
+        <string/>
+       </property>
+      </widget>
+     </item>
+     <item>
       <spacer name="horizontalSpacer">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
Index: scribus/plugins/short-words/prefs_shortwords.cpp
===================================================================
--- scribus/plugins/short-words/prefs_shortwords.cpp	(revision 14625)
+++ scribus/plugins/short-words/prefs_shortwords.cpp	(revision 14626)
@@ -7,12 +7,37 @@
 
 #include "prefs_shortwords.h"
 #include "prefsstructs.h"
+#include "ui/scmessagebox.h"
+#include "swsyntaxhighlighter.h"
+#include "version.h"
+#include "scpaths.h"
+#include "commonstrings.h"
 
 Prefs_ShortWords::Prefs_ShortWords(QWidget* parent)
 	: Prefs_Pane(parent)
 {
 	setupUi(this);
 	languageChange();
+
+	// defaults
+	if (QFile::exists(RC_PATH_USR))
+	{
+		messageLabel->setText( tr("User settings"));
+		loadCfgFile(RC_PATH_USR);
+	}
+	else
+	{
+		messageLabel->setText( tr("System wide configuration"));
+		resetButton->setEnabled(false);
+		loadCfgFile(RC_PATH);
+	}
+	saveButton->setEnabled(false);
+	new SWSyntaxHighlighter(cfgEdit);
+
+	// signals
+	connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButton_pressed()));
+	connect(resetButton, SIGNAL(clicked()), this, SLOT(resetButton_pressed()));
+	connect(cfgEdit, SIGNAL(textChanged()), this, SLOT(cfgEdit_changed()));
 }
 
 Prefs_ShortWords::~Prefs_ShortWords()
@@ -30,3 +55,74 @@
 void Prefs_ShortWords::saveGuiToPrefs(struct ApplicationPrefs *prefsData) const
 {
 }
+
+void Prefs_ShortWords::apply()
+{
+	if (saveButton->isEnabled())
+		saveButton_pressed();
+}
+
+void Prefs_ShortWords::saveButton_pressed()
+{
+	if (cfgEdit->document()->isModified() && QFile::exists(RC_PATH_USR))
+	{
+		if ((ScMessageBox::warning(this, tr("Short Words"),
+				"<qt>" + tr("User configuration exists already. "
+						"Do you really want to overwrite it?") + "</qt>",
+				CommonStrings::trYes,
+				CommonStrings::trNo, 0, 0, 1)
+			) == 1)
+			return;
+	}
+
+	QFile f(RC_PATH_USR);
+	if (!f.open(QIODevice::WriteOnly))
+	{
+		QMessageBox::warning(this, tr("Short Words"),
+			 "<qt>" + tr("Cannot write file %1.").arg(RC_PATH_USR) + "</qt>",
+			 CommonStrings::tr_OK);
+	}
+	QTextStream stream(&f);
+	stream.setCodec("UTF-8");
+	stream << cfgEdit->toPlainText();
+	f.close();
+	messageLabel->setText( tr("User settings saved"));
+	saveButton->setEnabled(false);
+}
+
+void Prefs_ShortWords::resetButton_pressed()
+{
+	loadCfgFile(RC_PATH);
+	QDir d;
+	d.remove(RC_PATH_USR);
+	saveButton->setEnabled(false);
+	resetButton->setEnabled(false);
+	messageLabel->setText( tr("System wide configuration reloaded"));
+}
+
+void Prefs_ShortWords::cfgEdit_changed()
+{
+	resetButton->setEnabled(true);
+	saveButton->setEnabled(true);
+}
+
+bool Prefs_ShortWords::loadCfgFile(QString filename)
+{
+	QFile f(filename);
+	if (!f.open(QIODevice::ReadOnly))
+	{
+		messageLabel->setText( tr("Cannot open file %1").arg(f.fileName()));
+		return false;
+	}
+	cfgEdit->clear();
+	QTextStream stream(&f);
+	stream.setCodec("UTF-8");
+	while (!stream.atEnd())
+		cfgEdit->append(stream.readLine());
+	f.close();
+	// it's a must to prevent "overwrite?" message box on saving prefs
+	cfgEdit->document()->setModified(false);
+	return true;
+}
+
+
Index: scribus/plugins/short-words/prefs_shortwords.h
===================================================================
--- scribus/plugins/short-words/prefs_shortwords.h	(revision 14625)
+++ scribus/plugins/short-words/prefs_shortwords.h	(revision 14626)
@@ -24,6 +24,21 @@
 
 	public slots:
 		void languageChange();
+		//! \brief Apply changes to prefs. Auto connected.
+		void apply();
+
+	protected slots:
+		/*! \brief Save the content into user file. */
+		virtual void saveButton_pressed();
+		/*! \brief Re-reads system wide config file. */
+		virtual void resetButton_pressed();
+		/*! \brief Text editor changed. */
+		virtual void cfgEdit_changed();
+
+	protected:
+		/*! \brief Load cfg file.
+		\param filename string with full path and name.*/
+		bool loadCfgFile(QString filename);
 };
 
 #endif // PREFS_SHORTWORDS_H




More information about the scribus-commit mailing list