1 /****************************************************************************
2 ** $Id: rs_patternlist.cpp 1938 2004-12-09 23:09:53Z andrew $
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
6 ** This file is part of the qcadlib Library project.
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 ** See http://www.ribbonsoft.com for further details.
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
25 **********************************************************************/
27 #include "rs_patternlist.h"
29 #include "rs_system.h"
31 RS_PatternList * RS_PatternList::uniqueInstance = NULL;
34 * Default constructor.
36 RS_PatternList::RS_PatternList(): patternIterator(patterns)
38 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
39 // patterns.setAutoDelete(true);
40 //patternListListeners.setAutoDelete(false);
43 /*virtual*/ RS_PatternList::~RS_PatternList()
48 * @return Instance to the unique pattern list.
50 /*static*/ RS_PatternList * RS_PatternList::instance()
52 if (uniqueInstance == NULL)
53 uniqueInstance = new RS_PatternList();
55 return uniqueInstance;
59 * Initializes the pattern list by creating empty RS_Pattern
60 * objects, one for each pattern that could be found.
62 void RS_PatternList::init()
64 RS_DEBUG->print("RS_PatternList::initPatterns");
66 QStringList list = RS_SYSTEM->getPatternList();
71 for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
73 RS_DEBUG->print("pattern: %s:", (*it).toLatin1().data());
76 pattern = new RS_Pattern(fi.baseName().toLower());
77 patterns.append(pattern);
79 RS_DEBUG->print("base: %s", pattern->getFileName().toLatin1().data());
84 * Removes all patterns in the patternlist.
86 void RS_PatternList::clearPatterns()
91 int RS_PatternList::countPatterns()
93 return patterns.count();
97 * Removes a pattern from the list.
98 * Listeners are notified after the pattern was removed from
99 * the list but before it gets deleted.
101 void RS_PatternList::removePattern(RS_Pattern * pattern)
103 RS_DEBUG->print("RS_PatternList::removePattern()");
105 // here the pattern is removed from the list but not deleted
106 // patterns.remove(pattern);
107 //Apparently we need to delete this shit here because of missing AutoDelete
108 //function when going from Qt3->4
109 int i = patterns.indexOf(pattern);
112 delete patterns.takeAt(i);
114 //for (uint i=0; i<patternListListeners.count(); ++i) {
115 // RS_PatternListListener* l = patternListListeners.at(i);
116 // l->patternRemoved(pattern);
121 * @return Pointer to the pattern with the given name or
122 * \p NULL if no such pattern was found. The pattern will be loaded into
123 * memory if it's not already.
125 RS_Pattern * RS_PatternList::requestPattern(const QString & name)
127 RS_DEBUG->print("RS_PatternList::requestPattern %s", name.toLatin1().data());
129 QString name2 = name.toLower();
130 RS_Pattern * foundPattern = NULL;
132 RS_DEBUG->print("name2: %s", name2.toLatin1().data());
134 // Search our list of available patterns:
135 // for(RS_Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
136 for(int i=0; i<patterns.size(); i++)
138 RS_Pattern * p = patterns[i];
140 if (p->getFileName() == name2)
142 // Make sure this pattern is loaded into memory:
149 //if (foundPattern==NULL && name!="standard") {
150 // foundPattern = requestPattern("standard");
156 //! @return First pattern of the list.
157 RS_Pattern * RS_PatternList::firstPattern()
159 patternIterator.toFront();
160 return patternIterator.next();
164 * @return Next pattern from the list after
165 * calling firstPattern() or nextPattern().
167 RS_Pattern * RS_PatternList::nextPattern()
169 return patternIterator.next();
172 bool RS_PatternList::contains(const QString & name)
174 QString name2 = name.toLower();
176 // Search our list of available patterns:
177 // for(RS_Pattern * p=patterns.first(); p!=NULL; p=patterns.next())
178 for(int i=0; i<patterns.size(); i++)
180 RS_Pattern * p = patterns[i];
182 if (p->getFileName() == name2)
190 * Dumps the patterns to stdout.
192 std::ostream & operator<<(std::ostream & os, RS_PatternList & l)
194 os << "Patternlist: \n";
195 for(RS_Pattern * p=l.firstPattern(); p!=NULL; p=l.nextPattern())