moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / opscolors.cpp
blob723fd559d2e2c9aef266c479a2dd3d46dd4d42ed
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 ***************************************************************************/
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 ***************************************************************************/
18 #include <qfile.h>
20 #include <klocale.h>
21 #include <knuminput.h>
22 #include <kcombobox.h>
23 #include <kpushbutton.h>
24 #include <kcolordialog.h>
25 #include <kmessagebox.h>
26 #include <kinputdialog.h>
27 #include <kstandarddirs.h>
29 #include "opscolors.h"
30 #include "kstars.h"
31 #include "kstarsdata.h"
32 #include "skymap.h"
33 #include "colorscheme.h"
35 OpsColors::OpsColors( QWidget *p, const char *name, WFlags fl )
36 : OpsColorsUI( p, name, fl )
38 ksw = (KStars *)p;
40 //Populate list of adjustable colors
41 for ( unsigned int i=0; i < ksw->data()->colorScheme()->numberOfColors(); ++i ) {
42 QPixmap col( 30, 20 );
43 col.fill( QColor( ksw->data()->colorScheme()->colorAt( i ) ) );
44 ColorPalette->insertItem( col, ksw->data()->colorScheme()->nameAt( i ) );
47 PresetBox->insertItem( i18n( "use default color scheme", "Default Colors" ) );
48 PresetBox->insertItem( i18n( "use 'star chart' color scheme", "Star Chart" ) );
49 PresetBox->insertItem( i18n( "use 'night vision' color scheme", "Night Vision" ) );
50 PresetBox->insertItem( i18n( "use 'moonless night' color scheme", "Moonless Night" ) );
52 PresetFileList.append( "default.colors" );
53 PresetFileList.append( "chart.colors" );
54 PresetFileList.append( "night.colors" );
55 PresetFileList.append( "moonless-night.colors" );
57 QFile file;
58 QString line, schemeName, filename;
59 file.setName( locate( "appdata", "colors.dat" ) );
60 if ( file.exists() && file.open( IO_ReadOnly ) ) {
61 QTextStream stream( &file );
63 while ( !stream.eof() ) {
64 line = stream.readLine();
65 schemeName = line.left( line.find( ':' ) );
66 filename = line.mid( line.find( ':' ) +1, line.length() );
67 PresetBox->insertItem( schemeName );
68 PresetFileList.append( filename );
70 file.close();
73 kcfg_StarColorIntensity->setValue( ksw->data()->colorScheme()->starColorIntensity() );
74 kcfg_StarColorMode->insertItem( i18n( "use realistic star colors", "Real Colors" ) );
75 kcfg_StarColorMode->insertItem( i18n( "show stars as red circles", "Solid Red" ) );
76 kcfg_StarColorMode->insertItem( i18n( "show stars as black circles", "Solid Black" ) );
77 kcfg_StarColorMode->insertItem( i18n( "show stars as white circles", "Solid White" ) );
78 kcfg_StarColorMode->setCurrentItem( ksw->data()->colorScheme()->starColorMode() );
80 if ( ksw->data()->colorScheme()->starColorMode() != 0 ) //mode is not "Real Colors"
81 kcfg_StarColorIntensity->setEnabled( false );
82 else
83 kcfg_StarColorIntensity->setEnabled( true );
85 connect( ColorPalette, SIGNAL( clicked( QListBoxItem* ) ), this, SLOT( newColor( QListBoxItem* ) ) );
86 connect( kcfg_StarColorIntensity, SIGNAL( valueChanged( int ) ), this, SLOT( slotStarColorIntensity( int ) ) );
87 connect( kcfg_StarColorMode, SIGNAL( activated( int ) ), this, SLOT( slotStarColorMode( int ) ) );
88 connect( PresetBox, SIGNAL( highlighted( int ) ), this, SLOT( slotPreset( int ) ) );
89 connect( AddPreset, SIGNAL( clicked() ), this, SLOT( slotAddPreset() ) );
90 connect( RemovePreset, SIGNAL( clicked() ), this, SLOT( slotRemovePreset() ) );
92 RemovePreset->setEnabled( false );
95 //empty destructor
96 OpsColors::~OpsColors() {}
98 void OpsColors::newColor( QListBoxItem *item ) {
99 QPixmap pixmap( 30, 20 );
100 QColor NewColor;
101 unsigned int i;
103 for ( i=0; i < ksw->data()->colorScheme()->numberOfColors(); ++i ) {
104 if ( item->text() == ksw->data()->colorScheme()->nameAt( i ) ) {
105 QColor col( ksw->data()->colorScheme()->colorAt( i ) );
107 if(KColorDialog::getColor( col )) NewColor = col;
108 break;
112 //NewColor will only be valid if the above if statement was found to be true during one of the for loop iterations
113 if ( NewColor.isValid() ) {
114 pixmap.fill( NewColor );
115 ColorPalette->changeItem( pixmap, item->text(), ColorPalette->index( item ) );
116 ksw->data()->colorScheme()->setColor( ksw->data()->colorScheme()->keyAt( i ), NewColor.name() );
119 ksw->map()->forceUpdate();
122 void OpsColors::slotPreset( int index ) {
123 QStringList::Iterator it = PresetFileList.at( index );
124 bool result = setColors( *it );
125 if (!result) {
126 QString message = i18n( "The specified color scheme file (%1) could not be found, or was corrupt." ).arg( QString(*it) );
127 KMessageBox::sorry( 0, message, i18n( "Could Not Set Color Scheme" ) );
131 bool OpsColors::setColors( QString filename ) {
132 QPixmap *temp = new QPixmap( 30, 20 );
134 //just checking if colorscheme is removable...
135 QFile test;
136 test.setName( locateLocal( "appdata", filename ) ); //try filename in local user KDE directory tree.
137 if ( test.exists() ) RemovePreset->setEnabled( true );
138 else RemovePreset->setEnabled( false );
139 test.close();
141 ksw->loadColorScheme( filename );
142 kcfg_StarColorMode->setCurrentItem( ksw->data()->colorScheme()->starColorMode() );
143 kcfg_StarColorIntensity->setValue( ksw->data()->colorScheme()->starColorIntensity() );
145 if ( ksw->map()->starColorMode() != ksw->data()->colorScheme()->starColorMode() )
146 ksw->map()->setStarColorMode( ksw->data()->colorScheme()->starColorMode() );
148 if ( ksw->map()->starColorIntensity() != ksw->data()->colorScheme()->starColorIntensity() )
149 ksw->map()->setStarColorIntensity( ksw->data()->colorScheme()->starColorIntensity() );
151 for ( unsigned int i=0; i < ksw->data()->colorScheme()->numberOfColors(); ++i ) {
152 temp->fill( QColor( ksw->data()->colorScheme()->colorAt( i ) ) );
153 ColorPalette->changeItem( *temp, ksw->data()->colorScheme()->nameAt( i ), i );
156 ksw->map()->forceUpdate();
157 return true;
160 void OpsColors::slotAddPreset() {
161 bool okPressed = false;
162 QString schemename = KInputDialog::getText( i18n( "New Color Scheme" ),
163 i18n( "Enter a name for the new color scheme:" ),
164 QString::null, &okPressed, 0 );
166 if ( okPressed && ! schemename.isEmpty() ) {
167 if ( ksw->data()->colorScheme()->save( schemename ) ) {
168 PresetBox->insertItem( schemename );
169 PresetBox->setCurrentItem( PresetBox->findItem( schemename ) );
170 QString fname = ksw->data()->colorScheme()->fileName();
171 PresetFileList.append( fname );
172 ksw->addColorMenuItem( schemename, QString("cs_" + fname.left(fname.find(".colors"))).utf8() );
177 void OpsColors::slotRemovePreset() {
178 QString name = PresetBox->currentText();
179 QString filename = PresetFileList[ PresetBox->currentItem() ];
180 QFile cdatFile;
181 cdatFile.setName( locateLocal( "appdata", "colors.dat" ) ); //determine filename in local user KDE directory tree.
183 //Remove action from color-schemes menu
184 ksw->removeColorMenuItem( QString("cs_" + filename.left( filename.find(".colors"))).utf8() );
186 if ( !cdatFile.exists() || !cdatFile.open( IO_ReadWrite ) ) {
187 QString message = i18n( "Local color scheme index file could not be opened.\nScheme cannot be removed." );
188 KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
189 } else {
190 //Remove entry from the ListBox and from the QStringList holding filenames.
191 //We don't want another color scheme to be selected, so first
192 //temporarily disconnect the "highlighted" signal.
193 disconnect( PresetBox, SIGNAL( highlighted( int ) ), this, SLOT( slotPreset( int ) ) );
194 PresetBox->removeItem( PresetBox->currentItem() );
195 PresetBox->setCurrentItem( -1 );
196 RemovePreset->setEnabled( false );
198 //Reconnect the "highlighted" signal
199 connect( PresetBox, SIGNAL( highlighted( int ) ), this, SLOT( slotPreset( int ) ) );
201 //Read the contents of colors.dat into a QStringList, except for the entry to be removed.
202 QTextStream stream( &cdatFile );
203 QStringList slist;
204 bool removed = false;
206 while ( !stream.eof() ) {
207 QString line = stream.readLine();
208 if ( line.left( line.find(':') ) != name ) slist.append( line );
209 else removed = true;
212 if ( removed ) { //Entry was removed; delete the corresponding .colors file.
213 QFile colorFile;
214 colorFile.setName( locateLocal( "appdata", filename ) ); //determine filename in local user KDE directory tree.
215 if ( !colorFile.remove() ) {
216 QString message = i18n( "Could not delete the file: %1" ).arg( colorFile.name() );
217 KMessageBox::sorry( 0, message, i18n( "Error Deleting File" ) );
220 //remove the old colors.dat file, and rebuild it with the modified string list.
221 cdatFile.remove();
222 cdatFile.open( IO_ReadWrite );
223 QTextStream stream2( &cdatFile );
224 for( unsigned int i=0; i<slist.count(); ++i )
225 stream << slist[i] << endl;
226 } else {
227 QString message = i18n( "Could not find an entry named %1 in colors.dat." ).arg( name );
228 KMessageBox::sorry( 0, message, i18n( "Scheme Not Found" ) );
230 cdatFile.close();
234 void OpsColors::slotStarColorMode( int i ) {
235 ksw->data()->colorScheme()->setStarColorMode( i );
236 if ( ksw->map()->starColorMode() != ksw->data()->colorScheme()->starColorMode() )
237 ksw->map()->setStarColorMode( ksw->data()->colorScheme()->starColorMode() );
239 if ( ksw->data()->colorScheme()->starColorMode() != 0 ) //mode is not "Real Colors"
240 kcfg_StarColorIntensity->setEnabled( false );
241 else
242 kcfg_StarColorIntensity->setEnabled( true );
245 void OpsColors::slotStarColorIntensity( int i ) {
246 ksw->data()->colorScheme()->setStarColorIntensity( i );
247 if ( ksw->map()->starColorIntensity() != ksw->data()->colorScheme()->starColorIntensity() )
248 ksw->map()->setStarColorIntensity( ksw->data()->colorScheme()->starColorIntensity() );
252 #include "opscolors.moc"