- now tries to download missing files off ldraw.org

Sun, 31 Aug 2014 03:10:51 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 31 Aug 2014 03:10:51 +0300
changeset 867
557cb07dbe57
parent 866
4951b737f8cb
child 868
6e03c68c81ba

- now tries to download missing files off ldraw.org

changelog.txt file | annotate | diff | comparison | revisions
src/configDialog.cc file | annotate | diff | comparison | revisions
src/ldDocument.cc file | annotate | diff | comparison | revisions
src/ldDocument.h file | annotate | diff | comparison | revisions
src/ldObject.cc file | annotate | diff | comparison | revisions
src/partDownloader.cc file | annotate | diff | comparison | revisions
src/partDownloader.h file | annotate | diff | comparison | revisions
--- a/changelog.txt	Sat Aug 30 20:09:30 2014 +0300
+++ b/changelog.txt	Sun Aug 31 03:10:51 2014 +0300
@@ -43,6 +43,7 @@
 +	- Added an action for splitting lines into equal-sized segments.
 +	- Added three togglable actions for filtering what's drawn (Draw surfaces/Draw edgelines/Draw conditional lines)
 +	- Added the magic wand tool.
++	- Can now attempt to download missing subfiles from ldraw.org like LDView.
 
 -	- The camera is now changed to the top one if switching to draw mode while using the free camera instead of disabling the draw mode.
 -	- The color selector now uses the color's edge color for the borders instead of black.
--- a/src/configDialog.cc	Sat Aug 30 20:09:30 2014 +0300
+++ b/src/configDialog.cc	Sun Aug 31 03:10:51 2014 +0300
@@ -317,7 +317,7 @@
 	}
 
 	Config::Save();
-	ReloadAllSubfiles();
+	LDDocument::current()->reloadAllSubfiles();
 	LoadLogoStuds();
 	g_win->R()->setBackground();
 	g_win->doFullRefresh();
--- a/src/ldDocument.cc	Sat Aug 30 20:09:30 2014 +0300
+++ b/src/ldDocument.cc	Sun Aug 31 03:10:51 2014 +0300
@@ -31,6 +31,7 @@
 #include "dialogs.h"
 #include "glRenderer.h"
 #include "glCompiler.h"
+#include "partDownloader.h"
 
 CFGENTRY (String, LDrawPath, "")
 CFGENTRY (List, RecentFiles, {})
@@ -749,6 +750,32 @@
 	// Add it to the recent files list.
 	AddRecentFile (path);
 	g_loadingMainFile = false;
+
+	// If there were problems loading subfile references, try see if we can find these
+	// files on the parts tracker.
+	QStringList unknowns;
+
+	for (LDObjectPtr obj : file->objects())
+	{
+		if (obj->type() != OBJ_Error or obj.staticCast<LDError>()->fileReferenced().isEmpty())
+			continue;
+
+		unknowns << obj.staticCast<LDError>()->fileReferenced();
+	}
+
+	if (not unknowns.isEmpty())
+	{
+		PartDownloader dl;
+		dl.setSource (PartDownloader::PartsTracker);
+		dl.setPrimaryFile (file);
+
+		for (QString const& unknown : unknowns)
+			dl.downloadFromPartsTracker (unknown);
+
+		dl.exec();
+		dl.checkIfFinished();
+		file->reloadAllSubfiles();
+	}
 }
 
 // =============================================================================
@@ -1086,23 +1113,27 @@
 
 // =============================================================================
 //
