]> Shamusworld >> Repos - architektonas/blob - src/base/rs_patternlist.cpp
8095a723fac98cf69eb569e1f1410417f6b535d3
[architektonas] / src / base / rs_patternlist.cpp
1 // rs_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 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/01/2010  Added this text. :-)
13 //
14
15 #include "rs_patternlist.h"
16
17 #include "rs_system.h"
18
19 RS_PatternList * RS_PatternList::uniqueInstance = NULL;
20
21 /**
22  * Default constructor.
23  */
24 RS_PatternList::RS_PatternList(): patternIterator(patterns)
25 {
26 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
27 //    patterns.setAutoDelete(true);
28     //patternListListeners.setAutoDelete(false);
29 }
30
31 /*virtual*/ RS_PatternList::~RS_PatternList()
32 {
33 }
34
35 /**
36  * @return Instance to the unique pattern list.
37  */
38 /*static*/ RS_PatternList * RS_PatternList::instance()
39 {
40         if (uniqueInstance == NULL)
41                 uniqueInstance = new RS_PatternList();
42
43         return uniqueInstance;
44 }
45
46 /**
47  * Initializes the pattern list by creating empty RS_Pattern
48  * objects, one for each pattern that could be found.
49  */
50 void RS_PatternList::init()
51 {
52     RS_DEBUG->print("RS_PatternList::initPatterns");
53
54     QStringList list = RS_SYSTEM->getPatternList();
55     RS_Pattern * pattern;
56
57         patterns.clear();
58
59     for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
60         {
61         RS_DEBUG->print("pattern: %s:", (*it).toLatin1().data());
62
63         QFileInfo fi(*it);
64         pattern = new RS_Pattern(fi.baseName().toLower());
65         patterns.append(pattern);
66
67         RS_DEBUG->print("base: %s", pattern->getFileName().toLatin1().data());
68     }
69 }
70
71 /**
72  * Removes all patterns in the patternlist.
73  */
74 void RS_PatternList::clearPatterns()
75 {
76     patterns.clear();
77 }
78
79 int RS_PatternList::countPatterns()
80 {
81         return patterns.count();
82 }
83
84 /**
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.
88  */
89 void RS_PatternList::removePattern(RS_Pattern * pattern)
90 {
91         RS_DEBUG->print("RS_PatternList::removePattern()");
92
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);
98
99         if (i != -1)
100                 delete patterns.takeAt(i);
101
102         //for (uint i=0; i<patternListListeners.count(); ++i) {
103         //    RS_PatternListListener* l = patternListListeners.at(i);
104         //    l->patternRemoved(pattern);
105         //}
106 }
107
108 /**
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.
112  */
113 RS_Pattern * RS_PatternList::requestPattern(const QString & name)
114 {
115         RS_DEBUG->print("RS_PatternList::requestPattern %s", name.toLatin1().data());
116
117         QString name2 = name.toLower();
118         RS_Pattern * foundPattern = NULL;
119
120         RS_DEBUG->print("name2: %s", name2.toLatin1().data());
121
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++)
125         {
126                 RS_Pattern * p = patterns[i];
127
128                 if (p->getFileName() == name2)
129                 {
130                         // Make sure this pattern is loaded into memory:
131                         p->loadPattern();
132                         foundPattern = p;
133                         break;
134                 }
135         }
136
137         //if (foundPattern==NULL && name!="standard") {
138         //    foundPattern = requestPattern("standard");
139         //}
140
141         return foundPattern;
142 }
143
144 //! @return First pattern of the list.
145 RS_Pattern * RS_PatternList::firstPattern()
146 {
147         patternIterator.toFront();
148         return patternIterator.next();
149 }
150
151 /**
152  * @return Next pattern from the list after
153  * calling firstPattern() or nextPattern().
154  */
155 RS_Pattern * RS_PatternList::nextPattern()
156 {
157         return patternIterator.next();
158 }
159
160 bool RS_PatternList::contains(const QString & name)
161 {
162         QString name2 = name.toLower();
163
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++)
167         {
168                 RS_Pattern * p = patterns[i];
169
170                 if (p->getFileName() == name2)
171                         return true;
172         }
173
174         return false;
175 }
176
177 /**
178  * Dumps the patterns to stdout.
179  */
180 std::ostream & operator<<(std::ostream & os, RS_PatternList & l)
181 {
182     os << "Patternlist: \n";
183     for(RS_Pattern * p=l.firstPattern(); p!=NULL; p=l.nextPattern())
184         os << *p << "\n";
185
186     return os;
187 }