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
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/27/2010 Added this text. :-)
23 //[DONE] #warning "!!! NEED TO FIX ITERATORS IN THIS CLASS !!!"
25 FontList * FontList::uniqueInstance = NULL;
28 * Default constructor.
30 FontList::FontList(): fontIterator(fonts)
33 //#warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
34 // fonts.setAutoDelete(true);
35 //fontListListeners.setAutoDelete(false);
38 /*static*/ FontList * FontList::instance()
40 if (uniqueInstance == NULL)
41 uniqueInstance = new FontList();
43 return uniqueInstance;
52 * Initializes the font list by creating empty Font
53 * objects, one for each font that could be found.
57 DEBUG->print("FontList::initFonts");
59 QStringList list = SYSTEM->getFontList();
60 // Q3Dict<char> added; //used to remeber added fonts (avoid duplication)
61 QMultiHash<QString, char *> added; //used to remeber added fonts (avoid duplication)
63 for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
65 DEBUG->print("font: %s:", (*it).toLatin1().data());
66 //printf("FontList::init(): font: %s:\n", (*it).toLatin1().data());
70 // if (!added[fi.baseName()])
71 if (added.value(fi.baseName()) == 0)
73 Font * font = new Font(fi.baseName());
75 added.insert(fi.baseName(), (char *)1);
78 DEBUG->print("base: %s", fi.baseName().toLatin1().data());
83 * Removes all fonts in the fontlist.
85 void FontList::clearFonts()
88 while (!fonts.isEmpty())
89 delete fonts.takeFirst();
92 int FontList::countFonts()
98 * Removes a font from the list.
99 * Listeners are notified after the font was removed from
100 * the list but before it gets deleted.
102 void FontList::removeFont(Font * font)
104 DEBUG->print("FontList::removeFont()");
106 // here the font is removed from the list but not deleted
107 // fonts.remove(font);
108 // Here we have to delete this ourselves, because there is no AutoDelete
110 int i = fonts.indexOf(font);
113 delete fonts.takeAt(i);
115 //for (uint i=0; i<fontListListeners.count(); ++i) {
116 // FontListListener* l = fontListListeners.at(i);
117 // l->fontRemoved(font);
122 * @return Pointer to the font with the given name or
123 * \p NULL if no such font was found. The font will be loaded into
124 * memory if it's not already.
126 Font * FontList::requestFont(const QString & name)
128 DEBUG->print("FontList::requestFont %s", name.toLatin1().data());
130 QString name2 = name.toLower();
131 Font * foundFont = NULL;
133 // QCad 1 compatibility:
134 if (name2.contains('#') && name2.contains('_'))
135 // name2 = name2.left(name2.find('_'));
136 name2 = name2.left(name2.indexOf('_'));
137 else if (name2.contains('#'))
138 // name2 = name2.left(name2.find('#'));
139 name2 = name2.left(name2.indexOf('#'));
141 DEBUG->print("name2: %s", name2.toLatin1().data());
143 // Search our list of available fonts:
144 // for(Font * f=fonts.first(); f!=NULL; f=fonts.next())
145 for(int i=0; i<fonts.size(); i++)
149 if (f->getFileName() == name2)
151 // Make sure this font is loaded into memory:
158 if (foundFont == NULL && name != "standard")
159 foundFont = requestFont("standard");
164 //! @return First font of the list.
165 Font * FontList::firstFont()
167 // return fonts.first();
168 // fontIterator.toFront();
169 // return fontIterator.next();
171 fontIterator = fonts;
172 return (fontIterator.hasNext() ? fontIterator.next() : NULL);
176 * @return Next font from the list after
177 * calling firstFont() or nextFont().
179 Font * FontList::nextFont()
181 // return fonts.next();
182 // return fontIterator.next();
183 return (fontIterator.hasNext() ? fontIterator.next() : NULL);
187 * Dumps the fonts to stdout.
189 std::ostream & operator<<(std::ostream & os, FontList & l)
191 os << "Fontlist: \n";
193 for(Font * f=l.firstFont(); f!=NULL; f=l.nextFont())