• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

objectnamelist.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           objectnamelist.cpp  -  description
00003                              -------------------
00004     begin                : Mon Feb 18 2002
00005     copyright          : (C) 2002 by Thomas Kabelmann
00006     email                : tk78@gmx.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "objectnamelist.h"
00019 #include "skyobjectname.h"
00020 #include "skyobject.h"
00021 #include "starobject.h"
00022 
00023 #include <qstring.h>
00024 #include <kdebug.h>
00025 
00026 ObjectNameList::ObjectNameList() {
00027     amount = 0;
00028     language = latin;
00029     mode = allLists;
00030 
00031     // delete just objects of local list
00032     for (int i= 0; i< 27; i++) {
00033         list[local][i].setAutoDelete(true);
00034         unsorted[i] = false;
00035     }
00036 
00037     constellations.setAutoDelete(true);
00038 }
00039 
00040 ObjectNameList::~ObjectNameList(){
00041 }
00042 
00043 void ObjectNameList::setLanguage( Language lang ) {
00044     language = lang;
00045 }
00046 
00047 void ObjectNameList::setLanguage( bool lang ) {
00048     language = ( Language ) lang;
00049 }
00050 
00051 void ObjectNameList::setMode( Mode m ) {
00052     mode = m;
00053 }
00054 
00055 void ObjectNameList::append( SkyObject *object, bool useLongName ) {
00056     amount++;
00057     // create name string and init with longname if forced by parameter else default name
00058     QString name = ( useLongName ) ? object->longname() : object->name();
00059 
00060     //if star's name is it's genetive name, make sure we don't use the Greek charcter here
00061     if ( object->type() == 0 && name == ((StarObject*)object)->gname() )
00062         name = ((StarObject*)object)->gname( false );
00063     
00064     // create string with translated name
00065     QString iName;
00066 
00067     if ( object->type() == -1 ) {  // constellation
00068         iName = i18n( "Constellation name (optional)", name.local8Bit().data() );
00069     }
00070     else {  // all other types
00071         iName = i18n( name.local8Bit() );
00072     }
00073 
00074     // create SkyObjectName with translated name
00075     SkyObjectName *soName = new SkyObjectName( iName, object );
00076     // append in localized list
00077     currentIndex = getIndex( name );
00078     list[local] [currentIndex].append(soName);
00079 
00080     // type == -1 -> constellation
00081     if (object->type() == -1) {
00082         // get latin name (default name)
00083         iName = name;
00084         // create new SkyObject with localized name
00085         soName = new SkyObjectName(iName, object);
00086         // to delete these objects store them in separate list
00087         constellations.append(soName);
00088     }
00089 
00090     // append in latin list
00091     currentIndex = getIndex(name);
00092     list[latin][currentIndex].append(soName);
00093     // set list unsorted
00094     unsorted[currentIndex] = true;
00095 }
00096 
00097 SkyObjectName* ObjectNameList::first( const QString &name ) {
00098     sort();
00099     SkyObjectName *soName = 0;
00100     // set mode: string is empty set mode to all lists
00101     name.isEmpty() ? setMode( allLists ) : setMode( oneList );
00102 
00103     // start with first list in array
00104     if ( mode == allLists ) {
00105         currentIndex = 0;
00106     } else {
00107         // start with list which contains the first letter
00108         currentIndex = getIndex( name );
00109     }
00110 
00111     soName = list[language][currentIndex].first();
00112 
00113     //It's possible that there is no object that belongs to currentIndex
00114     //If not, and mode==allLists, try the next index
00115     while ( !soName && mode==allLists && currentIndex < 26 ) {
00116         currentIndex++;  // loop through the array
00117         soName = list[language][currentIndex].first();
00118   }
00119 
00120     return soName;
00121 }
00122 
00123 SkyObjectName* ObjectNameList::next() {
00124     SkyObjectName *soName = 0;
00125     // get next SkyObjectName object
00126     soName = list[ language ] [ currentIndex ].next();
00127 
00128     // if all lists must checked and SkyObjectName is NULL
00129     // check next available list in array and set to first element in list
00130     // if currentIndex == 26 -> last index is reached
00131     if ( mode==allLists && soName==0 && currentIndex<26 ) {
00132         do {
00133             currentIndex++;  // loop through the array
00134             soName = list[ language ] [ currentIndex ].first();
00135         } while ( currentIndex<26 && soName==0 );  // break if currentIndex == 27 or soName is found
00136     }
00137 
00138     return soName;
00139 }
00140 
00141 int ObjectNameList::getIndex( const QString &name ) {
00142     //  default index is 0 if object name starts with a number
00143     int index = 0;
00144 
00145     // if object name starts with a letter, so get index number between 1 and 26
00146     if ( !name.isEmpty() ) {
00147         QChar firstLetter = name[0];
00148         if ( firstLetter ) {
00149             if ( firstLetter.isLetter() ) {
00150                 const unsigned char letter = (unsigned char) firstLetter.lower();
00151                 index = letter % 96;  // a == 97 in ASCII code => 97 % 96 = 1
00152             }
00153 
00158             if (index > 26) {
00159                 switch (index) {
00160                     case 41 : index = 5; break; // �= e
00161                     case 54 : index = 15; break;    // �= o
00162                     default : index = 0;                        // all other letters
00163                 }
00164                 kdDebug() << k_funcinfo << "Object: " << name << " starts with non ASCII letter. Put it in list #" << index << endl;
00165             }
00166         }
00167     }
00168 
00169     return index;
00170 }
00171 
00172 void ObjectNameList::sort() {
00173     for (int i=0; i<27; ++i) {
00174         if (unsorted[i] == true) {
00175             unsorted[i] = false;
00176             list[latin][i].sort();
00177             list[local][i].sort();
00178         }
00179     }
00180 }
00181 
00182 void ObjectNameList::remove ( const QString &name ) {
00183     setMode(oneList);
00184     int index = getIndex(name);
00185     SortedList <SkyObjectName> *l = &(list[language][index]);
00186 
00187     SkyObjectName *son = find( name );
00188     if ( son ) l->remove( son );
00189 }
00190 
00191 SkyObjectName* ObjectNameList::find(const QString &name) {
00192     sort();
00193     if (name.isNull()) return 0;
00194     // find works only in one list and not in all lists
00195     setMode(oneList);
00196 
00197     // items are stored translated (JH: Why?  this whole class is confusing...)
00198     QString translatedName = i18n(name.utf8());
00199 
00200     int index = getIndex(name);
00201 
00202     // first item
00203     int lower = 0;
00204     SortedList <SkyObjectName> *l = &(list[language][index]);
00205     // last item
00206     int upper = l->count() - 1;
00207     // break if list is empty
00208     if (upper == -1) return 0;
00209 
00210     int next;
00211 
00212     // it's the "binary search" algorithm
00213     SkyObjectName *o;
00214     while (upper >= lower) {
00215         next = (lower + upper) / 2;
00216         o = l->at(next);
00217         if (translatedName == o->text()) { return o; }
00218         if (translatedName < o->text())
00219             upper = next - 1;
00220         else
00221             lower = next + 1;
00222     }
00223     return 0;
00224 }

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal