]> Shamusworld >> Repos - architektonas/blob - src/base/patternlist.cpp
Fixed hatch dialog, added snap/preview to circle tools.
[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 // JLH  09/13/2010  Fixed next() function to return a valid pointer.
16 //
17
18 #include "patternlist.h"
19
20 #include "system.h"
21
22 PatternList * PatternList::uniqueInstance = NULL;
23
24 /**
25  * Default constructor.
26  */
27 PatternList::PatternList(): patternIterator(patterns)
28 {
29 }
30
31 /*virtual*/ PatternList::~PatternList()
32 {
33         clearPatterns();
34 }
35
36 /**
37  * @return Instance to the unique pattern list.
38  */
39 /*static*/ PatternList * PatternList::instance()
40 {
41         if (uniqueInstance == NULL)
42                 uniqueInstance = new PatternList();
43
44         return uniqueInstance;
45 }
46
47 /**
48  * Initializes the pattern list by creating empty Pattern
49  * objects, one for each pattern that could be found.
50  */
51 void PatternList::init()
52 {
53         DEBUG->print("PatternList::initPatterns");
54         QStringList list = SYSTEM->getPatternList();
55 //      patterns.clear();
56         clearPatterns();
57
58         for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
59         {
60                 DEBUG->print("pattern: %s:", (*it).toAscii().data());
61                 QFileInfo fi(*it);
62                 Pattern * pattern = new Pattern(fi.baseName().toLower());
63                 patterns.append(pattern);
64                 DEBUG->print("base: %s", pattern->getFileName().toAscii().data());
65         }
66 }
67
68 /**
69  * Removes all patterns in the patternlist.
70  */
71 void PatternList::clearPatterns()
72 {
73         patternIterator = patterns;
74
75         while (patternIterator.hasNext())
76                 delete patternIterator.next();
77
78         // Apparently this is still needed as the list will still be populated with
79         // stale pointers...
80         patterns.clear();
81 }
82
83 int PatternList::countPatterns()
84 {
85         return patterns.count();
86 }
87
88 /**
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.
92  */
93 void PatternList::removePattern(Pattern * pattern)
94 {
95         DEBUG->print("PatternList::removePattern()");
96         int i = patterns.indexOf(pattern);
97
98         if (i != -1)
99                 delete patterns.takeAt(i);
100 }
101
102 /**
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.
106  */
107 Pattern * PatternList::requestPattern(const QString & name)
108 {
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());
113
114         // Search our list of available patterns:
115         for(int i=0; i<patterns.size(); i++)
116         {
117                 Pattern * p = patterns[i];
118
119                 if (p->getFileName() == name2)
120                 {
121                         // Make sure this pattern is loaded into memory:
122                         p->loadPattern();
123                         foundPattern = p;
124                         break;
125                 }
126         }
127
128         return foundPattern;
129 }
130
131 //! @return First pattern of the list.
132 Pattern * PatternList::firstPattern()
133 {
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();
138 }
139
140 /**
141  * @return Next pattern from the list after
142  * calling firstPattern() or nextPattern().
143  */
144 Pattern * PatternList::nextPattern()
145 {
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);
149 }
150
151 bool PatternList::contains(const QString & name)
152 {
153         QString name2 = name.toLower();
154
155         // Search our list of available patterns:
156         for(int i=0; i<patterns.size(); i++)
157         {
158                 Pattern * p = patterns[i];
159
160                 if (p->getFileName() == name2)
161                         return true;
162         }
163
164         return false;
165 }
166
167 /**
168  * Dumps the patterns to stdout.
169  */
170 std::ostream & operator<<(std::ostream & os, PatternList & l)
171 {
172         os << "Patternlist: \n";
173
174         for(Pattern * p=l.firstPattern(); p!=NULL; p=l.nextPattern())
175                 os << *p << "\n";
176
177         return os;
178 }