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