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