casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QPImageCache.h
Go to the documentation of this file.
1 //# QPImageCache.h: Classes for caching axes images.
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 QPIMAGECACHE_H_
28 #define QPIMAGECACHE_H_
29 
30 #ifdef AIPS_HAS_QWT
31 
33 
34 #include <QCache>
35 #include <QImage>
36 #include <QMap>
37 
38 namespace casa {
39 
40 //# Forward Declarations
41 class QPCanvas;
42 
43 
44 // Abstraction of whatever Qt class the plotter uses to store images.
45 // Currently is a QImage to avoid threading issues associated with QPixmap.
46 // This class is assumed to use memory intelligently, so that only one
47 // underlying image is used, with copy-on-write semantics. This is done
48 // automatically with most Qt classes (such as QImage or QPixmap).
49 class QPImageCache {
50 public:
51  // Creates a null, blank image.
52  QPImageCache();
53 
54  // Creates a new image with the given size.
55  // <group>
56  QPImageCache(const QSize& size);
57  QPImageCache(int width, int height);
58  // </group>
59 
60  // Copies the given image, with copy-on-write semantics.
61  QPImageCache(const QPImageCache& copy);
62 
63  // Destructor.
64  ~QPImageCache();
65 
66 
67  // Returns true if this image is null/invalid, false otherwise.
68  bool isNull() const;
69 
70  // Returns the size of this image.
71  QSize size() const;
72 
73  // Fills the image with the given value.
74  void fill(unsigned int fillValue);
75 
76  // Returns the depth of the image.
77  int depth() const;
78 
79  // Returns a painter that can draw into this image. It is the caller's
80  // responsibility to delete the painter upon completion.
81  QPainter* painter();
82 
83  // Draws the image using the given painter, into the given draw rect. If
84  // the image is not the same size as the draw rect, it will be stretched
85  // appropriately.
86  // <group>
87  void paint(QPainter& painter, const QRect& drawRect) const;
88  void paint(QPainter* painter, const QRect& drawRect) const {
89  if(painter != NULL) paint(*painter, drawRect); }
90  // </group>
91 
92 
93  // Used to access the underlying data structure. Direct access should be
94  // avoided, in case it changes in the future.
95  // <group>
96  QImage& asQImage();
97  const QImage& asQImage() const;
98  // </group>
99 
100 
101  // Copy operator, with copy-on-write semantics.
102  QPImageCache& operator=(const QPImageCache& copy);
103 
104  // Equality operators.
105  // <group>
106  bool operator==(const QPImageCache& other);
107  bool operator!=(const QPImageCache& other) { return !(operator==(other)); }
108  // </group>
109 
110 private:
111  // Underlying image.
112  QImage m_image;
113 };
114 
115 
116 // Class to managed cached images associated with a canvas axes stack.
117 class QPAxesCache {
118 public:
119  // Static //
120 
121  // Default cache size limit, in kilobytes.
122  static const int DEFAULT_MEMORY_LIMIT_KB;
123 
124  // Convenient access to class name for logging.
125  static const casacore::String CLASS_NAME;
126 
127  // Convenience class to use as a key. Every axis that has at least one
128  // item attached to it will get an entry in the map, along with its range.
129  class Key : public QMap<PlotAxis, QPair<double, double> > {
130  public:
131  // Default constructor. Empty key.
132  Key();
133 
134  // Constructor which uses the current axes state of the given canvas.
135  Key(const QPCanvas& canvas);
136 
137  // Destructor.
138  ~Key();
139 
140 
141  // Sets the key value using the current axes state of the given canvas.
142  void setValue(const QPCanvas& canvas);
143 
144  // Returns a hash for this key.
145  uint hash() const;
146  };
147 
148 
149  // Non-Static //
150 
151  // Constructor which takes a size limit for the cache.
152  QPAxesCache(QPCanvas& canvas, int sizeLimitKb = DEFAULT_MEMORY_LIMIT_KB);
153 
154  // Destructor.
155  ~QPAxesCache();
156 
157 
158  // Returns the current size of the cache.
159  unsigned int size() const;
160 
161  // Returns the (approximate) current memory size of the cache, in
162  // kilobytes.
163  int memorySize() const;
164 
165  // Gets/Sets the size limit for the cache, in kilobytes.
166  // <group>
167  int memoryLimit() const;
168  void setMemoryLimit(int memoryLimitKb);
169  // </group>
170 
171  // Returns the current size of the images that are cached. This can be
172  // used to check for resizing, in which case the cache should probably be
173  // cleared.
174  QSize currImageSize() const;
175 
176  // Gets/Sets the fixed cache image size. See
177  // PlotCanvas::cachedAxesStackImageSize().
178  // <group>
179  QSize fixedImageSize() const;
180  void setFixedSize(QSize size);
181  // </group>
182 
183  // Clears all cached images.
184  void clear();
185 
186  // Clears all cached images for the given layer(s).
187  // <group>
188  void clearLayer(PlotCanvasLayer layer);
189  void clearLayers(int layersFlag);
190  // </group>
191 
192  // Returns true if the cache has an entry for the current axes state of its
193  // canvas, false otherwise.
194  bool currHasImage() const;
195 
196  // Returns true if the cache has an image for the current axes state of its
197  // parent for the given layer, false otherwise.
198  bool currHasImage(PlotCanvasLayer layer) const;
199 
200  // Returns the image for the current axes state of its canvas for the given
201  // layer, or a null image if there is none.
202  QPImageCache currImage(PlotCanvasLayer layer);
203 
204  // Adds the given image for the current axes state of its canvas for the
205  // given layer. The current image size is set to the size of the given
206  // image (unless it is a null image).
207  void addCurrImage(PlotCanvasLayer layer, const QPImageCache& image);
208 
209 private:
210  // Convenience class to use as a value.
211  class Value : public QMap<PlotCanvasLayer, QPImageCache> {
212  public:
213  // Default constructor.
214  Value();
215 
216  // Copy constructor.
217  Value(const QMap<PlotCanvasLayer, QPImageCache>& copy);
218 
219  // Destructor.
220  ~Value();
221 
222  // Returns the approximate memory size, in kilobytes.
223  int memorySize() const;
224  };
225 
226 
227  // Parent canvas.
228  QPCanvas& m_canvas;
229 
230  // Image cache.
231  QCache<Key, Value> m_cache;
232 
233  // Fixed image size.
234  QSize m_fixedSize;
235 };
236 
237 }
238 
239 // Provides a hashing function for QPAxesCache::Key, for use with Qt.
240 uint qHash(const casa::QPAxesCache::Key& key);
241 
242 #endif
243 #endif /* QPIMAGECACHE_H_ */
StatsData< AccumType > copy(const StatsData< AccumType > &stats)
PtrHolder< T > & operator=(const PtrHolder< T > &other)
LatticeExprNode operator!=(const LatticeExprNode &left, const LatticeExprNode &right)
size_t size() const
String: the storage and methods of handling collections of characters.
Definition: String.h:223
Bool operator==(const MVTime &lh, const MVTime &rh)
is equal operator, uses operator Double which returns days
Definition: MVTime.h:465
PlotCanvasLayer
The canvas is composed of multiple layers, where changing/adding items from one layer will not affect...
Definition: PlotOptions.h:109