• 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
  • options
opscolors.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  opscolors.cpp - K Desktop Planetarium
3  -------------------
4  begin : Sun Feb 29 2004
5  copyright : (C) 2004 by Jason Harris
6  email : jharris@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 "opscolors.h"
19 
20 #include <QFile>
21 #include <QPixmap>
22 #include <QTextStream>
23 
24 #include <kactioncollection.h>
25 #include <kapplication.h>
26 #include <klocale.h>
27 #include <knuminput.h>
28 #include <kcombobox.h>
29 #include <kpushbutton.h>
30 #include <kcolordialog.h>
31 #include <kmessagebox.h>
32 #include <kinputdialog.h>
33 #include <kstandarddirs.h>
34 
35 #include "kstars.h"
36 #include "kstarsdata.h"
37 #include "skymap.h"
38 #include "colorscheme.h"
39 #include "skyobjects/starobject.h"
40 
41 static int ItemColorData = Qt::UserRole + 1;
42 
43 OpsColors::OpsColors( KStars *_ks )
44  : QFrame( _ks ), ksw(_ks)
45 {
46  setupUi( this );
47 
48  //Populate list of adjustable colors
49  for ( unsigned int i=0; i < ksw->data()->colorScheme()->numberOfColors(); ++i ) {
50  QPixmap col( 30, 20 );
51  QColor itemColor( ksw->data()->colorScheme()->colorAt( i ) );
52  col.fill( itemColor );
53  QListWidgetItem *item = new QListWidgetItem( ksw->data()->colorScheme()->nameAt( i ), ColorPalette );
54  item->setData( Qt::DecorationRole, col );
55  item->setData( ItemColorData, itemColor );
56  }
57 
58  PresetBox->addItem( i18nc( "use default color scheme", "Default Colors" ) );
59  PresetBox->addItem( i18nc( "use 'star chart' color scheme", "Star Chart" ) );
60  PresetBox->addItem( i18nc( "use 'night vision' color scheme", "Night Vision" ) );
61  PresetBox->addItem( i18nc( "use 'moonless night' color scheme", "Moonless Night" ) );
62 
63  PresetFileList.append( "classic.colors" );
64  PresetFileList.append( "chart.colors" );
65  PresetFileList.append( "night.colors" );
66  PresetFileList.append( "moonless-night.colors" );
67 
68  QFile file;
69  QString line, schemeName, filename;
70  file.setFileName( KStandardDirs::locate( "appdata", "colors.dat" ) );
71  if ( file.exists() && file.open( QIODevice::ReadOnly ) ) {
72  QTextStream stream( &file );
73 
74  while ( !stream.atEnd() ) {
75  line = stream.readLine();
76  schemeName = line.left( line.indexOf( ':' ) );
77  filename = line.mid( line.indexOf( ':' ) +1, line.length() );
78  PresetBox->addItem( schemeName );
79  PresetFileList.append( filename );
80  }
81  file.close();
82  }
83 
84  kcfg_StarColorIntensity->setValue( ksw->data()->colorScheme()->starColorIntensity() );
85  kcfg_StarColorMode->addItem( i18nc( "use realistic star colors", "Real Colors" ) );
86  kcfg_StarColorMode->addItem( i18nc( "show stars as red circles", "Solid Red" ) );
87  kcfg_StarColorMode->addItem( i18nc( "show stars as black circles", "Solid Black" ) );
88  kcfg_StarColorMode->addItem( i18nc( "show stars as white circles", "Solid White" ) );
89  kcfg_StarColorMode->setCurrentIndex( ksw->data()->colorScheme()->starColorMode() );
90 
91  if ( ksw->data()->colorScheme()->starColorMode() != 0 ) //mode is not "Real Colors"
92  kcfg_StarColorIntensity->setEnabled( false );
93  else
94  kcfg_StarColorIntensity->setEnabled( true );
95 
96  connect( ColorPalette, SIGNAL( itemClicked( QListWidgetItem* ) ), this, SLOT( newColor( QListWidgetItem* ) ) );
97  connect( kcfg_StarColorIntensity, SIGNAL( valueChanged( int ) ), this, SLOT( slotStarColorIntensity( int ) ) );
98  connect( kcfg_StarColorMode, SIGNAL( activated( int ) ), this, SLOT( slotStarColorMode( int ) ) );
99  connect( PresetBox, SIGNAL( currentRowChanged( int ) ), this, SLOT( slotPreset( int ) ) );
100  connect( AddPreset, SIGNAL( clicked() ), this, SLOT( slotAddPreset() ) );
101  connect( RemovePreset, SIGNAL( clicked() ), this, SLOT( slotRemovePreset() ) );
102 
103  RemovePreset->setEnabled( false );
104 }
105 
106 //empty destructor
107 OpsColors::~OpsColors() {}
108 
109 void OpsColors::newColor( QListWidgetItem *item ) {
110  if ( !item ) return;
111 
112  QPixmap pixmap( 30, 20 );
113  QColor NewColor;
114 
115  int index = ColorPalette->row( item );
116  if ( index < 0 || index >= ColorPalette->count() ) return;
117  QColor col = item->data( ItemColorData ).value<QColor>();
118  if ( KColorDialog::getColor( col ) ) NewColor = col;
119 
120  //NewColor will only be valid if the above if statement was found to be true during one of the for loop iterations
121  if ( NewColor.isValid() ) {
122  pixmap.fill( NewColor );
123  item->setData( Qt::DecorationRole, pixmap );
124  item->setData( ItemColorData, NewColor );
125  ksw->data()->colorScheme()->setColor( ksw->data()->colorScheme()->keyAt( index ), NewColor.name() );
126  }
127 
128  ksw->map()->forceUpdate();
129 }
130 
131 void OpsColors::slotPreset( int index ) {
132  QString sPreset = PresetFileList.at( index );
133  setColors( sPreset );
134 }
135 
136 bool OpsColors::setColors( const QString &filename ) {
137  QPixmap temp( 30, 20 );
138 
139  //check if colorscheme is removable...
140  QFile test;
141  test.setFileName( KStandardDirs::locateLocal( "appdata", filename ) ); //try filename in local user KDE directory tree.
142  if ( test.exists() ) RemovePreset->setEnabled( true );
143  else RemovePreset->setEnabled( false );
144  test.close();
145 
146  QString actionName = QString("cs_" + filename.left(filename.indexOf(".colors"))).toUtf8();
147  QAction *a = ksw->actionCollection()->action( actionName );
148  if ( a ) a->setChecked( true );
149  kapp->processEvents();
150 
151  kcfg_StarColorMode->setCurrentIndex( ksw->data()->colorScheme()->starColorMode() );
152  kcfg_StarColorIntensity->setValue( ksw->data()->colorScheme()->starColorIntensity() );
153 
154  for ( unsigned int i=0; i < ksw->data()->colorScheme()->numberOfColors(); ++i ) {
155  QColor itemColor( ksw->data()->colorScheme()->colorAt( i ) );
156  temp.fill( itemColor );
157  ColorPalette->item( i )->setData( Qt::DecorationRole, temp );
158  ColorPalette->item( i )->setData( ItemColorData, itemColor );
159  }
160 
161  ksw->map()->forceUpdate();
162  return true;
163 }
164 
165 void OpsColors::slotAddPreset() {
166  bool okPressed = false;
167  QString schemename = KInputDialog::getText( i18n( "New Color Scheme" ),
168  i18n( "Enter a name for the new color scheme:" ),
169  QString(), &okPressed, 0 );
170 
171  if ( okPressed && ! schemename.isEmpty() ) {
172  if ( ksw->data()->colorScheme()->save( schemename ) ) {
173  QListWidgetItem *item = new QListWidgetItem( schemename, PresetBox );
174  QString fname = ksw->data()->colorScheme()->fileName();
175  PresetFileList.append( fname );
176  QString actionName = QString("cs_" + fname.left(fname.indexOf(".colors"))).toUtf8();
177  ksw->addColorMenuItem( schemename, actionName );
178 
179  QAction *a = ksw->actionCollection()->action( actionName );
180  if ( a ) a->setChecked( true );
181  PresetBox->setCurrentItem( item );
182  }
183  }
184 }
185 
186 void OpsColors::slotRemovePreset() {
187  QListWidgetItem *current = PresetBox->currentItem();
188  if ( !current ) return;
189  QString name = current->text();
190  QString filename = PresetFileList[ PresetBox->currentRow() ];
191  QFile cdatFile;
192  cdatFile.setFileName( KStandardDirs::locateLocal( "appdata", "colors.dat" ) ); //determine filename in local user KDE directory tree.
193 
194  //Remove action from color-schemes menu
195  ksw->removeColorMenuItem( QString("cs_" + filename.left( filename.indexOf(".colors"))).toUtf8() );
196 
197  if ( !cdatFile.exists() || !cdatFile.open( QIODevice::ReadWrite ) ) {
198  QString message = i18n( "Local color scheme index file could not be opened.\nScheme cannot be removed." );
199  KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
200  } else {
201  //Remove entry from the ListBox and from the QStringList holding filenames.
202  //There seems to be no way to set no item selected, so select
203  // the first item.
204  PresetBox->setCurrentRow(0);
205  delete current;
206  RemovePreset->setEnabled( false );
207 
208  //Read the contents of colors.dat into a QStringList, except for the entry to be removed.
209  QTextStream stream( &cdatFile );
210  QStringList slist;
211  bool removed = false;
212 
213  while ( !stream.atEnd() ) {
214  QString line = stream.readLine();
215  if ( line.left( line.indexOf(':') ) != name ) slist.append( line );
216  else removed = true;
217  }
218 
219  if ( removed ) { //Entry was removed; delete the corresponding .colors file.
220  QFile colorFile;
221  colorFile.setFileName( KStandardDirs::locateLocal( "appdata", filename ) ); //determine filename in local user KDE directory tree.
222  if ( !colorFile.remove() ) {
223  QString message = i18n( "Could not delete the file: %1", colorFile.fileName() );
224  KMessageBox::sorry( 0, message, i18n( "Error Deleting File" ) );
225  }
226 
227  //remove the old colors.dat file, and rebuild it with the modified string list.
228  cdatFile.remove();
229  cdatFile.open( QIODevice::ReadWrite );
230  QTextStream stream2( &cdatFile );
231  for( int i=0; i<slist.count(); ++i )
232  stream << slist[i] << endl;
233  } else {
234  QString message = i18n( "Could not find an entry named %1 in colors.dat.", name );
235  KMessageBox::sorry( 0, message, i18n( "Scheme Not Found" ) );
236  }
237  cdatFile.close();
238  }
239 }
240 
241 void OpsColors::slotStarColorMode( int i ) {
242  ksw->data()->colorScheme()->setStarColorMode( i );
243 
244  if ( ksw->data()->colorScheme()->starColorMode() != 0 ) //mode is not "Real Colors"
245  kcfg_StarColorIntensity->setEnabled( false );
246  else
247  kcfg_StarColorIntensity->setEnabled( true );
248 }
249 
250 void OpsColors::slotStarColorIntensity( int i ) {
251  ksw->data()->colorScheme()->setStarColorIntensity( i );
252 }
253 
254 #include "opscolors.moc"
OpsColors::OpsColors
OpsColors(KStars *_ks)
Definition: opscolors.cpp:43
ColorScheme::nameAt
QString nameAt(int i) const
i the index of the long name to retrieve
Definition: colorscheme.cpp:110
KStarsData::colorScheme
ColorScheme * colorScheme()
Definition: kstarsdata.h:149
opscolors.h
KStars
This is the main window for KStars.
Definition: kstars.h:94
ColorScheme::starColorIntensity
int starColorIntensity() const
Definition: colorscheme.h:108
OpsColors::~OpsColors
~OpsColors()
Definition: opscolors.cpp:107
ColorScheme::numberOfColors
unsigned int numberOfColors() const
Definition: colorscheme.h:102
ColorScheme::colorAt
QColor colorAt(int i) const
i the index of the color to retrieve
Definition: colorscheme.cpp:106
skymap.h
i18nc
i18nc("string from libindi, used in the config dialog","100x")
ItemColorData
static int ItemColorData
Definition: opscolors.cpp:41
QTextStream
starobject.h
KStars::data
KStarsData * data() const
Definition: kstars.h:131
kstarsdata.h
ColorScheme::starColorMode
int starColorMode() const
Definition: colorscheme.h:105
QFrame
colorscheme.h
kstars.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:20 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