]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLImage.C
Basic Qt image loader support, clipboard support
[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 // FIXME 
128 Pixmap QLImage::getPixmap() const
129 {
130         return 1;
131 }
132
133
134 void QLImage::load(string const & filename)
135 {
136         if (!pixmap_.isNull()) {
137                 lyxerr[Debug::GRAPHICS]
138                         << "Image is loaded already!" << std::endl;
139                 finishedLoading(false);
140                 return;
141         }
142
143         if (!pixmap_.load(filename.c_str())) {
144                 lyxerr[Debug::GRAPHICS]
145                         << "Unable to open image" << std::endl;
146                 finishedLoading(false);
147                 return;
148         }
149         finishedLoading(true); 
150 }
151
152
153 bool QLImage::setPixmap(Params const & params)
154 {
155         if (pixmap_.isNull() || params.display == NoDisplay)
156                 return false;
157
158 // FIXME
159 #if 0  
160         int color_key;
161         switch (params.display) {
162         case MonochromeDisplay:
163                 color_key = FL_IMAGE_MONO;
164                 break;
165         case GrayscaleDisplay:
166                 color_key = FL_IMAGE_GRAY;
167                 break;
168         case ColorDisplay:
169         default: // NoDisplay cannot happen!
170                 color_key = FL_IMAGE_RGB;
171                 break;
172         }
173
174         if (color_key != FL_IMAGE_RGB) {
175                 flimage_convert(image_, color_key, 0);
176         }
177
178         unsigned int fill = packedcolor(LColor::graphicsbg);
179         if (fill != image_->fill_color) {
180                 // the background color has changed.
181                 // Note that in grayscale/monochrome images the background is
182                 // grayed also, so this call will have no visible effect. Sorry!
183                 flimage_replace_pixel(image_, image_->fill_color, fill);
184                 image_->fill_color = fill;
185         }
186 #endif 
187
188         xformed_pixmap_ = pixmap_;
189         return true;
190 }
191
192
193 void QLImage::clip(Params const & params)
194 {
195         if (pixmap_.isNull())
196                 return;
197
198         if (params.bb.empty())
199                 // No clipping is necessary.
200                 return;
201
202         int const new_width  = params.bb.xr - params.bb.xl;
203         int const new_height = params.bb.yt - params.bb.yb;
204
205         // No need to check if the width, height are > 0 because the
206         // Bounding Box would be empty() in this case.
207         if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
208                 // Bounds are invalid.
209                 return;
210         }
211
212         if (new_width == pixmap_.width() && new_height == pixmap_.height())
213                 return;
214
215         int const xoffset_l = std::max(0, params.bb.xl);
216         int const yoffset_t = std::max(0, pixmap_.height() - params.bb.yt);
217
218         xformed_pixmap_.resize(new_width, new_height);
219         QPainter p;
220         p.begin(&xformed_pixmap_);
221         p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
222         p.end();
223 }
224
225
226 void QLImage::rotate(Params const & params)
227 {
228         if (xformed_pixmap_.isNull())
229                 return;
230
231         if (!params.angle)
232                 return;
233
234         // The angle passed to flimage_rotate is the angle in one-tenth of a
235         // degree units.
236
237         QWMatrix m;
238         m.rotate(params.angle / 10.0);
239         xformed_pixmap_.xForm(m);
240 }
241
242
243 void QLImage::scale(Params const & params)
244 {
245         if (xformed_pixmap_.isNull())
246                 return;
247
248         unsigned int width;
249         unsigned int height;
250         boost::tie(width, height) = getScaledDimensions(params);
251
252         if (width == getWidth() && height == getHeight())
253                 return;
254
255         xformed_pixmap_.resize(width, height);
256 }
257
258 } // namespace grfx