-void ReloadAllSubfiles()
+void LDDocument::reloadAllSubfiles()
 {
-	if (not CurrentDocument())
-		return;
+	print ("Reloading subfiles of %1", getDisplayName());
 
 	// Go through all objects in the current file and reload the subfiles
-	for (LDObjectPtr obj : CurrentDocument()->objects())
+	for (LDObjectPtr obj : objects())
 	{
 		if (obj->type() == OBJ_Subfile)
 		{
 			LDSubfilePtr ref = obj.staticCast<LDSubfile>();
 			LDDocumentPtr fileInfo = GetDocument (ref->fileInfo()->name());
 
-			if (fileInfo)
+			if (fileInfo != null)
+			{
 				ref->setFileInfo (fileInfo);
+			}
 			else
-				ref->replace (LDSpawn<LDError> (ref->asText(), format ("Could not open %1", ref->fileInfo()->name())));
+			{
+				ref->replace (LDSpawn<LDError> (ref->asText(),
+					format ("Could not open %1", ref->fileInfo()->name())));
+			}
 		}
 
 		// Reparse gibberish files. It could be that they are invalid because
@@ -1110,6 +1141,11 @@
 		if (obj->type() == OBJ_Error)
 			obj->replace (ParseLine (obj.staticCast<LDError>()->contents()));
 	}
+
+	m_needsReCache = true;
+
+	if (self() == CurrentDocument())
+		g_win->buildObjList();
 }
 
 // =============================================================================
@@ -1268,7 +1304,14 @@
 
 		for (LDObjectPtr obj : inlineContents (true, true))
 		{
-			assert (obj->type() != OBJ_Subfile);
+			if (obj->type() == OBJ_Subfile)
+			{
+				print ("Warning: unable to inline %1 into %2",
+					obj.staticCast<LDSubfile>()->fileInfo()->getDisplayName(),
+					getDisplayName());
+				continue;
+			}
+
 			LDPolygon* data = obj->getPolygon();
 
 			if (data != null)
--- a/src/ldDocument.h	Sat Aug 30 20:09:30 2014 +0300
+++ b/src/ldDocument.h	Sun Aug 31 03:10:51 2014 +0300
@@ -108,6 +108,7 @@
 	void addKnownVertices (LDObjectPtr obj);
 	void redoVertices();
 	void needVertexMerge();
+	void reloadAllSubfiles();
 
 	inline LDDocument& operator<< (LDObjectPtr obj)
 	{
@@ -208,9 +209,6 @@
 // from file if necessary. Can return null if neither succeeds.
 LDDocumentPtr GetDocument (QString filename);
 
-// Re-caches all subfiles.
-void ReloadAllSubfiles();
-
 // Is it safe to close all files?
 bool IsSafeToCloseAll();
 
--- a/src/ldObject.cc	Sat Aug 30 20:09:30 2014 +0300
+++ b/src/ldObject.cc	Sun Aug 31 03:10:51 2014 +0300
@@ -382,6 +382,7 @@
 	// Transform the objects
 	for (LDObjectPtr obj : objs)
 	{
+		assert (obj->type() != OBJ_Subfile);
 		// Set the parent now so we know what inlined the object.
 		obj->setParent (self());
 		TransformObject (obj, transform(), position(), color());
--- a/src/partDownloader.cc	Sat Aug 30 20:09:30 2014 +0300
+++ b/src/partDownloader.cc	Sun Aug 31 03:10:51 2014 +0300
@@ -69,7 +69,9 @@
 
 // =============================================================================
 //
-PartDownloader::PartDownloader (QWidget* parent) : QDialog (parent)
+PartDownloader::PartDownloader (QWidget* parent) :
+	QDialog (parent),
+	m_source (Source (0))
 {
 	setForm (new Ui_DownloadFrom);
 	form()->setupUi (this);
@@ -188,7 +190,15 @@
 //
 PartDownloader::Source PartDownloader::getSource() const
 {
-	return (Source) form()->source->currentIndex();
+	return m_source;
+}
+
+// =============================================================================
+//
+void PartDownloader::setSource (Source src)
+{
+	m_source = src;
+	form()->source->setCurrentIndex (int (src));
 }
 
 // =============================================================================
@@ -199,6 +209,8 @@
 		form()->fileNameLabel->setText (tr ("URL:"));
 	else
 		form()->fileNameLabel->setText (tr ("File name:"));
+
+	m_source = Source (i);
 }
 
 // =============================================================================
@@ -234,14 +246,7 @@
 				return;
 		}
 
-		downloadButton()->setEnabled (false);
-		form()->progress->setEnabled (true);
-		form()->fname->setEnabled (false);
-		form()->source->setEnabled (false);
 		downloadFile (dest, getURL(), true);
-		getButton (Close)->setEnabled (false);
-		getButton (Abort)->setEnabled (true);
-		getButton (Download)->setEnabled (false);
 	}
 }
 
