casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QPCanvasHelpers.qo.h
Go to the documentation of this file.
1 //# QPCanvasHelpers.qo.h: Helper classes for QPCanvas.
2 //# Copyright (C) 2008
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id: $
27 #ifndef QPCANVASHELPERS_QO_H_
28 #define QPCANVASHELPERS_QO_H_
29 
30 #ifdef AIPS_HAS_QWT
31 
35 
37 
38 #include <qwt_legend.h>
39 #if QWT_VERSION >= 0x060000
40 #include <qwt_plot_legenditem.h>
41 #endif
42 #include <qwt_plot.h>
43 #include <qwt_plot_grid.h>
44 #include <qwt_scale_draw.h>
45 
46 #include <QObject>
47 #include <QMouseEvent>
48 #include <QSpacerItem>
49 
50 namespace casa {
51 
52 //# Forward Declarations
53 class QPCanvas;
54 
55 
56 // Miscellaneous Classes //
57 
58 // Filter to install on the QwtPlotCanvas to catch mouse move events (which
59 // are otherwise stolen by other filters in QwtPicker classes).
60 class QPMouseFilter : public QObject {
61  Q_OBJECT
62 
63 public:
64  // Constructor which takes the canvas to install itself on.
65  QPMouseFilter(QwtPlotCanvas* canvas);
66  QPMouseFilter(QWidget* widget);
67 
68  // Destructor.
69  ~QPMouseFilter();
70 
71  // Turns mouse tracking on or off on the underlying canvas.
72  void turnTracking(bool on);
73 
74 signals:
75  // This signal is emitted if tracking is turned on, and when a
76  // QMouseEvent is received for a mouse move on the underlying canvas.
77  void mouseMoveEvent(QMouseEvent* e);
78 
79 protected:
80  // Filter the events on the given object. Checks only for mouse events
81  // on the underlying canvas.
82  bool eventFilter(QObject* obj, QEvent* ev);
83 
84 private:
85  // Canvas.
86  QwtPlotCanvas* m_canvas;
87 };
88 
89 
90 // Subclass of QwtScaleDraw to implement additional functionality. Keeps track
91 // of which PlotAxisScale is set, and can use reference values instead of
92 // absolute values (see PlotCanvas::setAxisReferenceValue()). Additionally,
93 // for date values it can convert a double in either modified julian seconds or
94 // modified julian days into a casacore::String representation of the date, using a
95 // format that can be set.
96 class QPScaleDraw : public QwtScaleDraw {
97 public:
98  // Constructor that takes the parent and axis to attach to.
99  QPScaleDraw(QwtPlot* parent, QwtPlot::Axis axis);
100 
101  // Destructor.
102  ~QPScaleDraw();
103 
104  // Gets/Sets the scale used.
105  // <group>
106  PlotAxisScale scale() const;
107  void setScale(PlotAxisScale scale, casacore::uInt base=10);
108  // </group>
109 
110  // Gets/Sets the format used for date values. See Plotter::dateFormat().
111  // <group>
112  const casacore::String& dateFormat() const;
113  void setDateFormat(const casacore::String& newFormat);
114  // </group>
115 
116  // Gets/Sets the format used for relative date values. See
117  // Plotter::relativeDateFormat().
118  // <group>
119  const casacore::String& relativeDateFormat() const;
120  void setRelativeDateFormat(const casacore::String& newFormat);
121  // </group>
122 
123  // Gets/Sets the reference value. See PlotCanvas::setAxisReferenceValue().
124  // <group>
125  bool referenceValueSet() const;
126  double referenceValue() const;
127  void setReferenceValue(bool on, double value = 0);
128  // </group>
129 
130  // Gets/Sets the format used for angle values
131  AngleFormat angleFormat() const;
132  void setAngleFormat(AngleFormat newFormat);
133 
134  // Overrides QwtScaleDraw::label().
135  QwtText label(double value) const;
136 
137  virtual void draw(QPainter* painter, const QPalette& palette) const;
138 
139 #if QWT_VERSION < 0x060000
140  virtual int extent( const QPen& pen, const QFont& font ) const;
141 #endif
142 
143 private:
144  // finds step between tick values for precision
145  int getTickPrecision() const;
146 
147  // Parent.
148  QwtPlot* m_parent;
149 
150  // Axis.
151  QwtPlot::Axis m_axis;
152 
153  // Scale.
154  PlotAxisScale m_scale;
155 
156  // Date formats.
157  // <group>
158  casacore::String m_dateFormat;
159  casacore::String m_relativeDateFormat;
160  // </group>
161 
162  // Reference value.
163  // <group>
164  bool m_referenceSet;
165  double m_referenceValue;
166  // </group>
167 
168  // Angle Format
169  AngleFormat m_angleFormat;
170  casacore::MVAngle::Format m_mvAngleFormat;
171  static const casacore::uInt m_angleFormatDefaultPrecision = 6;
172 };
173 
174 // Legend Classes //
175 
176 // Subclass of QwtLegend to handle outline and background, and be able to draw
177 // itself using a QPainter.
178 class QPLegend : public QwtLegend {
179 public:
180  // Constructor which takes optional parent widget.
181  QPLegend(QWidget* parent = NULL);
182 
183  // Destructor.
184  ~QPLegend();
185 
186 
187  // Overrides QwtLegend::sizeHint() to take border into account.
188  QSize sizeHint() const;
189 
190  // Gets/Sets the outline.
191  // <group>
192  const QPLine& line() const;
193  const QPen& pen() const;
194  void setLine(const PlotLine& line);
195  void setLine(const QPen& pen) { setLine(QPLine(pen)); }
196  // </group>
197 
198  // Gets/Sets the background.
199  // <group>
200  const QPAreaFill& areaFill() const;
201  const QBrush& brush() const;
202  //The update flag allows an export operation to set the fill without affecting
203  //what is shown on the GUI.
204  void setAreaFill(const PlotAreaFill& fill, bool update=true);
205  void setAreaFill(const QBrush& brush) { setAreaFill(QPAreaFill(brush)); }
206  // </group>
207 
208  // Draws the legend's outline and bacgkround using the given painter on the
209  // given rect. If useQwtPainter is true, QwtPainter is used (which
210  // preserves any set QwtMetricsMap).
211  void drawOutlineAndBackground(QPainter* painter, const QRect& rect,
212  bool useQwtPainter = false);
213 
214 protected:
215  // Overrides QWidget::paintEvent() to draw outline and background if
216  // needed.
217  void paintEvent(QPaintEvent* event);
218 
219 private:
220  // Outline.
221  QPLine m_line;
222 
223  // Background.
224  QPAreaFill m_areaFill;
225 };
226 
227 
228 // Holder for QPLegend that is responsible for its placement.
229 class QPLegendHolder : public QWidget {
230  Q_OBJECT
231 
232 public:
233  // Default padding for internal legends.
234  static const int DEFAULT_INTERNAL_PADDING;
235 
236  // Converts from our legend position to Qwt's.
237  static QwtPlot::LegendPosition legendPosition(
239 
240 
241  // Constructor which takes the canvas and initial position.
242  QPLegendHolder(QPCanvas* canvas, PlotCanvas::LegendPosition position,
243  int padding = DEFAULT_INTERNAL_PADDING);
244 
245  // Destructor.
246  ~QPLegendHolder();
247 
248  QPLegend* legend() { return m_legend; }
249  // Shows/hides the legend.
250  // <group>
251  bool legendShown() const;
252  void showLegend(bool show = true);
253  // </group>
254 
255  // Gets/Sets the legend position.
256  // <group>
257  bool isInternal() const;
258  PlotCanvas::LegendPosition position() const;
259  void setPosition(PlotCanvas::LegendPosition position);
260  // </group>
261 
262  // Gets/Sets the outline.
263  // <group>
264  const QPLine& line() const { return m_legend->line(); }
265  const QPen& pen() const { return m_legend->pen(); }
266  void setLine(const PlotLine& line) { m_legend->setLine(line); }
267  void setLine(const QPen& pen) { m_legend->setLine(pen); }
268  // </group>
269 
270  // Gets/Sets the background.
271  // <group>
272  const QPAreaFill& areaFill() const { return m_legend->areaFill(); }
273  const QBrush& brush() const { return m_legend->brush(); }
274  //The update flag allows an export operation to set the fill without
275  //affecting what is shown on the GUI.
276  void setAreaFill(const PlotAreaFill& fill, bool update = true ) {
277  if (m_legend) m_legend->setAreaFill(fill, update); }
278  void setAreaFill(const QBrush& brush) {
279  if (m_legend) m_legend->setAreaFill(brush); }
280  // </group>
281 
282  // Returns the rect for the internal legend, given a canvas rect.
283  QRect internalLegendRect(const QRect& canvasRect) const;
284 
285  // See QPLegend::drawOutlineAndBackground.
286  void drawOutlineAndBackground(QPainter* painter, const QRect& rect,
287  bool useQwtPainter = false) {
288  m_legend->drawOutlineAndBackground(painter, rect, useQwtPainter); }
289 
290 private:
291  // Canvas.
292  QPCanvas* m_canvas;
293 
294  // Actual legend.
295  QPLegend* m_legend;
296 
297 #if QWT_VERSION >= 0x060000
298  QwtPlotLegendItem* m_legendItem;
299  void setupLegendItem();
300  void setAlignment(PlotCanvas::LegendPosition pos);
301 #endif
302 
303  // Current position.
304  PlotCanvas::LegendPosition m_position;
305 
306  // Spacers for internal legends.
307  QSpacerItem* m_spaceTop, *m_spaceLeft, *m_spaceRight, *m_spaceBottom;
308 
309  // Padding for internal legends.
310  int m_padding;
311 
312 
313  // Update the spacers for the position, padding, and line width.
314  void updateSpacers();
315 };
316 
317 
318 // "Base" Item Classes //
319 
320 // Abstract superclass for all "base" item classes. Base items are a special
321 // type of plot item that are not PlotItems but *are* QwtPlotItems that go
322 // below the normal items.
323 class QPBaseItem : public QPLayerItem {
324  friend class QPLayeredCanvas;
325 
326 public:
327  // Z indexes for known base items.
328  // <group>
329  static const double BASE_Z_CARTAXIS;
330  static const double BASE_Z_GRID;
331  // </group>
332 
333 
334  // Constructor.
335  QPBaseItem();
336 
337  // Destructor.
338  virtual ~QPBaseItem();
339 
340 
341  // Implements QPLayerItem::itemChanged(). Calls default definition.
342  virtual void itemChanged() { QwtPlotItem::itemChanged(); }
343 
344  // Implements QPLayerItem::itemDrawCount().
345  virtual unsigned int itemDrawCount() const { return 1; }
346 
347 protected:
348  // Attached canvas, or NULL for none.
349  QPCanvas* m_canvas;
350 
351 
352  // Attaches this item to the given canvas.
353  void qpAttach(QPCanvas* canvas);
354 
355  // Detaches this item from its canvas.
356  void qpDetach();
357 };
358 
359 
360 // Subclass of QPBaseItem for drawing grids. Basically just a wrapper for
361 // QwtPlotGrid.
362 class QPGrid : public QPBaseItem, public QwtPlotGrid {
363 public:
364  // Constructor.
365  QPGrid();
366 
367  // Destructor.
368  ~QPGrid();
369 
370 
371  // Overrides QwtPlotItem::itemChanged() to use QPBaseItem's definition.
372  void itemChanged() { QPBaseItem::itemChanged(); }
373 
374  // Implements QPLayerItem::shouldDraw().
375  bool shouldDraw() const;
376 
377  // Implements QPLayerItem::itemTitle().
378  casacore::String itemTitle() const { return "grid"; }
379 
380  // Overrides QwtPlotItem::boundingRect() to use QwtPlotGrid's definition.
381  QwtDoubleRect boundingRect() const { return QwtPlotGrid::boundingRect(); }
382 
383  // Overrides QwtPlotItem::updateScaleDiv() to use QwtPlotGrid's definition.
384  void updateScaleDiv(const QwtScaleDiv& xDiv, const QwtScaleDiv& yDiv) {
385  QwtPlotGrid::updateScaleDiv(xDiv, yDiv); }
386 
387 protected:
388  // Implements QPLayerItem::draw_(). Ignores draw index and count.
389 #if QWT_VERSION >= 0x060000
390  void draw_(QPainter* p, const QwtScaleMap& xMap,
391  const QwtScaleMap& yMap, const QRectF& drawRect,
392  unsigned int drawIndex, unsigned int drawCount) const
393 #else
394  void draw_(QPainter* p, const QwtScaleMap& xMap,
395  const QwtScaleMap& yMap, const QRect& drawRect,
396  unsigned int drawIndex, unsigned int drawCount) const
397 #endif
398  {
399  (void)drawIndex; (void)drawCount;
400  QwtPlotGrid::draw(p, xMap, yMap, drawRect); }
401 };
402 
403 
404 // Subclass of QPBaseItem for drawing cartesian axes. See
405 // http://pyqwt.sourceforge.net/examples/CartesianDemo.py.html .
406 class QPCartesianAxis : public QPBaseItem {
407 public:
408  // "Master" is the one being drawn; "slave" is the one that the master
409  // is attached to.
410  QPCartesianAxis(QwtPlot::Axis master, QwtPlot::Axis slave);
411 
412  // Destructor.
413  ~QPCartesianAxis();
414 
415 
416  // Implements QPLayerItem::shouldDraw().
417  bool shouldDraw() const { return true; }
418 
419  // Implements QPLayerItem::itemTitle().
420  casacore::String itemTitle() const { return "cartesian axis"; }
421 
422 protected:
423  // Implements QPLayerItem::draw_(). Ignores draw index and count.
424 #if QWT_VERSION >= 0x060000
425  void draw_(QPainter* p, const QwtScaleMap& xMap,
426  const QwtScaleMap& yMap, const QRectF& drawRect,
427  unsigned int drawIndex, unsigned int drawCount) const;
428 #else
429  void draw_(QPainter* p, const QwtScaleMap& xMap,
430  const QwtScaleMap& yMap, const QRect& drawRect,
431  unsigned int drawIndex, unsigned int drawCount) const;
432 #endif
433 
434 private:
435  // Master axis.
436  QwtPlot::Axis m_axis;
437 
438  // Scale draw.
439  QwtScaleDraw m_scaleDraw;
440 };
441 
442 } // end namespace casa
443 
444 #endif
445 
446 #endif /* QPCANVASHELPERS_QO_H_ */
LegendPosition
Static //.
Definition: PlotCanvas.h:88
void show(const variant &v)
AngleFormat
Enum for angle formats.
Definition: PlotOptions.h:87
const Double e
e and functions thereof:
Format structure.
Definition: MVAngle.h:278
String: the storage and methods of handling collections of characters.
Definition: String.h:223
PlotAxisScale
Enum for possible axis scales.
Definition: PlotOptions.h:78
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
unsigned int uInt
Definition: aipstype.h:51