]> Shamusworld >> Repos - architektonas/blob - src/base/patternlist.cpp
Removed unnecessary RS_ prefix from classes and whatnot.
[architektonas] / src / base / patternlist.cpp
1 // patternlist.cpp
2 //
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
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/01/2010  Added this text. :-)
15 //
16
17 #include "patternlist.h"
18
19 #include "system.h"
20
21 PatternList * PatternList::uniqueInstance = NULL;
22
23 /**
24  * Default constructor.
25  */
26 PatternList::PatternList(): patternIterator(patterns)
27 {
28 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
29 //    patterns.setAutoDelete(true);
30     //patternListListeners.setAutoDelete(false);
31 }
32
33 /*virtual*/ PatternList::~PatternList()
34 {
35 }
36
37 /**
38  * @return Instance to the unique pattern list.
39  */
40 /*static*/ PatternList * PatternList::instance()
41 {
42         if (uniqueInstance == NULL)
43                 uniqueInstance = new PatternList();
44
45         return uniqueInstance;
46 }
47
48 /**
49  * Initializes the pattern list by creating empty Pattern
50  * objects, one for each pattern that could be found.
51  */
52 void PatternList::init()
53 {
54     DEBUG->print("PatternList::initPatterns");
55
56     QStringList list = SYSTEM->getPatternList();
57     Pattern * pattern;
58
59         patterns.clear();
60
61     for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
62         {
63         DEBUG->print("pattern: %s:", (*it).toLatin1().data());
64
65         QFileInfo fi(*it);
66         pattern = new Pattern(fi.baseName().toLower());
67         patterns.append(pattern);
68
69         DEBUG->print("base: %s", pattern->getFileName().toLatin1().data());
70     }
71 }
72
73 /**
74  * Removes all patterns in the patternlist.
75  */
76 void PatternList::clearPatterns()
77 {
78     patterns.clear();
79 }
80
81 int PatternList::countPatterns()
82 {
83         return patterns.count();
84 }
85
86 /**
87  * Removes a pattern from the list.
88  * Listeners are notified after the pattern was removed from
89  * the list but before it gets deleted.
90  */
91 void PatternList::removePattern(Pattern * pattern)
92 {
93         DEBUG->print("PatternList::removePattern()");
94
95         // here the pattern is removed from the list but not deleted
96 //      patterns.remove(pattern);
97 //Apparently we need to delete this shit here because of missing AutoDelete
98 //function when going from Qt3->4
99         int i = patterns.indexOf(pattern);
100
101         if (i != -1)
102                 delete patterns.takeAt(i);
103
104         //for (uint i=0; i<patternListListeners.count(); ++i) {
105         //    PatternListListener* l = patternListListeners.at(i);
106         //    l->patternRemoved(pattern);
107         //}
108 }
109
110 /**
111  * @return Pointer to the pattern with the given name or
112  * \p NULL if no such pattern was found. The pattern will be loaded into
113  * memory if it's not already.
114  */
115 Pattern * PatternList::requestPattern(const QString & name)
116 {
117         DEBUG->print("PatternList::requestPattern %s", name.toLatin1().data());
118
119         QString name2 = name.toLower();
120         Pattern * foundPattern = NULL;
121
122         DEBUG->print("name2: %s", name2.toLatin1().data());
123
124         // Search our list of available patterns:
125 //      for(Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
126         for(int i=0; i<patterns.size(); i++)
127         {
128                 Pattern * p = patterns[i];
129
130                 if (p->getFileName() == name2)
131                 {
132                         // Make sure this pattern is loaded into memory:
133                         p->loadPattern();
134                         foundPattern = p;
135                         break;
136                 }
137         }
138
139         //if (foundPattern==NULL && name!="standard") {
140         //    foundPattern = requestPattern("standard");
141         //}
142
143         return foundPattern;
144 }
145
146 //! @return First pattern of the list.
147 Pattern * PatternList::firstPattern()
148 {
149         patternIterator.toFront();
150         return patternIterator.next();
151 }
152
153 /**
154  * @return Next pattern from the list after
155  * calling firstPattern() or nextPattern().
156  */
157 Pattern * PatternList::nextPattern()
158 {
159         return patternIterator.next();
160 }
161
162 bool PatternList::contains(const QString & name)
163 {
164         QString name2 = name.toLower();
165
166         // Search our list of available patterns:
167 //      for(Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
168         for(int i=0; i<patterns.size(); i++)
169         {
170                 Pattern * p = patterns[i];
171
172                 if (p->getFileName() == name2)
173                         return true;
174         }
175
176         return false;
177 }
178
179 /**
180  * Dumps the patterns to stdout.
181  */
182 std::ostream & operator<<(std::ostream & os, PatternList & l)
183 {
184     os << "Patternlist: \n";
185     for(Pattern * p=l.firstPattern(); p!=NULL; p=l.nextPattern())
186         os << *p << "\n";
187
188     return os;
189 }