]> Shamusworld >> Repos - architektonas/blob - src/base/rs_patternlist.cpp
Fixed thumbnail rendering in LibraryWidget and DXF detection.
[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 // 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 "rs_patternlist.h"
18
19 #include "rs_system.h"
20
21 RS_PatternList * RS_PatternList::uniqueInstance = NULL;
22
23 /**
24  * Default constructor.
25  */
26 RS_PatternList::RS_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*/ RS_PatternList::~RS_PatternList()
34 {
35 }
36
37 /**
38  * @return Instance to the unique pattern list.
39  */
40 /*static*/ RS_PatternList * RS_PatternList::instance()
41 {
42         if (uniqueInstance == NULL)
43                 uniqueInstance = new RS_PatternList();
44
45         return uniqueInstance;
46 }
47
48 /**
49  * Initializes the pattern list by creating empty RS_Pattern
50  * objects, one for each pattern that could be found.
51  */
52 void RS_PatternList::init()
53 {
54     RS_DEBUG->print("RS_PatternList::initPatterns");
55
56     QStringList list = RS_SYSTEM->getPatternList();
57     RS_Pattern * pattern;
58
59         patterns.clear();
60
61     for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
62         {
63         RS_DEBUG->print("pattern: %s:", (*it).toLatin1().data());
64
65         QFileInfo fi(*it);
66         pattern = new RS_Pattern(fi.baseName().toLower());
67         patterns.append(pattern);
68
69         RS_DEBUG->print("base: %s", pattern->getFileName().toLatin1().data());
70     }
71 }
72
73 /**
74  * Removes all patterns in the patternlist.
75  */
76 void RS_PatternList::clearPatterns()
77 {
78     patterns.clear();
79 }
80
81 int RS_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 RS_PatternList::removePattern(RS_Pattern * pattern)
92 {
93         RS_DEBUG->print("RS_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         //    RS_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 RS_Pattern * RS_PatternList::requestPattern(const QString & name)
116 {
117         RS_DEBUG->print("RS_PatternList::requestPattern %s", name.toLatin1().data());
118
119         QString name2 = name.toLower();
120         RS_Pattern * foundPattern = NULL;
121
122         RS_DEBUG->print("name2: %s", name2.toLatin1().data());
123
124         // Search our list of available patterns:
125 //      for(RS_Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
126         for(int i=0; i<patterns.size(); i++)
127         {
128                 RS_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 RS_Pattern * RS_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 RS_Pattern * RS_PatternList::nextPattern()
158 {
159         return patternIterator.next();
160 }
161
162 bool RS_PatternList::contains(const QString & name)
163 {
164         QString name2 = name.toLower();
165
166         // Search our list of available patterns:
167 //      for(RS_Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
168         for(int i=0; i<patterns.size(); i++)
169         {
170                 RS_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, RS_PatternList & l)
183 {
184     os << "Patternlist: \n";
185     for(RS_Pattern * p=l.firstPattern(); p!=NULL; p=l.nextPattern())
186         os << *p << "\n";
187
188     return os;
189 }