Added a dialog for inquering the user's LDraw directory, not having it set leads to crashing...

Wed, 08 May 2013 03:53:45 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 08 May 2013 03:53:45 +0300
changeset 179
f98391b179ab
parent 178
bc30a6d59f7c
child 180
16306e69a895

Added a dialog for inquering the user's LDraw directory, not having it set leads to crashing...

file.cpp file | annotate | diff | comparison | revisions
file.h file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
ldforge.pro file | annotate | diff | comparison | revisions
main.cpp file | annotate | diff | comparison | revisions
zz_colorSelectDialog.cpp file | annotate | diff | comparison | revisions
zz_ldrawPathDialog.cpp file | annotate | diff | comparison | revisions
zz_ldrawPathDialog.h file | annotate | diff | comparison | revisions
--- a/file.cpp	Wed May 08 01:40:07 2013 +0300
+++ b/file.cpp	Wed May 08 03:53:45 2013 +0300
@@ -19,6 +19,7 @@
 #include <vector>
 #include <stdio.h>
 #include <qmessagebox.h>
+#include <QDir>
 #include "common.h"
 #include "config.h"
 #include "file.h"
@@ -26,11 +27,62 @@
 #include "bbox.h"
 #include "gui.h"
 #include "history.h"
+#include "zz_ldrawPathDialog.h"
 
 cfg (str, io_ldpath, "");
 cfg (str, io_recentfiles, "");
 
 // =============================================================================
+namespace LDPaths {
+	static str pathError;
+	
+	struct {
+		str LDConfigPath;
+		str partsPath, primsPath;
+	} pathInfo;
+	
+	void initPaths () {
+		if (!tryConfigure (io_ldpath)) {
+			LDrawPathDialog dlg;
+			
+			if (!dlg.exec ())
+				exit (0);
+			
+			io_ldpath = dlg.path ();
+		}
+	}
+	
+	bool tryConfigure (str path) {
+		QDir dir;
+		
+		if (!dir.cd (path)) {
+			pathError = "Directory does not exist.";
+			return false;
+		}
+		
+		QStringList mustHave = {"LDConfig.ldr", "parts", "p"};
+		QStringList contents = dir.entryList (mustHave);
+		
+		if (contents.size () != mustHave.size ()) {
+			pathError = "Not an LDraw directory! Must<br />have LDConfig.ldr, parts/ and p/.";
+			return false;
+		}
+		
+		pathInfo.partsPath = fmt ("%s" DIRSLASH "parts", path.chars ());
+		pathInfo.LDConfigPath = fmt ("%s" DIRSLASH "LDConfig.ldr", path.chars ());
+		pathInfo.primsPath = fmt ("%s" DIRSLASH "p", path.chars ());
+		
+		return true;
+	}
+	
+	// Accessors
+	str getError () { return pathError; }
+	str ldconfig () { return pathInfo.LDConfigPath; }
+	str prims () { return pathInfo.primsPath; }
+	str parts () { return pathInfo.partsPath; }
+}
+
+// =============================================================================
 OpenFile::OpenFile () {
 	m_implicit = true;
 	savePos = -1;
--- a/file.h	Wed May 08 01:40:07 2013 +0300
+++ b/file.h	Wed May 08 03:53:45 2013 +0300
@@ -23,6 +23,16 @@
 #include "ldtypes.h"
 #include "str.h"
 
+namespace LDPaths {
+	void initPaths ();
+	bool tryConfigure (str path);
+	
+	str ldconfig ();
+	str prims ();
+	str parts ();
+	str getError ();
+}
+
 // =============================================================================
 // OpenFile
 //
--- a/gui.cpp	Wed May 08 01:40:07 2013 +0300
+++ b/gui.cpp	Wed May 08 03:53:45 2013 +0300
@@ -389,6 +389,7 @@
 			meta.push_back ({null, null, true});
 		} else {
 			color* col = getColor (atoi (colorname));
+			assert (col != null);
 			meta.push_back ({col, null, false});
 		}
 	}
@@ -400,12 +401,12 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void ForgeWindow::updateToolBars () {
-	for (QToolBar* qBar : m_toolBars)
-		qBar->setIconSize (QSize (gui_toolbar_iconsize, gui_toolbar_iconsize));
+	for (QToolBar* bar : m_toolBars)
+		bar->setIconSize (QSize (gui_toolbar_iconsize, gui_toolbar_iconsize));
 	
 	// Update the quick color toolbar.
-	for (QPushButton* qButton : m_colorButtons)
-		delete qButton;
+	for (QPushButton* btn : m_colorButtons)
+		delete btn;
 	
 	m_colorButtons.clear ();
 	
@@ -416,16 +417,16 @@
 		if (entry.bSeparator)
 			m_colorToolBar->addSeparator ();
 		else {
-			QPushButton* qColorButton = new QPushButton;
-			qColorButton->setAutoFillBackground (true);
-			qColorButton->setStyleSheet (fmt ("background-color: %s", entry.col->zColorString.chars()));
-			qColorButton->setToolTip (entry.col->zName);
+			QPushButton* colorButton = new QPushButton;
+			colorButton->setAutoFillBackground (true);
+			colorButton->setStyleSheet (fmt ("background-color: %s", entry.col->zColorString.chars()));
+			colorButton->setToolTip (entry.col->zName);
 			
-			connect (qColorButton, SIGNAL (clicked ()), this, SLOT (slot_quickColor ()));
-			m_colorToolBar->addWidget (qColorButton);
-			m_colorButtons.push_back (qColorButton);
+			connect (colorButton, SIGNAL (clicked ()), this, SLOT (slot_quickColor ()));
+			m_colorToolBar->addWidget (colorButton);
+			m_colorButtons.push_back (colorButton);
 			
-			entry.btn = qColorButton;
+			entry.btn = colorButton;
 		}
 	}
 	
--- a/ldforge.pro	Wed May 08 01:40:07 2013 +0300
+++ b/ldforge.pro	Wed May 08 03:53:45 2013 +0300
@@ -31,6 +31,7 @@
 	zz_addObjectDialog.h \
 	zz_colorSelectDialog.h \
 	zz_configDialog.h \
+	zz_ldrawPathDialog.h \
 	zz_historyDialog.h \
 	zz_newPartDialog.h \
 	zz_setContentsDialog.h
@@ -56,6 +57,7 @@
 	zz_addObjectDialog.cpp \
 	zz_colorSelectDialog.cpp \
 	zz_configDialog.cpp \
+	zz_ldrawPathDialog.cpp \
 	zz_historyDialog.cpp \
 	zz_newPartDialog.cpp \
 	zz_setContentsDialog.cpp
