QtCanvas.qo.h

Go to the documentation of this file.
00001 //# Copyright (C) 2005
00002 //# Associated Universities, Inc. Washington DC, USA.
00003 //#
00004 //# This library is free software; you can redistribute it and/or modify it
00005 //# under the terms of the GNU Library General Public License as published by
00006 //# the Free Software Foundation; either version 2 of the License, or (at your
00007 //# option) any later version.
00008 //#
00009 //# This library is distributed in the hope that it will be useful, but WITHOUT
00010 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012 //# License for more details.
00013 //#
00014 //# You should have received a copy of the GNU Library General Public License
00015 //# along with this library; if not, write to the Free Software Foundation,
00016 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00017 //#
00018 //# Correspondence concerning AIPS++ should be addressed as follows:
00019 //#        Internet email: aips2-request@nrao.edu.
00020 //#        Postal address: AIPS++ Project Office
00021 //#                        National Radio Astronomy Observatory
00022 //#                        520 Edgemont Road
00023 //#                        Charlottesville, VA 22903-2475 USA
00024 //#
00025 
00026 #ifndef QTCANVAS_H
00027 #define QTCANVAS_H
00028 
00029 #include <casa/aips.h>
00030 #include <casa/BasicSL/String.h>
00031 #include <casa/Containers/Record.h>
00032 #include <casa/Arrays/Array.h>
00033 #include <casa/Arrays/ArrayMath.h>
00034 #include <casa/Arrays/Vector.h>
00035 #include <casa/Arrays/Matrix.h>
00036 #include <casa/Inputs/Input.h>
00037 #include <casa/Arrays/IPosition.h>
00038 #include <coordinates/Coordinates/SpectralCoordinate.h>
00039 
00040 #include <display/QtPlotter/QtPlotSettings.h>
00041 #include <display/QtPlotter/CanvasCurve.h>
00042 #include <display/QtPlotter/WorldCanvasTranslator.h>
00043 #include <display/QtPlotter/ProfileFitMarker.h>
00044 #include <display/QtPlotter/MolecularLine.h>
00045 #include <display/QtPlotter/canvasMode/CanvasMode.h>
00046 
00047 #include <graphics/X11/X_enter.h>
00048 #include <QDir>
00049 #include <QColor>
00050 #include <QHash>
00051 #include <QWidget>
00052 #include <QMouseEvent>
00053 #include <QToolButton>
00054 #include <QDialog>
00055 #include <QPixmap>
00056 #include <QVBoxLayout>
00057 #include <QPainterPath>
00058 #include <QLabel>
00059 #include <QMenu>
00060 #include <map>
00061 #include <vector>
00062 #include <graphics/X11/X_exit.h>
00063 
00064 namespace casa {
00065 
00066         class Annotation;
00067         class CanvasModeFactory;
00068 
00069 #define QT_DIAMOND_SIZE 5
00070 
00071         class GraphLabel {
00072         public:
00073                 QString text;
00074                 QString fontName;
00075                 int fontSize;
00076                 QColor color;
00077                 GraphLabel() : text(""), fontName(""), fontSize(12), color(Qt::blue) {}
00078         };
00079 
00080         class QtCanvas : public QWidget, public WorldCanvasTranslator {
00081                 Q_OBJECT
00082 
00083                 friend class CanvasModeAnnotation;
00084                 friend class CanvasModeRangeSelection;
00085                 friend class CanvasModeChannel;
00086                 friend class CanvasModeContextMenu;
00087                 friend class CanvasModeZoom;
00088 
00089         public:
00090                 QtCanvas(QWidget *parent = 0);
00091 
00092                 void setPlotSettings(const QtPlotSettings &settings);
00093                 void setTopAxisRange(const Vector<Float> &xValues, bool topAxisDescending );
00094                 void setFrameMarker( float framePositionX );
00095                 void setZoomRectColor( QColor color );
00096                 //Returns the spectrum as it is currently displayed in the canvas (with
00097                 //possible zooming).
00098                 void getCanvasDomain( double* minValue, double* maxValue, QString& units);
00099 
00100                 CurveData getCurveData(int);
00101                 ErrorData getCurveError(int id);
00102                 QString getCurveName(int id);
00103                 void setCurveName(int id, const QString& name );
00104                 void curveColorsChanged();
00105                 QColor getCurveColor( int id );
00106                 CanvasCurve getCurve( const QString& curveName );
00107                 int getLineCount();
00108                 void curveLabelsChanged();
00109                 void clearCurve( bool inDtor=false );
00110                 void clearFitCurves();
00111                 void clearEverything();
00112                 void clearMolecularLines( bool refresh = true );
00113                 void setDataRange();
00114                 void setImageMode(bool);
00115                 QPixmap* graph();
00116                 void drawBackBuffer(QPainter *);
00117                 //Draws a vertical line indicating the current frame.
00118                 void drawFrameMarker( QPainter* );
00119                 QString getType( QtPlotSettings::AxisIndex axisIndex = QtPlotSettings::xBottom );
00120                 QString getUnits( QtPlotSettings::AxisIndex axisIndex = QtPlotSettings::xBottom );
00121                 QList<MolecularLine*> getMolecularLines() const;
00122                 void setGaussianEstimateCount( int count );
00123 
00124                 //Plotting curves
00125                 void plotPolyLines(QString);
00126                 template<class T> void plotPolyLine(const Vector<T>&, const Vector<T>&,
00127                                 double beamAngle, double beamArea, SpectralCoordinate coord);
00128                 void plotPolyLine(const Vector<Float> &x, const Vector<Float> &y, const Vector<Float> &e,
00129                                   double beamAngle, double beamArea, SpectralCoordinate coord, const QString& lb="");
00130                 enum ColorCategory { TITLE_COLOR, CURVE_COLOR, CURVE_COLOR_PRIMARY,
00131                                      CURVE_COLOR_SECONDARY, WARNING_COLOR, CURVE_TRADITIONAL};
00132                 void addPolyLine(const Vector<Float> &x, const Vector<Float> &y,
00133                                 double beamAngle, double beamArea, SpectralCoordinate coord,
00134                                  const QString& lb="", ColorCategory colorCategory=CURVE_COLOR );
00135                 void addMolecularLine(MolecularLine* molecularLine );
00136                 QList<QString> getMolecularLineNames() const;
00137 
00138                 template<class T> void plotPolyLine(const Matrix<T> &verts, double beamAngle,
00139                                 double beamArea, SpectralCoordinate coord, double restValue );
00140 
00141                 QSize minimumSizeHint() const;
00142                 QSize sizeHint() const;
00143                 ~QtCanvas();
00144 
00145 
00146                 void setTitle(const QString &text, int fontSize = 13,const QString &font = FONT_NAME);
00147                 QString getTitle() {
00148                         return title.text;
00149                 };
00150 
00151                 bool isShowChannelLine();
00152                 void setShowChannelLine( bool showLine );
00153                 void setChannelLineColor( QColor color );
00154 
00155                 void setXLabel(const QString &text, int fontSize = 10,
00156                                const QString &font = FONT_NAME, QtPlotSettings::AxisIndex axisIndex=QtPlotSettings::xBottom);
00157                 void setWelcome(const QString &text, int fontSize = 14,
00158                                 const QString &font = FONT_NAME);
00159                 void setAutoScaleX(bool autoScale);
00160                 void setAutoScaleY(bool autoScale);
00161                 void setShowGrid(int a)   {
00162                         showGrid = a;
00163                         refreshPixmap();
00164                 }
00165                 bool getAutoScaleX( ) {
00166                         return autoScaleX;
00167                 }
00168                 bool getAutoScaleY( ) {
00169                         return autoScaleY;
00170                 }
00171                 int getShowGrid( )   {
00172                         return showGrid;
00173                 }
00174                 void setPlotError(int a)  {
00175                         plotError = a;
00176                         setDataRange();
00177                 }
00178 
00179                 //Whether or not to show a top axis on the plot.
00180                 void setShowTopAxis( bool showAxis );
00181                 /*bool getShowTopAxis() const {
00182                         return showTopAxis;
00183                 }*/
00184 
00185                 //Even if the user wants to show a top axis, we won't do it
00186                 //when there are multiple curves which are not compatible in their
00187                 //x-axis units.
00188                 void setTopAxisCompatible( bool compatible );
00189 
00190                 //Whether or not to show tool tips with the x,y coordinates
00191                 //of the points on the plot.
00192                 void setShowToolTips( bool toolTipsVisible ) {
00193                         showToolTips = toolTipsVisible;
00194                 }
00195                 bool getShowToolTips() const {
00196                         return showToolTips;
00197                 }
00198 
00199                 //Sets the coordinates for the x and y points that are displayed
00200                 //as tooltips.
00201                 void setToolTipXUnit( const QString& xUnit ) {
00202                         toolTipXUnit = xUnit;
00203                 }
00204 
00205                 //Stores the location of a (center,peak) point that represents an initial
00206                 //Gaussian estimate for spectral line fitting.
00207                 void setProfileFitMarkerCenterPeak( int index, double center, double peak);
00208 
00209                 //Stores the fwhm and the y value of the fwhm for an initial Gaussian estimate
00210                 //for spectral line fitting.
00211                 void setProfileFitMarkerFWHM( int index, double fwhm, double fwhmHeight);
00212 
00213                 //Whether or not to plot a line graph or instead plot the points as a
00214                 //step function.
00215                 bool isDisplayStepFunction() const;
00216                 void setDisplayStepFunction( bool displayAsStepFunction );
00217 
00218                 //Used for customizing the curve colors on the canvas.
00219                 void setTraditionalCurveColors( const QList<QString>& colors );
00220                 void setMainCurveColors( const QList<QString>& colors );
00221                 void setFitCurveColors( const QList<QString>& colors );
00222                 void setSummaryCurveColors( const QList<QString>& colors );
00223 
00224                 //If this flag is set, only 'traditional' colors will be used.
00225                 void setTraditionalColors( bool traditionalColors );
00226                 static const QString FONT_NAME;
00227 
00228                 //Customization of the curve legend.
00229                 void setShowLegend( bool visible );
00230                 bool isShowLegend() const;
00231                 void setLegendPosition( int position );
00232                 int getLegendPosition() const;
00233 
00234                 //Set the yaxis units the image is using ( units the data we are getting is in).
00235                 void setImageYUnits( const QString& imageUnits );
00236 
00237 
00238                 enum TaskMode {/*UNKNOWN_MODE,*/ SPECTRAL_LINE_MODE, LINE_OVERLAY_MODE, MODE_COUNT };
00239 
00240 
00241                 //Units we will be displaying the y-axis in which may be different
00242                 //from the intrinsic image units.
00243                 QString getDisplayYUnits();
00244                 void setDisplayYUnits( const QString& displayUnits );
00245 
00246                 int getCurZoom();
00247                 int getZoomStackSize();
00248 
00253                 bool isTopAxisRange() const;
00254 
00255                 //Force a repaint
00256                 void refreshPixmap();
00257 
00258                 //Used for conversion purposes.
00259                 void setSpectralCoordinate( const SpectralCoordinate& coord );
00260 
00261 
00262         public slots:
00263                 void zoomIn();
00264                 void zoomOut();
00265                 void zoomNeutral();
00266                 void changeTaskMode( int mode );
00267                 void createAnnotationText();
00268                 void rangeSelectionMode();
00269                 void channelPositioningMode();
00270 
00271         signals:
00272                 void xRangeChanged(double xmin, double xmax);
00273                 void channelSelect(float xvalue);
00274                 void specFitEstimateSpecified( double xValue, double yValue, bool centerPeak );
00275                 void findRedshiftAt( double center, double peak );
00276                 void channelRangeSelect( float startVal, float endVal );
00277                 void curvesChanged();
00278                 void clearPaletteModes();
00279                 void togglePalette( int modeIndex );
00280 
00281         protected:
00282                 void paintEvent(QPaintEvent *event);
00283                 void resizeEvent(QResizeEvent *event);
00284                 void mousePressEvent(QMouseEvent *event);
00285                 void mouseMoveEvent(QMouseEvent *event);
00286                 void mouseReleaseEvent(QMouseEvent *event);
00287                 void keyPressEvent(QKeyEvent *event);
00288                 void keyReleaseEvent(QKeyEvent *event);
00289                 void wheelEvent(QWheelEvent *event);
00290 
00291                 void updateRubberBandRegion();
00292                 void updatexRangeBandRegion();
00293                 void drawGrid(QPainter *painter);
00294                 void drawTicks(QPainter *painter);
00295                 void drawLabels(QPainter *painter);
00296                 void drawWelcome(QPainter *painter);
00297                 void drawCurves(QPainter *painter);
00298 
00299         private slots:
00300                 void deleteSelectedAnnotation();
00301                 void editSelectedAnnotation();
00302                 void gaussianCenterPeakSelected();
00303                 void gaussianFWHMSelected();
00304                 void findRedshift();
00305 
00306         private:
00307 
00308                 //Zoom functionality
00309                 void zoomYBasedOnX( QtPlotSettings& settings, double zoomFactor, bool zoomIn );
00310                 pair<double,double> getRangeFor( double zoomFactor, bool zoomIn, double minX, double maxX );
00311                 void increaseCurZoom();
00312                 void defaultZoomIn();
00313                 void defaultZoomOut();
00314 
00324                 void setCurveData(int id, const CurveData &data, double beamAngle, double beamArea,
00325                                 SpectralCoordinate coord, const ErrorData &error=ErrorData(),
00326                         const QString& lb="", ColorCategory colorCategory = CURVE_COLOR);
00327 
00328 
00333                 void displayToolTip( QMouseEvent* event ) const;
00334 
00339                 void adjustExtremes( double* const min, double* const max ) const;
00340 
00346                 double getDataY( int pixelPosition ) const;
00347 
00353                 double getDataX( int pixelPosition ) const;
00354 
00355                 //WorldCanvasTranslator interface methods.
00361                 int getPixelX( double dataX ) const;
00362 
00368                 int getPixelY ( double dataY ) const;
00369 
00380                 QString findCoords( double x, double y ) const;
00381 
00385                 int getRectHeight() const;
00386 
00390                 int getRectWidth() const;
00391 
00395                 int getRectBottom() const;
00396 
00400                 int getRectLeft() const;
00401 
00410                 QString getXTickLabel( int index, int tickCount, QtPlotSettings::AxisIndex axisIndex ) const;
00411 
00417                 void initContextMenu();
00418                 void addDiamond( int x, int y, int diamondSize, QPainterPath& points ) const;
00419                 bool storeClickPosition( QMouseEvent* event );
00420                 int getIndependentCurveCount();
00421                 int getLastAxis();
00422                 void stripCurveTitleNumbers( QString& curveName ) const;
00423                 int duplicateCurve( QString& targetLabel );
00424 
00425                 void setYLabel(const QString &text, int fontSize = 10, const QString &font = FONT_NAME);
00426 
00427                 //Annotation methods
00428                 void selectAnnotation( QMouseEvent* event, bool select = true );
00429                 bool isAnnotation( QMouseEvent* event ) const;
00430                 Annotation* getSelectedAnnotation() const;
00431 
00432                 //Mouse Events
00433                 void storeActiveAnnotation( QMouseEvent* event );
00434                 void selectChannel( QMouseEvent* event );
00435                 void updateChannel( QMouseEvent* event );
00436                 void startRangeX( QMouseEvent* event );
00437                 void updateRangeX( QMouseEvent* event );
00438                 void endRangeX( QMouseEvent* event );
00439                 void startZoomRect( QMouseEvent* event );
00440                 void updateZoomRect( QMouseEvent* event );
00441                 void endZoomRect( QMouseEvent* event );
00442                 void resetSelectedAnnotation( QMouseEvent* event );
00443                 void drawMolecularLines( QPainter& painter );
00444                 void drawAnnotations( QPainter& painter );
00445 
00446                 //Plot Margins
00447                 const int MARGIN_LEFT;
00448                 const int MARGIN_BOTTOM;
00449                 const int MARGIN_TOP;
00450                 const int MARGIN_RIGHT;
00451                 const int FRACZOOM;
00452 
00453 
00454                 GraphLabel title;
00455                 GraphLabel xLabel[2];
00456                 GraphLabel yLabel;
00457                 GraphLabel welcome;
00458 
00459                 std::map<int, CanvasCurve> curveMap;
00460                 std::vector<QtPlotSettings> zoomStack;
00461                 QList<MolecularLine*> molecularLineStack;
00462                 std::pair<double,double> topAxisRange;
00463 
00464                 int curZoom;
00465                 int curMarker;
00466                 bool rubberBandIsShown;
00467                 bool xRangeIsShown;
00468                 bool imageMode;
00469                 bool xRangeMode;
00470                 double xRangeStart;
00471                 double xRangeEnd;
00472                 QRect rubberBandRect;
00473                 QRect xRangeRect;
00474                 QPixmap pixmap;
00475                 QPixmap backBuffer;
00476                 Matrix<uInt> *pMask;
00477 
00478                 QPoint currentCursorPosition;
00479                 QColor xcursor;
00480 
00481                 bool autoScaleX;
00482                 bool autoScaleY;
00483                 int plotError;
00484                 int showGrid;
00485 
00486                 int xRectStart;
00487                 int xRectEnd;
00488 
00489                 //Whether or not the user WANTS to see a top axis.
00490                 bool showTopAxis;
00491                 //We can only show the top axis in the case of multiple curves, when
00492                 //all the curves have equally spaced units in their top axis.
00493                 bool topAxisCompatible;
00494                 bool showToolTips;
00495                 bool showFrameMarker;
00496                 bool displayStepFunction;
00497                 QString toolTipXUnit;
00498                 QString yUnitImage;
00499                 QString yUnitDisplay;
00500 
00501                 //Context Menu
00502                 QMenu contextMenu;
00503                 QAction* centerPeakAction;
00504                 QAction* fwhmAction;
00505                 QAction* findRedshiftAction;
00506                 QAction* createAnnotationAction;
00507                 QAction* editAnnotationAction;
00508                 QAction* deleteAnnotationAction;
00509                 void showContextMenu( QMouseEvent* event );
00510 
00511                 //Profile Fitting Gaussian Estimation
00512                 double gaussianEstimateX;
00513                 double gaussianEstimateY;
00514                 QList<ProfileFitMarker> profileFitMarkers;
00515 
00516                 ProfileFitMarker getMarker( int index ) const;
00517                 void setMarker( int index, ProfileFitMarker& marker );
00518 
00519 
00527                 QColor getDiscreteColor(ColorCategory colorCategory, int id=0);
00528                 QColor frameMarkerColor;
00529                 QColor zoomColor;
00530                 QList<QString> mainCurveColorList;
00531                 QList<QString> fitCurveColorList;
00532                 QList<QString> fitSummaryCurveColorList;
00533                 QList<QString>  traditionalCurveColorList;
00534 
00535                 int curveCount;
00536                 int curveCountPrimary;
00537                 int curveCountSecondary;
00538 
00539                 //The optical spectral line fitting curve needs to use
00540                 //the traditional color list rather than a customizable one.
00541                 //That is the purpose of this flag.
00542                 bool traditionalColors;
00543 
00544                 //Whether the pixel canvas should show a legend with the curves.
00545                 bool showLegend;
00546 
00547                 //Where the curve legend should appear relative to this
00548                 //canvas.
00549                 int legendPosition;
00550 
00551                 //The x location for the vertical line representing the
00552                 //current frame position.
00553                 float framePositionX;
00554 
00555                 //Indicates the current work focus of the profiler
00556                 TaskMode taskMode;
00557 
00558                 float channelSelectValue;
00559 
00560                 //Needed to determine if we show the gauss estimate context
00561                 //items.
00562                 int gaussEstimateCount;
00563 
00564                 Annotation* selectedAnnotation;
00565                 vector<Annotation*> annotations;
00566 
00567                 //Returns true if the user is in the process of drawing an
00568                 //annotation.
00569                 bool isAnnotationActive() const;
00570 
00571                 Annotation* getActiveAnnotation() const;
00572                 CanvasMode* currentMode;
00573                 CanvasModeFactory* modeFactory;
00574                 SpectralCoordinate m_spectralCoordinate;
00575         };
00576 
00577 }
00578 #endif
00579 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1