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