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

kstars

colorscheme.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           colorscheme.cpp  -  description
00003                              -------------------
00004     begin                : Wed May 8 2002
00005     copyright            : (C) 2002 by Jason Harris
00006     email                : kstars@30doradus.org
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 <qfile.h>
00019 #include <kconfig.h>
00020 #include <kdebug.h>
00021 #include <klocale.h>
00022 #include <kmessagebox.h>
00023 #include <kstandarddirs.h>
00024 
00025 #include "ksutils.h"
00026 #include "colorscheme.h"
00027 
00028 typedef QStringList::const_iterator SL_it;
00029 
00030 ColorScheme::ColorScheme() : FileName() {
00031     //Each color has two names associated with it.  The KeyName is its
00032     //identification in the QMap, the *.colors file, and the config file.
00033     //The Name is what appears in the ViewOpsDialog ListBox.
00034     //In addition, we define default RGB strings for each item.
00035     //To add another color to the Palette, just add an entry for KeyName,
00036     //Name and Default here.
00037     
00038     KeyName.append( "SkyColor" );
00039     Name.append( i18n( "Sky" ) );
00040     Default.append( "#002" );
00041     KeyName.append( "MessColor" );
00042     Name.append( i18n( "Messier Object" ) );
00043     Default.append( "#0F0" );
00044     KeyName.append( "NGCColor" );
00045     Name.append( i18n( "New General Catalog object", "NGC Object" ) );
00046     Default.append( "#066" );
00047     KeyName.append( "ICColor" );
00048     Name.append( i18n( "Index Catalog object", "IC Object" ) );
00049     Default.append( "#439" );
00050     KeyName.append( "HSTColor" );
00051     Name.append( i18n( "Object with extra attached URLs", "Object w/ Links" ) );
00052     Default.append( "#A00" );
00053     KeyName.append( "SNameColor" );
00054     Name.append( i18n( "Star Name" ) );
00055     Default.append( "#7AA" );
00056     KeyName.append( "PNameColor" );
00057     Name.append( i18n( "Planet Name" ) );
00058     Default.append( "#A77" );
00059     KeyName.append( "CNameColor" );
00060     Name.append( i18n( "Constellation Name", "Constell. Name" ) );
00061     Default.append( "#AA7" );
00062     KeyName.append( "CLineColor" );
00063     Name.append( i18n( "Constellation Line", "Constell. Line" ) );
00064     Default.append( "#555" );
00065     KeyName.append( "CBoundColor" );
00066     Name.append( i18n( "Constellation Boundary", "Constell. Boundary" ) );
00067     Default.append( "#222" );
00068     KeyName.append( "MWColor" );
00069     Name.append( i18n( "refers to the band of stars in the sky due to the Galactic plane", "Milky Way" ) );
00070     Default.append( "#123" );
00071     KeyName.append( "EqColor" );
00072     Name.append( i18n( "Equator" ) );
00073     Default.append( "#FFF" );
00074     KeyName.append( "EclColor" );
00075     Name.append( i18n( "Ecliptic" ) );
00076     Default.append( "#663" );
00077     KeyName.append( "HorzColor" );
00078     Name.append( i18n( "Horizon" ) );
00079     Default.append( "#5A3" );
00080     KeyName.append( "CompassColor" );
00081     Name.append( i18n( "Compass Labels" ) );
00082     Default.append( "#002" );
00083     KeyName.append( "GridColor" );
00084     Name.append( i18n( "Coordinate Grid" ) );
00085     Default.append( "#456" );
00086     KeyName.append( "BoxTextColor" );
00087     Name.append( i18n( "Info Box Text" ) );
00088     Default.append( "#FFF" );
00089     KeyName.append( "BoxGrabColor" );
00090     Name.append( i18n( "Info Box Selected" ) );
00091     Default.append( "#F00" );
00092     KeyName.append( "BoxBGColor" );
00093     Name.append( i18n( "Info Box Background" ) );
00094     Default.append( "#000" );
00095     KeyName.append( "TargetColor" );
00096     Name.append( i18n( "Target Indicator" ) );
00097     Default.append( "#8B8" );
00098     KeyName.append( "UserLabelColor" );
00099     Name.append( i18n( "User Labels" ) );
00100     Default.append( "#FFF" );
00101     KeyName.append( "PlanetTrailColor" );
00102     Name.append( i18n( "Planet Trails" ) );
00103     Default.append( "#963" );
00104     KeyName.append( "AngularRuler" );
00105     Name.append( i18n( "Angular Distance Ruler" ) );
00106     Default.append( "#FFF" );
00107     KeyName.append( "ObsListColor" );
00108     Name.append( i18n( "Observing List Label" ) );
00109     Default.append( "#F00" );
00110 
00111     //Set the default colors in the Palette.
00112     for( uint i=0; i<KeyName.count(); ++i ) {
00113         setColor( KeyName[i], Default[i] );
00114     }
00115     
00116     //Default values for integer variables:
00117     StarColorMode = 0;
00118     StarColorIntensity = 4;
00119 }
00120 
00121 ColorScheme::ColorScheme( const ColorScheme &cs ) {
00122     KeyName = cs.KeyName;
00123     Name = cs.Name;
00124     Default = cs.Default;
00125     StarColorMode = cs.StarColorMode;
00126     StarColorIntensity = cs.StarColorIntensity;
00127     Palette = cs.Palette;
00128     FileName = cs.FileName;
00129 }
00130 
00131 ColorScheme::~ColorScheme(){
00132 }
00133 
00134 void ColorScheme::copy( const ColorScheme &cs ) {
00135     KeyName = cs.KeyName;
00136     Name = cs.Name;
00137     Default = cs.Default;
00138     StarColorMode = cs.StarColorMode;
00139     StarColorIntensity = cs.StarColorIntensity;
00140     Palette = cs.Palette;
00141     FileName = cs.FileName;
00142 }
00143 
00144 QString ColorScheme::colorNamed( const QString &name ) const {
00145     //QString color( Palette[ name ] );
00146     if ( ! hasColorNamed( name ) ) {
00147         kdWarning() << i18n( "No color named \"%1\" found in color scheme." ).arg( name ) << endl;
00148         //color = "#FFFFFF"; //set to white if no color found
00149         return "#FFFFFF";
00150     }
00151 
00152     return Palette[ name ];
00153 }
00154 
00155 QString ColorScheme::colorAt( int i ) const {
00156     SL_it it = KeyName.at(i);
00157     return Palette[ QString(*it) ];
00158 }
00159 
00160 QString ColorScheme::nameAt( int i ) const {
00161     SL_it it = Name.at(i);
00162     return QString(*it);
00163 }
00164 
00165 QString ColorScheme::keyAt( int i ) const {
00166     SL_it it = KeyName.at(i);
00167     return QString(*it);
00168 }
00169 
00170 QString ColorScheme::nameFromKey( const QString &key ) const {
00171     return nameAt( KeyName.findIndex( key ) );
00172 }
00173 
00174 void ColorScheme::setColor( const QString &key, const QString &color ) {
00175     //We can blindly insert() the new value; if the key exists, the old value is replaced
00176     Palette.insert( key, color );
00177 }
00178 
00179 bool ColorScheme::load( const QString &filename ) {
00180     QFile file;
00181     int inew(0),iold(0);
00182 
00183     if ( !KSUtils::openDataFile( file, filename ) ) 
00184         return false;
00185 
00186     QTextStream stream( &file );
00187     QString line;
00188 
00189     //first line is the star-color mode and star color intensity
00190   line = stream.readLine();
00191     bool ok(false);
00192     int newmode = line.left(1).toInt( &ok );
00193     if ( ok ) setStarColorMode( newmode );
00194     if ( line.contains(':') ) {
00195         int newintens = line.mid( line.find(':')+1, 2 ).toInt( &ok );
00196         if ( ok ) setStarColorIntensity( newintens );
00197     }
00198 
00199 //More flexible method for reading in color values.  Any order is acceptable, and
00200 //missing entries are ignored.
00201     while ( !stream.eof() ) {
00202         line = stream.readLine();
00203 
00204         if ( line.contains(':')==1 ) { //the new color preset format contains a ":" in each line, followed by the name of the color
00205       ++inew;
00206             if ( iold ) return false; //we read at least one line without a colon...file is corrupted.
00207 
00208 //If this line has a valid Key, set the color.
00209             QString tkey = line.mid( line.find(':')+1 ).stripWhiteSpace();
00210             QString tname = line.left( line.find(':')-1 );
00211 
00212             if ( KeyName.contains( tkey ) ) {
00213                 setColor( tkey, tname );
00214             } else { //attempt to translate from old color ID
00215                 QString k( line.mid( 5 ).stripWhiteSpace() + "Color" );
00216                 if ( KeyName.contains( k ) ) {
00217                     setColor( k, tname );
00218                 } else {
00219                     kdWarning() << "Could not use the key \"" << tkey <<
00220                             "\" from the color scheme file \"" << filename << "\".  I also tried \"" <<
00221                             k << "\"." << endl;
00222                 }
00223             }
00224 
00225         } else { // no ':' seen in the line, so we must assume the old format
00226             ++iold;
00227             if ( inew ) return false; //a previous line had a colon, this line doesn't.  File is corrupted.
00228 
00229             //Assuming the old *.colors format.  Loop through the KeyName list,
00230             //and assign each color.  Note that order matters here, but only here
00231             //(so if you don't use the old format, then order doesn't ever matter)
00232             QStringList::Iterator it = KeyName.begin();
00233             QStringList::Iterator it_end = KeyName.end();
00234             for ( ; it != it_end; ++it )
00235                 setColor( QString(*it), line.left( 7 ) );
00236         }
00237     }
00238 
00239     FileName = filename;
00240     return true;
00241 }
00242 
00243 bool ColorScheme::save( const QString &name ) {
00244     QFile file;
00245 
00246     //Construct a file name from the scheme name.  Make lowercase, replace spaces with "-",
00247     //and append ".colors".
00248     QString filename = name.lower().stripWhiteSpace();
00249     if ( !filename.isEmpty() ) {
00250         for( unsigned int i=0; i<filename.length(); ++i)
00251             if ( filename.at(i)==' ' ) filename.replace( i, 1, "-" );
00252 
00253         filename = filename.append( ".colors" );
00254         file.setName( locateLocal( "appdata", filename ) ); //determine filename in local user KDE directory tree.
00255 
00256         if ( file.exists() || !file.open( IO_ReadWrite | IO_Append ) ) {
00257             QString message = i18n( "Local color scheme file could not be opened.\nScheme cannot be recorded." );
00258             KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
00259             return false;
00260         } else {
00261             QTextStream stream( &file );
00262             stream << StarColorMode << ":" << StarColorIntensity << endl;
00263 
00264             QStringList::Iterator it = KeyName.begin();
00265             QStringList::Iterator it_end = KeyName.end();
00266             for ( ; it != it_end; ++it )
00267                 stream << Palette[ (*it) ] << " :" << (*it) << endl;
00268 
00269             file.close();
00270         }
00271 
00272         file.setName( locateLocal( "appdata", "colors.dat" ) ); //determine filename in local user KDE directory tree.
00273 
00274         if ( !file.open( IO_ReadWrite | IO_Append ) ) {
00275             QString message = i18n( "Local color scheme index file could not be opened.\nScheme cannot be recorded." );
00276             KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
00277             return false;
00278         } else {
00279             QTextStream stream( &file );
00280             stream << name << ":" << filename << endl;
00281             file.close();
00282         }
00283     } else {
00284         QString message = i18n( "Invalid filename requested.\nScheme cannot be recorded." );
00285         KMessageBox::sorry( 0, message, i18n( "Invalid Filename" ) );
00286         return false;
00287     }
00288 
00289     FileName = filename;
00290     return true;
00291 }
00292 
00293 void ColorScheme::loadFromConfig( KConfig *conf ) {
00294     QStringList::Iterator it = KeyName.begin();
00295     QStringList::Iterator it_end = KeyName.end();
00296     for ( ; it != it_end; ++it )
00297         setColor( QString(*it), conf->readEntry( QString(*it), QString( *Default.at( KeyName.findIndex(*it) ) ) ) );
00298 
00299     setStarColorMode( conf->readNumEntry( "StarColorMode", 0 ) );
00300     setStarColorIntensity( conf->readNumEntry( "StarColorIntensity", 5 ) );
00301 }
00302 
00303 void ColorScheme::saveToConfig( KConfig *conf ) {
00304     QStringList::Iterator it = KeyName.begin();
00305     QStringList::Iterator it_end = KeyName.end();
00306     for ( ; it != it_end; ++it )
00307         conf->writeEntry( QString(*it), colorNamed( QString(*it) ) );
00308 
00309     conf->writeEntry( "StarColorMode", starColorMode() );
00310     conf->writeEntry( "StarColorIntensity", starColorIntensity() );
00311 }

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