casa
$Rev:20696$
|
00001 //# QPImageCache.h: Classes for caching axes images. 00002 //# Copyright (C) 2009 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 QPIMAGECACHE_H_ 00028 #define QPIMAGECACHE_H_ 00029 00030 #ifdef AIPS_HAS_QWT 00031 00032 #include <graphics/GenericPlotter/PlotOptions.h> 00033 00034 #include <QCache> 00035 #include <QImage> 00036 #include <QMap> 00037 00038 #include <casa/namespace.h> 00039 00040 namespace casa { 00041 00042 //# Forward Declarations 00043 class QPCanvas; 00044 00045 00046 // Abstraction of whatever Qt class the plotter uses to store images. 00047 // Currently is a QImage to avoid threading issues associated with QPixmap. 00048 // This class is assumed to use memory intelligently, so that only one 00049 // underlying image is used, with copy-on-write semantics. This is done 00050 // automatically with most Qt classes (such as QImage or QPixmap). 00051 class QPImageCache { 00052 public: 00053 // Creates a null, blank image. 00054 QPImageCache(); 00055 00056 // Creates a new image with the given size. 00057 // <group> 00058 QPImageCache(const QSize& size); 00059 QPImageCache(int width, int height); 00060 // </group> 00061 00062 // Copies the given image, with copy-on-write semantics. 00063 QPImageCache(const QPImageCache& copy); 00064 00065 // Destructor. 00066 ~QPImageCache(); 00067 00068 00069 // Returns true if this image is null/invalid, false otherwise. 00070 bool isNull() const; 00071 00072 // Returns the size of this image. 00073 QSize size() const; 00074 00075 // Fills the image with the given value. 00076 void fill(unsigned int fillValue); 00077 00078 // Returns the depth of the image. 00079 int depth() const; 00080 00081 // Returns a painter that can draw into this image. It is the caller's 00082 // responsibility to delete the painter upon completion. 00083 QPainter* painter(); 00084 00085 // Draws the image using the given painter, into the given draw rect. If 00086 // the image is not the same size as the draw rect, it will be stretched 00087 // appropriately. 00088 // <group> 00089 void paint(QPainter& painter, const QRect& drawRect) const; 00090 void paint(QPainter* painter, const QRect& drawRect) const { 00091 if(painter != NULL) paint(*painter, drawRect); } 00092 // </group> 00093 00094 00095 // Used to access the underlying data structure. Direct access should be 00096 // avoided, in case it changes in the future. 00097 // <group> 00098 QImage& asQImage(); 00099 const QImage& asQImage() const; 00100 // </group> 00101 00102 00103 // Copy operator, with copy-on-write semantics. 00104 QPImageCache& operator=(const QPImageCache& copy); 00105 00106 // Equality operators. 00107 // <group> 00108 bool operator==(const QPImageCache& other); 00109 bool operator!=(const QPImageCache& other) { return !(operator==(other)); } 00110 // </group> 00111 00112 private: 00113 // Underlying image. 00114 QImage m_image; 00115 }; 00116 00117 00118 // Class to managed cached images associated with a canvas axes stack. 00119 class QPAxesCache { 00120 public: 00121 // Static // 00122 00123 // Default cache size limit, in kilobytes. 00124 static const int DEFAULT_MEMORY_LIMIT_KB; 00125 00126 // Convenient access to class name for logging. 00127 static const String CLASS_NAME; 00128 00129 // Convenience class to use as a key. Every axis that has at least one 00130 // item attached to it will get an entry in the map, along with its range. 00131 class Key : public QMap<PlotAxis, QPair<double, double> > { 00132 public: 00133 // Default constructor. Empty key. 00134 Key(); 00135 00136 // Constructor which uses the current axes state of the given canvas. 00137 Key(const QPCanvas& canvas); 00138 00139 // Destructor. 00140 ~Key(); 00141 00142 00143 // Sets the key value using the current axes state of the given canvas. 00144 void setValue(const QPCanvas& canvas); 00145 00146 // Returns a hash for this key. 00147 uint hash() const; 00148 }; 00149 00150 00151 // Non-Static // 00152 00153 // Constructor which takes a size limit for the cache. 00154 QPAxesCache(QPCanvas& canvas, int sizeLimitKb = DEFAULT_MEMORY_LIMIT_KB); 00155 00156 // Destructor. 00157 ~QPAxesCache(); 00158 00159 00160 // Returns the current size of the cache. 00161 unsigned int size() const; 00162 00163 // Returns the (approximate) current memory size of the cache, in 00164 // kilobytes. 00165 int memorySize() const; 00166 00167 // Gets/Sets the size limit for the cache, in kilobytes. 00168 // <group> 00169 int memoryLimit() const; 00170 void setMemoryLimit(int memoryLimitKb); 00171 // </group> 00172 00173 // Returns the current size of the images that are cached. This can be 00174 // used to check for resizing, in which case the cache should probably be 00175 // cleared. 00176 QSize currImageSize() const; 00177 00178 // Gets/Sets the fixed cache image size. See 00179 // PlotCanvas::cachedAxesStackImageSize(). 00180 // <group> 00181 QSize fixedImageSize() const; 00182 void setFixedSize(QSize size); 00183 // </group> 00184 00185 // Clears all cached images. 00186 void clear(); 00187 00188 // Clears all cached images for the given layer(s). 00189 // <group> 00190 void clearLayer(PlotCanvasLayer layer); 00191 void clearLayers(int layersFlag); 00192 // </group> 00193 00194 // Returns true if the cache has an entry for the current axes state of its 00195 // canvas, false otherwise. 00196 bool currHasImage() const; 00197 00198 // Returns true if the cache has an image for the current axes state of its 00199 // parent for the given layer, false otherwise. 00200 bool currHasImage(PlotCanvasLayer layer) const; 00201 00202 // Returns the image for the current axes state of its canvas for the given 00203 // layer, or a null image if there is none. 00204 QPImageCache currImage(PlotCanvasLayer layer); 00205 00206 // Adds the given image for the current axes state of its canvas for the 00207 // given layer. The current image size is set to the size of the given 00208 // image (unless it is a null image). 00209 void addCurrImage(PlotCanvasLayer layer, const QPImageCache& image); 00210 00211 private: 00212 // Convenience class to use as a value. 00213 class Value : public QMap<PlotCanvasLayer, QPImageCache> { 00214 public: 00215 // Default constructor. 00216 Value(); 00217 00218 // Copy constructor. 00219 Value(const QMap<PlotCanvasLayer, QPImageCache>& copy); 00220 00221 // Destructor. 00222 ~Value(); 00223 00224 // Returns the approximate memory size, in kilobytes. 00225 int memorySize() const; 00226 }; 00227 00228 00229 // Parent canvas. 00230 QPCanvas& m_canvas; 00231 00232 // Image cache. 00233 QCache<Key, Value> m_cache; 00234 00235 // Fixed image size. 00236 QSize m_fixedSize; 00237 }; 00238 00239 } 00240 00241 // Provides a hashing function for QPAxesCache::Key, for use with Qt. 00242 uint qHash(const QPAxesCache::Key& key); 00243 00244 #endif 00245 #endif /* QPIMAGECACHE_H_ */