1 /***************************************************************************
2 objectnamelist.h - description
4 begin : Mon Feb 18 2002
5 copyright : (C) 2002 by Thomas Kabelmann
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
18 #ifndef OBJECTNAMELIST_H
19 #define OBJECTNAMELIST_H
22 /**@class ObjectNameList
23 *This class provides an interface like a QPtrList, but sorts objects internally
24 *in 27 lists. The objects are sorted alphabetically. List 0 contains all objects
25 *beginning not with a letter. List 1 - 26 contains objects beginning with a letter.
26 *The number of the list is similar to positon of letter in alphabet. (A = 1 .. Z = 26 )
27 *@author Thomas Kabelmann
39 /**Reimplemented from QPtrList for sorting objects in the list. */
40 template <class T
> class SortedList
: public QPtrList
<T
> {
42 int compareItems(QPtrCollection::Item item1
, QPtrCollection::Item item2
) {
43 if ( *((T
*)item1
) == *((T
*)item2
) ) return 0;
44 return ( *((T
*)item1
) < *((T
*)item2
) ) ? -1 : 1;
48 class ObjectNameList
{
57 /**Appends a skyobject to the list.
58 *@param object pointer to the SkyObject to be appended to the list.
59 *@param useLongName if TRUE, use the longname of the object,
60 *rather than the primary name
62 void append(SkyObject
*object
, bool useLongName
=false);
64 /**Select the list which contains objects whose names begin with the
65 *same letter as the argument. The selected list ID is recorded in
66 *an internal variable, so it is persistent.
67 *@note This function is case insensitive.
68 *@note if no string is given, then the first list is selected, and
69 *also a flag is set so that all lists will be traversed by the next()
71 *@return pointer to the first object in the selected list.
72 *@param name the name to use in selecting a list.
74 SkyObjectName
* first(const QString
&name
= QString::null
);
76 /**Returns next object in the currently selected list.
77 *@return pointer to the next object in the current list, or NULL if
78 *the end of the list was reached.
79 *@note if the "all lists" flag is set, then it will traverse all lists
80 *before returning NULL.
82 SkyObjectName
* next();
84 /**@return pointer to the object, if it is in one of the lists;
85 *otherwise return the NULL pointer.
86 *@note This function is case sensitive.
87 *@param name name of object to find.
88 *@return pointer to object with the given name
90 SkyObjectName
* find(const QString
&name
= QString::null
);
92 /**@short remove the named object from the list.
93 *@param name the name of the object to be removed.
94 *@note If the object is not found, then nothing happens.
96 void remove( const QString
&name
= QString::null
);
98 /**Define the language which should be used for constellation names
100 enum Language
{ local
= 0, latin
= 1 };
102 /**Change constellation-name language option.
103 *@param lang the language to use (0=local; 1=latin)
105 void setLanguage( Language lang
);
107 /**Change constellation-name language option.
108 *This function behaves just like the above function;
109 *it differs only in the data type of its argument.
110 *@param lang the language to use (0=local; 1=latin)
112 void setLanguage( bool lang
);
114 /**@return the total count of the number of named objects.
116 uint
count() const { return amount
; }
120 /**Sorts the lists with objects for faster access.
121 *It's needed for find(). first() and find() call this function.
126 /**@return the list index which contains the object whose name matches the argument.
127 *@note this does not return the position within a list of names; it returns the ID
129 *@param name the name of the object whose index is to be found
131 int getIndex( const QString
&name
= QString::null
);
133 /**Two modes are available:
134 *allLists = loop through the whole list if next() is called
135 *oneList = loop through one list if next is called
136 *If oneList is set, just check all objects which start with same letter to save cpu time.
138 enum Mode
{ allLists
, oneList
} mode
;
143 void setMode( Mode m
);
145 /**Hold 2 pointer lists in memory. Number of lists can easily increased for adding more languages.
146 *First index is the language and second index is reserved for alphabetically sorted list.
147 * 0 = all objects beginning with a number
150 SortedList
<SkyObjectName
> list
[2][27];
152 /**Hold status if list is unsorted or not.*/
156 *Constellations has latin names and alloc extra SkyObjectNames which will stored in 2 list.
157 *But second list will not delete objects while clearing it, because most of objects in this list
158 *are in first list too. We just have to delete objects which are not in first list. These objects
159 *will stored in this list.
161 QPtrList
<SkyObjectName
> constellations
;
164 *Which list was accessed last time by first() or next()