casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QPLayeredCanvas.qo.h
Go to the documentation of this file.
1 //# QPLayeredCanvas.qo.h: Subclass of QwtPlot to add layers and other features.
2 //# Copyright (C) 2009
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 QPLAYEREDCANVAS_QO_H_
28 #define QPLAYEREDCANVAS_QO_H_
29 
30 #ifdef AIPS_HAS_QWT
31 
34 
35 #include <qwt_plot.h>
36 
37 #include <QPicture>
38 
39 namespace casa {
40 
41 //# Forward declarations
42 class QPCanvas;
43 
44 
45 // Abstract class for a single cached layer.
46 class QPLayer {
47 public:
48  // Constructor.
49  QPLayer();
50 
51  // Destructor.
52  virtual ~QPLayer();
53 
54 
55  // Clears the cache image.
56  virtual void clearImage() const;
57 
58  // Initializes the cache image to the given size using the given color for
59  // a fill base (see QPImageCache::fill()).
60  virtual void initializeImage(const QSize& size,
61  unsigned int fillValue = Qt::transparent) const;
62 
63  // Returns whether this layer has at least one item for which
64  // QPLayerItem::shouldDraw() returns true.
65  virtual bool hasItemsToDraw() const;
66 
67  // Returns the draw count for items on this layer (see
68  // QPLayerItem::drawCount()).
69  virtual unsigned int itemDrawCount() const;
70 
71  // Returns the number of draw segments for items on this layer, using the
72  // given segment threshold.
73  virtual unsigned int itemDrawSegments(unsigned int threshold =
74  QPDrawThread::DEFAULT_SEGMENT_THRESHOLD) const;
75 
76  // Draws the attached items using the given painter, canvas rect, and scale
77  // maps. If PlotOperation parameters are given, they are updated as
78  // needed.
79  virtual void drawItems(QPainter* painter, const QRect& canvasRect,
80  const QwtScaleMap maps[QwtPlot::axisCnt],
82  unsigned int currentSegment = 0,
83  unsigned int totalSegments = 0,
84  unsigned int segmentThreshold =
85  QPDrawThread::DEFAULT_SEGMENT_THRESHOLD) const;
86 
87  // Draws the attached layer items into the image cache, using the given
88  // canvas rect and scale maps. If PlotOperation parametesr are given, they
89  // are updated as needed.
90  virtual void cacheItems(const QRect& canvasRect,
91  const QwtScaleMap maps[QwtPlot::axisCnt],
93  unsigned int currentSegment = 0,
94  unsigned int totalSegments = 0,
95  unsigned int segmentThreshold =
96  QPDrawThread::DEFAULT_SEGMENT_THRESHOLD) const;
97 
98  // Draws the cached image using the given painter and draw rect.
99  virtual void drawCache(QPainter* painter, const QRect& rect) const;
100 
101  // Returns the cached image.
102  // <group>
103  QPImageCache& cachedImage();
104  const QPImageCache& cachedImage() const;
105  // </group>
106 
107 
108  // ABSTRACT METHODS //
109 
110  // Returns all attached layer items.
111  // <group>
112  virtual QList<QPLayerItem*> items() = 0;
113  virtual QList<const QPLayerItem*> items() const = 0;
114  // </group>
115 
116 protected:
117  // Cached image.
118  QPImageCache m_cachedImage;
119 };
120 
121 
122 // Subclass of QPLayer for QPPlotItems.
123 class QPCanvasLayer : public QPLayer {
124 public:
125  // Constructor.
126  QPCanvasLayer();
127 
128  // Destructor.
129  ~QPCanvasLayer();
130 
131 
132  // Adds/Removes the given QPPlotItem.
133  // <group>
134  void addItem(QPPlotItem* item);
135  void removeItem(QPPlotItem* item);
136  // </group>
137 
138  // Returns true if the given item is in this layer, false otherwise.
139  bool containsItem(QPPlotItem* item);
140 
141  // Returns all attached canvas items.
142  // <group>
143  QList<QPPlotItem*> canvasItems() { return m_items; }
144  QList<const QPPlotItem*> canvasItems() const;
145  // </group>
146 
147 
148  // Implements QPLayer::items().
149  // <group>
150  QList<QPLayerItem*> items();
151  QList<const QPLayerItem*> items() const;
152  // </group>
153 
154 private:
155  // Items.
156  QList<QPPlotItem*> m_items;
157 };
158 
159 
160 // Subclass of QPLayer for QPBaseItems.
161 class QPBaseLayer : public QPLayer {
162 public:
163  // Constructor.
164  QPBaseLayer();
165 
166  // Destructor.
167  ~QPBaseLayer();
168 
169 
170  // Adds/Removes the given QPBaseItem.
171  // <group>
172  void addItem(QPBaseItem* item);
173  void removeItem(QPBaseItem* item);
174  // </group>
175 
176  // Returns true if the given item is in this layer, false otherwise.
177  bool containsItem(QPBaseItem* item);
178 
179  // Returns all attached canvas items.
180  // <group>
181  QList<QPBaseItem*> canvasItems() { return m_items; }
182  QList<const QPBaseItem*> canvasItems() const;
183  // </group>
184 
185 
186  // Implements QPLayer::items().
187  // <group>
188  QList<QPLayerItem*> items();
189  QList<const QPLayerItem*> items() const;
190  // </group>
191 
192 private:
193  // Items.
194  QList<QPBaseItem*> m_items;
195 };
196 
197 
198 // Subclass of QwtPlot to manage layers as specified by PlotCanvas and
199 // PlotCanvasLayer. Also has an additional "base" layer for things like the
200 // grid and Cartesian axes. Works with QPCanvas and QPPlotItem.
201 class QPLayeredCanvas : public QwtPlot {
202  Q_OBJECT
203 
204  friend class QPAxesCache;
205  friend class QPCanvas;
206  friend class QPAxis;
207  friend class QPPlotItem;
208  friend class QPBaseItem;
209  friend class QPLegendHolder;
210 
211 public:
212  // Convenient access to class name.
213  static const casacore::String CLASS_NAME;
214 
215 
216  // Constructor which takes parent canvas (optional) parent widget.
217  QPLayeredCanvas(QPCanvas* parent, QWidget* parentWidget = NULL);
218 
219  // Constructor which takes the canvas title, the parent canvas, and an
220  // (optional) parent widget.
221  QPLayeredCanvas(const QwtText& title, QPCanvas* parent,
222  QWidget* parentWidget = NULL);
223 
224  // Destructor.
225  ~QPLayeredCanvas();
226 
227 
228  // Include overloaded methods.
229  using QwtPlot::replot;
230 
231 
232  // Item Methods //
233 
234  // Returns a list of all attached QPPlotItems on this canvas.
235  QList<const QPPlotItem*> allAttachedItems() const {
236  return allLayerItems(PlotCanvas::allLayersFlag()); }
237 
238  // Returns a list of the QPPlotItems attached to this canvas in the
239  // layers as indicated (an or'ed value of PlotCanvasLayer enum values).
240  QList<const QPPlotItem*> allLayerItems(int layersFlag) const;
241 
242  // Returns the rect for drawing items.
243  QRect canvasDrawRect() const;
244 
245 
246  // Draw Methods //
247 
248  // Overrides QwtPlot::print() to properly handle pixmap caches.
249 #if QWT_VERSION >= 0x060000
250  void print(QPainter* painter, const QRect& rect);
251  void print(QPaintDevice& paintDev);
252 #else
253  using QwtPlot::print;
254  void print(QPainter* painter, const QRect& rect,
255  const QwtPlotPrintFilter& filter = QwtPlotPrintFilter()) const;
256 #endif
257 
258  // Is drawing in progress?
259  virtual bool isDrawing();
260 
261  // Event Methods //
262 
263  // Overrides QObject::eventFilter(). Used to pass events to the canvas
264  // when it is being covered by an interior legend.
265  bool eventFilter(QObject* watched, QEvent* event);
266 
267 protected:
268  // Item Methods //
269 
270  // Attaches the given item to the given layer.
271  void attachLayeredItem(QPPlotItem* item);
272 
273  // Detaches the given item.
274  void detachLayeredItem(QPPlotItem* item);
275 
276 
277 
278 
279 
280  // Draw Methods //
281 
282  // Overrides QwtPlot::drawItems().
283  // No QwtPlotPrintFilter in qwt 6
284  void drawItems(QPainter* painter, const QRect& rect,
285  const QwtScaleMap maps[axisCnt]);
286  void printItems(QPainter* painter, const QRect& rect,
287  const QwtScaleMap maps[axisCnt]);
288 
289 #if QWT_VERSION < 0x060000
290  // QwtPlotPrintFilter not used, keep signature but call other method
291  void drawItems(QPainter* painter, const QRect& rect,
292  const QwtScaleMap maps[axisCnt],
293  const QwtPlotPrintFilter& );
294 
295  // Like drawItems, but doesn't do threaded/cached drawing.
296  // QwtPlotPrintFilter not used, keep signature but call other method
297  void printItems(QPainter* painter, const QRect& rect,
298  const QwtScaleMap maps[axisCnt],
299  const QwtPlotPrintFilter& /*filter*/) {
300  printItems(painter, rect, maps);
301  }
302 #endif
303 
304  // Overrides QwtPlot::printLegend().
305  //void printLegend(QPainter* painter, const QRect& rect) const;
306 
307  // Provides access to QwtPlot::printLegend(), and also lets the legend
308  // draw its outline and background if needed.
309  //void printLegend_(QPainter* painter, const QRect& rect) const;
310 
311  // Hold/Release drawing.
312  // <group>
313  bool drawingIsHeld() const;
314  void holdDrawing();
315  void releaseDrawing();
316  // </group>
317 
318 
319  // Layer Methods //
320 
321  // Sets whether items in the layers have changed or not. Each layer has
322  // a changed flag that is or'ed with the given value. These flags are
323  // reset to false after the next draw.
324  // <group>
325  void setLayerChanged(PlotCanvasLayer layer);
326  void setLayersChanged(int layersFlag);
327  void setAllLayersChanged() {
328  setLayersChanged(PlotCanvas::allLayersFlag()); }
329  // </group>
330 
331  // Returns whether the given layer has changed or not since the last draw.
332  bool changedLayer(PlotCanvasLayer layer) const;
333 
334  // Returns whether any layer has changed since the last draw or not.
335  bool anyChangedLayer() const;
336 
337  // Returns the or'ed value of PlotCanvasLayers which have changed since the
338  // last draw.
339  int changedLayersFlag() const;
340 
341 
342  // Base Items Methods //
343 
344  // Provides access to the grid.
345  // <group>
346  const QPGrid& grid() const;
347  QPGrid& grid();
348  // </group>
349 
350  // Provides access to the cartesian axes.
351  // <group>
352  const QHash<PlotAxis, QPCartesianAxis*>& cartesianAxes() const;
353  QHash<PlotAxis, QPCartesianAxis*>& cartesianAxes();
354  // </group>
355 
356  // See PlotCanvas::cartesianAxisShown().
357  bool cartesianAxisShown(PlotAxis axis) const;
358 
359  // See PlotCanvas::showCartesianAxis().
360  void showCartesianAxis(PlotAxis mirrorAxis, PlotAxis secondaryAxis,
361  bool show);
362 
363 
364  // Event Methods //
365 
366  // Filters input events on the given frame to pass to the canvas.
367  void installLegendFilter(QWidget* legendFrame);
368 
369 private:
370  // Parent QPCanvas.
371  QPCanvas* m_parent;
372 
373 
374  // Base layer.
375  QPBaseLayer m_layerBase;
376 
377  // Canvas layers.
378  QMap<PlotCanvasLayer, QPCanvasLayer*> m_layers;
379 
380  // Layer changed flags.
381  QHash<PlotCanvasLayer, bool> m_changedLayers;
382 
383 
384  // Flag for whether drawing is currently held or not.
385  bool m_drawingHeld;
386 
387  // Flag for whether we're printing rather than drawing onto a widget.
388  bool m_isPrinting;
389 
390  // Printing painters, so we can call printItem insteads of drawItems.
391  QList<QPainter*> m_printPainters;
392 
393  // Current draw thread, or NULL for none.
394  mutable QPDrawThread* m_drawThread;
395 
396  // Flag for whether a redraw currently in progress should be restarted.
397  bool m_redrawWaiting;
398 
399 
400  // Canvas grid.
401  QPGrid* m_grid;
402 
403  // Cartesian axes.
404  QHash<PlotAxis, QPCartesianAxis*> m_cartAxes;
405 
406 
407  // Legend frame to watch for events.
408  QWidget* m_legendFrame;
409 
410 
411  // Initializes object (meant to be called from constructor).
412  void initialize();
413 
414 private slots:
415  // Slot for when the current draw thread is finished.
416  void itemDrawingFinished();
417 };
418 
419 }
420 
421 #endif
422 
423 #endif /* QPLAYEREDCANVAS_QO_H_ */
static int allLayersFlag()
Returns the or&#39;ed value of all PlotCanvasLayer enum values.
size_t size() const
void show(const variant &v)
PlotAxis
Enum for the four plot axes.
Definition: PlotOptions.h:62
std::set< ScanKey > filter(const std::set< ScanKey > scans, const ArrayKey &arrayKey)
given a set of scan keys, return the subset that matches the given array key
casacore::CountedPtr< PlotOperation > PlotOperationPtr
String: the storage and methods of handling collections of characters.
Definition: String.h:223
PlotCanvasLayer
The canvas is composed of multiple layers, where changing/adding items from one layer will not affect...
Definition: PlotOptions.h:109