casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
QtDisplayData.qo.h
Go to the documentation of this file.
00001  //# QtDisplayData.qo.h: Qt DisplayData wrapper.
00002 //# Copyright (C) 2005
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 
00028 #ifndef QTDISPLAYDATA_H
00029 #define QTDISPLAYDATA_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/BasicSL/String.h>
00033 #include <casa/Containers/Record.h>
00034 #include <coordinates/Coordinates/CoordinateSystem.h>
00035 #include <display/Display/DParameterChoice.h>
00036 #include <display/Display/DParameterRange.h>
00037 #include <casa/BasicMath/Math.h>
00038 #include <display/Display/DisplayEnums.h>
00039 #include <display/DisplayDatas/DisplayData.h>
00040 #include <display/DisplayDatas/DisplayDataOptions.h>
00041 #include <display/Utilities/ImageProperties.h>
00042 #include <vector>
00043 
00044 
00045 #include <graphics/X11/X_enter.h>
00046 #include <QtCore>
00047 #include <QObject>
00048 #include <graphics/X11/X_exit.h>
00049 
00050 
00051 namespace casa { //# NAMESPACE CASA - BEGIN
00052 
00053 class String;
00054 class Record;
00055 class DisplayData;
00056 class WCMotionEvent;
00057 template <class T> class ImageInterface;
00058 class QtDisplayPanel;
00059 class ImageRegion;
00060 class WorldCanvasHolder;
00061 class Colormap;
00062 class QtDisplayPanelGui;
00063 class WedgeDD;
00064 
00065 class QtDisplayData : public QObject {
00066 
00067   Q_OBJECT      //# Allows slot/signal definition.  Must only occur in
00068                 //# implement/.../*.h files; also, makefile must include
00069                 //# name of this file in 'mocs' section.
00070 
00071  public:
00072 
00073   static std::string path(const DisplayData *);
00074   
00075   QtDisplayData( QtDisplayPanelGui *panel, String path, String dataType, String displayType,
00076                  const viewer::DisplayDataOptions &ddo = viewer::DisplayDataOptions( ),
00077                  const viewer::ImageProperties &props = viewer::ImageProperties( ) );
00078   ~QtDisplayData();
00079   String getPositionInformation( const Vector<double> world);
00080   virtual std::string name() { return name_;  }
00081   virtual const char* nameChrs() { return name_.c_str();  }
00082   virtual void setName(const std::string& name) { name_ = name;  }
00083  
00084   virtual std::string dataType() const { return dataType_;  }
00085   virtual std::string displayType() { return displayType_;  }
00086 
00087   //Display Type
00088    Bool isRaster() const;
00089    Bool isContour() const;
00090    Bool isVector() const;
00091    Bool isMarker() const;
00092    Bool isImage() const;
00093 
00094   //virtual Bool delTmpData() const;
00095   virtual void delTmpData() const;
00096   virtual void setDelTmpData(Bool delTmpData);
00097 
00098   std::string description( ) const;
00099   std::string path( ) const { return path_; }
00100   
00101   virtual String errMsg() { return errMsg_;  }
00102   
00103   // retrieve the Record of options.  This is similar to a 'Parameter Set',
00104   // containing option types, default values, and meta-information,
00105   // suitable for building a user interface for controlling the DD.
00106   virtual Record getOptions();
00107     
00108   // retrieve wrapped DisplayData.
00109   //# (should probably be private, and 'friend'ed only to QtDP, which
00110   //# needs it for purposes like registration...).
00111   virtual DisplayData* dd() { return dd_;  }
00112   
00113   // Did creation of wrapped DD fail?
00114   virtual Bool isEmpty() { return dd_==0;  }
00115 
00116 
00117   
00118   // Possible valuse: Raster, Vector, Annotation, CanvasAnnotation
00119   virtual Display::DisplayDataType ddType();
00120   bool isSkyCatalog() const;
00121   
00122   // Can the QDD display tracking information?
00123   virtual Bool usesTracking() { return !isEmpty() &&
00124                                        ddType()!=Display::Annotation && 
00125                                        ddType()!=Display::CanvasAnnotation;  }
00126   
00127   // Returns a String with value and position information,
00128   // suitable for a cursor tracking display.
00129   virtual pair<String,String> trackingInfo(const WCMotionEvent& ev);
00130 
00131   
00132   // Convert 2-D 'pseudoregion' (or 'mouse region' Record, from the region
00133   // mouse tools) to a full Image Region, with same number of axes as the
00134   // DD's Lattice (and relative to its CoordinateSystem).
00135   // Return value is 0 if the conversion can't be made or does not apply
00136   // to this DD for any reason (ignored by non-Lattice DDs, e.g.).
00137   //
00138   // If allChannels==True, the region is extended along spectral axis, if
00139   // it exists (but ONLY if the spectral axis is not also on display; the
00140   // visible mouse region always defines region shape on the display axes).
00141   //
00142   // If allAxes is True, the region is extended over all (non-display)
00143   // axes (including any spectral axis; allAxes=True makes the allChannels
00144   // setting irrelevant).
00145   //
00146   // If both allchannels and allAxes are False (the default), the region
00147   // will be confined to the currently-displayed plane.
00148   //
00149   // -->If the returned ImageRegion* is non-zero, the caller
00150   // -->is responsible for deleting it.
00151   virtual ImageRegion* mouseToImageRegion(Record mouseRegion,
00152                                           WorldCanvasHolder* wch,
00153                                           Bool allChannels=False,
00154                                           Bool allPols=False,
00155                                           Bool allRAs=False,
00156                                           Bool allDECs=False,
00157                                           Bool allAxes=False);
00158   virtual ImageRegion* mouseToImageRegion(
00159         Record mouseRegion, WorldCanvasHolder* wch,
00160         String& extChan, String& extPol);
00161  
00162   // Print statistics on image for given region.
00163   // Returns False if unable to do so. 
00164   virtual Bool printRegionStats(ImageRegion& imgReg);
00165   virtual Bool printLayerStats(ImageRegion& imgReg);
00166   
00167 
00168   // Return the number of the spectral axis within the DD's original
00169   // image lattice and coordinate system (-1 if none).
00170   //virtual Int spectralAxis();
00171   virtual Int getAxisIndex(String axtype=String("Spectral"));
00172 
00173   //#  colorbar methods
00174   
00175   
00176   // Would this QDD want to display a color bar if registered and
00177   // conformant for drawing?
00178   virtual Bool wouldDisplayColorBar() {
00179         bool displayColorBar = false;
00180         bool wouldDisplayColorBar = false;
00181         if ( colorBarDisplayOpt_ != NULL ){
00182                 if (colorBarDisplayOpt_->value() == WEDGE_YES){
00183                         displayColorBar = true;
00184                 }
00185                 wouldDisplayColorBar = hasColorBar() && displayColorBar;
00186         }
00187     return wouldDisplayColorBar;
00188   }
00189 
00190       
00191   // Is a color bar WDD defined for this QDD?
00192   virtual Bool hasColorBar() {
00193           bool colorBarExists = false;
00194           if ( colorBar_!= 0 ){
00195                   colorBarExists = true;
00196           }
00197           return colorBarExists;
00198   }
00199   
00200   
00201   // User-requested adjustment to colorbar thickness (will probably
00202   // remain at the default value of 1).  Used by QtDisplayPanel to
00203   // aid in sizing colorbar panels.
00204   virtual Float colorBarSizeAdj() {
00205     if(!wouldDisplayColorBar()) return 0.;
00206     return max(colorBarThicknessOpt_->minimum(),
00207            min(colorBarThicknessOpt_->maximum(),
00208                colorBarThicknessOpt_->value()));  }
00209  
00210                   
00211   // Used (by QtDisplayPanel) to compute margin space for colorbar labels.
00212   // It is the (pgplot) character size for colorbar labels (default 1.2)
00213   // times a label space 'adjustment' user option (default 1.).
00214   virtual Float colorBarLabelSpaceAdj();
00215 
00216 
00217   // Retrieve color bar ('wedge') DD.  (0 if none.  It will exist if
00218   // hasColorBar() is True).  (Not for general use).
00219   //# (should probably be private, and 'friend'ed only to QtDP, which
00220   //# which manages its registration and other aspects).
00221   virtual WedgeDD* colorBar() { return colorBar_;  }
00222   
00223   
00224   // Does this DD currently own a colormap?
00225   virtual Bool hasColormap() const { return clrMap_!=0;  }
00226   // Different DisplayDatas *could* have different colormap palettes
00227   // thus this is non-static and specific to a display data
00228   virtual bool isValidColormap( const QString &name ) const;
00229   void setColorMap( Colormap* colorMap );
00230   void removeColorMap( const String& name );
00231 
00232   // Get/set colormap shift/slope ('fiddle') and brightness/contrast
00233   // settings.  (At present this is usually set for the PC's current
00234   // colormap via mouse tools.  These may want to to into get/setOptions
00235   // (thus into guis) eventually...).  Return value will be False if
00236   // DD has no colormap [at present].
00237   //<group>
00238   virtual Bool getCMShiftSlope(Vector<Float>& params) const;
00239   virtual Bool getCMBrtCont(Vector<Float>& params) const;
00240   virtual Bool setCMShiftSlope(const Vector<Float>& params);
00241   virtual Bool setCMBrtCont(const Vector<Float>& params);
00242   //</group>
00243   
00244   
00245   //# (dk note: If you use this, you must assure you do not change it in ways
00246   //# that will crash QDD.  Do not assume it is non-zero -- im_ may be zero if
00247   //# the QDD's image is complex.  However, if it _is_ non-zero, you should
00248   //# be able to assume it will exist for the life of the QDD).
00249   ImageInterface<Float>* imageInterface() { return im_;  }
00250   const viewer::ImageProperties &imageProperties( );
00251  
00252   // force unlocking of paged images
00253   void unlock( );
00254   static const String WEDGE_LABEL_CHAR_SIZE;
00255   static const String WEDGE_YES;
00256 
00257   void init();
00258   void initImage();
00259   void setImage( ImageInterface<Float>* img);
00260   static void setGlobalColorOptions( bool global );
00261   void setInvertColorMap( bool invert );
00262 
00263  public slots:
00264   
00265   // (Should only be used by QtDisplayPanels to notify the QDD that it
00266   // has been registered, or is about to be unregistered, on the QDP).
00267   //<group>  
00268   virtual void registerNotice(QtDisplayPanel*);
00269   virtual void unregisterNotice(QtDisplayPanel*);
00270   //</group>
00271   
00272   // Apply option values to the DisplayData.  Method will
00273   // emit optionsChanged() if other option values, limits, etc.
00274   // should also change as a result.
00275   // Set emitAll = True if the call was not initiated by the options gui
00276   // itself (e.g. via scripting or save-restore); that will assure that
00277   // the options gui does receive all option updates (via the optionsChanged
00278   // signal) and updates its user interface accordingly.
00279 
00280   virtual void setOptions(Record opts, Bool emitAll=False);
00281   void emitOptionsChanged( Record changedOpts );
00282   
00283   
00284   virtual void done();
00285  
00286   
00287   virtual void checkAxis(); 
00288  
00289  
00290   const String &getColormap( ) { return clrMapName_; }
00291   void setColormap(const String& clrMapName) { setColormap_(clrMapName); }
00292   void setHistogramColorMapping( float minValue, float maxValue, float powerScale );
00293   //  This is used to get the display data to set a saturation range
00294   //  from another image.
00295   void setSaturationRange( double min, double max );
00296 
00297  signals:
00298 
00299   // Signals changes the DD has made internally to option values, limits,
00300   // etc., that ui (if any) will want to reflect.  Calling setOptions()
00301   // to change one option value may cause this to be emitted with any other
00302   // options which have changed as a result.
00303   void optionsChanged(Record changedOptions);
00304 
00305   // Emitted when problems encountered (in setOptions, e.g.) 
00306   void qddError(String errmsg);
00307   
00308   // Emitted when options successfully set without error.
00309   //# (same purpose as above -- clean this up).
00310   void optionsSet();
00311   
00312   // Emitted when color bars may need rearrangement on panels where
00313   // this QDD is registered.  QDP connects this to its checkColorBars_()
00314   // slot.
00315   void colorBarChange();
00316   
00317   // Emitted when something in the DD (besides mouse movement) means
00318   // that tracking data for this QDD could be usefully recomputed
00319   // and redisplayed.  Underlying DDs can indicate this in setOptions
00320   // by defining the "trackingchange" field in chgdOpts (recOut).
00321   void trackingChange(QtDisplayData*);
00322   
00323   
00324   //# Emitted when an operation is successfully completed.  It is intended
00325   //# that, e.g., a gui that responds to qddError signals by setting
00326   //# an error message onto a status line vould clear the status line
00327   //# when this signal occurs.
00328   //# (let qdpg clear status line itself b4 QDP calls instead?...)
00329 //#void qddOK();
00330  
00331   //# This object will be destroyed after this signal is processed.
00332   //# (Note: if this DD is managed in QtViewer's list, it is preferable
00333   //# to connect to QtViewerBase::ddRemoved() instead).
00334 //# void dying(QtDisplayData*);
00335   
00336   // (mkApr2012) axisChanged and axisChangedProfile have the identical
00337   // functionality, to send out the names of the current x/y/z axes.
00338   // The profiler needs to be notified first such subsequent  plot
00339   // request from regions are accepted. For that reason there is the
00340   // signal axisChangedProfile which connects to the profiler and is
00341   // emitted prior to axisChanged (in QtDisplayData::checkAxis())
00342   void axisChanged(String, String, String, std::vector<int> );
00343   void axisChangedProfile(String, String, String, std::vector<int> );
00344 
00345   void spectrumChanged(String spcTypeUnit, String spcRval, String spcSys);
00346 
00347   void statsReady(const String&);
00348   void showColorHistogram( QtDisplayData* );
00349 
00350   void globalOptionsChanged( QtDisplayData*, Record);
00351 
00352  protected slots:
00353   
00354   // Set the color bar orientation option according to the master
00355   // value stored in the QtViewerBase (panel_->viewer()->colorBarsVertical_).
00356   // Connected to QtViewerBase's colorBarOrientationChange() signal;
00357   // also called during initialization.
00358   virtual void setColorBarOrientation_();
00359  
00360 
00361  protected:
00362   
00363   // Heuristic used internally to set initial axes to display on X, Y and Z,
00364   // for PADDs.  shape should be that of Image/Array, and have same nelements
00365   // as axs.  On return, axs[0], axs[1] and (if it exists) axs[2] will be axes
00366   // to display initially on X, Y, and animator, respectively.
00367   // If you pass a CS for the image, it will give special consideration to
00368   // Spectral [/ Direction] axes (users expect their spectral axes on Z, e.g.)
00369   //# (Lifted bodily from GTkDD).
00370   virtual void getInitialAxes_(Block<uInt>& axs, const IPosition& shape,
00371                                const CoordinateSystem* cs=0);
00372   
00373   // Set named colormap onto underlying dd (called from public setOptions()).
00374   // Pass "" to remove/delete any existing colormap for the QDD.
00375   // In the case that no colormap is set on a dd that needs one (raster dds,
00376   // mostly), the drawing canvas will provide a default.
00377   // See ColormapDefinition.h, and the Table gui/colormaps/default.tbl (in the
00378   // data repository) for the list of valid default colormap names (and
00379   // information on creating/installing custom ones).  If an invalid name is
00380   // passed, an (ignorable) error message is signalled, and the dd's colormap
00381   // will remain unchanged.
00382   virtual void setColormap_(const String& clrMapName, bool invertChanged = false);
00383   virtual void removeColormap_() { setColormap_("");  }
00384 
00385 
00386 
00387   //# (could be exposed publicly, if useful).
00388   //  Does this DD use/need a public colormap?
00389   virtual Bool usesClrMap_() {
00390     return (isRaster() || displayType_=="pksmultibeam");  }
00391         //# These are the only DD types currently needing a colormap and
00392         //# supporting the selection option; add more if/when needed....
00393   
00394   // Can this QDD use a color bar?
00395   virtual Bool usesColorBar_() { return isRaster();  }
00396 
00397   typedef std::map<const DisplayData*,QtDisplayData*> data_to_qtdata_map_type;
00398   static data_to_qtdata_map_type dd_source_map;
00399 
00400  private:
00401   // Not intended for use.
00402   QtDisplayData() : panel_(0), im_(0), cim_(0), dd_(0) {  }
00403   static bool globalColorSettings;
00404   bool setColorBarOptions( Record& opts, Record& chgdOpts );
00405   void checkGlobalChange( Record& chgdOpts );
00406 
00407   //# data
00408   QtDisplayPanelGui *panel_;
00409   std::string path_, dataType_, displayType_;
00410   const std::string DISPLAY_RASTER;
00411   const std::string DISPLAY_CONTOUR;
00412   const std::string DISPLAY_VECTOR;
00413   const std::string DISPLAY_MARKER;
00414   const std::string TYPE_IMAGE;
00415   ImageInterface<Float>* im_;
00416   ImageInterface<Complex>* cim_;
00417   DisplayData* dd_;
00418 
00419   std::string name_;
00420   
00421   bool invertColorMap;
00422   // Name of colormap used by dd_  ("" if none)
00423   String clrMapName_;
00424   // Color maps can be removed.  In such a case, the restoreColorMapName
00425   //holds the previous one so we can reset to that in case the one we
00426   //are using is removed.
00427   String restoreColorMapName;
00428   
00429   // Will be set onto underlying dd_ if needed (0 if none)
00430   Colormap* clrMap_;
00431   
00432   // Parses colormap choice out of a setOptions record.
00433   DParameterChoice* clrMapOpt_;
00434   
00435   // All the valid ('primary') colormap names.
00436   // (This interface doesn't support use of 'synonym' names).
00437   typedef ColormapDefinition::colormapnamemap colormapnamemap;
00438   colormapnamemap clrMapNames_;
00439   
00440   // Set of colormaps currently or previously used by this DD.  Once
00441   // a Colormap is created, it is retained for the life of the DD.
00442   //# (A main reason for this is to remember the colormap's 
00443   //# 'transfer function' state (brightness/contrast/shift/slope) in case
00444   //# it is reused -- see PCITFiddler.h (colormap mouse tools)).
00445   typedef std::map<String, Colormap*> colormapmap;
00446   colormapmap clrMaps_;
00447   const static String COLOR_MAP;
00448   
00449   
00450   // Latest error message, retrievable via errMsg().  (Where possible, errors
00451   // are indicated via the qddError signal rather than a throw, and the code
00452   // attempts something sensible in case the caller chooses to ignore it).
00453   String errMsg_;
00454   
00455   
00456   // The DD that draws the color bar key for the main DD -- 0 if N/A.
00457   WedgeDD* colorBar_;
00458   
00459   //# User interface parsing objects for some color bar parameters that are
00460   //# controlled externally to the WedgeDD (colorBar_): whether to display
00461   //# the color bar, its thickness, label (margin) space, orientation,
00462   //# and character size.
00463   //# Changes to any of these DisplayParameters means that the arrangement
00464   //# of colorbars must be checked on all QtDisplayPanels where this QDD
00465   //# is registered (orientation change is sent to base viewer class and
00466   //# handled there).
00467   
00468   // Is color bar display turned on? ("Yes" / "No")
00469   DParameterChoice*       colorBarDisplayOpt_;
00470   
00471   // Manual user adjustment factor for color bar thickness.  Hopefully
00472   // the automatic choice (by QtDisplayPanel) will be adequate in most
00473   // cases, and this can remain at the default value of 1.
00474   DParameterRange<Float>* colorBarThicknessOpt_;
00475   
00476   // Manual user adjustment factor for color bar label space.  Hopefully
00477   // the automatic choice (by QtDisplayPanel) will be adequate in most
00478   // cases, and this can remain at the default value of 1.
00479   DParameterRange<Float>* colorBarLabelSpaceOpt_;  
00480   
00481   // "horizontal" / "vertical"
00482   DParameterChoice*       colorBarOrientationOpt_;
00483   
00484   // Size of label characters on color bar (affects margins only).
00485   //# (WedgeDD reacts to this option, but it is also monitored on this level).
00486   DParameterRange<Float>* colorBarCharSizeOpt_;  
00487 
00488   viewer::ImageProperties image_properties;
00489   void setPlotTitle();
00490 };
00491 
00492 
00493 } //# NAMESPACE CASA - END
00494 
00495 
00496 //# Allows QtDisplayData* to be stored in a QVariant; e.g., to be
00497 //# the data associated with a QAction....  See QMetaType and
00498 //# QVariant class doc.  QVariants are rather well-designed
00499 //# generic value holders.
00500 //# Note: this declaration cannot be placed within the casa namespace.
00501 Q_DECLARE_METATYPE(casa::QtDisplayData*)
00502 
00503 
00504 #endif