summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/main.cpp
blob: c9024d33bab5900ed9757cf436b43fb831212f33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/***************************************************************************
                          main.cpp  -  Trinity Desktop Planetarium
                             -------------------
    begin                : Mon Feb  5 01:11:45 PST 2001
    copyright            : (C) 2001 by Jason Harris
    email                : jharris@30doradus.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tdecmdlineargs.h>
#include <tdeaboutdata.h>
#include <dcopclient.h>
#include <tdelocale.h>
#include <kdebug.h>

#include "kstars.h"
#include "kstarsdata.h"
#include "kstarsdatetime.h"
#include "skymap.h"
#include "simclock.h"
#include "ksnumbers.h"
#include "Options.h"

#define KSTARS_VERSION "1.2.9"

static const char description[] =
	I18N_NOOP("Desktop Planetarium");
static const char notice[] =
	I18N_NOOP("Some images in KStars are for non-commercial use only.  See README.images.");


static TDECmdLineOptions options[] =
{
	{ "!dump", I18N_NOOP( "Dump sky image to file" ), 0 },
	{ "script ", I18N_NOOP( "Script to execute" ), 0 },
	{ "width ", I18N_NOOP( "Width of sky image" ), "640" },
	{ "height ", I18N_NOOP( "Height of sky image" ), "480" },
	{ "filename ", I18N_NOOP( "Filename for sky image" ), "kstars.png" },
	{ "date ", I18N_NOOP( "Date and time" ), "" },
	{ "!paused", I18N_NOOP( "Start with clock paused" ), 0 },
	TDECmdLineLastOption
};

int main(int argc, char *argv[])
{
	TDEAboutData aboutData( "kstars", I18N_NOOP("KStars"),
		KSTARS_VERSION, description, TDEAboutData::License_GPL,
		I18N_NOOP("(c) 2001-2003, The KStars Team"), notice, "http://edu.kde.org/kstars");
	aboutData.addAuthor("Jason Harris",0, "jharris@30doradus.org", "http://www.30doradus.org");
	aboutData.addAuthor("Heiko Evermann",0, "heiko@evermann.de", "http://www.evermann.de");
	aboutData.addAuthor("Thomas Kabelmann", 0, "tk78@gmx.de", 0);
	aboutData.addAuthor("Pablo de Vicente", 0, "pvicentea@wanadoo.es", 0);
	aboutData.addAuthor("Jasem Mutlaq", 0, "mutlaqja@ikarustech.com", 0 );
	aboutData.addAuthor("Carsten Niehaus", 0, "cniehaus@gmx.de", 0);
	aboutData.addAuthor("Mark Hollomon", 0, "mhh@mindspring.com", 0);
	TDECmdLineArgs::init( argc, argv, &aboutData );
	TDECmdLineArgs::addCmdLineOptions( options ); // Add our own options.
	TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

	TDEApplication a;

	if ( args->isSet( "dump" ) ) {
		kdDebug() << i18n( "Dumping sky image" ) << endl;

		//parse filename and image format
		const char* format = "PNG";
		TQString fname = args->getOption( "filename" );
		TQString ext = fname.mid( fname.findRev(".")+1 );
		if ( ext.lower() == "png" ) { format = "PNG"; }
		else if ( ext.lower() == "jpg" || ext.lower() == "jpeg" ) { format = "JPG"; }
		else if ( ext.lower() == "gif" ) { format = "GIF"; }
		else if ( ext.lower() == "pnm" ) { format = "PNM"; }
		else if ( ext.lower() == "bmp" ) { format = "BMP"; }
		else { kdWarning() << i18n( "Could not parse image format of %1; assuming PNG." ).arg( fname ) << endl; }

		//parse width and height
		bool ok(false);
		int w(0), h(0);
		w = args->getOption( "width" ).toInt( &ok );
		if ( ok ) h =  args->getOption( "height" ).toInt( &ok );
		if ( !ok ) {
			kdWarning() << "Unable to parse arguments: " << endl;
			kdWarning() << "Width: " << args->getOption( "width" )
				<< "  Height: " << args->getOption( "height" ) << endl;
			return 1;
		}

		KStarsData *dat = new KStarsData();
		TQObject::connect( dat, TQT_SIGNAL( progressText(TQString) ), dat, TQT_SLOT( slotConsoleMessage(TQString) ) );
		dat->initialize();
		while (!dat->startupComplete) { kapp->processEvents(50); }

		//Set Geographic Location
		dat->setLocationFromOptions(); 

		//Set color scheme
		dat->colorScheme()->loadFromConfig( kapp->config() );

		//set clock now that we have a location:
		//Check to see if user provided a date/time string.  If not, use current CPU time
		TQString datestring = args->getOption( "date" );
		KStarsDateTime kdt;
		if ( ! datestring.isEmpty() ) {
			if ( datestring.contains( "-" ) ) { //assume ISODate format
				if ( datestring.contains( ":" ) ) { //also includes time
					kdt = KStarsDateTime::fromString( datestring, TQt::ISODate );
				} else { //string probably contains date only
					kdt.setDate( ExtDate::fromString( datestring, TQt::ISODate ) );
					kdt.setTime( TQTime( 0, 0, 0 ) );
				}
			} else { //assume Text format for date string
				kdt = dat->geo()->LTtoUT( KStarsDateTime::fromString( datestring, TQt::TextDate ) );
			}
			
			if ( ! kdt.isValid() ) {
				kdWarning() << i18n( "Could not parse Date/Time string: " ) << datestring << endl;
				kdWarning() << i18n( "Valid date formats: " ) << endl;
				kdWarning() << "  1950-02-25  ;  1950-02-25 05:30:00" << endl;
				kdWarning() << "  Feb 25 1950 ;  Feb 25 1950 05:30:00" << endl;
				kdWarning() << "  25 Feb 1950 ;  25 Feb 1950 05:30:00" << endl;
				kdWarning() << i18n( "Using CPU date/time instead." ) << endl;
				
				kdt = dat->geo()->LTtoUT( KStarsDateTime::currentDateTime() );
			}
		} else { 
			kdt = dat->geo()->LTtoUT( KStarsDateTime::currentDateTime() );
		}
		dat->clock()->setUTC( kdt );
		
		KSNumbers num( dat->ut().djd() );
		dat->initGuides(&num);

		SkyMap *map = new SkyMap( dat );
		map->resize( w, h );
		TQPixmap sky( w, h );

		map->setDestination( new SkyPoint( Options::focusRA(), Options::focusDec() ) );
		map->destination()->EquatorialToHorizontal( dat->lst(), dat->geo()->lat() );
		map->setFocus( map->destination() );
		map->focus()->EquatorialToHorizontal( dat->lst(), dat->geo()->lat() );

		//Execute the specified script
		TQString scriptfile = args->getOption( "script" );
		if ( ! scriptfile.isEmpty() ) {
			if ( dat->executeScript( scriptfile, map ) ) {
				std::cout << i18n( "Script executed." ).utf8().data() << std::endl;
			} else {
				kdWarning() << i18n( "Could not execute script." ) << endl;
			}
		}

		dat->setFullTimeUpdate();
		dat->updateTime(dat->geo(), map );

		kapp->processEvents(100000);
		map->setMapGeometry();
		map->exportSkyImage( TQT_TQPAINTDEVICE(&sky) );
		kapp->processEvents(100000);

		if ( ! sky.save( fname, format ) ) kdWarning() << i18n( "Unable to save image: %1 " ).arg( fname ) << endl;
		else kdDebug() << i18n( "Saved to file: %1" ).arg( fname ) << endl;

		delete map;
		delete dat;
		return 0;
	}

	//start up normally in GUI mode
	
	//Try to parse the given date string
	TQString datestring = args->getOption( "date" );

	if ( ! datestring.isEmpty() && ! KStarsDateTime::fromString( datestring ).isValid() ) {
		kdWarning() << i18n("Specified date (%1) is invalid.  Will use current CPU date instead." ).arg( datestring ) << endl;
		datestring = "";
	}
	
	new KStars( true, ! args->isSet( "paused" ), datestring );
	args->clear();
	TQObject::connect(kapp, TQT_SIGNAL(lastWindowClosed()), kapp, TQT_SLOT(quit()));
	return a.exec();

}