@@ -255,6 +260,7 @@
 	if (filesToDownload().indexOf (dest) != -1)
 		return;
 
+	print ("Downloading %1 from %2\n", dest, url);
 	modifyDestination (dest);
 	PartDownloadRequest* req = new PartDownloadRequest (url, dest, primary, this);
 	m_filesToDownload << dest;
@@ -262,6 +268,21 @@
 	form()->progress->insertRow (row);
 	req->setTableRow (row);
 	req->updateToTable();
+	downloadButton()->setEnabled (false);
+	form()->progress->setEnabled (true);
+	form()->fname->setEnabled (false);
+	form()->source->setEnabled (false);
+	getButton (Close)->setEnabled (false);
+	getButton (Abort)->setEnabled (true);
+	getButton (Download)->setEnabled (false);
+}
+
+// =============================================================================
+//
+void PartDownloader::downloadFromPartsTracker (QString file)
+{
+	modifyDestination (file);
+	downloadFile (file, g_unofficialLibraryURL + file, false);
 }
 
 // =============================================================================
@@ -289,11 +310,13 @@
 	if (primaryFile() != null)
 	{
 		LDDocument::setCurrent (primaryFile());
-		ReloadAllSubfiles();
 		g_win->doFullRefresh();
 		g_win->R()->resetAngles();
 	}
 
+		for (LDDocumentPtr f : m_files)
+		f->reloadAllSubfiles();
+
 	if (cfg::AutoCloseDownloadDialog and not failed)
 	{
 		// Close automatically if desired.
@@ -427,10 +450,13 @@
 		if (isPrimary() and not prompt()->isAborted())
 			CriticalError (networkReply()->errorString());
 
+		print ("Unable to download %1: %2\n", m_destinaton, networkReply()->errorString());
         setState (State::Failed);
 	}
     elif (state() != State::Failed)
+	{
         setState (State::Finished);
+	}
 
 	setNumBytesRead (numBytesTotal());
 	updateToTable();
@@ -469,10 +495,11 @@
 			continue;
 
 		QString dest = err->fileReferenced();
-		prompt()->modifyDestination (dest);
-		prompt()->downloadFile (dest, g_unofficialLibraryURL + dest, false);
+		prompt()->downloadFromPartsTracker (dest);
 	}
 
+	prompt()->addFile (f);
+
 	if (isPrimary())
 	{
 		AddRecentFile (filePath());
@@ -484,6 +511,13 @@
 
 // =============================================================================
 //
+void PartDownloader::addFile (LDDocumentPtr f)
+{
+	m_files << f;
+}
+
+// =============================================================================
+//
 void PartDownloadRequest::downloadProgress (int64 recv, int64 total)
 {
 	setNumBytesRead (recv);
--- a/src/partDownloader.h	Sat Aug 30 20:09:30 2014 +0300
+++ b/src/partDownloader.h	Sun Aug 31 03:10:51 2014 +0300
@@ -69,10 +69,13 @@
 	explicit		PartDownloader (QWidget* parent = null);
 	virtual			~PartDownloader();
 
+	void			addFile (LDDocumentPtr f);
 	void			downloadFile (QString dest, QString url, bool primary);
+	void			downloadFromPartsTracker (QString file);
 	QPushButton*	getButton (Button i);
 	QString			getURL();
 	Source			getSource() const;
+	void			setSource (Source src);
 	void			modifyDestination (QString& dest) const;
 
 	static QString	getDownloadPath();
@@ -82,6 +85,10 @@
 	void			buttonClicked (QAbstractButton* btn);
 	void			checkIfFinished();
 	void			sourceChanged (int i);
+
+private:
+	Source m_source;
+	QList<LDDocumentPtr> m_files;
 };
 
 // =============================================================================

mercurial