]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsImage.h
dae4ecea64fd6e78ad8ce9004968d97d0ffef675
[lyx.git] / src / graphics / GraphicsImage.h
1 // -*- C++ -*-
2 /**
3  * \file GraphicsImage.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Baruch Even
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * An abstract base class for the images themselves.
13  * Allows the user to retrieve the pixmap, once loaded and to issue commands
14  * to modify it.
15  *
16  * The boost::functions newImage and loadableFormats are connected to the
17  * appropriate derived classes elsewhere, allowing the graphics cache to
18  * access them without knowing anything about their instantiation.
19  *
20  * The loading process can be asynchronous, but cropping, rotating and
21  * scaling block execution.
22  */
23
24 #ifndef GRAPHICSIMAGE_H
25 #define GRAPHICSIMAGE_H
26
27 #include <boost/function.hpp>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/signal.hpp>
30
31 #include <vector>
32 #include <utility>
33
34 namespace lyx {
35
36 namespace support { class FileName; }
37
38 namespace graphics {
39
40 class Params;
41
42 class Image {
43 public:
44         /** This is to be connected to a function that will return a new
45          *  instance of a viable derived class.
46          */
47         typedef boost::shared_ptr<Image> ImagePtr;
48         ///
49         static boost::function<ImagePtr()> newImage;
50
51         ///
52         typedef std::vector<std::string> FormatList;
53         /// Return the list of loadable formats.
54         static boost::function<FormatList()> loadableFormats;
55
56         ///
57         virtual ~Image() {}
58
59         /// Create a copy
60         Image * clone() const;
61
62         /// Get the image width
63         unsigned int getWidth() const;
64
65         /// Get the image height
66         unsigned int getHeight() const;
67
68         /// Is the image drawable ?
69         bool isDrawable() const;
70
71         /** At the end of the loading process inform the outside world
72          *  by emitting a signal
73          */
74         typedef boost::signal<void(bool)> SignalType;
75         ///
76         SignalType finishedLoading;
77
78         /** Start loading the image file.
79          *  The caller should expect this process to be asynchronous and
80          *  so should connect to the "finished" signal above.
81          */
82         void load(support::FileName const & filename);
83
84         /** Generate the pixmap.
85          *  Uses the params to decide on color, grayscale etc.
86          *  Returns true if the pixmap is created.
87          */
88         bool setPixmap(Params const & params);
89
90         /// Clip the image using params.
91         void clip(Params const & params);
92
93         /// Rotate the image using params.
94         void rotate(Params const & params);
95
96         /// Scale the image using params.
97         void scale(Params const & params);
98
99 protected:
100         /// Must define default c-tor explicitly as we define a copy c-tor.
101         Image() {}
102         /// Don't copy the signal finishedLoading
103         Image(Image const &) {}
104
105         /** Uses the params to ascertain the dimensions of the scaled image.
106          *  Returned as make_pair(width, height).
107          *  If something goes wrong, returns make_pair(getWidth(), getHeight())
108          */
109         std::pair<unsigned int, unsigned int>
110         getScaledDimensions(Params const & params) const;
111
112 private:
113         /// Create a copy
114         virtual Image * clone_impl() const = 0;
115         /// Get the image width
116         virtual unsigned int getWidth_impl() const = 0;
117
118         /// Get the image height
119         virtual unsigned int getHeight_impl() const = 0;
120
121         /// is the image drawable ?
122         virtual bool isDrawable_impl() const = 0;
123
124         /** Start loading the image file.
125          *  The caller should expect this process to be asynchronous and
126          *  so should connect to the "finished" signal above.
127          */
128         virtual void load_impl(support::FileName const & filename) = 0;
129
130         /** Generate the pixmap.
131          *  Uses the params to decide on color, grayscale etc.
132          *  Returns true if the pixmap is created.
133          */
134         virtual bool setPixmap_impl(Params const & params) = 0;
135
136         /// Clip the image using params.
137         virtual void clip_impl(Params const & params) = 0;
138
139         /// Rotate the image using params.
140         virtual void rotate_impl(Params const & params) = 0;
141
142         /// Scale the image using params.
143         virtual void scale_impl(Params const & params) = 0;
144 };
145
146
147 inline
148 Image * Image::clone() const
149 {
150         return clone_impl();
151 }
152
153
154 inline
155 unsigned int Image::getWidth() const
156 {
157         return getWidth_impl();
158 }
159
160
161 inline
162 unsigned int Image::getHeight() const
163 {
164         return getHeight_impl();
165 }
166
167
168 inline
169 bool Image::isDrawable() const
170 {
171         return isDrawable_impl();
172 }
173
174
175 inline
176 void Image::load(support::FileName const & filename)
177 {
178         return load_impl(filename);
179 }
180
181
182 inline
183 bool Image::setPixmap(Params const & params)
184 {
185         return setPixmap_impl(params);
186 }
187
188
189 inline
190 void Image::clip(Params const & params)
191 {
192         return clip_impl(params);
193 }
194
195
196 inline
197 void Image::rotate(Params const & params)
198 {
199         return rotate_impl(params);
200 }
201
202
203 inline
204 void Image::scale(Params const & params)
205 {
206         return scale_impl(params);
207 }
208
209 } // namespace graphics
210 } // namespace lyx
211
212 #endif // GRAPHICSIMAGE_H