casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
QtRegionShapeManager.qo.h
Go to the documentation of this file.
00001 //# QtRegionShapeManager.qo.h: Classes for managing/loading region shapes.
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 QTREGIONSHAPEMANAGER_QO_H_
00028 #define QTREGIONSHAPEMANAGER_QO_H_
00029 
00030 #include <QDialog>
00031 #include <QStackedLayout>
00032 #include <QtXml>
00033 
00034 #include <display/RegionShapes/QtRegionShapeManager.ui.h>
00035 #include <display/RegionShapes/QtRSFileLoader.ui.h>
00036 #include <display/RegionShapes/QtRSFileSaver.ui.h>
00037 
00038 #include <display/RegionShapes/RegionShape.h>
00039 
00040 #include <casa/namespace.h>
00041 
00042 namespace casa {
00043 
00044 class QtDisplayPanel;
00045 class RSFileReader;
00046 class RSFileWriter;
00047 class QtSingleRegionShape;
00048 
00049 // Region Shape Manager.  Main interaction between region shapes and user.
00050 // Provides functionality to:
00051 // <ul><li>Load region shapes from a file</li>
00052 //     <li>Display loaded region shapes</li>
00053 //     <li>Show/hide loaded region shapes</li>
00054 //     <li>Delete loaded region shapes</li>
00055 //     <li>Edit region shapes</li></ul>
00056 class QtRegionShapeManager : public QWidget, Ui::RegionShapeManager {
00057     Q_OBJECT
00058 
00059 public:
00060     // Constant message used when a error was reported during a shape drawing.    
00061     static const String DRAWERRORMSG;
00062     
00063     
00064     // Constructor which takes the parent panel.
00065     QtRegionShapeManager(QtDisplayPanel* panel);
00066 
00067     // Destructor.
00068     ~QtRegionShapeManager();
00069 
00070     // Returns the parent panel.
00071     QtDisplayPanel* panel() const;
00072     
00073     // Appends an XML state representation of loaded shapes to the given
00074     // document.
00075     void saveState(QDomDocument& document);
00076     
00077     // Restores loaded shapes from the given document.
00078     void restoreState(QDomDocument& document);
00079     
00080     // Returns the number of shapes in the manager.  If includeComposites is
00081     // false, composites are not counted towards the return total.  Composite
00082     // children are not included.
00083     unsigned int numShapes(bool includeComposites = true) const;
00084     
00085     // Returns the shapes in the manager.  If include composites is false,
00086     // composites are not included.  Composite children are not included.
00087     vector<RegionShape*> shapes(bool includeComposites = true) const;
00088     
00089     // Returns the QtSingleRegionShape wrapper for the given shape, or NULL
00090     // for invalid.
00091     QtSingleRegionShape* shapeWidget(RegionShape* shape) const;
00092     
00093     // Shows the given error message (should be short).
00094     void showSimpleError(const String& message, bool warn = true) const;
00095     
00096     // Shows the given detailed error message.  "message" should contain a
00097     // short overview while "details" should contain longer information.
00098     void showDetailedError(const String& message, const String& details,
00099                            bool warn = true) const;
00100     
00101 public slots:
00102     // Adds the given shape to the manager.  Should only be used for individual
00103     // shapes, since the display panel will refresh after every call which can
00104     // get very slow with many shapes.  For multiple shapes, use addShapes().
00105     // The given shape becomes owned by the manager which is responsible for
00106     // its deletion.
00107     void addShape(RegionShape* shape);
00108     
00109     // Adds the given shapes to the manager.  Holds the drawing until all
00110     // shapes have been added.  The given shapes become owned by the manager
00111     // which is responsible for their deletion.
00112     void addShapes(const vector<RegionShape*>& shapes);
00113     
00114     // Removes and (optionally) deletes the given shape from the manager.
00115     void removeShape(RegionShape* shape, bool deleteShape = true);    
00116     
00117     // Delete all loaded region shapes.
00118     void deleteAll();
00119 
00120     // Enables the manager.
00121     void enable() { setEnabled(true); }
00122 
00123 private:
00124     QtDisplayPanel* m_panel;               // Parent panel.
00125     vector<QtSingleRegionShape*> m_shapes; // Loaded region shapes.
00126     String m_lastDirectory,                // Last loaded directory, file,
00127            m_lastFile,                     // and format.  Initially empty.
00128            m_lastFormat;
00129     
00130     // Adds the given shape with the given composite parent (or NULL if the
00131     // shape is not a composite child) to the manager.
00132     void addShape(RegionShape* shape, RegionShape* compositeParent);
00133     
00134     // Adds the given shapes with the given composite parents (or NULL if the
00135     // shapes do not not parents) to the manager.
00136     void addShapes(const vector<RegionShape*>& shapes,
00137                    const vector<RegionShape*>& compositeParents);
00138     
00139     
00140     // XML attributes.
00141     // <group>
00142     static const QString HIDDEN;
00143     static const QString LAST_DIRECTORY;
00144     static const QString LAST_FILE;
00145     static const QString LAST_FORMAT;
00146     static const QString WINDOW_VISIBLE;
00147     // </group>
00148     
00149 private slots:
00150     // Show or hide all loaded region shapes.
00151     void showHideAll(bool checked);
00152 
00153     // Load region shapes from a file.
00154     void load();
00155     
00156     // Saves loaded region shapes from a file.
00157     void save();
00158 
00159     // Create a new shape.
00160     void newShape();
00161 
00162     // Dismiss/close window.
00163     void dismiss();
00164 };
00165 
00166 
00167 // Class for loading region shape files.  The main use is
00168 // QtRSFileLoader::getFileReader(), which prompts the user for a filename
00169 // (with file chooser dialog) and a file format and then returns a RSFileReader
00170 // appropriate for reading that file.
00171 class QtRSFileLoader : public QDialog, Ui::RSFileLoader {
00172     Q_OBJECT
00173 
00174 public:
00175     // Constructor.  The initial file and format will be set to the given, and
00176     // if a filechooser is opened the starting directory will be set to the
00177     // given.
00178     QtRSFileLoader(String file = "", String format = "", String dir = "");
00179 
00180     // Destructor.
00181     ~QtRSFileLoader();
00182 
00183 
00184     // Opens a new QtRSFileLoader so that the user can input a filename and
00185     // file format, and then returns a RSFileReader appropriate for reading
00186     // that file.  Returns NULL if the user cancels or if an error occurs
00187     // (shouldn't happen).  If the String* arguments are given, they will be
00188     // used for the initial settings and then updated to show the opened
00189     // file, format, and directory, respectively.
00190     static RSFileReader* getFileReader(String* file = NULL,
00191                                        String* format = NULL,
00192                                        String* directory = NULL);
00193 
00194 private:
00195     QString m_lastDir;   // directory to start filechooser
00196     
00197     // Gets the current filename chosen by the user.
00198     String getFilename();
00199 
00200     // Gets the directory of the filename chosen by the user.
00201     String getDirectory();
00202     
00203     // Gets the current region file format chosen by the user.
00204     // Guaranteed to be one of the values in
00205     // RegionFileReader::supportedTypesStrings().
00206     String getFormat();
00207     
00208 private slots:
00209     // Show a file chooser dialog.
00210     void browse();
00211     
00212     // Check that the entered file is valid, then accept().
00213     void ok();
00214 };
00215 
00216 
00217 // Class for saving region files.  The main use is
00218 // QtRSFileSaver::getFileWriter(), which prompts the user for a filename
00219 // (with file chooser dialog), a file format, and options specific to the file
00220 // format, and then returns a RSFileWriter appropriate for writing that file.
00221 class QtRSFileSaver : public QDialog, Ui::RSFileSaver {
00222     Q_OBJECT
00223     
00224 public:
00225     // Constructor.  The initial file and format will be set to the given, and
00226     // if a filechooser is opened the starting directory will be set to the
00227     // given.
00228     QtRSFileSaver(String file = "", String format = "", String dir = "");
00229     
00230     // Destructor.
00231     ~QtRSFileSaver();
00232 
00233     
00234     // Opens a new QtRegionFileSaver so that the user can input a filename,
00235     // file format, and format options, and then returns a RegionFileWriter
00236     // appropriate for writing that file.  Returns NULL if the user cancels or
00237     // an error occurs (shouldn't happen).  If the String* arguments are given,
00238     // they will be used for the initial settings and then updated to show the
00239     // saved file, format, and directory, respectively.
00240     static RSFileWriter* getFileWriter(String* file = NULL,
00241                                        String* format = NULL,
00242                                        String* directory = NULL);
00243     
00244 private:
00245     QString m_lastDir;  // directory to start filechooser
00246     
00247     // Layout holding widgets for specialized format options.
00248     QStackedLayout* m_formatOptions;
00249     
00250     // Gets the current filename chosen by the user.
00251     String getFilename();
00252     
00253     // Gets the directory of the filename chosen by the user.
00254     String getDirectory();
00255 
00256     // Gets the current region file format chosen by the user.
00257     // Guaranteed to be one of the values in
00258     // RegionFileReader::supportedTypesStrings().
00259     String getFormat();
00260     
00261     // Gets the current options widget.
00262     QWidget* getOptions();
00263     
00264 private slots:
00265     // Show a file chooser dialog.
00266     void browse();
00267     
00268     // Show/hide the region-specific options frame.
00269     void showHideOptions(bool show);
00270     
00271     // Check that the entered file is valid, then accept().
00272     void ok();
00273 };
00274 
00275 }
00276 
00277 #endif /* QTREGIONSHAPEMANAGER_QO_H_ */