• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
colorscheme.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  colorscheme.cpp - description
3  -------------------
4  begin : Wed May 8 2002
5  copyright : (C) 2002 by Jason Harris
6  email : kstars@30doradus.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
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. *
15  * *
16  ***************************************************************************/
17 
18 #include "colorscheme.h"
19 
20 #include <QFile>
21 #include <QTextStream>
22 
23 #include <kconfig.h>
24 #include <kdebug.h>
25 #include <klocale.h>
26 #include <kmessagebox.h>
27 #include <kstandarddirs.h>
28 #include <kconfiggroup.h>
29 
30 #include "ksutils.h"
31 #include "Options.h"
32 #include "skyobjects/starobject.h"
33 #include "skyqpainter.h"
34 
35 ColorScheme::ColorScheme() : FileName() {
36  //Each color has two names associated with it. The KeyName is its
37  //identification in the QMap, the *.colors file, and the config file.
38  //The Name is what appears in the ViewOpsDialog ListBox.
39  //In addition, we define default RGB strings for each item.
40  //To add another color to the Palette, just add an entry for KeyName,
41  //Name and Default here.
42 
43  appendItem("SkyColor", i18n("Sky"), "#000000");
44  appendItem("MessColor", i18n("Messier Object"), "#008f00");
45  appendItem("NGCColor", i18nc("New General Catalog object", "NGC Object"), "#006666");
46  appendItem("ICColor", i18nc("Index Catalog object", "IC Object"), "#382a7d");
47  appendItem("HSTColor",
48  i18nc("Object with extra attached URLs", "Object w/ Links"), "#930000");
49  appendItem("SNameColor", i18n("Star Name"), "#577d7d");
50  appendItem("DSNameColor", i18n("Deep Sky Object Name"), "#75759c");
51  appendItem("PNameColor", i18n("Planet Name"), "#ac9800");
52  appendItem("CNameColor",
53  i18nc("Constellation Name", "Constell. Name"), "#718488");
54  appendItem("CLineColor",
55  i18nc("Constellation Line", "Constell. Line"), "#3d3d3d");
56  appendItem("CBoundColor",
57  i18nc("Constellation Boundary", "Constell. Boundary"), "#222f2f");
58  appendItem("CBoundHighColor",
59  i18nc("Highlighted Constellation Boundary", "Constell. Boundary Highlight"), "#445555");
60  appendItem("MWColor",
61  i18nc("refers to the band of stars in the sky due to the Galactic plane", "Milky Way"), "#0d1115");
62  appendItem("EqColor", i18n("Equator"), "#909090");
63  appendItem("EclColor", i18n("Ecliptic"), "#613d12");
64  appendItem("HorzColor", i18n("Horizon"), "#091f14");
65  appendItem("CompassColor", i18n("Compass Labels"), "#909055");
66  appendItem("EquatorialGridColor", i18n("Equatorial Coordinate Grid"), "#445566");
67  appendItem("HorizontalGridColor", i18n("Horizontal Coordinate Grid"), "#091f14");
68  appendItem("BoxTextColor", i18n("Info Box Text"), "#d2dbef");
69  appendItem("BoxGrabColor", i18n("Info Box Selected"), "#900000");
70  appendItem("BoxBGColor", i18n("Info Box Background"), "#000000");
71  appendItem("TargetColor", i18n("Target Indicator"), "#DD0000");
72  appendItem("UserLabelColor", i18n("User Labels"), "#AAAAAA");
73  appendItem("PlanetTrailColor", i18n("Planet Trails"), "#993311");
74  appendItem("AngularRuler", i18n("Angular Distance Ruler"), "#445566");
75  appendItem("ObsListColor", i18n("Observing List Label"), "#FF0000");
76  appendItem("StarHopRouteColor", i18n("Star-Hop Route"), "#00FFFF");
77  appendItem("VisibleSatColor", i18n("Visible Satellites"), "#00FF00");
78  appendItem("SatColor", i18n("Satellites"), "#FF0000");
79  appendItem("SatLabelColor", i18n("Satellites Labels"), "#640000");
80  appendItem("SupernovaColor", i18n("Supernovae"), "#FFA500");
81 
82  //Load colors from config object
83  loadFromConfig();
84 
85  //Default values for integer variables:
86  StarColorMode = 0;
87  StarColorIntensity = 4;
88 }
89 
90 void ColorScheme::appendItem(QString key, QString name, QString def) {
91  KeyName.append( key );
92  Name.append( name );
93  Default.append( def );
94 
95 }
96 
97 QColor ColorScheme::colorNamed( const QString &name ) const {
98  if ( ! hasColorNamed( name ) ) {
99  kWarning() << i18n( "No color named \"%1\" found in color scheme.", name ) ;
100  // Return white if no color found
101  return QColor( Qt::white );
102  }
103  return QColor( Palette[ name ] );
104 }
105 
106 QColor ColorScheme::colorAt( int i ) const {
107  return QColor( Palette[ KeyName.at(i) ] );
108 }
109 
110 QString ColorScheme::nameAt( int i ) const {
111  return Name.at(i);
112 }
113 
114 QString ColorScheme::keyAt( int i ) const {
115  return KeyName.at(i);
116 }
117 
118 QString ColorScheme::nameFromKey( const QString &key ) const {
119  return nameAt( KeyName.indexOf( key ) );
120 }
121 
122 void ColorScheme::setColor( const QString &key, const QString &color ) {
123  //We can blindly insert() the new value; if the key exists, the old value is replaced
124  Palette.insert( key, color );
125 
126  KConfigGroup cg = KGlobal::config()->group( "Colors" );
127  cg.writeEntry( key, color );
128 }
129 
130 bool ColorScheme::load( const QString &name ) {
131  QString filename = name.toLower().trimmed();
132  QFile file;
133  int inew = 0, iold = 0;
134  bool ok;
135 
136  //Parse default names which don't follow the regular file-naming scheme
137  if ( name == i18nc("use default color scheme", "Default Colors") )
138  filename = "classic.colors";
139  if ( name == i18nc("use 'star chart' color scheme", "Star Chart") )
140  filename = "chart.colors";
141  if ( name == i18nc("use 'night vision' color scheme", "Night Vision") )
142  filename = "night.colors";
143 
144  //Try the filename if it ends with ".colors"
145  if ( filename.endsWith( QLatin1String( ".colors" ) ) )
146  ok = KSUtils::openDataFile( file, filename );
147 
148  //If that didn't work, try assuming that 'name' is the color scheme name
149  //convert it to a filename exactly as ColorScheme::save() does
150  if ( ! ok ) {
151  if ( !filename.isEmpty() ) {
152  filename.replace( ' ', '-' ).append( ".colors" );
153  ok = KSUtils::openDataFile( file, filename );
154  }
155 
156  if ( ! ok ) {
157  kDebug() << i18n( "Unable to load color scheme named %1. Also tried %2.", name, filename );
158  return false;
159  }
160  }
161 
162  //If we reach here, the file should have been successfully opened
163  QTextStream stream( &file );
164 
165  //first line is the star-color mode and star color intensity
166  QString line = stream.readLine();
167  int newmode = line.left(1).toInt( &ok );
168  if( ok )
169  setStarColorMode( newmode );
170  if( line.contains(':') ) {
171  int newintens = line.mid( line.indexOf(':')+1, 2 ).toInt( &ok );
172  if ( ok )
173  setStarColorIntensity( newintens );
174  }
175 
176  //More flexible method for reading in color values. Any order is acceptable, and
177  //missing entries are ignored.
178  while ( !stream.atEnd() ) {
179  line = stream.readLine();
180 
181  if ( line.count(':')==1 ) { //the new color preset format contains a ":" in each line, followed by the name of the color
182  ++inew;
183  if ( iold ) return false; //we read at least one line without a colon...file is corrupted.
184 
185  //If this line has a valid Key, set the color.
186  QString tkey = line.mid( line.indexOf(':')+1 ).trimmed();
187  QString tname = line.left( line.indexOf(':')-1 );
188 
189  if ( KeyName.contains( tkey ) ) {
190  setColor( tkey, tname );
191  } else { //attempt to translate from old color ID
192  QString k( line.mid( 5 ).trimmed() + "Color" );
193  if ( KeyName.contains( k ) ) {
194  setColor( k, tname );
195  } else {
196  kWarning() << "Could not use the key \"" << tkey
197  << "\" from the color scheme file \"" << filename
198  << "\". I also tried \"" << k << "\"." << endl;
199  }
200  }
201 
202  } else { // no ':' seen in the line, so we must assume the old format
203  ++iold;
204  if ( inew ) return false; //a previous line had a colon, this line doesn't. File is corrupted.
205 
206  //Assuming the old *.colors format. Loop through the KeyName list,
207  //and assign each color. Note that order matters here, but only here
208  //(so if you don't use the old format, then order doesn't ever matter)
209  foreach(const QString& key, KeyName)
210  setColor( key, line.left( 7 ) );
211  }
212  }
213 
214  FileName = filename;
215  return true;
216 }
217 
218 bool ColorScheme::save( const QString &name ) {
219  QFile file;
220 
221  //Construct a file name from the scheme name. Make lowercase, replace spaces with "-",
222  //and append ".colors".
223  QString filename = name.toLower().trimmed();
224  if ( !filename.isEmpty() ) {
225  for( int i=0; i<filename.length(); ++i)
226  if ( filename.at(i)==' ' ) filename.replace( i, 1, "-" );
227 
228  filename = filename.append( ".colors" );
229  file.setFileName( KStandardDirs::locateLocal( "appdata", filename ) ); //determine filename in local user KDE directory tree.
230 
231  if ( file.exists() || !file.open( QIODevice::ReadWrite | QIODevice::Append ) ) {
232  QString message = i18n( "Local color scheme file could not be opened.\nScheme cannot be recorded." );
233  KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
234  return false;
235  } else {
236  QTextStream stream( &file );
237  stream << StarColorMode << ":" << StarColorIntensity << endl;
238 
239  foreach(const QString& key, KeyName )
240  stream << Palette[ key ] << " :" << key << endl;
241  file.close();
242  }
243 
244  file.setFileName( KStandardDirs::locateLocal( "appdata", "colors.dat" ) ); //determine filename in local user KDE directory tree.
245 
246  if ( !file.open( QIODevice::ReadWrite | QIODevice::Append ) ) {
247  QString message = i18n( "Local color scheme index file could not be opened.\nScheme cannot be recorded." );
248  KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
249  return false;
250  } else {
251  QTextStream stream( &file );
252  stream << name << ":" << filename << endl;
253  file.close();
254  }
255  } else {
256  QString message = i18n( "Invalid filename requested.\nScheme cannot be recorded." );
257  KMessageBox::sorry( 0, message, i18n( "Invalid Filename" ) );
258  return false;
259  }
260 
261  FileName = filename;
262  saveToConfig();
263  return true;
264 }
265 
266 void ColorScheme::loadFromConfig() {
267  KConfigGroup cg = KGlobal::config()->group( "Colors" );
268 
269  for ( int i=0; i < KeyName.size(); ++i )
270  setColor( KeyName.at(i), cg.readEntry( KeyName.at(i).toUtf8().constData(), Default.at( i ) ) );
271 
272  setStarColorModeIntensity( cg.readEntry( "StarColorMode", 0 ), cg.readEntry( "StarColorIntensity", 5 ) );
273 
274  FileName = cg.readEntry( "ColorSchemeFile", "classic.colors" );
275 }
276 
277 void ColorScheme::saveToConfig() {
278  KConfigGroup cg = KGlobal::config()->group( "Colors" );
279  for ( int i=0; i < KeyName.size(); ++i ) {
280  QString c = colorNamed( KeyName.at(i) ).name();
281  cg.writeEntry( KeyName.at(i), c );
282  }
283 
284  cg.writeEntry( "StarColorMode", starColorMode() );
285  cg.writeEntry( "StarColorIntensity", starColorIntensity() );
286 }
287 
288 void ColorScheme::setStarColorMode( int mode ) {
289  StarColorMode = mode;
290  Options::setStarColorMode( mode );
291  SkyQPainter::initStarImages();
292 }
293 
294 void ColorScheme::setStarColorIntensity( int intens ) {
295  StarColorIntensity = intens;
296  Options::setStarColorIntensity( intens );
297  SkyQPainter::initStarImages();
298 }
299 
300 void ColorScheme::setStarColorModeIntensity( int mode, int intens) {
301  StarColorMode = mode;
302  StarColorIntensity = intens;
303  Options::setStarColorMode( mode );
304  Options::setStarColorIntensity( intens );
305  SkyQPainter::initStarImages();
306 }
SkyQPainter::initStarImages
static void initStarImages()
Recalculates the star pixmaps.
Definition: skyqpainter.cpp:140
ColorScheme::setStarColorMode
void setStarColorMode(int mode)
Set the star color mode used by the color scheme mode the star color mode to use. ...
Definition: colorscheme.cpp:288
ColorScheme::setStarColorModeIntensity
void setStarColorModeIntensity(int mode, int intens)
Set the star color mode and intensity value used by the color scheme mode the star color mode to use ...
Definition: colorscheme.cpp:300
ColorScheme::nameAt
QString nameAt(int i) const
i the index of the long name to retrieve
Definition: colorscheme.cpp:110
Options::setStarColorIntensity
static void setStarColorIntensity(uint v)
Set Saturation level of star colors.
Definition: Options.h:2927
ColorScheme::colorNamed
QColor colorNamed(const QString &name) const
Retrieve a color by name.
Definition: colorscheme.cpp:97
KSUtils::openDataFile
bool openDataFile(QFile &file, const QString &filename)
Attempt to open the data file named filename, using the QFile object "file".
ColorScheme::starColorIntensity
int starColorIntensity() const
Definition: colorscheme.h:108
ColorScheme::loadFromConfig
void loadFromConfig()
Read color-scheme data from the Config object.
Definition: colorscheme.cpp:266
ColorScheme::keyAt
QString keyAt(int i) const
i the index of the key name to retrieve
Definition: colorscheme.cpp:114
ColorScheme::colorAt
QColor colorAt(int i) const
i the index of the color to retrieve
Definition: colorscheme.cpp:106
ColorScheme::hasColorNamed
bool hasColorNamed(const QString &name) const
Definition: colorscheme.h:46
i18nc
i18nc("string from libindi, used in the config dialog","100x")
Options::setStarColorMode
static void setStarColorMode(uint v)
Set Mode for rendering stars.
Definition: Options.h:2901
ColorScheme::load
bool load(const QString &filename)
Load a color scheme from a *.colors file filename the filename of the color scheme to be loaded...
Definition: colorscheme.cpp:130
ColorScheme::setColor
void setColor(const QString &key, const QString &color)
Change the color with the given key to the given value key the key-name of the color to be changed co...
Definition: colorscheme.cpp:122
Options.h
QTextStream
ColorScheme::setStarColorIntensity
void setStarColorIntensity(int intens)
Set the star color intensity value used by the color scheme intens The star color intensity value...
Definition: colorscheme.cpp:294
skyqpainter.h
starobject.h
ColorScheme::saveToConfig
void saveToConfig()
Save color-scheme data to the Config object.
Definition: colorscheme.cpp:277
ColorScheme::save
bool save(const QString &name)
Save the current color scheme to a *.colors file.
Definition: colorscheme.cpp:218
ksutils.h
ColorScheme::starColorMode
int starColorMode() const
Definition: colorscheme.h:105
colorscheme.h
ColorScheme::ColorScheme
ColorScheme()
Constructor.
Definition: colorscheme.cpp:35
ColorScheme::nameFromKey
QString nameFromKey(const QString &key) const
Definition: colorscheme.cpp:118
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal