3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 06/01/2010 Added this text. :-)
15 // JLH 09/13/2010 Fixed next() function to return a valid pointer.
18 #include "patternlist.h"
22 PatternList * PatternList::uniqueInstance = NULL;
25 * Default constructor.
27 PatternList::PatternList(): patternIterator(patterns)
31 /*virtual*/ PatternList::~PatternList()
37 * @return Instance to the unique pattern list.
39 /*static*/ PatternList * PatternList::instance()
41 if (uniqueInstance == NULL)
42 uniqueInstance = new PatternList();
44 return uniqueInstance;
48 * Initializes the pattern list by creating empty Pattern
49 * objects, one for each pattern that could be found.
51 void PatternList::init()
53 DEBUG->print("PatternList::initPatterns");
54 QStringList list = SYSTEM->getPatternList();
58 for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
60 DEBUG->print("pattern: %s:", (*it).toAscii().data());
62 Pattern * pattern = new Pattern(fi.baseName().toLower());
63 patterns.append(pattern);
64 DEBUG->print("base: %s", pattern->getFileName().toAscii().data());
69 * Removes all patterns in the patternlist.
71 void PatternList::clearPatterns()
73 patternIterator = patterns;
75 while (patternIterator.hasNext())
76 delete patternIterator.next();
78 // Apparently this is still needed as the list will still be populated with
83 int PatternList::countPatterns()
85 return patterns.count();
89 * Removes a pattern from the list.
90 * Listeners are notified after the pattern was removed from
91 * the list but before it gets deleted.
93 void PatternList::removePattern(Pattern * pattern)
95 DEBUG->print("PatternList::removePattern()");
96 int i = patterns.indexOf(pattern);
99 delete patterns.takeAt(i);
103 * @return Pointer to the pattern with the given name or
104 * \p NULL if no such pattern was found. The pattern will be loaded into
105 * memory if it's not already.
107 Pattern * PatternList::requestPattern(const QString & name)
109 Pattern * foundPattern = NULL;
110 QString name2 = name.toLower();
111 DEBUG->print("PatternList::requestPattern %s", name.toAscii().data());
112 DEBUG->print("name2: %s", name2.toAscii().data());
114 // Search our list of available patterns:
115 for(int i=0; i<patterns.size(); i++)
117 Pattern * p = patterns[i];
119 if (p->getFileName() == name2)
121 // Make sure this pattern is loaded into memory:
131 //! @return First pattern of the list.
132 Pattern * PatternList::firstPattern()
134 // Looks like this is necessary--constructor doesn't do it...
135 // Constructor is necessary, apparently, even though we do this.
136 patternIterator = patterns;
137 return nextPattern();
141 * @return Next pattern from the list after
142 * calling firstPattern() or nextPattern().
144 Pattern * PatternList::nextPattern()
146 // We absolutely HAVE to do a hasNext() check or we'll hand back a bogus
147 // pointer/object. Not good!
148 return (patternIterator.hasNext() ? patternIterator.next() : NULL);
151 bool PatternList::contains(const QString & name)
153 QString name2 = name.toLower();
155 // Search our list of available patterns:
156 for(int i=0; i<patterns.size(); i++)
158 Pattern * p = patterns[i];
160 if (p->getFileName() == name2)
168 * Dumps the patterns to stdout.
170 std::ostream & operator<<(std::ostream & os, PatternList & l)
172 os << "Patternlist: \n";
174 for(Pattern * p=l.firstPattern(); p!=NULL; p=l.nextPattern())