dmsbox.cpp 4.76 KB
Newer Older
Chris Howells's avatar
adding  
Chris Howells committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***************************************************************************
                          dmsbox.cpp  -  description
                             -------------------
    begin                : wed Dec 19 2001
    copyright            : (C) 2001-2002 by Pablo de Vicente
    email                : vicente@oan.es
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <stdlib.h>
#include "dmsbox.h"
#include "dms.h"
21 22 23 24 25

#include <qstringlist.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
Chris Howells's avatar
adding  
Chris Howells committed
26 27 28 29 30
#include <qlabel.h>
#include <qhbox.h>
#include <qlineedit.h>
#include <qstring.h>
#include <qwidget.h>
Ralf Nolden's avatar
Ralf Nolden committed
31
#include <qregexp.h>
Chris Howells's avatar
adding  
Chris Howells committed
32

33
dmsBox::dmsBox(QWidget *parent, const char *name, bool dg) : QLineEdit(parent,name) {
Chris Howells's avatar
adding  
Chris Howells committed
34 35

	
36
//	QHBox * dBox = new QHBox(parent,name);
Chris Howells's avatar
adding  
Chris Howells committed
37
	
38
//	dmsName = new QLineEdit( dBox,"dmsName");
Chris Howells's avatar
adding  
Chris Howells committed
39
	
40
//	dBox->setSpacing(1);
41
//	setStretchFactor(dBox,0);
42
//	dBox->setMargin(6);
Chris Howells's avatar
adding  
Chris Howells committed
43

44 45 46 47
//	dmsName->setMaxLength(14);
//	dmsName->setMaximumWidth(160);
	setMaxLength(14);
	setMaximumWidth(160);
Chris Howells's avatar
adding  
Chris Howells committed
48

49
//	dBox->setMaximumWidth(180);
Chris Howells's avatar
adding  
Chris Howells committed
50 51 52 53 54 55 56 57

	deg = dg;
}

void dmsBox::showInDegrees (dms d)
{
	double seconds = d.getArcSec() + d.getmArcSec()/1000.;

58
	setDMS ( QString("%1 %2 %3").arg(d.degree(),2).arg(d.getArcMin(),2).arg(seconds,6,'f',2) );
Chris Howells's avatar
adding  
Chris Howells committed
59 60 61 62 63
}

void dmsBox::showInHours (dms d)
{
	double seconds = d.second() + d.msecond()/1000.;
64 65

	setDMS ( QString("%1 %2 %3").arg(d.hour(),2).arg(d.minute(),2).arg(seconds,6,'f',3) );
Chris Howells's avatar
adding  
Chris Howells committed
66 67 68 69 70 71 72 73 74 75 76

}

void dmsBox::show(dms d)
{
	if (deg)
		showInDegrees(d);
	else
		showInHours(d);
}

77
dms dmsBox::createDms ( bool *ok )
Chris Howells's avatar
adding  
Chris Howells committed
78
{
79 80 81 82 83 84
//	QString entry;
	int d = 0, m = 0;
	double s = 0.0;
	dms dmsAng;
	bool valueFound = false, badEntry = false , checkValue = false;

85 86
//Initialize bool for result
	if ( ok != NULL ) *ok = false;
87

Malcolm Hunter's avatar
Malcolm Hunter committed
88
//	QString errMsg = i18n( "Unable to parse %1 entry.  Specify a %1 value as a simple integer, a floating-point number, or a triplet of values using colons or spaces as separators." );
89

90
	QString entry = text().stripWhiteSpace();
91 92 93 94 95 96 97 98

	//Try simplest cases: integer or double representation

	d = entry.toInt( &checkValue );
	if ( checkValue ) {
		if (deg) dmsAng.setD( d, 0, 0 );
		else dmsAng.setH( d, 0, 0 );
		valueFound = true;
99
		if ( ok != NULL ) *ok = true;
100 101 102 103 104 105 106
		return dmsAng;
	} else {
		double x = entry.toDouble( &checkValue );
		if ( checkValue ) {
			if ( deg ) dmsAng.setD( x );
			else dmsAng.setH( x );
			valueFound = true;
107
			if ( ok != NULL ) *ok = true;
108 109
			return dmsAng;
		}
Chris Howells's avatar
adding  
Chris Howells committed
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

	//no success yet...try assuming multiple fields

	if ( !valueFound ) { 
		QStringList fields;
		
		//check for colon-delimiters or space-delimiters
		if ( entry.contains(':') ) 
			fields = QStringList::split( ':', entry );
		else fields = QStringList::split( " ", entry ); 

		// If two fields we will add a third one, and then parse with 
		// the 3-field code block. If field[1] is a double, convert 
		// it to integer arcmin, and convert the remainder to arcsec
		 
		if ( fields.count() == 2 ) {
			double mx = fields[1].toDouble( &checkValue );
			if ( checkValue ) {
				fields[1] = QString("%1").arg( int(mx) );
				fields.append( QString("%1").arg( int( 60.0*(mx - int(mx)) ) ) );
			} else {
				fields.append( QString( "0" ) );
			}
		}
Chris Howells's avatar
adding  
Chris Howells committed
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
		// Three fields space-delimited ( h/d m s ); 
		// ignore all after 3rd field

		if ( fields.count() >= 3 ) {
			fields[0].replace( QRegExp("h"), "" );
			fields[0].replace( QRegExp("d"), "" );
			fields[1].replace( QRegExp("m"), "" );
			fields[2].replace( QRegExp("s"), "" );
		}
		//See if first two fields parse as integers.
		//
		d = fields[0].toInt( &checkValue );
		if ( !checkValue ) badEntry = true;
		m = fields[1].toInt( &checkValue );
		if ( !checkValue ) badEntry = true;
		s = fields[2].toDouble( &checkValue );
		if ( !checkValue ) badEntry = true;

		if ( !badEntry ) {
			valueFound = true;
			double D = (double)abs(d) + (double)m/60. 
					+ (double)s/3600.;
			if ( d <0 ) {D = -1.0*D;}

160 161
			if ( ok != NULL ) *ok = true;

162 163 164 165 166 167 168
			if (deg) {  	
				return	dms( D );
			} else {
				dms h;
				h.setH (D);
				return	h;
			}
169 170
		} else {
			if ( ok != NULL ) *ok = false;
171
		}
Chris Howells's avatar
adding  
Chris Howells committed
172 173
	}

174
//	 if ( !valueFound )
175
//		KMessageBox::sorry( 0, errMsg.arg( "Angle" ), i18n( "Could not Set Value" ) );
Chris Howells's avatar
adding  
Chris Howells committed
176

177 178

	return dmsAng;
Chris Howells's avatar
adding  
Chris Howells committed
179 180 181 182
}

dmsBox::~dmsBox(){
}