Added ListConfig type, using it in recent files now

Wed, 21 Aug 2013 01:13:52 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 21 Aug 2013 01:13:52 +0300
changeset 467
59549731151a
parent 466
17f2fd25066c
child 468
7b3159f1a9e2

Added ListConfig type, using it in recent files now

src/config.cpp file | annotate | diff | comparison | revisions
src/config.h file | annotate | diff | comparison | revisions
src/configDialog.cpp file | annotate | diff | comparison | revisions
src/configDialog.h file | annotate | diff | comparison | revisions
src/file.cpp file | annotate | diff | comparison | revisions
src/gui.cpp file | annotate | diff | comparison | revisions
--- a/src/config.cpp	Tue Aug 20 21:23:56 2013 +0300
+++ b/src/config.cpp	Wed Aug 21 01:13:52 2013 +0300
@@ -102,34 +102,9 @@
 
 // =============================================================================
 // -----------------------------------------------------------------------------
-// TODO: make virtual
-str Config::toString() const {
-	switch (getType()) {
-	case Int:
-		return fmt ("%1", static_cast<const IntConfig*> (this)->value);
-		break;
-	
-	case String:
-		return static_cast<const StringConfig*> (this)->value;
-		break;
-	
-	case Float:
-		return fmt ("%1", static_cast<const FloatConfig*> (this)->value);
-		break;
-	
-	case Bool:
-		return (static_cast<const BoolConfig*> (this)->value) ? "true" : "false";
-		break;
-	
-	case KeySequence:
-		return static_cast<const KeySequenceConfig*> (this)->value.toString();
-		break;
-	
-	default:
-		break;
-	}
-	
-	return "";
+void ListConfig::loadFromConfig (const QSettings* cfg) {
+	QVariant val = cfg->value (name, defval);
+	value = val.toList();
 }
 
 // =============================================================================
@@ -143,7 +118,7 @@
 		if (!cfg)
 			break;
 		
-		settings->setValue (cfg->name, cfg->toString());
+		settings->setValue (cfg->name, cfg->toVariant());
 	}
 	
 	settings->sync();
--- a/src/config.h	Tue Aug 20 21:23:56 2013 +0300
+++ b/src/config.h	Wed Aug 21 01:13:52 2013 +0300
@@ -23,6 +23,7 @@
 
 // =============================================================================
 #include <QString>
+#include <QVariant>
 #include <QKeySequence>
 class QSettings;
 
@@ -44,6 +45,7 @@
 		Float,
 		Bool,
 		KeySequence,
+		List,
 	};
 
 	Config (const char* name, const char* defstring);
@@ -53,11 +55,11 @@
 		return (Type) 0;
 	}
 	
-	str toString() const;
 	str defaultString() const;
 	virtual void resetValue() {}
 	virtual void loadFromConfig (const QSettings* cfg) { (void) cfg; }
 	virtual bool isDefault() const { return false; }
+	virtual QVariant toVariant() const { return QVariant(); }
 	
 	// ------------------------------------------
 	static bool load();
@@ -84,7 +86,8 @@
 	Config::Type getType() const override { return Config::NAME; } \
 	virtual void resetValue() { value = defval; } \
 	virtual bool isDefault() const { return value == defval; } \
-	virtual void loadFromConfig (const QSettings* cfg) override;
+	virtual void loadFromConfig (const QSettings* cfg) override; \
+	virtual QVariant toVariant() const { return QVariant::fromValue<T> (value); }
 	
 #define DEFINE_UNARY_OPERATOR(T, OP) \
 	T operator OP() { \
@@ -202,4 +205,30 @@
 	DEFINE_ASSIGN_OPERATOR (QKeySequence, =)
 };
 
+// =============================================================================
+class ListConfig : public Config {
+public:
+	IMPLEMENT_CONFIG (List, QList<QVariant>)
+	DEFINE_ASSIGN_OPERATOR (QList<QVariant>, =)
+	
+	typedef QList<QVariant>::iterator it;
+	typedef QList<QVariant>::const_iterator c_it;
+	
+	it begin() {
+		return value.begin();
+	}
+	
+	c_it begin() const {
+		return value.constBegin();
+	}
+	
+	it end() {
+		return value.end();
+	}
+	
+	c_it end() const {
+		return value.constEnd();
+	}
+};
+
 #endif // CONFIG_H
\ No newline at end of file
--- a/src/configDialog.cpp	Tue Aug 20 21:23:56 2013 +0300
+++ b/src/configDialog.cpp	Wed Aug 21 01:13:52 2013 +0300
@@ -79,7 +79,9 @@
 
 // =============================================================================
 // -----------------------------------------------------------------------------
