casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DS9FileWriter.qo.h
Go to the documentation of this file.
00001 //# DS9FileWriter.qo.h: DS9 implementation of RSFileWriter classes.
00002 //# Copyright (C) 2008
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id$
00027 #ifndef DS9FILEWRITER_QO_H_
00028 #define DS9FILEWRITER_QO_H_
00029 
00030 #include <QTextStream>
00031 
00032 #include <display/RegionShapes/QtDS9WriteOptions.ui.h>
00033 #include <display/RegionShapes/RSFileReaderWriter.h>
00034 #include <display/RegionShapes/DS9FileReader.h>
00035 
00036 #include <casa/namespace.h>
00037 
00038 namespace casa {
00039 
00040 class RSMarker;
00041 class AnnRegion;
00042 
00043 // Implementation of RegionFileWriter for writing to DS9 files.
00044 class DS9FileWriter : public RSFileWriter {
00045 public:
00046     // Options constants.
00047     // </group>
00048     static const String CSYS_INDIVIDUAL;
00049     static const String CSYS_IMAGE;
00050     static const String CSYS_B1950;
00051     static const String CSYS_J2000;
00052     static const String CSYS_GALACTIC;
00053     static const String CSYS_ECLIPTIC;
00054     static const String UNIT_IPIXELS;
00055     static const String UNIT_DEGREES;
00056     static const String UNIT_RADIANS;
00057     static const String PUNIT_SEXAGESIMAL;
00058     static const String PUNIT_HMS;
00059     static const String PUNIT_DMS;
00060     static const String SUNIT_ARCSEC;
00061     static const String SUNIT_ARCMIN;
00062     static const String DEFAULT_COMMENTS;
00063     // </group>
00064     
00065     // Converts a coordinate system constant (as used in DS9RFWOptions) into
00066     // its DS9::CoordinateSystem equivalent.
00067     static DS9::CoordinateSystem coordinateSystem(const String& system) {
00068         if(system == CSYS_IMAGE) return DS9::Image;
00069         else if(system == CSYS_B1950) return DS9::FK4;
00070         else if(system == CSYS_J2000) return DS9::FK5;
00071         else if(system == CSYS_GALACTIC) return DS9::Galactic;
00072         else if(system == CSYS_ECLIPTIC) return DS9::Ecliptic;
00073         
00074         else return DS9::defaultCoordinateSystem();
00075     }
00076     
00077     // Returns true if the given CASA world system is supported by DS9.
00078     static bool isValidDS9System(MDirection::Types worldSys) {
00079         return worldSys == MDirection::B1950 ||
00080                worldSys == MDirection::J2000 ||
00081                worldSys == MDirection::GALACTIC ||
00082                worldSys == MDirection::ECLIPTIC;
00083     }
00084     
00085     
00086     // Constructor.
00087     DS9FileWriter();
00088     
00089     // Destructor.
00090     ~DS9FileWriter();
00091     
00092     
00093     // Implements RegionFileWriter::optionsWidget.
00094     QWidget* optionsWidget() const;
00095     
00096     // Implements RegionFileWriter::setOptions.
00097     void setOptions(const QWidget* widget);
00098 
00099     // Implements RegionFileWriter::write.
00100     bool write(const vector<RegionShape*>& shapes) const;
00101 
00102     bool writeHeader( QTextStream & ) const;
00103     bool writeFooter( QTextStream & ) const;
00104     bool write( QTextStream &, AnnRegion * ) const;
00105     
00106 private:
00107     // Custom options.
00108     // <group>
00109     bool m_custCoordSys;
00110     bool m_pixelCoordSys;
00111     String m_coordSys;
00112     String m_posUnits, m_sizeUnits;
00113     int m_precision;
00114     String m_comments;
00115     // </group>
00116     
00117     
00118     // Rests custom options to defaults.
00119     void resetOptions() const;
00120     
00121     // Writes header comments to the file.
00122     bool writeHeader(QTextStream& file, stringstream& errors) const;
00123     
00124     // Writes globals (default DS9Region properties) and, if applicable, the
00125     // overriding coordinate system.
00126     bool writeGlobals(QTextStream& file, stringstream& errors) const;
00127     
00128     // Writes a single shape to the file.
00129     bool writeShape(QTextStream& file, stringstream& errors,
00130                     const RegionShape* shape, bool isComposite = false) const;
00131 
00132     
00133     // Returns list of all properties to be used in the globals.
00134     static vector<String> globalProperties() {
00135         static vector<String> v(14);
00136         
00137         v[0]  = DS9Region::PROP_COLOR;    v[1]  = DS9Region::PROP_DASH;
00138         v[2]  = DS9Region::PROP_DASHLIST; v[3]  = DS9Region::PROP_DELETE;
00139         v[4]  = DS9Region::PROP_EDIT;     v[5]  = DS9Region::PROP_FIXED;
00140         v[6]  = DS9Region::PROP_FONT;     v[7]  = DS9Region::PROP_HIGHLITE;
00141         v[8]  = DS9Region::PROP_INCLUDE;  v[9]  = DS9Region::PROP_MOVE;
00142         v[10] = DS9Region::PROP_ROTATE;   v[11] = DS9Region::PROP_SELECT;
00143         v[12] = DS9Region::PROP_SOURCE;   v[13] = DS9Region::PROP_WIDTH;
00144         
00145         return v;
00146     }
00147     
00148     // Finds the DS9 region type for the given shape.  Returns true if no
00149     // errors were reported, false otherwise.  If errors occurred they are
00150     // logged on the given stringstream.  Note that RSMarkers are expected
00151     // to already be converted to a DS9-friendly format.  If this method
00152     // returns false, the given shape should NOT be written.
00153     static bool regionType(const RegionShape* shape, DS9::RegionType& type,
00154                            stringstream& errors);
00155     static bool regionType(const AnnRegion* shape, DS9::RegionType& type,
00156                            stringstream& errors);
00157     
00158     // Converts the given RSMarker into one or more DS9-friendly RSMarkers.
00159     // If marker is NULL or the markers is empty then an empty vector is
00160     // returned.  Note that the caller is responsible for deleting the new
00161     // markers.
00162     static vector<RSMarker*> convertMarker(const RSMarker* marker,
00163                                            bool& conversionWasNeeded);
00164     
00165     // Converts the given value to a QString version using the given units.
00166     // The value is assumed to be in units RegionShape::UNIT.  toUnits should
00167     // be one of: UNIT_IPIXELS, UNIT_DEGREES, UNIT_RADIANS, PUNIT_HMS,
00168     // PUNIT_DMS, SUNIT_ARCSEC, SUNIT_ARCMIN, or "".  If toUnits is blank, no
00169     // converting or adding a unit is done.
00170     static QString convertValue(double value, const String& toUnits,
00171                                 int precision);
00172     
00173     // Converts the given position unit, if applicable, from sexagesimal to
00174     // either HMS or DMS.  Does nothing if the position unit is not
00175     // sexagesimal.
00176     static String sgUnit(const String& posUnit, int index,
00177                          const String& coordSys) {
00178         if(posUnit == PUNIT_SEXAGESIMAL) {
00179             if(index % 2 == 0 && coordSys != CSYS_GALACTIC) return PUNIT_HMS;
00180             else return PUNIT_DMS;
00181         } else return posUnit;
00182     }
00183 };
00184 
00185 
00186 // Widget used for entering options specific to the DS9 format.
00187 class QtDS9RFWOptions : public QWidget, Ui::DS9WriteOptions {
00188     Q_OBJECT
00189     
00190 public:
00191     // Constructor.
00192     QtDS9RFWOptions();
00193     
00194     // Destructor.
00195     ~QtDS9RFWOptions();
00196     
00197     
00198     // Returns true if the user picked a custom coordinate system, false if
00199     // DS9RegionFileWriter::CSYS_INDIVIDUAL was chosen.
00200     bool isCustomCoordinateSystem() const;
00201     
00202     // Returns true if the user picked a custom coordinate system and that
00203     // system was DS9RegionFileWriter::CSYS_IMAGE, false otherwise.
00204     bool isPixelCoordinateSystem() const;
00205     
00206     // Returns the selected coordinate system.  This should only be used if
00207     // customCoordinateSystem() and pixelCoordinateSystem() were both false.
00208     // One of: DS9RegionFileWriter::CSYS_*
00209     String getCoordinateSystem() const;
00210     
00211     // Returns the selected position units.
00212     // One of: DS9RegionFileWriter::UNIT_* or DS9RegionFileWriter::PUNIT_*
00213     String getPositionUnits() const;
00214     
00215     // Returns the selected size units.
00216     // One of: DS9RegionFileWriter::UNIT_* or DS9RegionFileWriter::SUNIT_*
00217     String getSizeUnits() const;
00218     
00219     // Returns the entered decimal precision.
00220     int getPrecision() const;
00221     
00222     // Returned the entered comments.
00223     String getComments() const;
00224     
00225 private slots:
00226     // Enable/disable position and size unit chooser accordingly.
00227     void coordinateSystemChanged(int index);
00228 };
00229 
00230 }
00231 
00232 #endif /* DS9FILEWRITER_H_ */