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