QPCanvasHelpers.qo.h

Go to the documentation of this file.
00001 //# QPCanvasHelpers.qo.h: Helper classes for QPCanvas.
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 QPCANVASHELPERS_QO_H_
00028 #define QPCANVASHELPERS_QO_H_
00029 
00030 #ifdef AIPS_HAS_QWT
00031 
00032 #include <casaqt/QwtPlotter/QPOptions.h>
00033 #include <casaqt/QwtPlotter/QPPlotItem.qo.h>
00034 #include <graphics/GenericPlotter/PlotCanvas.h>
00035 
00036 #include <qwt_legend.h>
00037 #if QWT_VERSION >= 0x060000
00038 #include <qwt_plot_legenditem.h>
00039 #endif
00040 #include <qwt_plot.h>
00041 #include <qwt_plot_grid.h>
00042 #include <qwt_scale_draw.h>
00043 
00044 #include <QObject>
00045 #include <QMouseEvent>
00046 #include <QSpacerItem>
00047 
00048 namespace casa {
00049 
00050 //# Forward Declarations
00051 class QPCanvas;
00052 
00053 
00054 // Miscellaneous Classes //
00055 
00056 // Filter to install on the QwtPlotCanvas to catch mouse move events (which
00057 // are otherwise stolen by other filters in QwtPicker classes).
00058 class QPMouseFilter : public QObject {
00059     Q_OBJECT
00060     
00061 public:
00062     // Constructor which takes the canvas to install itself on.
00063     QPMouseFilter(QwtPlotCanvas* canvas);
00064     QPMouseFilter(QWidget* widget);
00065 
00066     // Destructor.
00067     ~QPMouseFilter();
00068     
00069     // Turns mouse tracking on or off on the underlying canvas.
00070     void turnTracking(bool on);
00071     
00072 signals:
00073     // This signal is emitted if tracking is turned on, and when a
00074     // QMouseEvent is received for a mouse move on the underlying canvas.
00075     void mouseMoveEvent(QMouseEvent* e);
00076     
00077 protected:
00078     // Filter the events on the given object.  Checks only for mouse events
00079     // on the underlying canvas.
00080     bool eventFilter(QObject* obj, QEvent* ev);
00081     
00082 private:
00083     // Canvas.
00084     QwtPlotCanvas* m_canvas;
00085 };
00086 
00087 
00088 // Subclass of QwtScaleDraw to implement additional functionality.  Keeps track
00089 // of which PlotAxisScale is set, and can use reference values instead of
00090 // absolute values (see PlotCanvas::setAxisReferenceValue()).  Additionally,
00091 // for date values it can convert a double in either modified julian seconds or
00092 // modified julian days into a String representation of the date, using a
00093 // format that can be set.
00094 class QPScaleDraw : public QwtScaleDraw {
00095 public: 
00096         // Constructor that takes the parent and axis to attach to.
00097     QPScaleDraw(QwtPlot* parent, QwtPlot::Axis axis);
00098     
00099     // Destructor.
00100     ~QPScaleDraw();
00101     
00102     // Gets/Sets the scale used.
00103     // <group>
00104     PlotAxisScale scale() const;
00105     void setScale(PlotAxisScale scale);
00106     // </group>
00107     
00108     // Gets/Sets the format used for date values.  See Plotter::dateFormat().
00109     // <group>
00110     const String& dateFormat() const;
00111     void setDateFormat(const String& newFormat);
00112     // </group>
00113     
00114     // Gets/Sets the format used for relative date values.  See
00115     // Plotter::relativeDateFormat().
00116     // <group>
00117     const String& relativeDateFormat() const;
00118     void setRelativeDateFormat(const String& newFormat);
00119     // </group>
00120     
00121     // Gets/Sets the reference value.  See PlotCanvas::setAxisReferenceValue().
00122     // <group>
00123     bool referenceValueSet() const;    
00124     double referenceValue() const;    
00125     void setReferenceValue(bool on, double value = 0);
00126     // </group>
00127     
00128     // Overrides QwtScaleDraw::label().
00129     QwtText label(double value) const;
00130     
00131     virtual void draw(QPainter* painter, const QPalette& palette) const;
00132 
00133 #if QWT_VERSION < 0x060000
00134     virtual int extent( const QPen& pen, const QFont& font ) const;
00135 #endif
00136 
00137 private:
00138         // Parent.
00139         QwtPlot* m_parent;
00140         
00141         // Axis.
00142         QwtPlot::Axis m_axis;
00143         
00144         // Scale.
00145         PlotAxisScale m_scale;
00146         
00147         // Date formats.
00148         // <group>
00149         String m_dateFormat;
00150         String m_relativeDateFormat;
00151         // </group>
00152         
00153         // Reference value.
00154         // <group>
00155         bool m_referenceSet;
00156         double m_referenceValue;
00157         // </group>
00158 };
00159 
00160 
00161 // Legend Classes //
00162 
00163 // Subclass of QwtLegend to handle outline and background, and be able to draw
00164 // itself using a QPainter.
00165 class QPLegend : public QwtLegend {
00166 public:
00167     // Constructor which takes optional parent widget.
00168     QPLegend(QWidget* parent = NULL);
00169     
00170     // Destructor.
00171     ~QPLegend();
00172     
00173     
00174     // Overrides QwtLegend::sizeHint() to take border into account.
00175     QSize sizeHint() const;
00176     
00177     // Gets/Sets the outline.
00178     // <group>
00179     const QPLine& line() const;
00180     const QPen& pen() const;
00181     void setLine(const PlotLine& line);
00182     void setLine(const QPen& pen) { setLine(QPLine(pen)); }
00183     // </group>
00184     
00185     // Gets/Sets the background.
00186     // <group>
00187     const QPAreaFill& areaFill() const;
00188     const QBrush& brush() const;
00189     //The update flag allows an export operation to set the fill without affecting
00190     //what is shown on the GUI.
00191     void setAreaFill(const PlotAreaFill& fill, bool update=true);
00192     void setAreaFill(const QBrush& brush) { setAreaFill(QPAreaFill(brush)); }
00193     // </group>
00194     
00195     // Draws the legend's outline and bacgkround using the given painter on the
00196     // given rect.  If useQwtPainter is true, QwtPainter is used (which
00197     // preserves any set QwtMetricsMap).
00198     void drawOutlineAndBackground(QPainter* painter, const QRect& rect,
00199             bool useQwtPainter = false);
00200     
00201 protected:
00202     // Overrides QWidget::paintEvent() to draw outline and background if
00203     // needed.
00204     void paintEvent(QPaintEvent* event);
00205     
00206 private:
00207     // Outline.
00208     QPLine m_line;
00209     
00210     // Background.
00211     QPAreaFill m_areaFill;
00212 };
00213 
00214 
00215 // Holder for QPLegend that is responsible for its placement.
00216 class QPLegendHolder : public QWidget {
00217     Q_OBJECT
00218     
00219 public:
00220     // Default padding for internal legends.
00221     static const int DEFAULT_INTERNAL_PADDING;
00222     
00223     // Converts from our legend position to Qwt's.
00224     static QwtPlot::LegendPosition legendPosition(
00225             PlotCanvas::LegendPosition pos);
00226     
00227     
00228     // Constructor which takes the canvas and initial position.
00229     QPLegendHolder(QPCanvas* canvas, PlotCanvas::LegendPosition position,
00230             int padding = DEFAULT_INTERNAL_PADDING);
00231     
00232     // Destructor.
00233     ~QPLegendHolder();
00234 
00235     QPLegend* legend() { return m_legend; }
00236     // Shows/hides the legend.
00237     // <group>
00238     bool legendShown() const;
00239     void showLegend(bool show = true);
00240     // </group>
00241     
00242     // Gets/Sets the legend position.
00243     // <group>
00244     bool isInternal() const;
00245     PlotCanvas::LegendPosition position() const;
00246     void setPosition(PlotCanvas::LegendPosition position);
00247     // </group>
00248     
00249     // Gets/Sets the outline.
00250     // <group>
00251     const QPLine& line() const { return m_legend->line(); }
00252     const QPen& pen() const { return m_legend->pen(); }
00253     void setLine(const PlotLine& line) { m_legend->setLine(line); }
00254     void setLine(const QPen& pen) { m_legend->setLine(pen); }
00255     // </group>
00256     
00257     // Gets/Sets the background.
00258     // <group>
00259     const QPAreaFill& areaFill() const { return m_legend->areaFill(); }
00260     const QBrush& brush() const { return m_legend->brush(); }
00261     //The update flag allows an export operation to set the fill without
00262     //affecting what is shown on the GUI.
00263     void setAreaFill(const PlotAreaFill& fill, bool update = true ) {
00264         if (m_legend) m_legend->setAreaFill(fill, update); }
00265     void setAreaFill(const QBrush& brush) { 
00266         if (m_legend) m_legend->setAreaFill(brush); }
00267     // </group>
00268     
00269     // Returns the rect for the internal legend, given a canvas rect.
00270     QRect internalLegendRect(const QRect& canvasRect) const;
00271     
00272     // See QPLegend::drawOutlineAndBackground.
00273     void drawOutlineAndBackground(QPainter* painter, const QRect& rect,
00274             bool useQwtPainter = false) {
00275         m_legend->drawOutlineAndBackground(painter, rect, useQwtPainter); }
00276     
00277 private:
00278     // Canvas.
00279     QPCanvas* m_canvas;
00280     
00281     // Actual legend.
00282     QPLegend* m_legend;
00283     
00284 #if QWT_VERSION >= 0x060000
00285     QwtPlotLegendItem* m_legendItem;
00286     void setupLegendItem();
00287     void setAlignment(PlotCanvas::LegendPosition pos);
00288 #endif
00289 
00290     // Current position.
00291     PlotCanvas::LegendPosition m_position;
00292     
00293     // Spacers for internal legends.
00294     QSpacerItem* m_spaceTop, *m_spaceLeft, *m_spaceRight, *m_spaceBottom;
00295     
00296     // Padding for internal legends.
00297     int m_padding;
00298     
00299     
00300     // Update the spacers for the position, padding, and line width.
00301     void updateSpacers();
00302 };
00303 
00304 
00305 // "Base" Item Classes //
00306 
00307 // Abstract superclass for all "base" item classes.  Base items are a special
00308 // type of plot item that are not PlotItems but *are* QwtPlotItems that go
00309 // below the normal items.
00310 class QPBaseItem : public QPLayerItem {
00311     friend class QPLayeredCanvas;
00312     
00313 public:
00314     // Z indexes for known base items.
00315     // <group>
00316     static const double BASE_Z_CARTAXIS;
00317     static const double BASE_Z_GRID;
00318     // </group>    
00319     
00320     
00321     // Constructor.
00322     QPBaseItem();
00323     
00324     // Destructor.
00325     virtual ~QPBaseItem();
00326     
00327     
00328     // Implements QPLayerItem::itemChanged().  Calls default definition.
00329     virtual void itemChanged() { QwtPlotItem::itemChanged(); }
00330     
00331     // Implements QPLayerItem::itemDrawCount().
00332     virtual unsigned int itemDrawCount() const { return 1; }
00333     
00334 protected:
00335     // Attached canvas, or NULL for none.
00336     QPCanvas* m_canvas;
00337     
00338     
00339     // Attaches this item to the given canvas.
00340     void qpAttach(QPCanvas* canvas);
00341     
00342     // Detaches this item from its canvas.
00343     void qpDetach();
00344 };
00345 
00346 
00347 // Subclass of QPBaseItem for drawing grids.  Basically just a wrapper for
00348 // QwtPlotGrid.
00349 class QPGrid : public QPBaseItem, public QwtPlotGrid {
00350 public:
00351     // Constructor.
00352     QPGrid();
00353     
00354     // Destructor.
00355     ~QPGrid();
00356     
00357 
00358     // Overrides QwtPlotItem::itemChanged() to use QPBaseItem's definition.
00359     void itemChanged() { QPBaseItem::itemChanged(); }
00360     
00361     // Implements QPLayerItem::shouldDraw().
00362     bool shouldDraw() const;
00363     
00364     // Implements QPLayerItem::itemTitle().
00365     String itemTitle() const { return "grid"; }
00366     
00367     // Overrides QwtPlotItem::boundingRect() to use QwtPlotGrid's definition.
00368     QwtDoubleRect boundingRect() const { return QwtPlotGrid::boundingRect(); }
00369     
00370     // Overrides QwtPlotItem::updateScaleDiv() to use QwtPlotGrid's definition.
00371     void updateScaleDiv(const QwtScaleDiv& xDiv, const QwtScaleDiv& yDiv) {
00372         QwtPlotGrid::updateScaleDiv(xDiv, yDiv); }
00373     
00374 protected:
00375     // Implements QPLayerItem::draw_().  Ignores draw index and count.
00376 #if QWT_VERSION >= 0x060000
00377     void draw_(QPainter* p, const QwtScaleMap& xMap,
00378                 const QwtScaleMap& yMap, const QRectF& drawRect,
00379                 unsigned int drawIndex, unsigned int drawCount) const {
00380 #else
00381     void draw_(QPainter* p, const QwtScaleMap& xMap,
00382                 const QwtScaleMap& yMap, const QRect& drawRect,
00383                 unsigned int drawIndex, unsigned int drawCount) const {
00384 #endif
00385                 (void)drawIndex; (void)drawCount;
00386         QwtPlotGrid::draw(p, xMap, yMap, drawRect); }
00387 };
00388 
00389 
00390 // Subclass of QPBaseItem for drawing cartesian axes.  See
00391 // http://pyqwt.sourceforge.net/examples/CartesianDemo.py.html .
00392 class QPCartesianAxis : public QPBaseItem {
00393 public:
00394     // "Master" is the one being drawn; "slave" is the one that the master
00395     // is attached to.
00396     QPCartesianAxis(QwtPlot::Axis master, QwtPlot::Axis slave);
00397     
00398     // Destructor.
00399     ~QPCartesianAxis();
00400 
00401     
00402     // Implements QPLayerItem::shouldDraw().
00403     bool shouldDraw() const { return true; }
00404     
00405     // Implements QPLayerItem::itemTitle().
00406     String itemTitle() const { return "cartesian axis"; }
00407     
00408 protected:
00409     // Implements QPLayerItem::draw_().  Ignores draw index and count.
00410 #if QWT_VERSION >= 0x060000
00411     void draw_(QPainter* p, const QwtScaleMap& xMap,
00412                 const QwtScaleMap& yMap, const QRectF& drawRect,
00413                 unsigned int drawIndex, unsigned int drawCount) const;
00414 #else
00415     void draw_(QPainter* p, const QwtScaleMap& xMap,
00416                 const QwtScaleMap& yMap, const QRect& drawRect,
00417                 unsigned int drawIndex, unsigned int drawCount) const;
00418 #endif
00419 
00420 private:
00421     // Master axis.
00422     QwtPlot::Axis m_axis;
00423     
00424     // Scale draw.
00425     QwtScaleDraw m_scaleDraw;
00426 };
00427 
00428 }
00429 
00430 #endif
00431 
00432 #endif /* QPCANVASHELPERS_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