[Tux4kids-commits] r1413 - in tux4kids-admin/trunk: libtux4kidsadmin_tuxmath tux4kids-admin/plugins/tuxmathPlugin

Michał Świtakowski swistakers-guest at alioth.debian.org
Tue Aug 11 18:16:32 UTC 2009


Author: swistakers-guest
Date: 2009-08-11 18:16:32 +0000 (Tue, 11 Aug 2009)
New Revision: 1413

Modified:
   tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions.cpp
   tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions.h
   tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions_p.h
   tux4kids-admin/trunk/tux4kids-admin/plugins/tuxmathPlugin/tuxmathPlugin.cpp
Log:
working TuxmathOptions

Modified: tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions.cpp	2009-08-11 15:48:31 UTC (rev 1412)
+++ tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions.cpp	2009-08-11 18:16:32 UTC (rev 1413)
@@ -1,20 +1,97 @@
 #include "tuxmathOptions.h"
 #include "tuxmathOptions_p.h"
 
+#include <QSettings>
+#include <QStringList>
+#include <QFile>
+#include <QVariant>
+#include <QDebug>
+
+/****************************** TuxmathOptionsPrivate *******************/
+
 TuxmathOptionsPrivate::TuxmathOptionsPrivate()
 {
+}
 
+TuxmathOptionsPrivate::~TuxmathOptionsPrivate()
+{
+}
 
+void TuxmathOptionsPrivate::loadFile()
+{
+	lines.clear();
+	linePos.clear();
+
+	QFile optionsFile(path);
+	if (!optionsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
+		return;
+	}
+	QTextStream stream(&optionsFile);
+
+	while (!stream.atEnd()) {
+		QString line = stream.readLine();
+		if (!line.startsWith("#") && !line.isEmpty()) {
+			line = line.simplified();
+			QStringList fields = line.split("=", QString::SkipEmptyParts);
+			fields[0] = fields[0].simplified();
+			linePos[fields[0]] = lines.size();
+			//qDebug() << "found" << fields[0] << fields[1];
+		}
+		lines.append(line);
+	}
 }
 
-TuxmathOptionsPrivate::~TuxmathOptionsPrivate()
+void TuxmathOptionsPrivate::writeFile()
 {
+	QFile optionsFile(path);
+	if (!optionsFile.open(QIODevice::WriteOnly | QFile::Truncate | QIODevice::Text)) {
+		return;
+	}
+	optionsFile.resize(0);
+	QTextStream stream(&optionsFile);
 
+	for (int i = 0; i < lines.size(); ++i) {
+		stream << lines.at(i) << "\n";
+	}
 }
 
-/****************************** TuxmathOptionsPrivate *******************/
+void TuxmathOptionsPrivate::setValue(QString key, QVariant value)
+{
+	if (!linePos.contains(key)) {
+		return;
+	}
 
+	int index = linePos[key];
+	QStringList fields = lines[index].split("=", QString::SkipEmptyParts);
 
+	switch (value.type()) {
+	case QVariant::Bool:
+		if (value.toBool()) {
+			fields[1] = "1";
+		} else {
+			fields[1] = "0";
+		}
+		break;
+	case QVariant::Int:
+		fields[1] = QString::number(value.toInt());
+		break;
+	default: ;
+	};
+	lines[index] = fields.join(" = ");
+}
+
+QString TuxmathOptionsPrivate::value(QString key) const
+{
+	if (!linePos.contains(key)) {
+		return QString();
+	}
+
+	int index = linePos[key];
+	QStringList fields = lines[index].split("=", QString::SkipEmptyParts);
+
+	return fields[1].trimmed();
+}
+
 /*************************** TuxmathOptions *******************/
 
 TuxmathOptions::TuxmathOptions(QString path)
@@ -22,6 +99,14 @@
 {
 	Q_D(TuxmathOptions);
 	d->q_ptr = this;
+
+	d->path = path;
+	d->loadFile();
+/*	d->writeFile();
+
+	qDebug() << additionAllowed() << substractionAllowed();
+	setAdditionAllowed(false);
+	qDebug() << additionAllowed() << substractionAllowed();*/
 }
 
 TuxmathOptions::TuxmathOptions(TuxmathOptionsPrivate &dd)
