casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QPPlotItem.qo.h
Go to the documentation of this file.
1 //# QPPlotItem.qo.h: Superclass for all plot items in qwt plotter.
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 QPPLOTITEM_H_
28 #define QPPLOTITEM_H_
29 
30 #ifdef AIPS_HAS_QWT
31 
36 
37 #include <QHash>
38 #include <QPainter>
39 #include <QThread>
40 
41 #include <qwt_plot.h>
42 #include <casaqt/QwtConfig.h>
43 
44 namespace casa {
45 
46 //# Forward Declarations
47 class QPCanvas;
48 
49 
50 // Abstract superclass for any layered item that will be drawn on a
51 // QPLayeredCanvas.
52 class QPLayerItem : public QwtPlotItem {
53  friend class QPCanvas;
54  friend class QPLayeredCanvas;
55  friend class QPDrawThread;
56 
57 public:
58  // Constructor.
59  QPLayerItem();
60 
61  // Destructor.
62  virtual ~QPLayerItem();
63 
64 
65  // Implements QwtPlotItem::draw().
66 #if QWT_VERSION >= 0x060000
67  virtual void draw(QPainter* p, const QwtScaleMap& xMap,
68  const QwtScaleMap& yMap, const QRectF& canvasRect) const;
69 #else
70  virtual void draw(QPainter* p, const QwtScaleMap& xMap,
71  const QwtScaleMap& yMap, const QRect& canvasRect) const;
72 #endif
73 
74  // See PlotItem::drawSegments().
75  virtual unsigned int itemDrawSegments(unsigned int segmentThreshold) const;
76 
77 
78  // ABSTRACT METHODS //
79 
80  // Forces subclasses to override QwtPlotItem::itemChanged() to redraw only
81  // the necessary layer.
82  virtual void itemChanged() = 0;
83 
84  // Returns true if this item should be drawn, false otherwise. This is
85  // used to avoid drawing attached items that are empty or otherwise have
86  // nothing to draw.
87  virtual bool shouldDraw() const = 0;
88 
89  // See PlotItem::drawCount().
90  virtual unsigned int itemDrawCount() const = 0;
91 
92  // See PlotItem::title().
93  virtual casacore::String itemTitle() const = 0;
94 
95 protected:
96  // Like QwtPlotItem::draw() except that the child item should only draw
97  // drawCount items starting at drawIndex. The indexing may not be
98  // applicable to all layer items (i.e., some items may draw everything in
99  // this call rather than segmenting).
100 #if QWT_VERSION >= 0x060000
101  virtual void draw_(QPainter* p, const QwtScaleMap& xMap,
102  const QwtScaleMap& yMap, const QRectF& drawRect,
103  unsigned int drawIndex, unsigned int drawCount) const = 0;
104 #else
105  virtual void draw_(QPainter* p, const QwtScaleMap& xMap,
106  const QwtScaleMap& yMap, const QRect& drawRect,
107  unsigned int drawIndex, unsigned int drawCount) const = 0;
108 #endif
109 };
110 
111 
112 // Subclass of PlotItem to take care of common functionality that is provided
113 // by QwtPlotItem.
114 class QPPlotItem : public virtual PlotItem, public QPLayerItem {
115  friend class QPCanvas;
116  friend class QPDrawThread;
117 
118 public:
119  // Static //
120 
121  // Convenient access to "origin" name for draw method for logging.
122  static const casacore::String DRAW_NAME;
123 
124  using QPLayerItem::draw;
125  // Returns true if the given pointer is a Qwt plotter implementation,
126  // false otherwise.
127  static bool isQPPlotItem(const PlotItemPtr item);
128 
129  // If the given item is not a Qwt plotter implementation, returns a copy
130  // of the proper class. Otherwise, returns the item. If wasCloned is
131  // given, it will be set to true if the returned item is new, false
132  // otherwise.
133  static QPPlotItem* cloneItem(const PlotItemPtr item, bool* wasCloned=NULL);
134 
135  // Returns true if the two items are the same type (class), false
136  // otherwise.
137  static bool sameType(QPPlotItem* item1, QPPlotItem* item2);
138 
139  // Returns true if the given item is a Plot type (QPBarPlot, QPHistogram,
140  // QPRasterPlot, or QPScatterPlot) or not.
141  static bool isPlot(QPPlotItem* item);
142 
143 
144  // Non-Static //
145 
146  // Constructor.
147  QPPlotItem();
148 
149  // Destructor.
150  virtual ~QPPlotItem();
151 
152 
153  // PlotItem methods //
154 
155  // Implements PlotItem::canvas().
156  PlotCanvas* canvas() const;
157 
158  // Implements PlotItem::title().
159  casacore::String title() const;
160 
161  // Implements PlotItem::setTitle().
162  void setTitle(const casacore::String& newTitle);
163 
164  // Implements PlotItem::isQWidget().
165  virtual bool isQWidget() const { return false; }
166 
167  // Implements PlotItem::xAxis().
168  PlotAxis xAxis() const;
169 
170  // Implements PlotItem::yAxis().
171  PlotAxis yAxis() const;
172 
173  // Implements PlotItem::setXAxis().
174  void setXAxis(PlotAxis x);
175 
176  // Implements PlotItem::setYAxis().
177  void setYAxis(PlotAxis y);
178 
179 
180  // QPLayerItem methods //
181 
182  // Implements QPLayerItem::itemChanged() to only redraw the canvas layer
183  // this item is in.
184  virtual void itemChanged();
185 
186  // Implements QPLayerItem::shouldDraw().
187  virtual bool shouldDraw() const { return isValid(); }
188 
189  // Implements QPLayerItem::itemDrawCount().
190  unsigned int itemDrawCount() const{ return shouldDraw()? drawCount() : 0; }
191 
192  // Implements QPLayerItem::itemTitle().
193  casacore::String itemTitle() const { return title(); }
194 
195 
196  // QPPlotItem methods //
197 
198  // Returns the layer this item is attached to.
199  PlotCanvasLayer canvasLayer() const { return m_layer; }
200 
201  // Provides access to QwtPlotItem methods that have been overloaded.
202  // <group>
203  const QwtText& qwtTitle() const { return QwtPlotItem::title(); }
204  void setQwtTitle(const QwtText& text) { QwtPlotItem::setTitle(text); }
205  QwtPlot::Axis qwtXAxis() const{return QwtPlot::Axis(QwtPlotItem::xAxis());}
206  QwtPlot::Axis qwtYAxis() const{return QwtPlot::Axis(QwtPlotItem::yAxis());}
207  void qwtAttach(QwtPlot* plot) { QwtPlotItem::attach(plot); }
208  void qwtDetach() { QwtPlotItem::detach(); }
209  // </group>
210 
211 
212  // ABSTRACT METHODS //
213 
214  // Forces children to override QwtPlotItem::boundingRect().
215  virtual QwtDoubleRect boundingRect() const = 0;
216 
217 #if QWT_VERSION < 0x060000
218  // Legends are totally different in Qwt 6
219  // Forces children to override QwtPlotItem::legendItem().
220  virtual QWidget* legendItem() const = 0;
221 #endif
222 
223 protected:
224  // Attached canvas (or NULL for none).
225  QPCanvas* m_canvas;
226 
227  // Flag for whether this object's creation has been logged or not. This
228  // happens the first time the item is attached to a canvas.
229  bool m_loggedCreation;
230 
231  // Which layer this item is in.
232  PlotCanvasLayer m_layer;
233 
234 
235  // Provides access to QwtPlotItem's attach and detach methods for QPCanvas.
236  // <group>
237  void attach(QPCanvas* canvas, PlotCanvasLayer layer);
238  void detach();
239  // </group>
240 
241  // Provides access to the plotter's logger for children.
242  PlotLoggerPtr logger() const;
243 
244  // Provides access to logging destruction, for children. Should be called
245  // in children's destructor.
246  void logDestruction();
247 
248  // Provides access to log method enter/exit, for children.
249  void logMethod(const casacore::String& methodName, bool entering,
250  const casacore::String& message = casacore::String()) const;
251 
252  // Provides access to QPCanvas's draw operation for children.
253  PlotOperationPtr drawOperation() const;
254 
255 
256  // ABSTRACT METHODS //
257 
258  // Returns the class name for the child, for logging purposes.
259  virtual const casacore::String& className() const = 0;
260 };
261 
262 
263 // Thread for drawing multiple QPPlotItems into QPImageCaches based on its
264 // canvas layer. Once the thread is finished, the QPImageCaches can be copied
265 // to the canvas caches and shown on the GUI widget as needed. The thread will
266 // emit a signal after each "segment" is drawn, either the whole item for small
267 // items or part of a large item.
268 class QPDrawThread : public QThread {
269  Q_OBJECT
270 
271 public:
272  // Static //
273 
274  // Convenient access to class name.
275  static const casacore::String CLASS_NAME;
276 
277  // Returns the default segment threshold.
278  static const unsigned int DEFAULT_SEGMENT_THRESHOLD;
279 
280  // Returns item1->z() < item2->z().
281  // <group>
282  static bool itemSortByZ(const QPLayerItem* item1,const QPLayerItem* item2);
283  // </group>
284 
285  // Draws the given items, sorted by z-order, using the given painter, rect,
286  // and maps. If PlotOperation parameters are given, they are updated as
287  // needed.
288  // <group>
289  static void drawItem(const QPLayerItem* item, QPainter* painter,
290  const QRect& rect, const QwtScaleMap maps[QwtPlot::axisCnt]);
291  static void drawItem(const QPLayerItem* item, QPainter* painter,
292  const QRect& rect, const QwtScaleMap maps[QwtPlot::axisCnt],
293  unsigned int drawIndex, unsigned int drawCount);
294  static void drawItems(const QList<const QPLayerItem*>& items,
295  QPainter* painter, const QRect& rect,
296  const QwtScaleMap maps[QwtPlot::axisCnt],
298  unsigned int currentSegment = 0, unsigned int totalSegments = 0,
299  unsigned int segmentThreshold =
300  QPDrawThread::DEFAULT_SEGMENT_THRESHOLD);
301  static void drawItems(const QList<const QPPlotItem*>& items,
302  QPainter* painter, const QRect& rect,
303  const QwtScaleMap maps[QwtPlot::axisCnt],
305  unsigned int currentSegment = 0, unsigned int totalSegments = 0,
306  unsigned int segmentThreshold =
307  QPDrawThread::DEFAULT_SEGMENT_THRESHOLD);
308  // </group>
309 
310 
311  // Non-Static //
312 
313  // Constructor which takes a list of items to draw, axes maps, the drawing
314  // rectangle, an optional fixed image size, and an optional segment
315  // threshold.
316  QPDrawThread(const QList<const QPPlotItem*>& items,
317  const QwtScaleMap maps[QwtPlot::axisCnt], QSize imageSize,
318  unsigned int segmentTreshold = DEFAULT_SEGMENT_THRESHOLD);
319 
320  // Destructor.
321  ~QPDrawThread();
322 
323  // Returns the total segments that will be drawn. This is AT LEAST the
324  // number of items to be drawn.
325  unsigned int totalSegments() const;
326 
327  // Implements QThread::run(). Draws the items into one of the two images
328  // depending on their layers. Emits segmentDrawn() after each segment is
329  // finished.
330  void run();
331 
332  // Returns the image result for the given layer. Should only be done AFTER
333  // the thread is finished. If clearResult is true, then the resulting
334  // image is removed from the thread's images.
335  QPImageCache drawResult(PlotCanvasLayer layer, bool clearResult = true);
336 
337  //Used for identifying threads when debugging.
338  int getId() const {
339  return id;
340  }
341 
342 public slots:
343  // Cancels this thread. If the thread is currently running, it will finish
344  // the segment it is on and then stop.
345  void cancel();
346 
347 private:
348  int id;
349 
350  // Items to draw.
351  QList<const QPPlotItem*> m_items;
352 
353  // Axes maps.
354  QwtScaleMap m_axesMaps[QwtPlot::axisCnt];
355 
356  // Images to draw into.
357  QHash<PlotCanvasLayer, QPImageCache*> m_images;
358 
359  // Maximum number of draw items per segment.
360  unsigned int m_segmentThreshold;
361 
362  // Flag that thread checks while running, to cancel rest of draw.
363  bool m_cancelFlag;
364 };
365 
366 }
367 
368 #endif
369 
370 #endif /* QPPLOTITEM_H_ */
virtual void attach(PlotCanvas *canvas)
Overrides PlotTool::attach().
PlotItem
Definition: Plot.h:598
PlotAxis
Enum for the four plot axes.
Definition: PlotOptions.h:62
casacore::CountedPtr< PlotLogger > PlotLoggerPtr
Definition: PlotLogger.h:534
casacore::CountedPtr< PlotItem > PlotItemPtr
Definition: PlotItem.h:98
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
virtual void detach()
Overrides PlotTool::detach().