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

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1