]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsImage.h
00c42f63aaa34b047a4edaea811b9de4592d4d0e
[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/signals/signal1.hpp>
30
31 #include <vector>
32 #include <utility>
33
34 namespace lyx {
35 namespace graphics {
36
37 class Params;
38
39 class Image {
40 public:
41         /** This is to be connected to a function that will return a new
42          *  instance of a viable derived class.
43          */
44         typedef boost::shared_ptr<Image> ImagePtr;
45         ///
46         static boost::function<ImagePtr()> newImage;
47
48         /// Return the list of loadable formats.
49         typedef std::vector<std::string> FormatList;
50         ///
51         static boost::function<FormatList()> loadableFormats;
52
53         ///
54         virtual ~Image() {}
55
56         /// Create a copy
57         Image * clone() const;
58
59         /// Get the image width
60         unsigned int getWidth() const;
61
62         /// Get the image height
63         unsigned int getHeight() const;
64
65         /// Is the image drawable ?
66         bool isDrawable() const;
67
68         /** At the end of the loading process inform the outside world
69          *  by emitting a signal.
70          */
71         typedef boost::signal1<void, bool> SignalType;
72         ///
73         SignalType finishedLoading;
74
75         /** Start loading the image file.
76          *  The caller should expect this process to be asynchronous and
77          *  so should connect to the "finished" signal above.
78          */
79         void load(std::string const & filename);
80
81         /** Generate the pixmap.
82          *  Uses the params to decide on color, grayscale etc.
83          *  Returns true if the pixmap is created.
84          */
85         bool setPixmap(Params const & params);
86
87         /// Clip the image using params.
88         void clip(Params const & params);
89
90         /// Rotate the image using params.
91         void rotate(Params const & params);
92
93         /// Scale the image using params.
94         void scale(Params const & params);
95
96 protected:
97         /// Must define default c-tor explicitly as we define a copy c-tor.
98         Image() {}
99         /// Don't copy the signal finishedLoading
100         Image(Image const &) {}
101
102         /** Uses the params to ascertain the dimensions of the scaled image.
103          *  Returned as make_pair(width, height).
104          *  If something goes wrong, returns make_pair(getWidth(), getHeight())
105          */
106         std::pair<unsigned int, unsigned int>
107         getScaledDimensions(Params const & params) const;
108
109 private:
110         /// Create a copy
111         virtual Image * clone_impl() const = 0;
112         /// Get the image width
113         virtual unsigned int getWidth_impl() const = 0;
114
115         /// Get the image height
116         virtual unsigned int getHeight_impl() const = 0;
117
118         /// is the image drawable ?
119         virtual bool isDrawable_impl() const = 0;
120
121         /** Start loading the image file.
122          *  The caller should expect this process to be asynchronous and
123          *  so should connect to the "finished" signal above.
124          */
125         virtual void load_impl(std::string const & filename) = 0;
126
127         /** Generate the pixmap.
128          *  Uses the params to decide on color, grayscale etc.
129          *  Returns true if the pixmap is created.
130          */
131         virtual bool setPixmap_impl(Params const & params) = 0;
132
133         /// Clip the image using params.
134         virtual void clip_impl(Params const & params) = 0;
135
136         /// Rotate the image using params.
137         virtual void rotate_impl(Params const & params) = 0;
138
139         /// Scale the image using params.
140         virtual void scale_impl(Params const & params) = 0;
141 };
142
143
144 inline
145 Image * Image::clone() const
146 {
147         return clone_impl();
148 }
149
150
151 inline
152 unsigned int Image::getWidth() const
153 {
154         return getWidth_impl();
155 }
156
157
158 inline
159 unsigned int Image::getHeight() const
160 {
161         return getHeight_impl();
162 }
163
164
165 inline
166 bool Image::isDrawable() const
167 {
168         return isDrawable_impl();
169 }
170
171
172 inline
173 void Image::load(std::string const & filename)
174 {
175         return load_impl(filename);
176 }
177
178
179 inline
180 bool Image::setPixmap(Params const & params)
181 {
182         return setPixmap_impl(params);
183 }
184
185
186 inline
187 void Image::clip(Params const & params)
188 {
189         return clip_impl(params);
190 }
191
192
193 inline
194 void Image::rotate(Params const & params)
195 {
196         return rotate_impl(params);
197 }
198
199
200 inline
201 void Image::scale(Params const & params)
202 {
203         return scale_impl(params);
204 }
205
206 } // namespace graphics
207 } // namespace lyx
208
209 #endif // GRAPHICSIMAGE_H