QPPlotItem.qo.h

Go to the documentation of this file.
00001 //# QPPlotItem.qo.h: Superclass for all plot items in qwt plotter.
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 QPPLOTITEM_H_
00028 #define QPPLOTITEM_H_
00029 
00030 #ifdef AIPS_HAS_QWT
00031 
00032 #include <casaqt/QwtPlotter/QPImageCache.h>
00033 #include <graphics/GenericPlotter/PlotLogger.h>
00034 #include <graphics/GenericPlotter/PlotOperation.h>
00035 #include <graphics/GenericPlotter/PlotItem.h>
00036 
00037 #include <QHash>
00038 #include <QPainter>
00039 #include <QThread>
00040 
00041 #include <qwt_plot.h>
00042 #include <casaqt/QwtConfig.h>
00043 
00044 namespace casa {
00045 
00046 //# Forward Declarations
00047 class QPCanvas;
00048 
00049 
00050 // Abstract superclass for any layered item that will be drawn on a
00051 // QPLayeredCanvas.
00052 class QPLayerItem : public QwtPlotItem {
00053     friend class QPCanvas;
00054     friend class QPLayeredCanvas;
00055     friend class QPDrawThread;
00056     
00057 public:
00058     // Constructor.
00059     QPLayerItem();
00060     
00061     // Destructor.
00062     virtual ~QPLayerItem();
00063     
00064     
00065     // Implements QwtPlotItem::draw().
00066 #if QWT_VERSION >= 0x060000
00067     virtual void draw(QPainter* p, const QwtScaleMap& xMap,
00068             const QwtScaleMap& yMap, const QRectF& canvasRect) const;
00069 #else
00070     virtual void draw(QPainter* p, const QwtScaleMap& xMap,
00071             const QwtScaleMap& yMap, const QRect& canvasRect) const;
00072 #endif
00073  
00074     // See PlotItem::drawSegments().
00075     virtual unsigned int itemDrawSegments(unsigned int segmentThreshold) const;
00076     
00077     
00078     // ABSTRACT METHODS //
00079     
00080     // Forces subclasses to override QwtPlotItem::itemChanged() to redraw only
00081     // the necessary layer.
00082     virtual void itemChanged() = 0;
00083     
00084     // Returns true if this item should be drawn, false otherwise.  This is
00085     // used to avoid drawing attached items that are empty or otherwise have
00086     // nothing to draw.
00087     virtual bool shouldDraw() const = 0;
00088     
00089     // See PlotItem::drawCount().
00090     virtual unsigned int itemDrawCount() const = 0;
00091     
00092     // See PlotItem::title().
00093     virtual String itemTitle() const = 0;
00094     
00095 protected:
00096     // Like QwtPlotItem::draw() except that the child item should only draw
00097     // drawCount items starting at drawIndex.  The indexing may not be
00098     // applicable to all layer items (i.e., some items may draw everything in
00099     // this call rather than segmenting).
00100 #if QWT_VERSION >= 0x060000
00101     virtual void draw_(QPainter* p, const QwtScaleMap& xMap,
00102             const QwtScaleMap& yMap, const QRectF& drawRect,
00103             unsigned int drawIndex, unsigned int drawCount) const = 0;
00104 #else
00105     virtual void draw_(QPainter* p, const QwtScaleMap& xMap,
00106             const QwtScaleMap& yMap, const QRect& drawRect,
00107             unsigned int drawIndex, unsigned int drawCount) const = 0;
00108 #endif
00109 };
00110 
00111 
00112 // Subclass of PlotItem to take care of common functionality that is provided
00113 // by QwtPlotItem.
00114 class QPPlotItem : public virtual PlotItem, public QPLayerItem {
00115     friend class QPCanvas;
00116     friend class QPDrawThread;
00117     
00118 public:
00119     // Static //
00120     
00121     // Convenient access to "origin" name for draw method for logging.
00122     static const String DRAW_NAME;
00123     
00124     using QPLayerItem::draw;
00125     // Returns true if the given pointer is a Qwt plotter implementation,
00126     // false otherwise.
00127     static bool isQPPlotItem(const PlotItemPtr item);
00128     
00129     // If the given item is not a Qwt plotter implementation, returns a copy
00130     // of the proper class.  Otherwise, returns the item.  If wasCloned is
00131     // given, it will be set to true if the returned item is new, false
00132     // otherwise.
00133     static QPPlotItem* cloneItem(const PlotItemPtr item, bool* wasCloned=NULL);
00134     
00135     // Returns true if the two items are the same type (class), false
00136     // otherwise.
00137     static bool sameType(QPPlotItem* item1, QPPlotItem* item2);
00138     
00139     // Returns true if the given item is a Plot type (QPBarPlot, QPHistogram,
00140     // QPRasterPlot, or QPScatterPlot) or not.
00141     static bool isPlot(QPPlotItem* item);
00142     
00143     
00144     // Non-Static //
00145     
00146     // Constructor.
00147     QPPlotItem();
00148     
00149     // Destructor.
00150     virtual ~QPPlotItem();
00151     
00152     
00153     // PlotItem methods //
00154     
00155     // Implements PlotItem::canvas().
00156     PlotCanvas* canvas() const;
00157     
00158     // Implements PlotItem::title().
00159     String title() const;
00160     
00161     // Implements PlotItem::setTitle().
00162     void setTitle(const String& newTitle);
00163     
00164     // Implements PlotItem::isQWidget().
00165     virtual bool isQWidget() const { return false; }
00166     
00167     // Implements PlotItem::xAxis().
00168     PlotAxis xAxis() const;
00169     
00170     // Implements PlotItem::yAxis().
00171     PlotAxis yAxis() const;
00172     
00173     // Implements PlotItem::setXAxis().
00174     void setXAxis(PlotAxis x);
00175     
00176     // Implements PlotItem::setYAxis().
00177     void setYAxis(PlotAxis y);
00178     
00179     
00180     // QPLayerItem methods //
00181     
00182     // Implements QPLayerItem::itemChanged() to only redraw the canvas layer
00183     // this item is in.
00184     virtual void itemChanged();
00185     
00186     // Implements QPLayerItem::shouldDraw().
00187     virtual bool shouldDraw() const { return isValid(); }
00188     
00189     // Implements QPLayerItem::itemDrawCount().
00190     unsigned int itemDrawCount() const{ return shouldDraw()? drawCount() : 0; }
00191     
00192     // Implements QPLayerItem::itemTitle().
00193     String itemTitle() const { return title(); }
00194     
00195     
00196     // QPPlotItem methods //
00197     
00198     // Returns the layer this item is attached to.
00199     PlotCanvasLayer canvasLayer() const { return m_layer; }
00200     
00201     // Provides access to QwtPlotItem methods that have been overloaded.
00202     // <group>
00203     const QwtText& qwtTitle() const { return QwtPlotItem::title(); }
00204     void setQwtTitle(const QwtText& text) { QwtPlotItem::setTitle(text); }
00205     QwtPlot::Axis qwtXAxis() const{return QwtPlot::Axis(QwtPlotItem::xAxis());}
00206     QwtPlot::Axis qwtYAxis() const{return QwtPlot::Axis(QwtPlotItem::yAxis());}    
00207     void qwtAttach(QwtPlot* plot) { QwtPlotItem::attach(plot); }
00208     void qwtDetach() { QwtPlotItem::detach(); }
00209     // </group>
00210 
00211     
00212     // ABSTRACT METHODS //
00213     
00214     // Forces children to override QwtPlotItem::boundingRect().
00215     virtual QwtDoubleRect boundingRect() const = 0;
00216 
00217 #if QWT_VERSION < 0x060000
00218     // Legends are totally different in Qwt 6
00219     // Forces children to override QwtPlotItem::legendItem().
00220     virtual QWidget* legendItem() const = 0;
00221 #endif
00222     
00223 protected:    
00224     // Attached canvas (or NULL for none).
00225     QPCanvas* m_canvas;
00226     
00227     // Flag for whether this object's creation has been logged or not.  This
00228     // happens the first time the item is attached to a canvas.
00229     bool m_loggedCreation;
00230     
00231     // Which layer this item is in.
00232     PlotCanvasLayer m_layer;
00233     
00234     
00235     // Provides access to QwtPlotItem's attach and detach methods for QPCanvas.
00236     // <group>
00237     void attach(QPCanvas* canvas, PlotCanvasLayer layer);
00238     void detach();
00239     // </group>
00240     
00241     // Provides access to the plotter's logger for children.
00242     PlotLoggerPtr logger() const;
00243     
00244     // Provides access to logging destruction, for children.  Should be called
00245     // in children's destructor.
00246     void logDestruction();
00247     
00248     // Provides access to log method enter/exit, for children.
00249     void logMethod(const String& methodName, bool entering,
00250             const String& message = String()) const;
00251     
00252     // Provides access to QPCanvas's draw operation for children.
00253     PlotOperationPtr drawOperation() const;
00254     
00255     
00256     // ABSTRACT METHODS //
00257     
00258     // Returns the class name for the child, for logging purposes.
00259     virtual const String& className() const = 0;
00260 };
00261 
00262 
00263 // Thread for drawing multiple QPPlotItems into QPImageCaches based on its
00264 // canvas layer.  Once the thread is finished, the QPImageCaches can be copied
00265 // to the canvas caches and shown on the GUI widget as needed.  The thread will
00266 // emit a signal after each "segment" is drawn, either the whole item for small
00267 // items or part of a large item.
00268 class QPDrawThread : public QThread {
00269     Q_OBJECT
00270     
00271 public:
00272     // Static //
00273     
00274     // Convenient access to class name.
00275     static const String CLASS_NAME;
00276     
00277     // Returns the default segment threshold.
00278     static const unsigned int DEFAULT_SEGMENT_THRESHOLD;
00279     
00280     // Returns item1->z() < item2->z().
00281     // <group>
00282     static bool itemSortByZ(const QPLayerItem* item1,const QPLayerItem* item2);
00283     // </group>
00284     
00285     // Draws the given items, sorted by z-order, using the given painter, rect,
00286     // and maps.  If PlotOperation parameters are given, they are updated as
00287     // needed.
00288     // <group>
00289     static void drawItem(const QPLayerItem* item, QPainter* painter,
00290             const QRect& rect, const QwtScaleMap maps[QwtPlot::axisCnt]);
00291     static void drawItem(const QPLayerItem* item, QPainter* painter,
00292             const QRect& rect, const QwtScaleMap maps[QwtPlot::axisCnt],
00293             unsigned int drawIndex, unsigned int drawCount);
00294     static void drawItems(const QList<const QPLayerItem*>& items,
00295             QPainter* painter, const QRect& rect,
00296             const QwtScaleMap maps[QwtPlot::axisCnt],
00297             PlotOperationPtr op = PlotOperationPtr(),
00298             unsigned int currentSegment = 0, unsigned int totalSegments = 0,
00299             unsigned int segmentThreshold =
00300                 QPDrawThread::DEFAULT_SEGMENT_THRESHOLD);
00301     static void drawItems(const QList<const QPPlotItem*>& items,
00302             QPainter* painter, const QRect& rect,
00303             const QwtScaleMap maps[QwtPlot::axisCnt],
00304             PlotOperationPtr op = PlotOperationPtr(),
00305             unsigned int currentSegment = 0, unsigned int totalSegments = 0,
00306             unsigned int segmentThreshold =
00307                 QPDrawThread::DEFAULT_SEGMENT_THRESHOLD);
00308     // </group>
00309     
00310     
00311     // Non-Static //
00312     
00313     // Constructor which takes a list of items to draw, axes maps, the drawing
00314     // rectangle, an optional fixed image size, and an optional segment
00315     // threshold.
00316     QPDrawThread(const QList<const QPPlotItem*>& items,
00317             const QwtScaleMap maps[QwtPlot::axisCnt], QSize imageSize,
00318             unsigned int segmentTreshold = DEFAULT_SEGMENT_THRESHOLD);
00319     
00320     // Destructor.
00321     ~QPDrawThread();
00322     
00323     // Returns the total segments that will be drawn.  This is AT LEAST the
00324     // number of items to be drawn.
00325     unsigned int totalSegments() const;
00326     
00327     // Implements QThread::run().  Draws the items into one of the two images
00328     // depending on their layers.  Emits segmentDrawn() after each segment is
00329     // finished.
00330     void run();
00331     
00332     // Returns the image result for the given layer.  Should only be done AFTER
00333     // the thread is finished.  If clearResult is true, then the resulting
00334     // image is removed from the thread's images.
00335     QPImageCache drawResult(PlotCanvasLayer layer, bool clearResult = true);
00336     
00337     //Used for identifying threads when debugging.
00338     int getId() const {
00339         return id;
00340     }
00341 
00342 public slots:
00343     // Cancels this thread.  If the thread is currently running, it will finish
00344     // the segment it is on and then stop.
00345     void cancel();
00346     
00347 private:
00348     int id;
00349 
00350     // Items to draw.
00351     QList<const QPPlotItem*> m_items;
00352     
00353     // Axes maps.
00354     QwtScaleMap m_axesMaps[QwtPlot::axisCnt];
00355     
00356     // Images to draw into.
00357     QHash<PlotCanvasLayer, QPImageCache*> m_images;
00358     
00359     // Maximum number of draw items per segment.
00360     unsigned int m_segmentThreshold;
00361     
00362     // Flag that thread checks while running, to cancel rest of draw.
00363     bool m_cancelFlag;
00364 };
00365 
00366 }
00367 
00368 #endif
00369 
00370 #endif /* QPPLOTITEM_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1