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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 06/01/2010 Added this text. :-)
15 #include "rs_patternlist.h"
17 #include "rs_system.h"
19 RS_PatternList * RS_PatternList::uniqueInstance = NULL;
22 * Default constructor.
24 RS_PatternList::RS_PatternList(): patternIterator(patterns)
26 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
27 // patterns.setAutoDelete(true);
28 //patternListListeners.setAutoDelete(false);
31 /*virtual*/ RS_PatternList::~RS_PatternList()
36 * @return Instance to the unique pattern list.
38 /*static*/ RS_PatternList * RS_PatternList::instance()
40 if (uniqueInstance == NULL)
41 uniqueInstance = new RS_PatternList();
43 return uniqueInstance;
47 * Initializes the pattern list by creating empty RS_Pattern
48 * objects, one for each pattern that could be found.
50 void RS_PatternList::init()
52 RS_DEBUG->print("RS_PatternList::initPatterns");
54 QStringList list = RS_SYSTEM->getPatternList();
59 for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
61 RS_DEBUG->print("pattern: %s:", (*it).toLatin1().data());
64 pattern = new RS_Pattern(fi.baseName().toLower());
65 patterns.append(pattern);
67 RS_DEBUG->print("base: %s", pattern->getFileName().toLatin1().data());
72 * Removes all patterns in the patternlist.
74 void RS_PatternList::clearPatterns()
79 int RS_PatternList::countPatterns()
81 return patterns.count();
85 * Removes a pattern from the list.
86 * Listeners are notified after the pattern was removed from
87 * the list but before it gets deleted.
89 void RS_PatternList::removePattern(RS_Pattern * pattern)
91 RS_DEBUG->print("RS_PatternList::removePattern()");
93 // here the pattern is removed from the list but not deleted
94 // patterns.remove(pattern);
95 //Apparently we need to delete this shit here because of missing AutoDelete
96 //function when going from Qt3->4
97 int i = patterns.indexOf(pattern);
100 delete patterns.takeAt(i);
102 //for (uint i=0; i<patternListListeners.count(); ++i) {
103 // RS_PatternListListener* l = patternListListeners.at(i);
104 // l->patternRemoved(pattern);
109 * @return Pointer to the pattern with the given name or
110 * \p NULL if no such pattern was found. The pattern will be loaded into
111 * memory if it's not already.
113 RS_Pattern * RS_PatternList::requestPattern(const QString & name)
115 RS_DEBUG->print("RS_PatternList::requestPattern %s", name.toLatin1().data());
117 QString name2 = name.toLower();
118 RS_Pattern * foundPattern = NULL;
120 RS_DEBUG->print("name2: %s", name2.toLatin1().data());
122 // Search our list of available patterns:
123 // for(RS_Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
124 for(int i=0; i<patterns.size(); i++)
126 RS_Pattern * p = patterns[i];
128 if (p->getFileName() == name2)
130 // Make sure this pattern is loaded into memory:
137 //if (foundPattern==NULL && name!="standard") {
138 // foundPattern = requestPattern("standard");
144 //! @return First pattern of the list.
145 RS_Pattern * RS_PatternList::firstPattern()
147 patternIterator.toFront();
148 return patternIterator.next();
152 * @return Next pattern from the list after
153 * calling firstPattern() or nextPattern().
155 RS_Pattern * RS_PatternList::nextPattern()
157 return patternIterator.next();
160 bool RS_PatternList::contains(const QString & name)
162 QString name2 = name.toLower();
164 // Search our list of available patterns:
165 // for(RS_Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
166 for(int i=0; i<patterns.size(); i++)
168 RS_Pattern * p = patterns[i];
170 if (p->getFileName() == name2)
178 * Dumps the patterns to stdout.
180 std::ostream & operator<<(std::ostream & os, RS_PatternList & l)
182 os << "Patternlist: \n";
183 for(RS_Pattern * p=l.firstPattern(); p!=NULL; p=l.nextPattern())