]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiImage.cpp
I assume this was a c&p error
[lyx.git] / src / frontends / qt4 / GuiImage.cpp
1 /**
2  * \file GuiImage.cpp
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 #include "GuiImage.h"
15 #include "qt_helpers.h"
16
17 #include "Format.h"
18
19 #include "graphics/GraphicsParams.h"
20
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/lstrings.h"       // ascii_lowercase
24
25 #include <QPainter>
26 #include <QImageReader>
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32 namespace graphics {
33
34 /// Implement factory method defined in GraphicsImage.h
35 Image * newImage()
36 {
37         return new GuiImage;
38 }
39
40
41 GuiImage::GuiImage() : is_transformed_(false)
42 {}
43
44
45 GuiImage::GuiImage(GuiImage const & other)
46         : Image(other), original_(other.original_),
47         transformed_(other.transformed_), is_transformed_(other.is_transformed_),
48         fname_(other.fname_)
49 {}
50
51
52 Image * GuiImage::clone() const
53 {
54         return new GuiImage(*this);
55 }
56
57
58 QImage const & GuiImage::image() const
59 {
60         return is_transformed_ ? transformed_ : original_;
61 }
62
63
64 unsigned int GuiImage::width() const
65 {
66         return is_transformed_ ? transformed_.width() : original_.width();
67 }
68
69
70 unsigned int GuiImage::height() const
71 {
72         return is_transformed_ ? transformed_.height() : original_.height();
73 }
74
75
76 bool GuiImage::load(FileName const & filename)
77 {
78         if (!original_.isNull()) {
79                 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
80                 return false;
81         }
82         fname_ = toqstr(filename.absFileName());
83         return load();
84 }
85
86
87 bool GuiImage::load()
88 {
89         if (!original_.load(fname_)) {
90                 LYXERR(Debug::GRAPHICS, "Unable to open image");
91                 return false;
92         }
93         return true;
94 }
95
96
97 bool GuiImage::setPixmap(Params const & params)
98 {
99         if (!params.display)
100                 return false;
101
102         if (original_.isNull()) {
103                 if (!load())
104                         return false;
105         }
106                 
107         is_transformed_ = clip(params);
108         is_transformed_ |= rotate(params);
109         is_transformed_ |= scale(params);
110
111         // Clear the pixmap to save some memory.
112         if (is_transformed_)
113                 original_ = QImage();
114         else
115                 transformed_ = QImage();
116
117         return true;
118 }
119
120
121 bool GuiImage::clip(Params const & params)
122 {
123         if (params.bb.empty())
124                 // No clipping is necessary.
125                 return false;
126
127         int const new_width  = params.bb.xr - params.bb.xl;
128         int const new_height = params.bb.yt - params.bb.yb;
129
130         QImage const & image = is_transformed_ ? transformed_ : original_;
131
132         // No need to check if the width, height are > 0 because the
133         // Bounding Box would be empty() in this case.
134         if (new_width > image.width() || new_height > image.height()) {
135                 // Bounds are invalid.
136                 return false;
137         }
138
139         if (new_width == image.width() && new_height == image.height())
140                 return false;
141
142         int const xoffset_l = params.bb.xl;
143         int const yoffset_t = (image.height() > int(params.bb.yt))
144                 ? image.height() - params.bb.yt : 0;
145
146         transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
147         return true;
148 }
149
150
151 bool GuiImage::rotate(Params const & params)
152 {
153         if (!params.angle)
154                 return false;
155
156         QImage const & image = is_transformed_ ? transformed_ : original_;
157         QMatrix m;
158         m.rotate(- params.angle);
159         transformed_ = image.transformed(m);
160         return true;
161 }
162
163
164 bool GuiImage::scale(Params const & params)
165 {
166         QImage const & image = is_transformed_ ? transformed_ : original_;
167
168         if (params.scale == 100)
169                 return false;
170
171         qreal scale = qreal(params.scale) / 100.0;
172
173 #if (QT_VERSION >= 0x040500) && (QT_VERSION <= 0x040502)
174         // Due to a bug in Qt, LyX will crash for certain
175         // scaling factors and sizes of the image.
176         // see bug #5957: http://www.lyx.org/trac/ticket/5957
177         scale += 0.0001;
178 #endif
179
180         QMatrix m;
181         m.scale(scale, scale);
182         transformed_ = image.transformed(m);
183         return true;
184 }
185
186 } // namespace graphics
187 } // lyx