• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

opscolors.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           opscolors.cpp  -  K Desktop Planetarium
00003                              -------------------
00004     begin                : Sun Feb 29  2004
00005     copyright            : (C) 2004 by Jason Harris
00006     email                : jharris@30doradus.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <qfile.h>
00019 
00020 #include <klocale.h>
00021 #include <knuminput.h>
00022 #include <kcombobox.h>
00023 #include <kpushbutton.h>
00024 #include <kcolordialog.h>
00025 #include <kmessagebox.h>
00026 #include <kinputdialog.h>
00027 #include <kstandarddirs.h>
00028 
00029 #include "opscolors.h"
00030 #include "kstars.h"
00031 #include "kstarsdata.h"
00032 #include "skymap.h"
00033 #include "colorscheme.h"
00034 
00035 OpsColors::OpsColors( QWidget *p, const char *name, WFlags fl ) 
00036     : OpsColorsUI( p, name, fl ) 
00037 {
00038     ksw = (KStars *)p;
00039 
00040     //Populate list of adjustable colors
00041     for ( unsigned int i=0; i < ksw->data()->colorScheme()->numberOfColors(); ++i ) {
00042         QPixmap col( 30, 20 );
00043         col.fill( QColor( ksw->data()->colorScheme()->colorAt( i ) ) );
00044         ColorPalette->insertItem( col, ksw->data()->colorScheme()->nameAt( i ) );
00045     }
00046 
00047     PresetBox->insertItem( i18n( "use default color scheme", "Default Colors" ) );
00048     PresetBox->insertItem( i18n( "use 'star chart' color scheme", "Star Chart" ) );
00049     PresetBox->insertItem( i18n( "use 'night vision' color scheme", "Night Vision" ) );
00050     PresetBox->insertItem( i18n( "use 'moonless night' color scheme", "Moonless Night" ) );
00051 
00052     PresetFileList.append( "default.colors" );
00053     PresetFileList.append( "chart.colors" );
00054     PresetFileList.append( "night.colors" );
00055     PresetFileList.append( "moonless-night.colors" );
00056 
00057     QFile file;
00058     QString line, schemeName, filename;
00059     file.setName( locate( "appdata", "colors.dat" ) );
00060     if ( file.exists() && file.open( IO_ReadOnly ) ) {
00061         QTextStream stream( &file );
00062 
00063     while ( !stream.eof() ) {
00064             line = stream.readLine();
00065             schemeName = line.left( line.find( ':' ) );
00066             filename = line.mid( line.find( ':' ) +1, line.length() );
00067             PresetBox->insertItem( schemeName );
00068             PresetFileList.append( filename );
00069       }
00070         file.close();
00071     }
00072 
00073     kcfg_StarColorIntensity->setValue( ksw->data()->colorScheme()->starColorIntensity() );
00074     kcfg_StarColorMode->insertItem( i18n( "use realistic star colors", "Real Colors" ) );
00075     kcfg_StarColorMode->insertItem( i18n( "show stars as red circles", "Solid Red" ) );
00076     kcfg_StarColorMode->insertItem( i18n( "show stars as black circles", "Solid Black" ) );
00077     kcfg_StarColorMode->insertItem( i18n( "show stars as white circles", "Solid White" ) );
00078     kcfg_StarColorMode->setCurrentItem( ksw->data()->colorScheme()->starColorMode() );
00079 
00080     if ( ksw->data()->colorScheme()->starColorMode() != 0 ) //mode is not "Real Colors"
00081         kcfg_StarColorIntensity->setEnabled( false );
00082     else
00083         kcfg_StarColorIntensity->setEnabled( true );
00084         
00085     connect( ColorPalette, SIGNAL( clicked( QListBoxItem* ) ), this, SLOT( newColor( QListBoxItem* ) ) );
00086     connect( kcfg_StarColorIntensity, SIGNAL( valueChanged( int ) ), this, SLOT( slotStarColorIntensity( int ) ) );
00087     connect( kcfg_StarColorMode, SIGNAL( activated( int ) ), this, SLOT( slotStarColorMode( int ) ) );
00088     connect( PresetBox, SIGNAL( highlighted( int ) ), this, SLOT( slotPreset( int ) ) );
00089     connect( AddPreset, SIGNAL( clicked() ), this, SLOT( slotAddPreset() ) );
00090     connect( RemovePreset, SIGNAL( clicked() ), this, SLOT( slotRemovePreset() ) );
00091 
00092     RemovePreset->setEnabled( false );
00093 }
00094 
00095 //empty destructor
00096 OpsColors::~OpsColors() {}
00097 
00098 void OpsColors::newColor( QListBoxItem *item ) {
00099     QPixmap pixmap( 30, 20 );
00100     QColor NewColor;
00101     unsigned int i;
00102 
00103     for ( i=0; i < ksw->data()->colorScheme()->numberOfColors(); ++i ) {
00104         if ( item->text() == ksw->data()->colorScheme()->nameAt( i ) ) {
00105             QColor col( ksw->data()->colorScheme()->colorAt( i ) );
00106 
00107             if(KColorDialog::getColor( col )) NewColor = col;
00108             break;
00109         }
00110     }
00111 
00112     //NewColor will only be valid if the above if statement was found to be true during one of the for loop iterations
00113     if ( NewColor.isValid() ) {
00114         pixmap.fill( NewColor );
00115         ColorPalette->changeItem( pixmap, item->text(), ColorPalette->index( item ) );
00116         ksw->data()->colorScheme()->setColor( ksw->data()->colorScheme()->keyAt( i ), NewColor.name() );
00117     }
00118 
00119     ksw->map()->forceUpdate();
00120 }
00121 
00122 void OpsColors::slotPreset( int index ) {
00123     QStringList::Iterator it = PresetFileList.at( index );
00124     bool result = setColors( *it );
00125     if (!result) {
00126         QString message = i18n( "The specified color scheme file (%1) could not be found, or was corrupt." ).arg( QString(*it) );
00127         KMessageBox::sorry( 0, message, i18n( "Could Not Set Color Scheme" ) );
00128     }
00129 }
00130 
00131 bool OpsColors::setColors( QString filename ) {
00132     QPixmap *temp = new QPixmap( 30, 20 );
00133 
00134     //just checking if colorscheme is removable...
00135     QFile test;
00136     test.setName( locateLocal( "appdata", filename ) ); //try filename in local user KDE directory tree.
00137     if ( test.exists() ) RemovePreset->setEnabled( true );
00138     else RemovePreset->setEnabled( false );
00139     test.close();
00140 
00141     ksw->loadColorScheme( filename );
00142     kcfg_StarColorMode->setCurrentItem( ksw->data()->colorScheme()->starColorMode() );
00143     kcfg_StarColorIntensity->setValue( ksw->data()->colorScheme()->starColorIntensity() );
00144 
00145     if ( ksw->map()->starColorMode() != ksw->data()->colorScheme()->starColorMode() )
00146         ksw->map()->setStarColorMode( ksw->data()->colorScheme()->starColorMode() );
00147 
00148     if ( ksw->map()->starColorIntensity() != ksw->data()->colorScheme()->starColorIntensity() )
00149         ksw->map()->setStarColorIntensity( ksw->data()->colorScheme()->starColorIntensity() );
00150 
00151     for ( unsigned int i=0; i < ksw->data()->colorScheme()->numberOfColors(); ++i ) {
00152         temp->fill( QColor( ksw->data()->colorScheme()->colorAt( i ) ) );
00153         ColorPalette->changeItem( *temp, ksw->data()->colorScheme()->nameAt( i ), i );
00154     }
00155 
00156     ksw->map()->forceUpdate();
00157     return true;
00158 }
00159 
00160 void OpsColors::slotAddPreset() {
00161     bool okPressed = false;
00162     QString schemename = KInputDialog::getText( i18n( "New Color Scheme" ),
00163                             i18n( "Enter a name for the new color scheme:" ),
00164                             QString::null, &okPressed, 0 );
00165 
00166     if ( okPressed && ! schemename.isEmpty() ) {
00167         if ( ksw->data()->colorScheme()->save( schemename ) ) {
00168             PresetBox->insertItem( schemename );
00169             PresetBox->setCurrentItem( PresetBox->findItem( schemename ) );
00170             QString fname = ksw->data()->colorScheme()->fileName();
00171             PresetFileList.append( fname );
00172             ksw->addColorMenuItem( schemename, QString("cs_" + fname.left(fname.find(".colors"))).utf8() );
00173         }
00174     }
00175 }
00176 
00177 void OpsColors::slotRemovePreset() {
00178     QString name = PresetBox->currentText();
00179     QString filename = PresetFileList[ PresetBox->currentItem() ];
00180     QFile cdatFile;
00181     cdatFile.setName( locateLocal( "appdata", "colors.dat" ) ); //determine filename in local user KDE directory tree.
00182 
00183     //Remove action from color-schemes menu
00184     ksw->removeColorMenuItem( QString("cs_" + filename.left( filename.find(".colors"))).utf8() );
00185 
00186     if ( !cdatFile.exists() || !cdatFile.open( IO_ReadWrite ) ) {
00187         QString message = i18n( "Local color scheme index file could not be opened.\nScheme cannot be removed." );
00188         KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
00189     } else {
00190         //Remove entry from the ListBox and from the QStringList holding filenames.
00191         //We don't want another color scheme to be selected, so first
00192         //temporarily disconnect the "highlighted" signal.
00193         disconnect( PresetBox, SIGNAL( highlighted( int ) ), this, SLOT( slotPreset( int ) ) );
00194         PresetBox->removeItem( PresetBox->currentItem() );
00195         PresetBox->setCurrentItem( -1 );
00196         RemovePreset->setEnabled( false );
00197         
00198         //Reconnect the "highlighted" signal
00199         connect( PresetBox, SIGNAL( highlighted( int ) ), this, SLOT( slotPreset( int ) ) );
00200 
00201         //Read the contents of colors.dat into a QStringList, except for the entry to be removed.
00202         QTextStream stream( &cdatFile );
00203         QStringList slist;
00204         bool removed = false;
00205 
00206         while ( !stream.eof() ) {
00207             QString line = stream.readLine();
00208             if ( line.left( line.find(':') ) != name ) slist.append( line );
00209             else removed = true;
00210         }
00211 
00212         if ( removed ) { //Entry was removed; delete the corresponding .colors file.
00213             QFile colorFile;
00214             colorFile.setName( locateLocal( "appdata", filename ) ); //determine filename in local user KDE directory tree.
00215             if ( !colorFile.remove() ) {
00216                 QString message = i18n( "Could not delete the file: %1" ).arg( colorFile.name() );
00217                 KMessageBox::sorry( 0, message, i18n( "Error Deleting File" ) );
00218             }
00219 
00220             //remove the old colors.dat file, and rebuild it with the modified string list.
00221             cdatFile.remove();
00222             cdatFile.open( IO_ReadWrite );
00223             QTextStream stream2( &cdatFile );
00224             for( unsigned int i=0; i<slist.count(); ++i )
00225                 stream << slist[i] << endl;
00226         } else {
00227             QString message = i18n( "Could not find an entry named %1 in colors.dat." ).arg( name );
00228             KMessageBox::sorry( 0, message, i18n( "Scheme Not Found" ) );
00229         }
00230         cdatFile.close();
00231     }
00232 }
00233 
00234 void OpsColors::slotStarColorMode( int i ) {
00235     ksw->data()->colorScheme()->setStarColorMode( i );
00236     if ( ksw->map()->starColorMode() != ksw->data()->colorScheme()->starColorMode() )
00237         ksw->map()->setStarColorMode( ksw->data()->colorScheme()->starColorMode() );
00238 
00239     if ( ksw->data()->colorScheme()->starColorMode() != 0 ) //mode is not "Real Colors"
00240         kcfg_StarColorIntensity->setEnabled( false );
00241     else
00242         kcfg_StarColorIntensity->setEnabled( true );
00243 }
00244 
00245 void OpsColors::slotStarColorIntensity( int i ) {
00246     ksw->data()->colorScheme()->setStarColorIntensity( i );
00247     if ( ksw->map()->starColorIntensity() != ksw->data()->colorScheme()->starColorIntensity() )
00248         ksw->map()->setStarColorIntensity( ksw->data()->colorScheme()->starColorIntensity() );
00249 
00250 }
00251 
00252 #include "opscolors.moc"

kstars

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

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal