Some cleanup in LDPaths

Thu, 09 Mar 2017 00:54:45 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 09 Mar 2017 00:54:45 +0200
changeset 1208
8d4b8a9df724
parent 1207
b5df72b194f4
child 1209
c2723022b173

Some cleanup in LDPaths

src/ldpaths.cpp file | annotate | diff | comparison | revisions
src/ldpaths.h file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
--- a/src/ldpaths.cpp	Thu Mar 09 00:40:07 2017 +0200
+++ b/src/ldpaths.cpp	Thu Mar 09 00:54:45 2017 +0200
@@ -21,25 +21,33 @@
 #include "mainwindow.h"
 #include "dialogs/ldrawpathdialog.h"
 
-LDPaths::LDPaths (Configuration *config, QObject* parent) :
-	QObject(parent),
-	m_config(config),
-	m_dialog(nullptr) {}
+LDPaths::LDPaths(QObject* parent) :
+    QObject {parent},
+    m_dialog {nullptr} {}
 
+LDPaths::~LDPaths()
+{
+	delete m_dialog;
+}
 
-void LDPaths::checkPaths()
+bool LDPaths::checkPaths()
 {
-	QString pathconfig = m_config->lDrawPath();
+	QString pathconfig = configuration().lDrawPath();
 
-	if (not configurePaths (pathconfig))
+	if (configurePaths (pathconfig))
+	{
+		return true;
+	}
+	else
 	{
 		m_dialog = new LDrawPathDialog (pathconfig, false);
 		connect(m_dialog, &LDrawPathDialog::pathChanged, this, &LDPaths::configurePaths);
 
 		if (m_dialog->exec() != QDialog::Accepted)
-			exit(1);
-		else
-			m_config->setLDrawPath(m_dialog->path());
+			return false;
+
+		configuration().setLDrawPath(m_dialog->path());
+		return true;
 	}
 }
 
@@ -70,19 +78,19 @@
 
 bool LDPaths::configurePaths (QString path)
 {
-	QDir dir (path);
-	bool ok = isValid (dir);
+	QDir dir {path};
+	bool ok = isValid(dir);
 
 	if (ok)
 	{
 		baseDir() = dir;
-		ldConfigPath() = format ("%1" DIRSLASH "LDConfig.ldr", path);
-		partsDir() = QDir (path + DIRSLASH "parts");
-		primitivesDir() = QDir (path + DIRSLASH "p");
+		ldConfigPath() = format("%1/LDConfig.ldr", path);
+		partsDir() = path + "/parts";
+		primitivesDir() = path + "/p";
 	}
 
 	if (m_dialog)
-		m_dialog->setStatusText (m_error.isEmpty() ? "OK" : m_error, ok);
+		m_dialog->setStatusText(m_error.isEmpty() ? "OK" : m_error, ok);
 
 	return ok;
 }
--- a/src/ldpaths.h	Thu Mar 09 00:40:07 2017 +0200
+++ b/src/ldpaths.h	Thu Mar 09 00:54:45 2017 +0200
@@ -27,8 +27,10 @@
 	Q_OBJECT
 
 public:
-	LDPaths(Configuration* config, QObject* parent = nullptr);
-	void checkPaths();
+	LDPaths(QObject* parent = nullptr);
+	~LDPaths();
+
+	bool checkPaths();
 	bool isValid (const class QDir& path) const;
 
 	static QDir& baseDir();
@@ -40,7 +42,6 @@
 	bool configurePaths (QString path);
 
 private:
-	Configuration* m_config;
 	mutable QString m_error;
 	class LDrawPathDialog* m_dialog;
-};
\ No newline at end of file
+};
--- a/src/main.cpp	Thu Mar 09 00:40:07 2017 +0200
+++ b/src/main.cpp	Thu Mar 09 00:54:45 2017 +0200
@@ -22,21 +22,27 @@
 #include "documentmanager.h"
 #include "mainwindow.h"
 
+/*
+ * Returns the configuration singleton.
+ */
 Configuration& configuration()
 {
 	static Configuration configuration;
 	return configuration;
 }
 
-int main (int argc, char* argv[])
+/*
+ * Runs the main LDforge application.
+ */
+int main(int argc, char* argv[])
 {
-	QApplication app (argc, argv);
-	app.setOrganizationName (APPNAME);
-	app.setApplicationName (APPNAME);
+	QApplication app {argc, argv};
+	app.setOrganizationName(APPNAME);
+	app.setApplicationName(APPNAME);
 
-	LDPaths* paths = new LDPaths(&configuration());
-	paths->checkPaths();
-	paths->deleteLater();
+	// Abort if we don't have a valid LDraw library path.
+	if (not LDPaths {}.checkPaths())
+		return 1;
 
 	initializeCrashHandler();
 	LDColor::initColors();
@@ -45,7 +51,7 @@
 
 	// Process the command line
 	for (int arg = 1; arg < argc; ++arg)
-		mainWindow.documents()->openMainModel(QString::fromLocal8Bit (argv[arg]));
+		mainWindow.documents()->openMainModel(QString::fromLocal8Bit(argv[arg]));
 
 	return app.exec();
 }

mercurial