@@ -36,3 +121,38 @@
 	delete d;
 }
 
+bool TuxmathOptions::additionAllowed() const
+{
+	Q_D(const TuxmathOptions);
+	if (d->value("ADDITION_ALLOWED") == "1") {
+		return true;
+	} else {
+		return false;
+	}
+}
+
+void TuxmathOptions::setAdditionAllowed(bool allow)
+{
+	Q_D(TuxmathOptions);
+	d->setValue("ADDITION_ALLOWED", allow);
+	d->writeFile();
+}
+
+bool TuxmathOptions::substractionAllowed() const
+{
+	Q_D(const TuxmathOptions);
+	if (d->value("SUBTRACTION_ALLOWED") == "1") {
+		return true;
+	} else {
+		return false;
+	}
+}
+
+void TuxmathOptions::setSubstractionAllowed(bool allow)
+{
+	Q_D(TuxmathOptions);
+	d->setValue("SUBTRACTION_ALLOWED", allow);
+	d->writeFile();
+}
+
+

Modified: tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions.h	2009-08-11 15:48:31 UTC (rev 1412)
+++ tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions.h	2009-08-11 18:16:32 UTC (rev 1413)
@@ -2,22 +2,29 @@
 #define TUXMATHOPTIONS_H
 
 #include <QObject>
+#include "libtux4kidsadmin_tuxmath_global.h"
 
 class TuxmathOptionsPrivate;
 
-class TuxmathOptions : public QObject
+class LIBTUX4KIDSADMIN_TUXMATH_SHARED_EXPORT TuxmathOptions : public QObject
 {
 	Q_OBJECT
 public:
 	TuxmathOptions(QString path);
 	~TuxmathOptions();
 
+	bool additionAllowed() const;
+	void setAdditionAllowed(bool allow);
+	bool substractionAllowed() const;
+	void setSubstractionAllowed(bool allow);
+
 protected:
 	TuxmathOptionsPrivate *d_ptr;
 	TuxmathOptions(TuxmathOptionsPrivate &dd);
 
 private:
 	Q_DECLARE_PRIVATE(TuxmathOptions)
+	Q_DISABLE_COPY(TuxmathOptions)
 
 };
 

Modified: tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions_p.h	2009-08-11 15:48:31 UTC (rev 1412)
+++ tux4kids-admin/trunk/libtux4kidsadmin_tuxmath/tuxmathOptions_p.h	2009-08-11 18:16:32 UTC (rev 1413)
@@ -3,7 +3,10 @@
 
 #include <QtGlobal>
 #include <QString>
+#include <QHash>
 
+class QSettings;
+
 class  TuxmathOptionsPrivate
 {
 public:
@@ -13,6 +16,18 @@
 	virtual ~TuxmathOptionsPrivate();
 
 	TuxmathOptions *q_ptr;
+
+	//QSettings *optionsFile;
+	QString path;
+	QList<QString> lines;
+	QHash<QString, int> linePos;
+
+	void loadFile();
+	void writeFile();
+	void setValue(QString key, QVariant value);
+	QString value(QString key) const;
+
+
 };
 
 #endif // TUXMATHOPTIONS_P_H

Modified: tux4kids-admin/trunk/tux4kids-admin/plugins/tuxmathPlugin/tuxmathPlugin.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/plugins/tuxmathPlugin/tuxmathPlugin.cpp	2009-08-11 15:48:31 UTC (rev 1412)
+++ tux4kids-admin/trunk/tux4kids-admin/plugins/tuxmathPlugin/tuxmathPlugin.cpp	2009-08-11 18:16:32 UTC (rev 1413)
@@ -1,10 +1,14 @@
 #include <QDebug>
 
 #include "tuxmathPlugin.h"
+#include "tuxmathOptions.h"
 
+
 TuxmathPlugin::TuxmathPlugin(QObject *parent) : QObject(parent)
 {
 	qDebug() << "tux math plugin constructed";
+	TuxmathOptions *tmp = new TuxmathOptions("/home/swistak/options");
+
 }
 
 TuxmathPlugin::~TuxmathPlugin()




More information about the Tux4kids-commits mailing list