]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiImage.cpp
589511cb90b3819eed547d8d005087e5d00bb820
[features.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 #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         double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
145         int const new_width  = static_cast<int>((params.bb.xr - params.bb.xl) * pixelRatio);
146         int const new_height = static_cast<int>((params.bb.yt - params.bb.yb) * pixelRatio);
147
148         QImage const & image = is_transformed_ ? transformed_ : original_;
149
150         // No need to check if the width, height are > 0 because the
151         // Bounding Box would be empty() in this case.
152         if (new_width > image.width() || new_height > image.height()) {
153                 // Bounds are invalid.
154                 return false;
155         }
156
157         if (new_width == image.width() && new_height == image.height())
158                 return false;
159
160         int const xoffset_l = params.bb.xl;
161         int const yoffset_t = (image.height() > int(params.bb.yt))
162                 ? image.height() - params.bb.yt : 0;
163
164         transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
165         return true;
166 }
167
168
169 bool GuiImage::rotate(Params const & params)
170 {
171         if (!params.angle)
172                 return false;
173
174         QImage const & image = is_transformed_ ? transformed_ : original_;
175         QMatrix m;
176         m.rotate(- params.angle);
177         transformed_ = image.transformed(m);
178         return true;
179 }
180
181
182 bool GuiImage::scale(Params const & params)
183 {
184         QImage const & image = is_transformed_ ? transformed_ : original_;
185
186         if (params.scale == 100)
187                 return false;
188
189         double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
190         qreal scale = qreal(params.scale) / 100.0 * pixelRatio;
191
192 #if (QT_VERSION >= 0x040500) && (QT_VERSION <= 0x040502)
193         // Due to a bug in Qt, LyX will crash for certain
194         // scaling factors and sizes of the image.
195         // see bug #5957: http://www.lyx.org/trac/ticket/5957
196         scale += 0.0001;
197 #endif
198
199         QMatrix m;
200         m.scale(scale, scale);
201         transformed_ = image.transformed(m);
202         return true;
203 }
204
205 } // namespace graphics
206 } // lyx