casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
QPCanvas.qo.h
Go to the documentation of this file.
00001 //# QPCanvas.qo.h: Qwt implementation of generic PlotCanvas class.
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 QPCANVAS_QO_H_
00028 #define QPCANVAS_QO_H_
00029 
00030 #ifdef AIPS_HAS_QWT
00031 
00032 #include <graphics/GenericPlotter/PlotCanvas.h>
00033 #include <graphics/GenericPlotter/PlotOptions.h>
00034 #include <graphics/GenericPlotter/PlotLogger.h>
00035 #include <graphics/GenericPlotter/Plotter.h>
00036 #include <casaqt/QwtPlotter/QPImageCache.h>
00037 #include <casaqt/QwtPlotter/QPLayeredCanvas.qo.h>
00038 #include <casaqt/QwtPlotter/QPOptions.h>
00039 #include <casaqt/QwtPlotter/QPPlotItem.qo.h>
00040 
00041 #include <qwt_plot_picker.h>
00042 
00043 #include <QtGui>
00044 
00045 #include <vector>
00046 
00047 #include <casa/namespace.h>
00048 using namespace std;
00049 
00050 namespace casa {
00051 
00052 //# Forward declarations
00053 class QPPlotter;
00054 
00055 
00056 // Implementation of PlotCanvas for the Qwt plotter.  Mainly consists of
00057 // wrappers and containers around a QwtPlot object.
00058 class QPCanvas : public QFrame, public PlotCanvas {
00059     Q_OBJECT
00060     
00061     friend class QPAxesCache;
00062     friend class QPDrawThread;
00063     friend class QPLayeredCanvas;
00064     friend class QPPlotItem;
00065     friend class QPPlotter;
00066     
00067 public:
00068     // Static //
00069     
00070     // Keep a z-order variable to increment for subsequent items.
00071     static double zOrder;
00072     
00073     // Convenient access to class name (QPCanvas).
00074     static const String CLASS_NAME;
00075     
00076     // Convenient access to "origin" names for logging.
00077     // <group>
00078     static const String DRAW_NAME;
00079     static const String EXPORT_NAME;
00080     // </group>
00081     
00082     
00083     // Exports the given plotter to the given format.
00084     static bool exportPlotter(QPPlotter* plotter,
00085             const PlotExportFormat& format);
00086     
00087     // Exports the given canvas to the given format.
00088     static bool exportCanvas(QPCanvas* canvas, const PlotExportFormat& format);
00089     
00090     
00091     // Non-Static //
00092     
00093     // Constructor which takes (optional) parent QPPlotter.
00094     QPCanvas(QPPlotter* parent = NULL);
00095 
00096     // Destructor.
00097     ~QPCanvas();
00098     
00099     
00100     // Include overloaded methods.
00101     using PlotCanvas::setBackground;
00102     using PlotCanvas::setSelectLine;
00103     using PlotCanvas::setTitleFont;
00104     using PlotCanvas::showAxes;
00105     using PlotCanvas::showCartesianAxis;
00106     using PlotCanvas::setAxisFont;
00107     using PlotCanvas::setAxisRange;
00108     using PlotCanvas::setAxesRanges;
00109     using PlotCanvas::showGrid;
00110     using PlotCanvas::setGridMajorLine;
00111     using PlotCanvas::setGridMinorLine;
00112     using PlotCanvas::setLegendLine;
00113     using PlotCanvas::setLegendFill;
00114     using PlotCanvas::setLegendFont;
00115 
00116 
00117     // PlotCanvas Methods //
00118     
00119     // Implements PlotCanvas::size().
00120     pair<int, int> size() const;
00121 
00122     // Implements PlotCanvas::title().
00123     String title() const;
00124 
00125     // Implements PlotCanvas::setTitle().
00126     void setTitle(const String& title);
00127     
00128     // Implements PlotCanvas::titleFont().
00129     PlotFontPtr titleFont() const;
00130     
00131     // Implements PlotCanvas::setTitleFont().
00132     void setTitleFont(const PlotFont& font);
00133     
00134     // Implements PlotCanvas::background().
00135     PlotAreaFillPtr background() const;
00136     
00137     // Implements PlotCanvas::setBackground().
00138     void setBackground(const PlotAreaFill& areaFill);
00139 
00140     // Implements PlotCanvas::cursor().
00141     PlotCursor cursor() const;
00142     
00143     // Implements PlotCanvas::setCursor().
00144     void setCursor(PlotCursor cursor);
00145     
00146     // Implements PlotCanvas::refresh().
00147     // <group>
00148     void refresh();
00149     void refresh(int drawLayersFlag);
00150     // </group>
00151     
00152     // Implements PlotCanvas::isQWidget().
00153     bool isQWidget() const { return true; }
00154 
00155     
00156     // Implements PlotCanvas::shownAxes().
00157     // Returns a bit set (really an int) of bits defined by PlotAxis enum or'd together
00158     PlotAxisBitset shownAxes() const;
00159 
00160     // Implements PlotCanvas::showAxes().
00161     // Takes a bit set, as an int, of bits defined by PlotAxis enum or'd together
00162     void showAxes(PlotAxisBitset axes);
00163     
00164     // Implements PlotCanvas::axisScale().
00165     PlotAxisScale axisScale(PlotAxis axis) const;
00166 
00167     // Implements PlotCanvas::setAxisScale().
00168     void setAxisScale(PlotAxis axis, PlotAxisScale scale);
00169 
00170     // Implements PlotCanvas::axisReferenceValueSet().
00171     bool axisReferenceValueSet(PlotAxis axis) const;
00172     
00173     // Implements PlotCanvas::axisReferenceValueValue().
00174     double axisReferenceValue(PlotAxis axis) const;
00175     
00176     // Implements PlotCanvas::setAxisReferenceValue().
00177     void setAxisReferenceValue(PlotAxis axis, bool on, double value = 0);
00178     
00179     // Implements PlotCanvas::cartesianAxisShown().
00180     bool cartesianAxisShown(PlotAxis axis) const;
00181 
00182     // Implements PlotCanvas::showCartesianAxis().
00183     void showCartesianAxis(PlotAxis mirrorAxis, PlotAxis secondaryAxis,
00184             bool show = true, bool hideNormalAxis = true);
00185     
00186     // Implements PlotCanvas::axisLabel().
00187     String axisLabel(PlotAxis axis) const;
00188 
00189     // Implements PlotCanvas::setAxisLabel().
00190     void setAxisLabel(PlotAxis axis, const String& title);
00191 
00192     // Implements PlotCanvas::axisFont().
00193     PlotFontPtr axisFont(PlotAxis axis) const;
00194     
00195     // Implements PlotCanvas::setAxisFont().
00196     void setAxisFont(PlotAxis axis, const PlotFont& font);
00197 
00198     // Implements PlotCanvas::colorBarShown().
00199     bool colorBarShown(PlotAxis axis = Y_RIGHT) const;
00200 
00201     // Implements PlotCanvas::showColorBar().
00202     void showColorBar(bool show = true, PlotAxis axis = Y_RIGHT);
00203 
00204     
00205     // Implements PlotCanvas::axisRange().
00206     prange_t axisRange(PlotAxis axis) const;
00207 
00208     // Implements PlotCanvas::setAxisRange().
00209     void setAxisRange(PlotAxis axis, double from, double to);
00210     
00211     // Overrides PlotCanvas::setAxesRanges().
00212     void setAxesRanges(PlotAxis xAxis, double xFrom, double xTo,
00213                        PlotAxis yAxis, double yFrom, double yTo);
00214     
00215     // Implements PlotCanvas::axesAutoRescale().
00216     bool axesAutoRescale() const;
00217 
00218     // Implements PlotCanvas::setAxesAutoRescale().
00219     void setAxesAutoRescale(bool autoRescale = true);
00220 
00221     // Implements PlotCanvas::rescaleAxes().
00222     void rescaleAxes();
00223 
00224     // Implements PlotCanvas::axesRatioLocked().
00225     bool axesRatioLocked() const;
00226     
00227     // Implements PlotCanvas::setAxesRatioLocked().
00228     void setAxesRatioLocked(bool locked = true);
00229     
00230        
00231     // Implements PlotCanvas::cachedAxesStackSizeLimit().
00232     int cachedAxesStackSizeLimit() const;
00233 
00234     // Implements PlotCanvas::setCachedAxesStackSizeLimit().
00235     void setCachedAxesStackSizeLimit(int sizeInKilobytes);
00236     
00237     // Overrides PlotCanvas::cachedAxesStackImageSize().
00238     pair<int, int> cachedAxesStackImageSize() const;
00239     
00240     // Overrides PlotCanvas::setCachedAxesStackImageSize().
00241     void setCachedAxesStackImageSize(int width, int height);
00242 
00243 
00244     // Implements PlotCanvas::plotItem().  If the given items is NOT an
00245     // instance of a QPPlotItem, a copy of the given items is made.  The
00246     // original item is NOT kept by the canvas, so any subsequent changes to
00247     // the original items will not be reflected on the canvas.
00248     bool plotItem(PlotItemPtr item, PlotCanvasLayer layer = MAIN);
00249 
00250     // Implements PlotCanvas::allPlotItems().
00251     vector<PlotItemPtr> allPlotItems() const;
00252     
00253     // Implements PlotCanvas::layerPlotItems().
00254     vector<PlotItemPtr> layerPlotItems(PlotCanvasLayer layer) const;
00255 
00256     // Overrides PlotCanvas::numPlotItems().
00257     unsigned int numPlotItems() const;
00258 
00259     // Overrides PlotCanvas::numLayerPlotItems().
00260     unsigned int numLayerPlotItems(PlotCanvasLayer layer) const;
00261     
00262     // Implements PlotCanvas::removePlotItems().
00263     void removePlotItems(const vector<PlotItemPtr>& items);
00264     
00265     // Overrides PlotCanvas::clearPlotItems().
00266     void clearPlotItems();
00267     
00268     // Overrides PlotCanvas::clearPlots().
00269     void clearPlots();
00270     
00271     // Overrides PlotCanvas::clearLayer().
00272     void clearLayer(PlotCanvasLayer layer);
00273 
00274     
00275     // Implements PlotCanvas::holdDrawing().
00276     void holdDrawing();
00277     
00278     // Implements PlotCanvas::releaseDrawing().
00279     void releaseDrawing();
00280     
00281     // Implements PlotCanvas::drawingIsHeld().
00282     bool drawingIsHeld() const;
00283 
00284         
00285     // Implements PlotCanvas::setSelectLineShown().
00286     void setSelectLineShown(bool shown = true);
00287     
00288     // Implements PlotCanvas::selectLine().
00289     PlotLinePtr selectLine() const;
00290     
00291     // Implements PlotCanvas::setSelectLine().
00292     void setSelectLine(const PlotLine& line);
00293 
00294     
00295     // Implements PlotCanvas::gridShown().
00296     bool gridShown(bool* xMajor = NULL, bool* xMinor = NULL,
00297             bool* yMajor = NULL, bool* yMinor = NULL) const;
00298     
00299     // Implements PlotCanvas::showGrid().
00300     void showGrid(bool xMajor, bool xMinor, bool yMajor,bool yMinor);
00301     
00302     // Implements PlotCanvas::gridMajorLine().
00303     PlotLinePtr gridMajorLine() const;
00304 
00305     // Implements PlotCanvas::setGridMajorLine().
00306     void setGridMajorLine(const PlotLine& line);
00307 
00308     // Implements PlotCanvas::gridMinorLine().
00309     PlotLinePtr gridMinorLine() const;
00310 
00311     // Implements PlotCanvas::setGridMinorLine().
00312     void setGridMinorLine(const PlotLine& line);
00313 
00314     
00315     // Implements PlotCanvas::legendShown().
00316     bool legendShown() const;
00317     
00318     // Implements PlotCanvas::showLegend().
00319     void showLegend(bool on = true, LegendPosition position = EXT_BOTTOM);
00320     
00321     // Implements PlotCanvas::legendPosition().
00322     LegendPosition legendPosition() const;
00323     
00324     // Implements PlotCanvas::setLegendPosition().
00325     void setLegendPosition(LegendPosition position);
00326     
00327     // Implements PlotCanvas::legendLine().
00328     PlotLinePtr legendLine() const;
00329     
00330     // Implements PlotCanvas::setLegendLine().
00331     void setLegendLine(const PlotLine& line);
00332     
00333     // Implements PlotCanvas::legendFill().
00334     PlotAreaFillPtr legendFill() const;
00335     
00336     // Implements PlotCanvas::setLegendFill().
00337     void setLegendFill(const PlotAreaFill& area);
00338     
00339     // Implements PlotCanvas::legendFont().
00340     PlotFontPtr legendFont() const;
00341     
00342     // Implements PlotCanvas::setLegendFont().
00343     void setLegendFont(const PlotFont& font);
00344 
00345 
00346     // Implements PlotCanvas::autoIncrementColors().
00347     bool autoIncrementColors() const;
00348 
00349     // Implements PlotCanvas::setAutoIncrementColors().
00350     void setAutoIncrementColors(bool autoInc = true);
00351 
00352     // Implements PlotCanvas::exportToFile().
00353     bool exportToFile(const PlotExportFormat& format);
00354 
00355     // Implements PlotCanvas::fileChooserDialog().
00356     String fileChooserDialog(const String& title = "File Chooser",
00357             const String& directory = "");
00358     
00359     // Implements PlotCanvas::dateFormat().
00360     const String& dateFormat() const;
00361     
00362     // Implements PlotCanvas::setDateFormat().
00363     void setDateFormat(const String& dateFormat);
00364     
00365     // Implements PlotCanvas::relativeDateFormat().
00366     const String& relativeDateFormat() const;
00367     
00368     // Implements PlotCanvas::setRelativeDateFormat().
00369     void setRelativeDateFormat(const String& dateFormat);
00370 
00371     // Implements PlotCanvas::convertCoordinate().
00372     PlotCoordinate convertCoordinate(const PlotCoordinate& coord,
00373            PlotCoordinate::System newSystem = PlotCoordinate::WORLD) const;
00374 
00375     // Implements PlotCanvas::textWidthHeightDescent().
00376     vector<double> textWidthHeightDescent(const String& text,
00377             PlotFontPtr font) const;
00378     
00379     // Implements PlotCanvas::implementation().
00380     int implementation() const { return Plotter::QWT; }
00381     
00382     // Implements PlotCanvas::implementationFactory().
00383     PlotFactory* implementationFactory() const;
00384     
00385 
00386     // QPCanvas Methods //
00387     
00388     // Provides access to the underlying QPLayeredCanvas.
00389     // <group>
00390     QPLayeredCanvas& asQwtPlot();
00391     const QPLayeredCanvas& asQwtPlot() const;
00392     // </group>
00393     
00394     // Provides access to the QwtPlotPicker used for selection events.
00395     QwtPlotPicker& getSelecter();
00396     
00397     // Reinstalls the tracker filter (in case another QwtPlotPicker is added to
00398     // the QwtPlotCanvas).
00399     void reinstallTrackerFilter();
00400     
00401     // Overrides QWidget::sizeHint() to return an invalid size.
00402     QSize sizeHint() const;
00403     
00404     // Overrides QWidget::minimumSizeHint() to return an invalid size.
00405     QSize minimumSizeHint() const;
00406     
00407 protected:
00408     // Sets the parent QPPlotter to the given.  This MUST be done when a canvas
00409     // is added to the plotter so that it can use the plotter's logger if
00410     // needed.
00411     void setQPPlotter(QPPlotter* parent);
00412     
00413     // Returns the parent's logger.
00414     PlotLoggerPtr logger() const;
00415     
00416     // See QPPlotter::logObject().  If called before setQPPlotter() is called,
00417     // creates a queue that is then posted when setQPPlotter() is called.
00418     void logObject(const String& className, void* address, bool creation,
00419             const String& message = String());
00420     
00421     // See QPPlotter::logMethod().  Does NOT queue messages if called before
00422     // setQPPlotter() is called.
00423     void logMethod(const String& className, const String& methodName,
00424             bool entering, const String& message = String());
00425     
00426     // Provides access to the cached axes stack.
00427     // <group>
00428     QPAxesCache& axesCache();
00429     const QPAxesCache& axesCache() const;
00430     // </group>
00431     
00432     // For catching Qt press events.
00433     void mousePressEvent(QMouseEvent* event);
00434     
00435     // For catching Qt click and release events.
00436     void mouseReleaseEvent(QMouseEvent* event);
00437     
00438     // For catching Qt double-click events.
00439     void mouseDoubleClickEvent(QMouseEvent* event);
00440     
00441     // For catching Qt key events.
00442     void keyReleaseEvent(QKeyEvent* event);
00443     
00444     // For catching Qt scroll wheel events.
00445     void wheelEvent(QWheelEvent* event);
00446     
00447     // For catching Qt resize events.
00448     void resizeEvent(QResizeEvent* event);
00449 
00450 private:
00451     // Parent QPPlotter.
00452     QPPlotter* m_parent;
00453     
00454     // Queued log messages before parent is set.
00455     vector<PlotLogObject> m_queuedLogs;
00456     
00457     // Main QwtPlot object.
00458     QPLayeredCanvas m_canvas;
00459 
00460     // Main-layer plot items.
00461     vector<pair<PlotItemPtr, QPPlotItem*> > m_plotItems;
00462     
00463     // Annotation-layer plot items.
00464     vector<pair<PlotItemPtr, QPPlotItem*> > m_layeredItems;
00465     
00466     // Scale draws.
00467     QPScaleDraw* m_scaleDraws[QwtPlot::axisCnt];
00468     
00469     // Whether the axes ratio is locked or not.
00470     bool m_axesRatioLocked;
00471     
00472     // Used for recalculating axes ranges if the ratio is locked.
00473     vector<double> m_axesRatios;
00474     
00475     // Cached axes stack.
00476     QPAxesCache m_stackCache;
00477 
00478     // Whether auto-increment colors is turned on or not.
00479     bool m_autoIncColors;
00480     
00481     // Used auto-incremented colors.
00482     vector<int> m_usedColors;
00483     
00484     // Picker used for select events.
00485     QwtPlotPicker m_picker;
00486     
00487     // Filter used for mouse move events.  Has to initialize after the picker
00488     // to be first in the filter.
00489     QPMouseFilter m_mouseFilter;
00490     
00491     // Legend, and properties.
00492     // <group>
00493     QPLegendHolder* m_legend;
00494     QPFont m_legendFont;
00495     bool m_legendFontSet;
00496     // </group>
00497     
00498     // Flag for whether we're in mouse dragging mode or not.
00499     bool m_inDraggingMode;
00500     
00501     /*
00502     // For catching single vs. double clicks.
00503     // <group>
00504     bool m_ignoreNextRelease;
00505     QTimer m_timer;
00506     QMouseEvent* m_clickEvent;
00507     // </group>
00508      */
00509     
00510     // Date formats.
00511     // <group>
00512     String m_dateFormat;
00513     String m_relativeDateFormat;
00514     // </group>
00515     
00516        
00517     // Converts the given Qt global position to a pixel PlotCoordinate.
00518     // <group>
00519     PlotCoordinate globalPosToPixelCoord(int x, int y);
00520     PlotCoordinate globalPosToPixelCoord(QMouseEvent* event) {
00521         return globalPosToPixelCoord(event->globalX(), event->globalY()); }
00522     PlotCoordinate globalPosToPixelCoord(QWheelEvent* event) {
00523         return globalPosToPixelCoord(event->globalX(), event->globalY()); }
00524     // </group>
00525     
00526     
00527     // Static //
00528     
00529     // Method for static exportPlotter() and exportCanvas() methods.
00530     // (Formerly exportHelper.  "Helper" in a class or method name is a code smell!)
00531     static bool exportCanvases(vector<PlotCanvasPtr>& canvases,
00532             const PlotExportFormat& format, QPCanvas* grabCanvas,
00533             QPPlotter* grabPlotter);
00534 
00535         
00536         
00537         // Methods to perform particular types of exports - internal use only!
00538         static 
00539         bool  exportToImageFile(
00540                                         const PlotExportFormat& format, 
00541                                         vector<QPCanvas*> &qcanvases,
00542                                         QPCanvas* grabCanvas,
00543                                         QPPlotter* grabPlotter
00544                                         );
00545     static 
00546     QImage produceHighResImage(
00547                                         const PlotExportFormat& format,
00548                                         vector<QPCanvas*> &qcanvases,
00549                                         int width, int height,
00550                                         bool &wasCanceled
00551                                         );   
00552     static 
00553     QImage  grabImageFromCanvas(
00554                                 const PlotExportFormat& format, 
00555                                 QPCanvas* grabCanvas,
00556                                 QPPlotter* grabPlotter
00557                                 );
00558     static 
00559     bool   exportPostscript(
00560                                 const PlotExportFormat& format, 
00561                                 vector<QPCanvas*> &qcanvases
00562                                 );
00563 
00564     
00565     // Converts between axes bitset flags (1,2,4,8 in PlotAxis and vector indices (0-3).
00566     // (Does not deal with bitsets for combinations of axes, only single axis)
00567     // <group>
00568     static unsigned int axisIndex(PlotAxis a);   
00569     static PlotAxis axisIndex(unsigned int i);
00570     // </group>
00571     
00572     
00573 private slots:    
00574     // For when the selecter has selected a region; emit a PlotSelectEvent.
00575     void regionSelected(const QwtDoubleRect&);
00576     
00577     // For catching single vs. double clicks.
00578     // void timeout();
00579     
00580     // For catching mouse move events from the filter.
00581     void trackerMouseEvent(QMouseEvent* event);
00582 };
00583 
00584 }
00585 
00586 #endif
00587 
00588 #endif /*QPCANVAS_QO_H_*/