1 /***************************************************************************
2 colorscheme.cpp - description
5 copyright : (C) 2002 by Jason Harris
6 email : kstars@30doradus.org
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 ***************************************************************************/
22 #include <kmessagebox.h>
23 #include <kstandarddirs.h>
26 #include "colorscheme.h"
28 typedef QStringList::const_iterator SL_it
;
30 ColorScheme::ColorScheme() : FileName() {
31 //Each color has two names associated with it. The KeyName is its
32 //identification in the QMap, the *.colors file, and the config file.
33 //The Name is what appears in the ViewOpsDialog ListBox.
34 //In addition, we define default RGB strings for each item.
35 //To add another color to the Palette, just add an entry for KeyName,
36 //Name and Default here.
38 KeyName
.append( "SkyColor" );
39 Name
.append( i18n( "Sky" ) );
40 Default
.append( "#002" );
41 KeyName
.append( "MessColor" );
42 Name
.append( i18n( "Messier Object" ) );
43 Default
.append( "#0F0" );
44 KeyName
.append( "NGCColor" );
45 Name
.append( i18n( "New General Catalog object", "NGC Object" ) );
46 Default
.append( "#066" );
47 KeyName
.append( "ICColor" );
48 Name
.append( i18n( "Index Catalog object", "IC Object" ) );
49 Default
.append( "#439" );
50 KeyName
.append( "HSTColor" );
51 Name
.append( i18n( "Object with extra attached URLs", "Object w/ Links" ) );
52 Default
.append( "#A00" );
53 KeyName
.append( "SNameColor" );
54 Name
.append( i18n( "Star Name" ) );
55 Default
.append( "#7AA" );
56 KeyName
.append( "PNameColor" );
57 Name
.append( i18n( "Planet Name" ) );
58 Default
.append( "#A77" );
59 KeyName
.append( "CNameColor" );
60 Name
.append( i18n( "Constellation Name", "Constell. Name" ) );
61 Default
.append( "#AA7" );
62 KeyName
.append( "CLineColor" );
63 Name
.append( i18n( "Constellation Line", "Constell. Line" ) );
64 Default
.append( "#555" );
65 KeyName
.append( "CBoundColor" );
66 Name
.append( i18n( "Constellation Boundary", "Constell. Boundary" ) );
67 Default
.append( "#222" );
68 KeyName
.append( "MWColor" );
69 Name
.append( i18n( "refers to the band of stars in the sky due to the Galactic plane", "Milky Way" ) );
70 Default
.append( "#123" );
71 KeyName
.append( "EqColor" );
72 Name
.append( i18n( "Equator" ) );
73 Default
.append( "#FFF" );
74 KeyName
.append( "EclColor" );
75 Name
.append( i18n( "Ecliptic" ) );
76 Default
.append( "#663" );
77 KeyName
.append( "HorzColor" );
78 Name
.append( i18n( "Horizon" ) );
79 Default
.append( "#5A3" );
80 KeyName
.append( "CompassColor" );
81 Name
.append( i18n( "Compass Labels" ) );
82 Default
.append( "#002" );
83 KeyName
.append( "GridColor" );
84 Name
.append( i18n( "Coordinate Grid" ) );
85 Default
.append( "#456" );
86 KeyName
.append( "BoxTextColor" );
87 Name
.append( i18n( "Info Box Text" ) );
88 Default
.append( "#FFF" );
89 KeyName
.append( "BoxGrabColor" );
90 Name
.append( i18n( "Info Box Selected" ) );
91 Default
.append( "#F00" );
92 KeyName
.append( "BoxBGColor" );
93 Name
.append( i18n( "Info Box Background" ) );
94 Default
.append( "#000" );
95 KeyName
.append( "TargetColor" );
96 Name
.append( i18n( "Target Indicator" ) );
97 Default
.append( "#8B8" );
98 KeyName
.append( "UserLabelColor" );
99 Name
.append( i18n( "User Labels" ) );
100 Default
.append( "#FFF" );
101 KeyName
.append( "PlanetTrailColor" );
102 Name
.append( i18n( "Planet Trails" ) );
103 Default
.append( "#963" );
104 KeyName
.append( "AngularRuler" );
105 Name
.append( i18n( "Angular Distance Ruler" ) );
106 Default
.append( "#FFF" );
107 KeyName
.append( "ObsListColor" );
108 Name
.append( i18n( "Observing List Label" ) );
109 Default
.append( "#F00" );
111 //Set the default colors in the Palette.
112 for( uint i
=0; i
<KeyName
.count(); ++i
) {
113 setColor( KeyName
[i
], Default
[i
] );
116 //Default values for integer variables:
118 StarColorIntensity
= 4;
121 ColorScheme::ColorScheme( const ColorScheme
&cs
) {
122 KeyName
= cs
.KeyName
;
124 Default
= cs
.Default
;
125 StarColorMode
= cs
.StarColorMode
;
126 StarColorIntensity
= cs
.StarColorIntensity
;
127 Palette
= cs
.Palette
;
128 FileName
= cs
.FileName
;
131 ColorScheme::~ColorScheme(){
134 void ColorScheme::copy( const ColorScheme
&cs
) {
135 KeyName
= cs
.KeyName
;
137 Default
= cs
.Default
;
138 StarColorMode
= cs
.StarColorMode
;
139 StarColorIntensity
= cs
.StarColorIntensity
;
140 Palette
= cs
.Palette
;
141 FileName
= cs
.FileName
;
144 QString
ColorScheme::colorNamed( const QString
&name
) const {
145 //QString color( Palette[ name ] );
146 if ( ! hasColorNamed( name
) ) {
147 kdWarning() << i18n( "No color named \"%1\" found in color scheme." ).arg( name
) << endl
;
148 //color = "#FFFFFF"; //set to white if no color found
152 return Palette
[ name
];
155 QString
ColorScheme::colorAt( int i
) const {
156 SL_it it
= KeyName
.at(i
);
157 return Palette
[ QString(*it
) ];
160 QString
ColorScheme::nameAt( int i
) const {
161 SL_it it
= Name
.at(i
);
165 QString
ColorScheme::keyAt( int i
) const {
166 SL_it it
= KeyName
.at(i
);
170 QString
ColorScheme::nameFromKey( const QString
&key
) const {
171 return nameAt( KeyName
.findIndex( key
) );
174 void ColorScheme::setColor( const QString
&key
, const QString
&color
) {
175 //We can blindly insert() the new value; if the key exists, the old value is replaced
176 Palette
.insert( key
, color
);
179 bool ColorScheme::load( const QString
&filename
) {
183 if ( !KSUtils::openDataFile( file
, filename
) )
186 QTextStream
stream( &file
);
189 //first line is the star-color mode and star color intensity
190 line
= stream
.readLine();
192 int newmode
= line
.left(1).toInt( &ok
);
193 if ( ok
) setStarColorMode( newmode
);
194 if ( line
.contains(':') ) {
195 int newintens
= line
.mid( line
.find(':')+1, 2 ).toInt( &ok
);
196 if ( ok
) setStarColorIntensity( newintens
);
199 //More flexible method for reading in color values. Any order is acceptable, and
200 //missing entries are ignored.
201 while ( !stream
.eof() ) {
202 line
= stream
.readLine();
204 if ( line
.contains(':')==1 ) { //the new color preset format contains a ":" in each line, followed by the name of the color
206 if ( iold
) return false; //we read at least one line without a colon...file is corrupted.
208 //If this line has a valid Key, set the color.
209 QString tkey
= line
.mid( line
.find(':')+1 ).stripWhiteSpace();
210 QString tname
= line
.left( line
.find(':')-1 );
212 if ( KeyName
.contains( tkey
) ) {
213 setColor( tkey
, tname
);
214 } else { //attempt to translate from old color ID
215 QString
k( line
.mid( 5 ).stripWhiteSpace() + "Color" );
216 if ( KeyName
.contains( k
) ) {
217 setColor( k
, tname
);
219 kdWarning() << "Could not use the key \"" << tkey
<<
220 "\" from the color scheme file \"" << filename
<< "\". I also tried \"" <<
225 } else { // no ':' seen in the line, so we must assume the old format
227 if ( inew
) return false; //a previous line had a colon, this line doesn't. File is corrupted.
229 //Assuming the old *.colors format. Loop through the KeyName list,
230 //and assign each color. Note that order matters here, but only here
231 //(so if you don't use the old format, then order doesn't ever matter)
232 QStringList::Iterator it
= KeyName
.begin();
233 QStringList::Iterator it_end
= KeyName
.end();
234 for ( ; it
!= it_end
; ++it
)
235 setColor( QString(*it
), line
.left( 7 ) );
243 bool ColorScheme::save( const QString
&name
) {
246 //Construct a file name from the scheme name. Make lowercase, replace spaces with "-",
247 //and append ".colors".
248 QString filename
= name
.lower().stripWhiteSpace();
249 if ( !filename
.isEmpty() ) {
250 for( unsigned int i
=0; i
<filename
.length(); ++i
)
251 if ( filename
.at(i
)==' ' ) filename
.replace( i
, 1, "-" );
253 filename
= filename
.append( ".colors" );
254 file
.setName( locateLocal( "appdata", filename
) ); //determine filename in local user KDE directory tree.
256 if ( file
.exists() || !file
.open( IO_ReadWrite
| IO_Append
) ) {
257 QString message
= i18n( "Local color scheme file could not be opened.\nScheme cannot be recorded." );
258 KMessageBox::sorry( 0, message
, i18n( "Could Not Open File" ) );
261 QTextStream
stream( &file
);
262 stream
<< StarColorMode
<< ":" << StarColorIntensity
<< endl
;
264 QStringList::Iterator it
= KeyName
.begin();
265 QStringList::Iterator it_end
= KeyName
.end();
266 for ( ; it
!= it_end
; ++it
)
267 stream
<< Palette
[ (*it
) ] << " :" << (*it
) << endl
;
272 file
.setName( locateLocal( "appdata", "colors.dat" ) ); //determine filename in local user KDE directory tree.
274 if ( !file
.open( IO_ReadWrite
| IO_Append
) ) {
275 QString message
= i18n( "Local color scheme index file could not be opened.\nScheme cannot be recorded." );
276 KMessageBox::sorry( 0, message
, i18n( "Could Not Open File" ) );
279 QTextStream
stream( &file
);
280 stream
<< name
<< ":" << filename
<< endl
;
284 QString message
= i18n( "Invalid filename requested.\nScheme cannot be recorded." );
285 KMessageBox::sorry( 0, message
, i18n( "Invalid Filename" ) );
293 void ColorScheme::loadFromConfig( KConfig
*conf
) {
294 QStringList::Iterator it
= KeyName
.begin();
295 QStringList::Iterator it_end
= KeyName
.end();
296 for ( ; it
!= it_end
; ++it
)
297 setColor( QString(*it
), conf
->readEntry( QString(*it
), QString( *Default
.at( KeyName
.findIndex(*it
) ) ) ) );
299 setStarColorMode( conf
->readNumEntry( "StarColorMode", 0 ) );
300 setStarColorIntensity( conf
->readNumEntry( "StarColorIntensity", 5 ) );
303 void ColorScheme::saveToConfig( KConfig
*conf
) {
304 QStringList::Iterator it
= KeyName
.begin();
305 QStringList::Iterator it_end
= KeyName
.end();
306 for ( ; it
!= it_end
; ++it
)
307 conf
->writeEntry( QString(*it
), colorNamed( QString(*it
) ) );
309 conf
->writeEntry( "StarColorMode", starColorMode() );
310 conf
->writeEntry( "StarColorIntensity", starColorIntensity() );