]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLImage.C
remove getPixmap() from GraphicsImage
[lyx.git] / src / frontends / qt2 / QLImage.C
1 /*
2  * \file QLImage.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming, a.leeming@ic.ac.uk
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "QLImage.h"
17 #include "graphics/GraphicsParams.h"
18 #include "converter.h"
19 #include "debug.h"
20 #include "support/LAssert.h"
21 #include "support/lyxfunctional.h"  // compare_memfun
22
23 #include <qimage.h>
24 #include <qwmatrix.h>
25 #include <qpainter.h>
26
27 #include <boost/tuple/tuple.hpp>
28
29 using std::find_if;
30
31 namespace grfx {
32
33 /// Access to this class is through this static method.
34 Image::ImagePtr QLImage::newImage()
35 {
36         ImagePtr ptr;
37         ptr.reset(new QLImage);
38         return ptr;
39 }
40
41
42 /// Return the list of loadable formats.
43 Image::FormatList QLImage::loadableFormats()
44 {
45         static FormatList fmts;
46  
47         if (!fmts.empty())
48                 return fmts;
49
50         // The formats recognised by LyX
51         Formats::const_iterator begin = formats.begin();
52         Formats::const_iterator end   = formats.end();
53
54         lyxerr[Debug::GRAPHICS]
55                 << "\nThe image loader can load the following directly:\n";
56
57         QStrList qt_formats = QImageIO::inputFormats();
58  
59         QStrListIterator it(qt_formats);
60
61         for (; it.current(); ++it) {
62                 lyxerr[Debug::GRAPHICS] << it.current() << endl;
63
64                 string ext = lowercase(it.current());
65          
66                 // special case
67                 if (ext == "jpeg")
68                         ext = "jpg";
69
70                 Formats::const_iterator fit =
71                         find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
72                 if (fit != end)
73                         fmts.push_back(fit->name());
74         }
75
76         lyxerr[Debug::GRAPHICS]
77                 << "\nOf these, LyX recognises the following formats:\n";
78
79         FormatList::const_iterator fbegin = fmts.begin();
80         FormatList::const_iterator fend   = fmts.end();
81         for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
82                 if (fit != fbegin)
83                         lyxerr[Debug::GRAPHICS] << ", ";
84                 lyxerr[Debug::GRAPHICS] << *fit;
85         }
86         lyxerr[Debug::GRAPHICS] << '\n' << std::endl;
87
88         return fmts;
89 }
90
91
92 QLImage::QLImage()
93         : Image()
94 {
95 }
96
97
98 QLImage::QLImage(QLImage const & other)
99         : Image(other), pixmap_(other.pixmap_)
100 {
101 }
102
103
104 QLImage::~QLImage()
105 {
106 }
107
108
109 Image * QLImage::clone() const
110 {
111         return new QLImage(*this);
112 }
113
114
115 unsigned int QLImage::getWidth() const
116 {
117         return pixmap_.width();
118 }
119
120
121 unsigned int QLImage::getHeight() const
122 {
123         return pixmap_.height();
124 }
125
126
127 void QLImage::load(string const & filename)
128 {
129         if (!pixmap_.isNull()) {
130                 lyxerr[Debug::GRAPHICS]
131                         << "Image is loaded already!" << std::endl;
132                 finishedLoading(false);
133                 return;
134         }
135
136         if (!pixmap_.load(filename.c_str())) {
137                 lyxerr[Debug::GRAPHICS]
138                         << "Unable to open image" << std::endl;
139                 finishedLoading(false);
140                 return;
141         }
142         finishedLoading(true); 
143 }
144
145
146 bool QLImage::setPixmap(Params const & params)
147 {
148         if (pixmap_.isNull() || params.display == NoDisplay)
149                 return false;
150
151 // FIXME
152 #if 0  
153         int color_key;
154         switch (params.display) {
155         case MonochromeDisplay:
156                 color_key = FL_IMAGE_MONO;
157                 break;
158         case GrayscaleDisplay:
159                 color_key = FL_IMAGE_GRAY;
160                 break;
161         case ColorDisplay:
162         default: // NoDisplay cannot happen!
163                 color_key = FL_IMAGE_RGB;
164                 break;
165         }
166
167         if (color_key != FL_IMAGE_RGB) {
168                 flimage_convert(image_, color_key, 0);
169         }
170
171         unsigned int fill = packedcolor(LColor::graphicsbg);
172         if (fill != image_->fill_color) {
173                 // the background color has changed.
174                 // Note that in grayscale/monochrome images the background is
175                 // grayed also, so this call will have no visible effect. Sorry!
176                 flimage_replace_pixel(image_, image_->fill_color, fill);
177                 image_->fill_color = fill;
178         }
179 #endif 
180
181         xformed_pixmap_ = pixmap_;
182         return true;
183 }
184
185
186 void QLImage::clip(Params const & params)
187 {
188         if (pixmap_.isNull())
189                 return;
190
191         if (params.bb.empty())
192                 // No clipping is necessary.
193                 return;
194
195         int const new_width  = params.bb.xr - params.bb.xl;
196         int const new_height = params.bb.yt - params.bb.yb;
197
198         // No need to check if the width, height are > 0 because the
199         // Bounding Box would be empty() in this case.
200         if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
201                 // Bounds are invalid.
202                 return;
203         }
204
205         if (new_width == pixmap_.width() && new_height == pixmap_.height())
206                 return;
207
208         int const xoffset_l = std::max(0, params.bb.xl);
209         int const yoffset_t = std::max(0, pixmap_.height() - params.bb.yt);
210
211         xformed_pixmap_.resize(new_width, new_height);
212         QPainter p;
213         p.begin(&xformed_pixmap_);
214         p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
215         p.end();
216 }
217
218
219 void QLImage::rotate(Params const & params)
220 {
221         if (xformed_pixmap_.isNull())
222                 return;
223
224         if (!params.angle)
225                 return;
226
227         // The angle passed to flimage_rotate is the angle in one-tenth of a
228         // degree units.
229
230         QWMatrix m;
231         m.rotate(params.angle / 10.0);
232         xformed_pixmap_.xForm(m);
233 }
234
235
236 void QLImage::scale(Params const & params)
237 {
238         if (xformed_pixmap_.isNull())
239                 return;
240
241         unsigned int width;
242         unsigned int height;
243         boost::tie(width, height) = getScaledDimensions(params);
244
245         if (width == getWidth() && height == getHeight())
246                 return;
247
248         xformed_pixmap_.resize(width, height);
249 }
250
251 } // namespace grfx