Hydra 0.20
hydra.src/desktop/ImageCache.h
00001 
00002 /*
00003  * Copyright (c) 2009 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_HYDRADESKTOP_IMAGECACHE_H__
00009 #define __INCLUDED_HYDRADESKTOP_IMAGECACHE_H__
00010 
00011 #include <list>
00012 #include <hydra/TR1.h>
00013 
00014 #include <QString>
00015 #include <QImage>
00016 #include <QPixmap>
00017 
00018 #include <desktop/LoadCache.h>
00019 
00020 namespace desktop
00021 {
00022   class ImageCache;
00023 }
00024 
00025 /**
00026  * A image-reading cache, useful for the big image viewers. Also does rescaling
00027  * of the image as needed. The rescaled versions however are not cached.
00028  *
00029  * @author Aleksander Demko
00030  */ 
00031 class desktop::ImageCache
00032 {
00033   public:
00034     /// constructor
00035     ImageCache(int maxhold = 5);
00036 
00037     /**
00038      * Is the given fullfilename in the cache?
00039      *
00040      * @author Aleksander Demko
00041      */
00042     bool containsImage(const QString &fullfilename) const { return dm_cache.containsItem(fullfilename); }
00043 
00044     /**
00045      * Loads the given file as an image, from cache if possible.
00046      * This function never fails and never returns null - on error, an empty
00047      * image will be returned.
00048      *
00049      * @author Aleksander Demko
00050      */ 
00051     desktop::cache_ptr<QImage> getImage(const QString &fullfilename) { return dm_cache.getItem(fullfilename); }
00052 
00053     /**
00054       * Note that this is a static function, and can take in image.
00055       * Obviously, the resulting QPixmaps are saved or cached in any way.
00056       *
00057       * @author Aleksander Demko
00058       */ 
00059     static QPixmap getPixmap(QImage &image, int windoww, int windowh, bool growtofit);
00060 
00061   private:
00062 
00063   private:
00064     class ImageLoader
00065     {
00066       public:
00067         // never returns null... failed loads will simply be empty images
00068         std::shared_ptr<QImage> operator()(const QString &fullfilename);
00069     };
00070 
00071     desktop::LoadCache<QImage, ImageLoader> dm_cache;
00072 };
00073 
00074 #endif
00075 
 All Classes Namespaces Functions Variables