Sat, 15 Jun 2013 19:14:42 +0300
Moved primitive handling to new code files
src/addObjectDialog.cpp | file | annotate | diff | comparison | revisions | |
src/file.cpp | file | annotate | diff | comparison | revisions | |
src/file.h | file | annotate | diff | comparison | revisions | |
src/gui_actions.cpp | file | annotate | diff | comparison | revisions | |
src/main.cpp | file | annotate | diff | comparison | revisions | |
src/primitives.cpp | file | annotate | diff | comparison | revisions | |
src/primitives.h | file | annotate | diff | comparison | revisions |
--- a/src/addObjectDialog.cpp Sat Jun 15 04:36:52 2013 +0300 +++ b/src/addObjectDialog.cpp Sat Jun 15 19:14:42 2013 +0300 @@ -33,14 +33,15 @@ #include "history.h" #include "widgets.h" #include "misc.h" +#include "primitives.h" class SubfileListItem : public QTreeWidgetItem { - PROPERTY (PrimitiveInfo*, primInfo, setPrimInfo) + PROPERTY (Primitive*, primInfo, setPrimInfo) public: - SubfileListItem (QTreeWidgetItem* parent, PrimitiveInfo* info) : + SubfileListItem (QTreeWidgetItem* parent, Primitive* info) : QTreeWidgetItem (parent), m_primInfo (info) {} - SubfileListItem (QTreeWidget* parent, PrimitiveInfo* info) : + SubfileListItem (QTreeWidget* parent, Primitive* info) : QTreeWidgetItem (parent), m_primInfo (info) {} }; @@ -99,7 +100,7 @@ parentItem->setText (0, "Primitives"); QList<QTreeWidgetItem*> subfileItems; - for (PrimitiveInfo& info : g_Primitives) { + for (Primitive& info : g_Primitives) { SubfileListItem* item = new SubfileListItem (parentItem, &info); item->setText (0, fmt ("%1 - %2", info.name, info.title)); subfileItems << item;
--- a/src/file.cpp Sat Jun 15 04:36:52 2013 +0300 +++ b/src/file.cpp Sat Jun 15 19:14:42 2013 +0300 @@ -20,7 +20,7 @@ #include <QFileDialog> #include <qprogressbar.h> #include <QDir> -#include <qthread.h> +#include <QThread> #include <stdlib.h> #include "common.h" @@ -38,9 +38,7 @@ cfg (str, io_recentfiles, ""); static bool g_loadingMainFile = false; -PrimitiveLister* g_activePrimLister = null; bool g_primListerMutex = false; -vector<PrimitiveInfo> g_Primitives; // ============================================================================= namespace LDPaths { @@ -929,108 +927,4 @@ g_loadedFiles.clear (); g_loadedFiles << filesUsed; -} - -// ============================================================================= -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -// ============================================================================= -void recursiveGetFilenames (QDir dir, vector<str>& fnames) { - QFileInfoList flist = dir.entryInfoList (); - for (const QFileInfo& info : flist) { - if (info.fileName () == "." || info.fileName () == "..") - continue; // skip . and .. - - if (info.isDir ()) - recursiveGetFilenames (QDir (info.absoluteFilePath ()), fnames); - else - fnames << info.absoluteFilePath (); - } -} - -void PrimitiveLister::work () { - g_activePrimLister = this; - m_prims.clear (); - - QDir dir (LDPaths::prims ()); - assert (dir.exists ()); - - ulong baselen = dir.absolutePath ().length (); - - vector<str> fnames; - recursiveGetFilenames (dir, fnames); - emit starting (fnames.size ()); - - ulong i = 0; - for (str fname : fnames) { - File f (fname, File::Read); - - PrimitiveInfo info; - info.name = fname.mid (baselen + 1); // make full path relative - info.name.replace ('/', '\\'); // use DOS backslashes, they're expected - - if (!f.readLine (info.title)) - info.title = ""; - - info.title = info.title.simplified (); - - if (info.title[0] == '0') { - info.title.remove (0, 1); // remove 0 - info.title = info.title.simplified (); - } - - m_prims << info; - emit update (++i); - } - - // Save to a config file - File conf (config::dirpath () + "prims.cfg", File::Write); - for (PrimitiveInfo& info : m_prims) - fprint (conf, "%1 %2\n", info.name, info.title); - - conf.close (); - - g_primListerMutex = true; - g_Primitives = m_prims; - g_primListerMutex = false; - - g_activePrimLister = null; - emit workDone (); -} - -void PrimitiveLister::start () { - if (g_activePrimLister) - return; - - PrimitiveLister* lister = new PrimitiveLister; - QThread* listerThread = new QThread; - lister->moveToThread (listerThread); - connect (lister, SIGNAL (starting (ulong)), g_win, SLOT (primitiveLoaderStart (ulong))); - connect (lister, SIGNAL (update (ulong)), g_win, SLOT (primitiveLoaderUpdate (ulong))); - connect (lister, SIGNAL (workDone ()), g_win, SLOT (primitiveLoaderEnd ())); - connect (listerThread, SIGNAL (started ()), lister, SLOT (work ())); - connect (listerThread, SIGNAL (finished ()), lister, SLOT (deleteLater ())); - listerThread->start (); -} - -void loadPrimitiveInfo () { - g_Primitives.clear (); - - // Try to load prims.cfg - File conf (config::dirpath () + "prims.cfg", File::Read); - if (!conf) { - // No prims.cfg, build it - PrimitiveLister::start (); - return; - } - - for (str line : conf) { - int space = line.indexOf (" "); - if (space == -1) - continue; - - PrimitiveInfo info; - info.name = line.left (space); - info.title = line.mid (space + 1); - g_Primitives << info; - } } \ No newline at end of file
--- a/src/file.h Sat Jun 15 04:36:52 2013 +0300 +++ b/src/file.h Sat Jun 15 19:14:42 2013 +0300 @@ -167,40 +167,4 @@ void workDone (); }; -struct PrimitiveInfo { - str name, title; -}; - -// ============================================================================= -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -// ============================================================================= -// PrimitiveLister -// -// Worker object that scans the primitives folder for primitives and -// builds an index of them. -// ============================================================================= -class PrimitiveLister : public QObject { - Q_OBJECT - -public: - static void start (); - -public slots: - void work (); - -signals: - void starting (ulong num); - void workDone (); - void update (ulong i); - -private: - vector<PrimitiveInfo> m_prims; -}; - -extern vector<PrimitiveInfo> g_Primitives; -extern PrimitiveLister* g_activePrimLister; -extern bool g_primListerMutex; - -void loadPrimitiveInfo (); - #endif // FILE_H \ No newline at end of file
--- a/src/gui_actions.cpp Sat Jun 15 04:36:52 2013 +0300 +++ b/src/gui_actions.cpp Sat Jun 15 19:14:42 2013 +0300 @@ -33,6 +33,7 @@ #include "misc.h" #include "gldraw.h" #include "dialogs.h" +#include "primitives.h" extern_cfg (bool, gl_wireframe);
--- a/src/main.cpp Sat Jun 15 04:36:52 2013 +0300 +++ b/src/main.cpp Sat Jun 15 19:14:42 2013 +0300 @@ -28,6 +28,7 @@ #include "config.h" #include "colors.h" #include "types.h" +#include "primitives.h" vector<LDOpenFile*> g_loadedFiles; LDOpenFile* g_curfile = null; @@ -73,13 +74,12 @@ } LDPaths::initPaths (); - initColors (); - + ForgeWindow* win = new ForgeWindow; + newFile (); - - loadPrimitiveInfo (); + loadPrimitives (); win->show (); return app.exec (); @@ -178,7 +178,7 @@ str errmsg = fmt ("Aborting over a call to fatal():\nFile: %1\nLine: %2\nFunction: %3\n\n%4", file, line, funcname, msg); - printf ("%s\n", qchars (errmsg)); + print ("%1\n", errmsg); if (g_win) g_win->deleteLater ();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/primitives.cpp Sat Jun 15 19:14:42 2013 +0300 @@ -0,0 +1,112 @@ +#include <QDir> +#include <QThread> +#include "file.h" +#include "gui.h" +#include "primitives.h" + +PrimitiveLister* g_activePrimLister = null; +vector<Primitive> g_Primitives; + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +void recursiveGetFilenames (QDir dir, vector<str>& fnames) { + QFileInfoList flist = dir.entryInfoList (); + for (const QFileInfo& info : flist) { + if (info.fileName () == "." || info.fileName () == "..") + continue; // skip . and .. + + if (info.isDir ()) + recursiveGetFilenames (QDir (info.absoluteFilePath ()), fnames); + else + fnames << info.absoluteFilePath (); + } +} + +void PrimitiveLister::work () { + g_activePrimLister = this; + m_prims.clear (); + + QDir dir (LDPaths::prims ()); + assert (dir.exists ()); + + ulong baselen = dir.absolutePath ().length (); + + vector<str> fnames; + recursiveGetFilenames (dir, fnames); + emit starting (fnames.size ()); + + ulong i = 0; + for (str fname : fnames) { + File f (fname, File::Read); + + Primitive info; + info.name = fname.mid (baselen + 1); // make full path relative + info.name.replace ('/', '\\'); // use DOS backslashes, they're expected + + if (!f.readLine (info.title)) + info.title = ""; + + info.title = info.title.simplified (); + + if (info.title[0] == '0') { + info.title.remove (0, 1); // remove 0 + info.title = info.title.simplified (); + } + + m_prims << info; + emit update (++i); + } + + // Save to a config file + File conf (config::dirpath () + "prims.cfg", File::Write); + for (Primitive& info : m_prims) + fprint (conf, "%1 %2\n", info.name, info.title); + + conf.close (); + + g_primListerMutex = true; + g_Primitives = m_prims; + g_primListerMutex = false; + + g_activePrimLister = null; + emit workDone (); +} + +void PrimitiveLister::start () { + if (g_activePrimLister) + return; + + PrimitiveLister* lister = new PrimitiveLister; + QThread* listerThread = new QThread; + lister->moveToThread (listerThread); + connect (lister, SIGNAL (starting (ulong)), g_win, SLOT (primitiveLoaderStart (ulong))); + connect (lister, SIGNAL (update (ulong)), g_win, SLOT (primitiveLoaderUpdate (ulong))); + connect (lister, SIGNAL (workDone ()), g_win, SLOT (primitiveLoaderEnd ())); + connect (listerThread, SIGNAL (started ()), lister, SLOT (work ())); + connect (listerThread, SIGNAL (finished ()), lister, SLOT (deleteLater ())); + listerThread->start (); +} + +void loadPrimitives () { + g_Primitives.clear (); + + // Try to load prims.cfg + File conf (config::dirpath () + "prims.cfg", File::Read); + if (!conf) { + // No prims.cfg, build it + PrimitiveLister::start (); + return; + } + + for (str line : conf) { + int space = line.indexOf (" "); + if (space == -1) + continue; + + Primitive info; + info.name = line.left (space); + info.title = line.mid (space + 1); + g_Primitives << info; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/primitives.h Sat Jun 15 19:14:42 2013 +0300 @@ -0,0 +1,61 @@ +/* + * 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 PRIMITIVES_H +#define PRIMITIVES_H + +#include "common.h" +#include "types.h" + +struct Primitive { + str name, title; +}; + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +// PrimitiveLister +// +// Worker object that scans the primitives folder for primitives and +// builds an index of them. +// ============================================================================= +class PrimitiveLister : public QObject { + Q_OBJECT + +public: + static void start (); + +public slots: + void work (); + +signals: + void starting (ulong num); + void workDone (); + void update (ulong i); + +private: + vector<Primitive> m_prims; +}; + +extern vector<Primitive> g_Primitives; +extern PrimitiveLister* g_activePrimLister; +extern bool g_primListerMutex; + +void loadPrimitives (); + +#endif // PRIMITIVES_H \ No newline at end of file