QPLayeredCanvas.qo.h

Go to the documentation of this file.
00001 //# QPLayeredCanvas.qo.h: Subclass of QwtPlot to add layers and other features.
00002 //# Copyright (C) 2009
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 QPLAYEREDCANVAS_QO_H_
00028 #define QPLAYEREDCANVAS_QO_H_
00029 
00030 #ifdef AIPS_HAS_QWT
00031 
00032 #include <casaqt/QwtPlotter/QPCanvasHelpers.qo.h>
00033 #include <casaqt/QwtPlotter/QPPlotItem.qo.h>
00034 
00035 #include <qwt_plot.h>
00036 
00037 #include <QPicture>
00038 
00039 namespace casa {
00040 
00041 //# Forward declarations
00042 class QPCanvas;
00043 
00044 
00045 // Abstract class for a single cached layer.
00046 class QPLayer {
00047 public:
00048     // Constructor.
00049     QPLayer();
00050     
00051     // Destructor.
00052     virtual ~QPLayer();
00053     
00054     
00055     // Clears the cache image.
00056     virtual void clearImage() const;
00057     
00058     // Initializes the cache image to the given size using the given color for
00059     // a fill base (see QPImageCache::fill()).
00060     virtual void initializeImage(const QSize& size,
00061             unsigned int fillValue = Qt::transparent) const;
00062     
00063     // Returns whether this layer has at least one item for which
00064     // QPLayerItem::shouldDraw() returns true.
00065     virtual bool hasItemsToDraw() const;
00066     
00067     // Returns the draw count for items on this layer (see
00068     // QPLayerItem::drawCount()).
00069     virtual unsigned int itemDrawCount() const;
00070     
00071     // Returns the number of draw segments for items on this layer, using the
00072     // given segment threshold.
00073     virtual unsigned int itemDrawSegments(unsigned int threshold =
00074             QPDrawThread::DEFAULT_SEGMENT_THRESHOLD) const;
00075     
00076     // Draws the attached items using the given painter, canvas rect, and scale
00077     // maps.  If PlotOperation parameters are given, they are updated as
00078     // needed.
00079     virtual void drawItems(QPainter* painter, const QRect& canvasRect,
00080             const QwtScaleMap maps[QwtPlot::axisCnt],
00081             PlotOperationPtr op = PlotOperationPtr(),
00082             unsigned int currentSegment = 0,
00083             unsigned int totalSegments = 0,
00084             unsigned int segmentThreshold =
00085                 QPDrawThread::DEFAULT_SEGMENT_THRESHOLD) const;
00086 
00087     // Draws the attached layer items into the image cache, using the given
00088     // canvas rect and scale maps.  If PlotOperation parametesr are given, they
00089     // are updated as needed.
00090     virtual void cacheItems(const QRect& canvasRect,
00091             const QwtScaleMap maps[QwtPlot::axisCnt],
00092             PlotOperationPtr op = PlotOperationPtr(),
00093             unsigned int currentSegment = 0,
00094             unsigned int totalSegments = 0,
00095             unsigned int segmentThreshold =
00096                 QPDrawThread::DEFAULT_SEGMENT_THRESHOLD) const;
00097     
00098     // Draws the cached image using the given painter and draw rect.
00099     virtual void drawCache(QPainter* painter, const QRect& rect) const;
00100     
00101     // Returns the cached image.
00102     // <group>
00103     QPImageCache& cachedImage();
00104     const QPImageCache& cachedImage() const;
00105     // </group>
00106     
00107     
00108     // ABSTRACT METHODS //
00109     
00110     // Returns all attached layer items.
00111     // <group>
00112     virtual QList<QPLayerItem*> items() = 0;
00113     virtual QList<const QPLayerItem*> items() const = 0;
00114     // </group>
00115     
00116 protected:
00117     // Cached image.
00118     QPImageCache m_cachedImage;
00119 };
00120 
00121 
00122 // Subclass of QPLayer for QPPlotItems.
00123 class QPCanvasLayer : public QPLayer {
00124 public:
00125     // Constructor.
00126     QPCanvasLayer();
00127     
00128     // Destructor.
00129     ~QPCanvasLayer();
00130 
00131     
00132     // Adds/Removes the given QPPlotItem.
00133     // <group>
00134     void addItem(QPPlotItem* item);
00135     void removeItem(QPPlotItem* item);
00136     // </group>
00137     
00138     // Returns true if the given item is in this layer, false otherwise.
00139     bool containsItem(QPPlotItem* item);
00140     
00141     // Returns all attached canvas items.
00142     // <group>
00143     QList<QPPlotItem*> canvasItems() { return m_items; }
00144     QList<const QPPlotItem*> canvasItems() const;
00145     // </group>
00146     
00147     
00148     // Implements QPLayer::items().
00149     // <group>
00150     QList<QPLayerItem*> items();
00151     QList<const QPLayerItem*> items() const;
00152     // </group>
00153     
00154 private:
00155     // Items.
00156     QList<QPPlotItem*> m_items;
00157 };
00158 
00159 
00160 // Subclass of QPLayer for QPBaseItems.
00161 class QPBaseLayer : public QPLayer {
00162 public:
00163     // Constructor.
00164     QPBaseLayer();
00165     
00166     // Destructor.
00167     ~QPBaseLayer();
00168     
00169     
00170     // Adds/Removes the given QPBaseItem.
00171     // <group>
00172     void addItem(QPBaseItem* item);
00173     void removeItem(QPBaseItem* item);
00174     // </group>
00175     
00176     // Returns true if the given item is in this layer, false otherwise.
00177     bool containsItem(QPBaseItem* item);
00178     
00179     // Returns all attached canvas items.
00180     // <group>
00181     QList<QPBaseItem*> canvasItems() { return m_items; }
00182     QList<const QPBaseItem*> canvasItems() const;
00183     // </group>
00184     
00185     
00186     // Implements QPLayer::items().
00187     // <group>
00188     QList<QPLayerItem*> items();
00189     QList<const QPLayerItem*> items() const;
00190     // </group>
00191     
00192 private:
00193     // Items.
00194     QList<QPBaseItem*> m_items;
00195 };
00196 
00197 
00198 // Subclass of QwtPlot to manage layers as specified by PlotCanvas and
00199 // PlotCanvasLayer.  Also has an additional "base" layer for things like the
00200 // grid and Cartesian axes.  Works with QPCanvas and QPPlotItem.
00201 class QPLayeredCanvas : public QwtPlot {
00202     Q_OBJECT
00203     
00204     friend class QPAxesCache;
00205     friend class QPCanvas;
00206     friend class QPAxis;
00207     friend class QPPlotItem;
00208     friend class QPBaseItem;
00209     friend class QPLegendHolder;
00210     
00211 public:
00212     // Convenient access to class name.
00213     static const String CLASS_NAME;
00214     
00215     
00216     // Constructor which takes parent canvas (optional) parent widget.
00217     QPLayeredCanvas(QPCanvas* parent, QWidget* parentWidget = NULL);
00218     
00219     // Constructor which takes the canvas title, the parent canvas, and an
00220     // (optional) parent widget.
00221     QPLayeredCanvas(const QwtText& title, QPCanvas* parent,
00222                     QWidget* parentWidget = NULL);
00223     
00224     // Destructor.
00225     ~QPLayeredCanvas();
00226     
00227     
00228     // Include overloaded methods.
00229     using QwtPlot::replot;
00230 
00231     
00232     // Item Methods //
00233     
00234     // Returns a list of all attached QPPlotItems on this canvas.
00235     QList<const QPPlotItem*> allAttachedItems() const {
00236         return allLayerItems(PlotCanvas::allLayersFlag()); }
00237     
00238     // Returns a list of the QPPlotItems attached to this canvas in the
00239     // layers as indicated (an or'ed value of PlotCanvasLayer enum values).
00240     QList<const QPPlotItem*> allLayerItems(int layersFlag) const;
00241     
00242     // Returns the rect for drawing items.
00243     QRect canvasDrawRect() const;
00244     
00245     
00246     // Draw Methods //
00247     
00248     // Overrides QwtPlot::print() to properly handle pixmap caches.
00249 #if QWT_VERSION >= 0x060000
00250     void print(QPainter* painter, const QRect& rect);
00251     void print(QPaintDevice& paintDev);
00252 #else
00253     using QwtPlot::print;
00254     void print(QPainter* painter, const QRect& rect,
00255             const QwtPlotPrintFilter& filter = QwtPlotPrintFilter()) const;
00256 #endif
00257     
00258     // Is drawing in progress?
00259     virtual Bool isDrawing();
00260     
00261     // Event Methods //
00262     
00263     // Overrides QObject::eventFilter().  Used to pass events to the canvas
00264     // when it is being covered by an interior legend.
00265     bool eventFilter(QObject* watched, QEvent* event);
00266     
00267 protected:
00268     // Item Methods //
00269     
00270     // Attaches the given item to the given layer.
00271     void attachLayeredItem(QPPlotItem* item);
00272     
00273     // Detaches the given item.
00274     void detachLayeredItem(QPPlotItem* item);
00275    
00276 
00277 
00278  
00279     
00280     // Draw Methods //
00281     
00282     // Overrides QwtPlot::drawItems().
00283     // No QwtPlotPrintFilter in qwt 6
00284     void drawItems(QPainter* painter, const QRect& rect,
00285                    const QwtScaleMap maps[axisCnt]);
00286     void printItems(QPainter* painter, const QRect& rect,
00287                    const QwtScaleMap maps[axisCnt]);
00288 
00289 #if QWT_VERSION < 0x060000
00290     // QwtPlotPrintFilter  not used, keep signature but call other method
00291     void drawItems(QPainter* painter, const QRect& rect,
00292                    const QwtScaleMap maps[axisCnt],
00293                    const QwtPlotPrintFilter& );
00294 
00295     // Like drawItems, but doesn't do threaded/cached drawing.
00296     // QwtPlotPrintFilter  not used, keep signature but call other method
00297     void printItems(QPainter* painter, const QRect& rect,
00298                    const QwtScaleMap maps[axisCnt],
00299                    const QwtPlotPrintFilter& /*filter*/) {
00300         printItems(painter, rect, maps);
00301     }
00302 #endif
00303  
00304     // Overrides QwtPlot::printLegend().
00305     //void printLegend(QPainter* painter, const QRect& rect) const;
00306     
00307     // Provides access to QwtPlot::printLegend(), and also lets the legend
00308     // draw its outline and background if needed.
00309     //void printLegend_(QPainter* painter, const QRect& rect) const;
00310     
00311     // Hold/Release drawing.
00312     // <group>
00313     bool drawingIsHeld() const;
00314     void holdDrawing();
00315     void releaseDrawing();
00316     // </group>
00317     
00318     
00319     // Layer Methods //
00320     
00321     // Sets whether items in the layers have changed or not.  Each layer has
00322     // a changed flag that is or'ed with the given value.  These flags are
00323     // reset to false after the next draw.
00324     // <group>
00325     void setLayerChanged(PlotCanvasLayer layer);
00326     void setLayersChanged(int layersFlag);
00327     void setAllLayersChanged() {
00328         setLayersChanged(PlotCanvas::allLayersFlag()); }
00329     // </group>
00330     
00331     // Returns whether the given layer has changed or not since the last draw.
00332     bool changedLayer(PlotCanvasLayer layer) const;
00333     
00334     // Returns whether any layer has changed since the last draw or not.
00335     bool anyChangedLayer() const;
00336     
00337     // Returns the or'ed value of PlotCanvasLayers which have changed since the
00338     // last draw.
00339     int changedLayersFlag() const;
00340     
00341     
00342     // Base Items Methods //
00343     
00344     // Provides access to the grid.
00345     // <group>
00346     const QPGrid& grid() const;
00347     QPGrid& grid();
00348     // </group>
00349     
00350     // Provides access to the cartesian axes.
00351     // <group>
00352     const QHash<PlotAxis, QPCartesianAxis*>& cartesianAxes() const;
00353     QHash<PlotAxis, QPCartesianAxis*>& cartesianAxes();
00354     // </group>
00355     
00356     // See PlotCanvas::cartesianAxisShown().
00357     bool cartesianAxisShown(PlotAxis axis) const;
00358     
00359     // See PlotCanvas::showCartesianAxis().
00360     void showCartesianAxis(PlotAxis mirrorAxis, PlotAxis secondaryAxis,
00361             bool show);
00362     
00363 
00364     // Event Methods //
00365     
00366     // Filters input events on the given frame to pass to the canvas.
00367     void installLegendFilter(QWidget* legendFrame);
00368     
00369 private:
00370     // Parent QPCanvas.
00371     QPCanvas* m_parent;
00372     
00373     
00374     // Base layer.
00375     QPBaseLayer m_layerBase;
00376     
00377     // Canvas layers.
00378     QMap<PlotCanvasLayer, QPCanvasLayer*> m_layers;
00379     
00380     // Layer changed flags.
00381     QHash<PlotCanvasLayer, bool> m_changedLayers;
00382     
00383     
00384     // Flag for whether drawing is currently held or not.
00385     bool m_drawingHeld;
00386     
00387     // Flag for whether we're printing rather than drawing onto a widget.
00388     bool m_isPrinting;
00389     
00390     // Printing painters, so we can call printItem insteads of drawItems.
00391     QList<QPainter*> m_printPainters;
00392     
00393     // Current draw thread, or NULL for none.
00394     mutable QPDrawThread* m_drawThread;
00395     
00396     // Flag for whether a redraw currently in progress should be restarted.
00397     bool m_redrawWaiting;
00398     
00399     
00400     // Canvas grid.
00401     QPGrid* m_grid;
00402     
00403     // Cartesian axes.
00404     QHash<PlotAxis, QPCartesianAxis*> m_cartAxes;
00405 
00406     
00407     // Legend frame to watch for events.
00408     QWidget* m_legendFrame;
00409     
00410     
00411     // Initializes object (meant to be called from constructor).
00412     void initialize();
00413 
00414 private slots:
00415     // Slot for when the current draw thread is finished.
00416     void itemDrawingFinished();
00417 };
00418 
00419 }
00420 
00421 #endif
00422 
00423 #endif /* QPLAYEREDCANVAS_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