-ConfigDialog::ConfigDialog (ForgeWindow* parent) : QDialog (parent) {
+ConfigDialog::ConfigDialog (ConfigDialog::Tab deftab, QWidget* parent, Qt::WindowFlags f) :
+	QDialog (parent, f)
+{
 	ui = new Ui_ConfigUI;
 	ui->setupUi (this);
 	
@@ -97,6 +99,8 @@
 	ui->m_profileName->setText (ld_defaultname);
 	ui->m_profileUsername->setText (ld_defaultuser);
 	ui->m_profileLicense->setCurrentIndex (ld_defaultlicense);
+	
+	ui->tabs->setCurrentIndex (deftab);
 }
 
 // =============================================================================
@@ -583,7 +587,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void ConfigDialog::staticDialog() {
-	ConfigDialog dlg (g_win);
+	ConfigDialog dlg (InterfaceTab, g_win);
 	
 	if (dlg.exec()) {
 		const alias ui = *dlg.getUI();
--- a/src/configDialog.h	Tue Aug 20 21:23:56 2013 +0300
+++ b/src/configDialog.h	Wed Aug 21 01:13:52 2013 +0300
@@ -41,7 +41,17 @@
 	Q_OBJECT
 	
 public:
-	ConfigDialog (ForgeWindow* parent);
+	enum Tab {
+		InterfaceTab,
+		ProfileTab,
+		ShortcutsTab,
+		QuickColorsTab,
+		GridsTab,
+		ExtProgsTab,
+		DownloadTab
+	};
+	
+	explicit ConfigDialog (Tab deftab = InterfaceTab, QWidget* parent = null, Qt::WindowFlags f = 0);
 	virtual ~ConfigDialog();
 	static void staticDialog();
 	const Ui_ConfigUI* getUI() const;
--- a/src/file.cpp	Tue Aug 20 21:23:56 2013 +0300
+++ b/src/file.cpp	Wed Aug 21 01:13:52 2013 +0300
@@ -33,7 +33,7 @@
 #include "build/moc_file.cpp"
 
 cfg (String, io_ldpath, "");
-cfg (String, io_recentfiles, "");
+cfg (List, io_recentfiles, {});
 extern_cfg (String, net_downloadpath);
 extern_cfg (Bool, gl_logostuds);
 
@@ -473,7 +473,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void addRecentFile (str path) {
-	QStringList rfiles = io_recentfiles.value.split ('@');
+	alias rfiles = io_recentfiles.value;
 	int idx = rfiles.indexOf (path);
 	
 	// If this file already is in the list, pop it out.
@@ -492,9 +492,6 @@
 	// Add the file
 	rfiles << path;
 	
-	// Rebuild the config string
-	io_recentfiles = rfiles.join ("@");
-	
 	Config::save();
 	g_win->updateRecentFilesMenu();
 }
--- a/src/gui.cpp	Tue Aug 20 21:23:56 2013 +0300
+++ b/src/gui.cpp	Wed Aug 21 01:13:52 2013 +0300
@@ -54,7 +54,7 @@
 cfg (Bool, lv_colorize, true);
 cfg (String, gui_colortoolbar, "16:24:|:1:2:4:14:0:15:|:33:34:36:46");
 cfg (Bool, gui_implicitfiles, false);
-extern_cfg (String, io_recentfiles);
+extern_cfg (List, io_recentfiles);
 extern_cfg (Bool, gl_axes);
 extern_cfg (String, gl_maincolor);
 extern_cfg (Float, gl_maincolor_alpha);
@@ -150,22 +150,21 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void ForgeWindow::updateRecentFilesMenu() {
-	QStringList files = io_recentfiles.value.split ("@", QString::SkipEmptyParts);
-	QStringListIterator it (files);
-	
 	// First, clear any items in the recent files menu
 	for (QAction* recent : m_recentFiles)
 		delete recent;
 	m_recentFiles.clear();
 	
-	it.toBack();
-	while (it.hasPrevious()) {
-		str file = it.previous();
+	QAction* first = null;
+	
+	for (const QVariant& it : io_recentfiles) {
+		str file = it.toString();
 		QAction* recent = new QAction (getIcon ("open-recent"), file, this);
 		
 		connect (recent, SIGNAL (triggered()), this, SLOT (slot_recentFile()));
-		ui->menuOpenRecent->addAction (recent);
+		ui->menuOpenRecent->insertAction (first, recent);
 		m_recentFiles << recent;
+		first = recent;
 	}
 }
 

mercurial