Thu, 17 Nov 2016 12:18:13 +0200
Cleaned up PrimitiveScanner
src/primitives.cpp | file | annotate | diff | comparison | revisions | |
src/primitives.h | file | annotate | diff | comparison | revisions |
--- a/src/primitives.cpp Wed Nov 16 01:43:37 2016 +0200 +++ b/src/primitives.cpp Thu Nov 17 12:18:13 2016 +0200 @@ -570,12 +570,12 @@ } -// -// --------------------------------------------------------------------------------------------------------------------- -// - - -PrimitiveScanner::PrimitiveScanner (PrimitiveManager* parent) : +/* + * PrimitiveScanner :: PrimitiveScanner + * + * Constructs a primitive scanner. + */ +PrimitiveScanner::PrimitiveScanner(PrimitiveManager* parent) : QObject(parent), HierarchyElement(parent), m_manager(parent), @@ -585,53 +585,57 @@ print("Scanning primitives..."); } - -const QList<Primitive>& PrimitiveScanner::scannedPrimitives() const +/* + * PrimitiveScanner :: scannedPrimitives + * + * Returns a vector containing all the primitives found. + */ +const QVector<Primitive> &PrimitiveScanner::scannedPrimitives() const { - return m_prims; + return m_scannedPrimitives; } - +/* + * PrimitiveScanner :: work + * + * Does one step of work, processes up to 100 primitives. + * If the scanner does not finish work by this function call, it will ask the event loop to call this method again. + */ void PrimitiveScanner::work() { for (int i = 0; m_iterator.hasNext() and i < 100; ++i) { QString filename = m_iterator.next(); - QFile file (filename); - - if (not file.open (QIODevice::ReadOnly)) - continue; - - Primitive primitive; - primitive.name = filename.mid (m_basePathLength + 1); // make full path relative - primitive.name.replace ('/', '\\'); // use DOS backslashes, they're expected - primitive.category = nullptr; - QByteArray titledata = file.readLine(); + QFile file = {filename}; - if (titledata != QByteArray()) - primitive.title = QString::fromUtf8 (titledata); - - primitive.title = primitive.title.simplified(); + if (file.open (QIODevice::ReadOnly)) + { + Primitive primitive; + primitive.name = filename.mid(m_basePathLength + 1); // make full path relative + primitive.name.replace('/', '\\'); // use DOS backslashes, they're expected + primitive.category = nullptr; + primitive.title = QString::fromUtf8(file.readLine().simplified()); - if (primitive.title[0] == '0') - { - primitive.title.remove (0, 1); // remove 0 - primitive.title = primitive.title.simplified(); + if (primitive.title[0] == '0') + { + primitive.title.remove(0, 1); // remove 0 + primitive.title = primitive.title.simplified(); + } + + m_scannedPrimitives << primitive; } - - m_prims << primitive; } if (not m_iterator.hasNext()) { - // Done with primitives, now save to a config file + // If there are no more primitives to iterate, we're done. Now save this information into a cache file. QString path = m_manager->getPrimitivesCfgPath(); - QFile configFile (path); + QFile configFile = {path}; - if (configFile.open (QIODevice::WriteOnly | QIODevice::Text)) + if (configFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - for (Primitive& info : m_prims) - fprint (configFile, "%1 %2\r\n", info.name, info.title); + for (Primitive& primitive : m_scannedPrimitives) + fprint(configFile, "%1 %2\r\n", primitive.name, primitive.title); configFile.close(); } @@ -644,7 +648,7 @@ } else { - // Defer to event loop, pick up the work later + // Otherwise, defer to event loop, pick up the work later. QMetaObject::invokeMethod (this, "work", Qt::QueuedConnection); } }
--- a/src/primitives.h Wed Nov 16 01:43:37 2016 +0200 +++ b/src/primitives.h Thu Nov 17 12:18:13 2016 +0200 @@ -102,7 +102,7 @@ private: QList<PrimitiveCategory*> m_categories; PrimitiveScanner* m_activeScanner; - QList<Primitive> m_primitives; + QVector<Primitive> m_primitives; PrimitiveCategory* m_unmatched; void loadCategories(); @@ -110,16 +110,18 @@ void clearCategories(); }; -// -// Worker object that scans the primitives folder for primitives and builds an index of them. -// +/* + * PrimitiveScanner + * + * Worker object that scans the primitives folder for primitives and builds an index of them. + */ class PrimitiveScanner : public QObject, HierarchyElement { Q_OBJECT public: PrimitiveScanner(PrimitiveManager* parent); - const QList<Primitive>& scannedPrimitives() const; + const QVector<Primitive>& scannedPrimitives() const; public slots: void work(); @@ -129,7 +131,7 @@ private: PrimitiveManager* m_manager; - QList<Primitive> m_prims; + QVector<Primitive> m_scannedPrimitives; QDirIterator m_iterator; int m_basePathLength; };