--- a/main.cpp	Wed May 08 01:40:07 2013 +0300
+++ b/main.cpp	Wed May 08 03:53:45 2013 +0300
@@ -37,7 +37,7 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-int main (int dArgc, char* saArgv[]) {
+int main (int argc, char* argv[]) {
 	// Load or create the configuration
 	if (!config::load()) {
 		printf ("Creating configuration file...\n");
@@ -47,10 +47,12 @@
 			printf ("failed to create configuration file!\n");
 	}
 	
+	const QApplication app (argc, argv);
+	LDPaths::initPaths ();
+	
 	initColors ();
 	initPartList ();
 	
-	const QApplication app (dArgc, saArgv);
 	ForgeWindow* win = new ForgeWindow;
 	
 	g_app = &app;
--- a/zz_colorSelectDialog.cpp	Wed May 08 01:40:07 2013 +0300
+++ b/zz_colorSelectDialog.cpp	Wed May 08 03:53:45 2013 +0300
@@ -167,11 +167,11 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-bool ColorSelectDialog::staticDialog (short int& dValue, short int defval, QWidget* parent) {
+bool ColorSelectDialog::staticDialog (short& val, short int defval, QWidget* parent) {
 	ColorSelectDialog dlg (defval, parent);
 	
 	if (dlg.exec () && dlg.selColor != -1) {
-		dValue = dlg.selColor;
+		val = dlg.selColor;
 		return true;
 	}
 	
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zz_ldrawPathDialog.cpp	Wed May 08 03:53:45 2013 +0300
@@ -0,0 +1,109 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 Santeri Piippo
+ *  
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <qlineedit.h>
+#include <qpushbutton.h>
+#include <qdialogbuttonbox.h>
+#include <QFileDialog>
+#include "zz_ldrawPathDialog.h"
+#include "gui.h"
+#include "file.h"
+
+
+// ========================================================================================================================================
+LDrawPathDialog::LDrawPathDialog (QWidget* parent, Qt::WindowFlags f) : QDialog (parent, f) {
+	lb_resolution = new QLabel ("---");
+	QLabel* lb_description = new QLabel ("Please input your LDraw directory");
+	QLabel* lb_path = new QLabel ("LDraw path:");
+	le_path = new QLineEdit;
+	btn_findPath = new QPushButton;
+	btn_findPath->setIcon (getIcon ("folder"));
+	
+	btn_tryConfigure = new QPushButton ("Configure");
+	btn_tryConfigure->setIcon (getIcon ("settings"));
+	
+	btn_exit = new QPushButton ("Exit");
+	btn_exit->setIcon (getIcon ("exit"));
+	
+	dbb_buttons = new QDialogButtonBox (QDialogButtonBox::Ok);
+	dbb_buttons->addButton (btn_tryConfigure, QDialogButtonBox::ApplyRole);
+	dbb_buttons->addButton (btn_exit, QDialogButtonBox::RejectRole);
+	okButton ()->setEnabled (false);
+	
+	QHBoxLayout* inputLayout = new QHBoxLayout;
+	inputLayout->addWidget (lb_path);
+	inputLayout->addWidget (le_path);
+	inputLayout->addWidget (btn_findPath);
+	
+	QVBoxLayout* mainLayout = new QVBoxLayout;
+	mainLayout->addWidget (lb_description);
+	mainLayout->addLayout (inputLayout);
+	mainLayout->addWidget (lb_resolution);
+	mainLayout->addWidget (dbb_buttons);
+	setLayout (mainLayout);
+	
+	connect (btn_findPath, SIGNAL (clicked ()), this, SLOT (slot_findPath ()));
+	connect (btn_tryConfigure, SIGNAL (clicked ()), this, SLOT (slot_tryConfigure ()));
+	connect (dbb_buttons, SIGNAL (accepted ()), this, SLOT (accept ()));
+	connect (dbb_buttons, SIGNAL (rejected ()), this, SLOT (slot_exit ()));
+}
+
+
+// ========================================================================================================================================
+QPushButton* LDrawPathDialog::okButton () {
+	return dbb_buttons->button (QDialogButtonBox::Ok);
+}
+
+
+// ========================================================================================================================================
+void LDrawPathDialog::setPath (str path) {
+	le_path->setText (path);
+}
+
+// ========================================================================================================================================
+str LDrawPathDialog::path () const {
+	return le_path->text ();
+}
+
+// ========================================================================================================================================
+void LDrawPathDialog::slot_findPath () {
+	str newpath = QFileDialog::getExistingDirectory (this, "Find LDraw Path");
+	
+	if (~newpath > 0 && newpath != path ()) {
+		setPath (newpath);
+		slot_tryConfigure ();
+	}
+}
+
+
+// ========================================================================================================================================
+void LDrawPathDialog::slot_exit () {
+	exit (1);
+}
+
+// ========================================================================================================================================
+void LDrawPathDialog::slot_tryConfigure () {
+	if (LDPaths::tryConfigure (path ()) == false) {
+		lb_resolution->setText (fmt ("<span style=\"color:red; font-weight: bold;\">%s</span>", LDPaths::getError().chars ()));
+		okButton ()->setEnabled (false);
+		return;
+	}
+	
+	lb_resolution->setText ("<span style=\"color: #7A0; font-weight: bold;\">OK!</span>");
+	okButton ()->setEnabled (true);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zz_ldrawPathDialog.h	Wed May 08 03:53:45 2013 +0300
@@ -0,0 +1,56 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 Santeri Piippo
+ *  
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LDRAWPATHDIALOG_H
+#define LDRAWPATHDIALOG_H
+
+#include <qdialog.h>
+#include "common.h"
+
+class QLabel;
+class QLineEdit;
+class QDialogButtonBox;
+
+class LDrawPathDialog : public QDialog {
+	Q_OBJECT
+	
+public:
+	explicit LDrawPathDialog (QWidget* parent = null, Qt::WindowFlags f = 0);
+	str path () const;
+	void setPath (str path);
+	void (*callback ()) () const { return m_callback; }
+	void setCallback (void (*callback) ()) { m_callback = callback; }
+	
+private:
+	Q_DISABLE_COPY (LDrawPathDialog)
+	
+	QLabel* lb_resolution;
+	QLineEdit* le_path;
+	QPushButton* btn_findPath, *btn_tryConfigure, *btn_exit;
+	QDialogButtonBox* dbb_buttons;
+	void (*m_callback) ();
+	
+	QPushButton* okButton ();
+	
+private slots:
+	void slot_findPath ();
+	void slot_tryConfigure ();
+	void slot_exit ();
+};
+
+#endif // LDRAWPATHDIALOG_H

mercurial