Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef UI_GFX_IMAGE_IMAGE_FAMILY_H_ | |
| 6 #define UI_GFX_IMAGE_IMAGE_FAMILY_H_ | |
| 7 | |
| 8 #include <iterator> | |
| 9 #include <map> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "ui/base/ui_export.h" | |
| 13 #include "ui/gfx/image/image.h" | |
| 14 | |
| 15 namespace gfx { | |
| 16 class ImageSkia; | |
| 17 | |
| 18 // A collection of images at different sizes. The images should be different | |
| 19 // representations of the same basic concept (for example, an icon) at various | |
| 20 // sizes and (optionally) aspect ratios. A method is provided for finding the | |
| 21 // most appropriate image to fit in a given rectangle. | |
| 22 class UI_EXPORT ImageFamily { | |
| 23 public: | |
| 24 // Type for iterating over all images in the family, in order. | |
| 25 // Dereferencing this iterator returns a gfx::Image. | |
| 26 class const_iterator; | |
| 27 | |
| 28 ImageFamily(); | |
| 29 ~ImageFamily(); | |
| 30 | |
| 31 // Gets an iterator to the first image. | |
| 32 const_iterator begin() const { return const_iterator(map_.begin()); } | |
| 33 // Gets an iterator to one after the last image. | |
| 34 const_iterator end() const { return const_iterator(map_.end()); } | |
| 35 | |
| 36 // Determines whether the image family has no images in it. | |
| 37 bool empty() const { return map_.empty(); } | |
| 38 | |
| 39 // Removes all images from the family. | |
| 40 void clear() { return map_.clear(); } | |
| 41 | |
| 42 // Adds an image to the family. If another image is already present at the | |
| 43 // same size, it will be overwritten. | |
| 44 void Add(const gfx::Image& image); | |
| 45 | |
| 46 // Adds an image to the family. If another image is already present at the | |
| 47 // same size, it will be overwritten. | |
| 48 void Add(const gfx::ImageSkia& image_skia); | |
| 49 | |
| 50 // Gets the best image to use in a rectangle of |width|x|height|. | |
| 51 // Gets an image at the same aspect ratio as |width|:|height|, if possible, or | |
| 52 // if not, the closest aspect ratio. Among images of that aspect ratio, | |
| 53 // returns the smallest image with both its width and height bigger or equal | |
| 54 // to the requested size. If none exists, returns the largest image of that | |
| 55 // aspect ratio. If there are no images in the family, returns NULL. | |
| 56 const gfx::Image* Get(int width, int height) const; | |
| 57 | |
| 58 // An <aspect ratio, DIP width> pair. | |
| 59 // A 0x0 image has aspect ratio 1.0. 0xN and Nx0 images are treated as 0x0. | |
| 60 struct MapKey : std::pair<float, int> { | |
| 61 MapKey(float aspect, int width) | |
| 62 : std::pair<float, int>(aspect, width) {} | |
| 63 | |
| 64 float aspect() const { return first; } | |
| 65 | |
| 66 float width() const { return second; } | |
| 67 }; | |
| 68 | |
| 69 class const_iterator : | |
|
pkotwicz
2013/03/29 00:25:35
Is there a way you can avoid creating a custom ite
Matt Giuca
2013/03/30 00:54:14
I know it's quite excessive and I feel bad about i
pkotwicz
2013/04/02 16:06:31
Ok, I am for keeping the iterator.
I think that w
| |
| 70 std::iterator<std::bidirectional_iterator_tag, const gfx::Image> { | |
| 71 public: | |
| 72 const_iterator() {} | |
| 73 | |
| 74 const_iterator& operator++() { | |
| 75 ++map_iterator_; | |
| 76 return *this; | |
| 77 } | |
| 78 | |
| 79 const_iterator operator++(int /*unused*/) { | |
| 80 const_iterator result(*this); | |
| 81 ++(*this); | |
| 82 return result; | |
| 83 } | |
| 84 | |
| 85 const_iterator& operator--() { | |
| 86 --map_iterator_; | |
| 87 return *this; | |
| 88 } | |
| 89 | |
| 90 const_iterator operator--(int /*unused*/) { | |
| 91 const_iterator result(*this); | |
| 92 --(*this); | |
| 93 return result; | |
| 94 } | |
| 95 | |
| 96 bool operator==(const const_iterator& other) const { | |
| 97 return map_iterator_ == other.map_iterator_; | |
| 98 } | |
| 99 | |
| 100 bool operator!=(const const_iterator& other) const { | |
| 101 return map_iterator_ != other.map_iterator_; | |
| 102 } | |
| 103 | |
| 104 const gfx::Image& operator*() const { | |
| 105 return map_iterator_->second; | |
| 106 } | |
| 107 | |
| 108 const gfx::Image* operator->() const { | |
| 109 return &**this; | |
| 110 } | |
| 111 | |
| 112 private: | |
| 113 explicit const_iterator( | |
| 114 const std::map<MapKey, gfx::Image>::const_iterator& other); | |
| 115 | |
| 116 std::map<MapKey, gfx::Image>::const_iterator map_iterator_; | |
| 117 | |
| 118 friend class ImageFamily; | |
| 119 }; | |
| 120 | |
| 121 private: | |
| 122 // Find the closest aspect ratio in the map to |desired_aspect|. | |
| 123 // Ties are broken by the thinner aspect. | |
| 124 // map_ must not be empty. | |
| 125 float GetClosestAspect(float desired_aspect) const; | |
| 126 | |
| 127 // Gets an image with aspect ratio |aspect|, at the best size for |width|. | |
| 128 // Returns the smallest image of aspect ratio |aspect| with its width bigger | |
| 129 // or equal to |width|. If none exists, returns the largest image of aspect | |
| 130 // ratio |aspect|. Behavior is undefined if there is not at least one image in | |
| 131 // map_ of aspect ratio |aspect|. | |
| 132 const gfx::Image* GetWithExactAspect(float aspect, int width) const; | |
| 133 | |
| 134 // Map from (aspect ratio, width) to image. | |
| 135 std::map<MapKey, gfx::Image> map_; | |
| 136 }; | |
| 137 | |
| 138 } // namespace gfx | |
| 139 | |
| 140 #endif // UI_GFX_IMAGE_IMAGE_FAMILY_H_ | |
| OLD | NEW |