kstars
colorscheme.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00032
00033
00034
00035
00036
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
00112 for( uint i=0; i<KeyName.count(); ++i ) {
00113 setColor( KeyName[i], Default[i] );
00114 }
00115
00116
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
00146 if ( ! hasColorNamed( name ) ) {
00147 kdWarning() << i18n( "No color named \"%1\" found in color scheme." ).arg( name ) << endl;
00148
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
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
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
00200
00201 while ( !stream.eof() ) {
00202 line = stream.readLine();
00203
00204 if ( line.contains(':')==1 ) {
00205 ++inew;
00206 if ( iold ) return false;
00207
00208
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 {
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 {
00226 ++iold;
00227 if ( inew ) return false;
00228
00229
00230
00231
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
00247
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 ) );
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" ) );
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 }