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 inline const_iterator begin() const { return const_iterator(map_.begin()); } | |
Robert Sesek
2013/03/27 17:42:50
Remove the |inline| keywords throughout this file.
Matt Giuca
2013/03/28 03:54:03
Done.
| |
33 // Gets an iterator to one after the last image. | |
34 inline const_iterator end() const { return const_iterator(map_.end()); } | |
35 | |
36 // Determines whether the image family has no images in it. | |
37 inline bool empty() const { return map_.empty(); } | |
38 | |
39 // Removes all images from the family. | |
40 inline 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, width> pair. | |
59 // A 0x0 image has aspect ratio 1.0. 0xN and Nx0 images are treated as 0x0. | |
60 // If the image contains high-DPI bitmaps, the width refers to the width at | |
pkotwicz
2013/03/27 19:04:53
Nit: An <aspect ratio, DIP width> pair.
"DIP width
Matt Giuca
2013/03/28 03:54:03
Done.
| |
61 // the 100% representation. | |
62 struct MapKey : std::pair<float, int> { | |
63 MapKey(float aspect, int width) | |
64 : std::pair<float, int>(aspect, width) {} | |
65 | |
66 inline float aspect() const { return first; } | |
67 | |
68 inline float width() const { return second; } | |
69 }; | |
70 | |
71 class const_iterator : | |
72 std::iterator<std::bidirectional_iterator_tag, const gfx::Image> { | |
73 public: | |
74 inline const_iterator() {} | |
75 | |
76 inline const_iterator& operator++() { | |
Robert Sesek
2013/03/27 17:42:50
Do all these definitions have to be in the header?
Matt Giuca
2013/03/28 03:54:03
But they are used (and expected to be inlined) ext
| |
77 ++map_iterator_; | |
78 return *this; | |
79 } | |
80 | |
81 inline const_iterator operator++(int /*unused*/) { | |
82 const_iterator result(*this); | |
83 ++(*this); | |
84 return result; | |
85 } | |
86 | |
87 inline const_iterator& operator--() { | |
88 --map_iterator_; | |
89 return *this; | |
90 } | |
91 | |
92 inline const_iterator operator--(int /*unused*/) { | |
93 const_iterator result(*this); | |
94 --(*this); | |
95 return result; | |
96 } | |
97 | |
98 inline bool operator==(const const_iterator& other) const { | |
99 return map_iterator_ == other.map_iterator_; | |
100 } | |
101 | |
102 inline bool operator!=(const const_iterator& other) const { | |
103 return map_iterator_ != other.map_iterator_; | |
104 } | |
105 | |
106 inline const gfx::Image& operator*() const { | |
107 return map_iterator_->second; | |
108 } | |
109 | |
110 inline const gfx::Image* operator->() const { | |
111 return &**this; | |
112 } | |
113 | |
114 private: | |
115 explicit const_iterator( | |
116 const std::map<MapKey, gfx::Image>::const_iterator& other); | |
117 | |
118 std::map<MapKey, gfx::Image>::const_iterator map_iterator_; | |
119 | |
120 friend class ImageFamily; | |
121 }; | |
122 | |
123 private: | |
124 // Map from (aspect ratio, width) to image. | |
125 std::map<MapKey, gfx::Image> map_; | |
126 }; | |
127 | |
128 } // namespace gfx | |
129 | |
130 #endif // UI_GFX_IMAGE_IMAGE_FAMILY_H_ | |
OLD | NEW |