]> Shamusworld >> Repos - architektonas/blobdiff - src/base/patternlist.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / base / patternlist.cpp
index c9f1499510f377f8cbc181f501ddbb3ed6a80cf6..41e26aed6b3045261a8b7a94530ec3db24352b1d 100644 (file)
 // Who  When        What
 // ---  ----------  -----------------------------------------------------------
 // JLH  06/01/2010  Added this text. :-)
+// JLH  09/13/2010  Fixed next() function to return a valid pointer.
 //
 
 #include "patternlist.h"
 
 #include "system.h"
 
-RS_PatternList * RS_PatternList::uniqueInstance = NULL;
+PatternList * PatternList::uniqueInstance = NULL;
 
 /**
  * Default constructor.
  */
-RS_PatternList::RS_PatternList(): patternIterator(patterns)
+PatternList::PatternList(): patternIterator(patterns)
 {
-#warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
-//    patterns.setAutoDelete(true);
-    //patternListListeners.setAutoDelete(false);
 }
 
-/*virtual*/ RS_PatternList::~RS_PatternList()
+/*virtual*/ PatternList::~PatternList()
 {
+       clearPatterns();
 }
 
 /**
  * @return Instance to the unique pattern list.
  */
-/*static*/ RS_PatternList * RS_PatternList::instance()
+/*static*/ PatternList * PatternList::instance()
 {
        if (uniqueInstance == NULL)
-               uniqueInstance = new RS_PatternList();
+               uniqueInstance = new PatternList();
 
        return uniqueInstance;
 }
 
 /**
- * Initializes the pattern list by creating empty RS_Pattern
+ * Initializes the pattern list by creating empty Pattern
  * objects, one for each pattern that could be found.
  */
-void RS_PatternList::init()
+void PatternList::init()
 {
-    RS_DEBUG->print("RS_PatternList::initPatterns");
+       DEBUG->print("PatternList::initPatterns");
+       QStringList list = SYSTEM->getPatternList();
+//     patterns.clear();
+       clearPatterns();
 
-    QStringList list = RS_SYSTEM->getPatternList();
-    RS_Pattern * pattern;
-
-       patterns.clear();
-
-    for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
+       for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
        {
-        RS_DEBUG->print("pattern: %s:", (*it).toLatin1().data());
-
-        QFileInfo fi(*it);
-        pattern = new RS_Pattern(fi.baseName().toLower());
-        patterns.append(pattern);
-
-        RS_DEBUG->print("base: %s", pattern->getFileName().toLatin1().data());
-    }
+               DEBUG->print("pattern: %s:", (*it).toAscii().data());
+               QFileInfo fi(*it);
+               Pattern * pattern = new Pattern(fi.baseName().toLower());
+               patterns.append(pattern);
+               DEBUG->print("base: %s", pattern->getFileName().toAscii().data());
+       }
 }
 
 /**
  * Removes all patterns in the patternlist.
  */
-void RS_PatternList::clearPatterns()
+void PatternList::clearPatterns()
 {
-    patterns.clear();
+       patternIterator = patterns;
+
+       while (patternIterator.hasNext())
+               delete patternIterator.next();
+
+       // Apparently this is still needed as the list will still be populated with
+       // stale pointers...
+       patterns.clear();
 }
 
-int RS_PatternList::countPatterns()
+int PatternList::countPatterns()
 {
        return patterns.count();
 }
@@ -88,23 +90,13 @@ int RS_PatternList::countPatterns()
  * Listeners are notified after the pattern was removed from
  * the list but before it gets deleted.
  */
-void RS_PatternList::removePattern(RS_Pattern * pattern)
+void PatternList::removePattern(Pattern * pattern)
 {
-       RS_DEBUG->print("RS_PatternList::removePattern()");
-
-       // here the pattern is removed from the list but not deleted
-//     patterns.remove(pattern);
-//Apparently we need to delete this shit here because of missing AutoDelete
-//function when going from Qt3->4
+       DEBUG->print("PatternList::removePattern()");
        int i = patterns.indexOf(pattern);
 
        if (i != -1)
                delete patterns.takeAt(i);
-
-       //for (uint i=0; i<patternListListeners.count(); ++i) {
-       //    RS_PatternListListener* l = patternListListeners.at(i);
-       //    l->patternRemoved(pattern);
-       //}
 }
 
 /**
@@ -112,20 +104,17 @@ void RS_PatternList::removePattern(RS_Pattern * pattern)
  * \p NULL if no such pattern was found. The pattern will be loaded into
  * memory if it's not already.
  */
-RS_Pattern * RS_PatternList::requestPattern(const QString & name)
+Pattern * PatternList::requestPattern(const QString & name)
 {
-       RS_DEBUG->print("RS_PatternList::requestPattern %s", name.toLatin1().data());
-
+       Pattern * foundPattern = NULL;
        QString name2 = name.toLower();
-       RS_Pattern * foundPattern = NULL;
-
-       RS_DEBUG->print("name2: %s", name2.toLatin1().data());
+       DEBUG->print("PatternList::requestPattern %s", name.toAscii().data());
+       DEBUG->print("name2: %s", name2.toAscii().data());
 
        // Search our list of available patterns:
-//     for(RS_Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
        for(int i=0; i<patterns.size(); i++)
        {
-               RS_Pattern * p = patterns[i];
+               Pattern * p = patterns[i];
 
                if (p->getFileName() == name2)
                {
@@ -136,38 +125,37 @@ RS_Pattern * RS_PatternList::requestPattern(const QString & name)
                }
        }
 
-       //if (foundPattern==NULL && name!="standard") {
-       //    foundPattern = requestPattern("standard");
-       //}
-
        return foundPattern;
 }
 
 //! @return First pattern of the list.
-RS_Pattern * RS_PatternList::firstPattern()
+Pattern * PatternList::firstPattern()
 {
-       patternIterator.toFront();
-       return patternIterator.next();
+       // Looks like this is necessary--constructor doesn't do it...
+       // Constructor is necessary, apparently, even though we do this.
+       patternIterator = patterns;
+       return nextPattern();
 }
 
 /**
  * @return Next pattern from the list after
  * calling firstPattern() or nextPattern().
  */
-RS_Pattern * RS_PatternList::nextPattern()
+Pattern * PatternList::nextPattern()
 {
-       return patternIterator.next();
+       // We absolutely HAVE to do a hasNext() check or we'll hand back a bogus
+       // pointer/object. Not good!
+       return (patternIterator.hasNext() ? patternIterator.next() : NULL);
 }
 
-bool RS_PatternList::contains(const QString & name)
+bool PatternList::contains(const QString & name)
 {
        QString name2 = name.toLower();
 
        // Search our list of available patterns:
-//     for(RS_Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
        for(int i=0; i<patterns.size(); i++)
        {
-               RS_Pattern * p = patterns[i];
+               Pattern * p = patterns[i];
 
                if (p->getFileName() == name2)
                        return true;
@@ -179,11 +167,12 @@ bool RS_PatternList::contains(const QString & name)
 /**
  * Dumps the patterns to stdout.
  */
-std::ostream & operator<<(std::ostream & os, RS_PatternList & l)
+std::ostream & operator<<(std::ostream & os, PatternList & l)
 {
-    os << "Patternlist: \n";
-    for(RS_Pattern * p=l.firstPattern(); p!=NULL; p=l.nextPattern())
-        os << *p << "\n";
+       os << "Patternlist: \n";
+
+       for(Pattern * p=l.firstPattern(); p!=NULL; p=l.nextPattern())
+               os << *p << "\n";
 
-    return os;
+       return os;
 }