casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
RSFileReaderWriter.h
Go to the documentation of this file.
00001 //# RSFileReaderWriter.h: Interfaces for classes that read/write shape files.
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 RSFILEREADERWRITER_H_
00028 #define RSFILEREADERWRITER_H_
00029 
00030 #include <display/RegionShapes/RegionShape.h>
00031 #include <casa/Containers/Record.h>
00032 #include <display/Display/PanelDisplay.h>
00033 #include <coordinates/Coordinates/CoordinateSystem.h>
00034 
00035 #include <utility>
00036 
00037 #include <casa/namespace.h>
00038 using namespace std;
00039 
00040 class QWidget;
00041 
00042 namespace casa {
00043 
00044 class RSFileReader;
00045 class RSFileWriter;
00046 
00047 // Convenience class for a String/bool pair.
00048 class RFError {
00049 public:
00050     // Constructor, blank error.
00051     RFError();
00052     
00053     // Constructor, error with the given text and isFatal flag.
00054     RFError(const String& error, bool isFatal = false);
00055     
00056     // Destructor.
00057     ~RFError();
00058     
00059     
00060     // Returns whether this error was fatal or not.
00061     bool isFatal() const;
00062     
00063     // Returns this error's text.
00064     const String& error() const;
00065     
00066     // Sets the error.
00067     void set(const String& error, bool isFatal = false);
00068     
00069 private:
00070     String m_error;
00071     bool m_fatal;
00072 };
00073 
00074 
00075 // Superclass for readers and writers containing common definitions and
00076 // operations.
00077 class RSFileReaderWriter {
00078 public:
00079     // Public Static Methods //
00080     
00081     // An enum of all known subclasses/formats supported.
00082     enum SupportedType {
00083         DS9, CASA_XML
00084     };
00085     
00086     // Converts between enum and String for SupportedType.
00087     // <group>
00088     static SupportedType supportedType(String type);
00089     static String supportedType(SupportedType type);
00090     // </group>
00091 
00092     // Returns the file extension for the given SupportedType.
00093     static String extensionForType(SupportedType type);
00094 
00095     // Returns all known SupportedTypes.
00096     // <group>
00097     static vector<SupportedType> supportedTypes();
00098     static vector<String> supportedTypesStrings();
00099     // </group>
00100 
00101     // Returns an appropriate child RegionFileReader class for the given
00102     // SupportedType, or NULL for an error (shouldn't happen).
00103     static RSFileReader* readerForType(SupportedType type);
00104     
00105     // Returns an new appropriate child RegionFileWriter class for the given
00106     // SupportedType, or NULL for an error (shouldn't happen).
00107     static RSFileWriter* writerForType(SupportedType type);
00108     
00109     // Returns an new appropriate options widget for the given SupportedType,
00110     // or NULL for an error (shouldn't happen).
00111     static QWidget* optionsWidgetForType(SupportedType type);
00112     
00113     
00114     // Non-Static Members //
00115     
00116     // Constructor.
00117     RSFileReaderWriter() { }
00118     
00119     // Destructor.
00120     virtual ~RSFileReaderWriter() { }
00121     
00122     // Sets the file to be read/written to the given.
00123     virtual void setFile(const String& filename);
00124     
00125     // Returns the last error set during read/write.
00126     virtual const RFError& lastError() const;
00127     
00128 protected:
00129     // Filename to be read/written.
00130     String m_filename;
00131     
00132     // Last error seen during read/write.
00133     RFError m_lastError;
00134     
00135     // Convenience method for setting last error during read/write.
00136     virtual void setError(const String& error, bool isFatal = false) const;
00137 };
00138 
00139 
00140 // Abstract superclass for any class that reads a format that produces
00141 // RegionShapes from a file.
00142 class RSFileReader : public virtual RSFileReaderWriter {
00143 public:
00144     // Constructor.
00145     RSFileReader() { }
00146 
00147     // Destructor.
00148     virtual ~RSFileReader() { }
00149 
00150     // Read the filename set with setFile and returns true if no errors were
00151     // reported, false otherwise.  If false is returned, the details can be
00152     // found using lastError().  Any valid RegionShapes that were read from the
00153     // file are placed in the given vector (which is cleared first).
00154     virtual bool read(vector<RegionShape*>& readShapes) = 0;
00155     
00156     // Calls setFile() then read().
00157     virtual bool readFile(const String& file, vector<RegionShape*>& shapes) {
00158         setFile(file);
00159         return read(shapes);
00160     }
00161 };
00162 
00163 
00164 // Abstract superclass for any class that writes RegionShapes to a region
00165 // file format.
00166 class RSFileWriter : public virtual RSFileReaderWriter {
00167 public:
00168     // Constructor.
00169     RSFileWriter() { }
00170     
00171     // Destructor.
00172     virtual ~RSFileWriter() { }    
00173     
00174     // Provides a custom widget that can be used to get/set options specific
00175     // to each format type.
00176     virtual QWidget* optionsWidget() const = 0;
00177     
00178     // Sets the options to the values given in the widget.  May ignore invalid
00179     // widgets (i.e., widgets different from the kind provided by
00180     // optionsWidget()).
00181     virtual void setOptions(const QWidget* widget) = 0;
00182 
00183     // Write the given regions to the filename set with setFile and returns
00184     // true if no errors were reported, false otherwise.  If false is returned,
00185     // the details can be found using lastError().
00186     virtual bool write(const vector<RegionShape*>& shapes) const = 0;    
00187     
00188     // Calls setFile then write.
00189     virtual bool writeFile(const String& filename,
00190                            const vector<RegionShape*>& shapes) {
00191         setFile(filename);
00192         return write(shapes);
00193     }
00194 };
00195 
00196 }
00197 
00198 #endif /*RSFILEREADERWRITER_H_*/