]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiImage.cpp
Remove a workaround that seems to be useless since Qt 4.8
[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         return static_cast<unsigned int>(ceil(is_transformed_ ?
68                 (transformed_.width() / transformed_.devicePixelRatio()) :
69                 (original_.width() / original_.devicePixelRatio())));
70 }
71
72
73 unsigned int GuiImage::height() const
74 {
75         return static_cast<unsigned int>(ceil(is_transformed_ ?
76                 (transformed_.height() / transformed_.devicePixelRatio()) :
77                 (original_.height() / original_.devicePixelRatio())));
78 }
79
80
81 bool GuiImage::load(FileName const & filename)
82 {
83         if (!original_.isNull()) {
84                 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
85                 return false;
86         }
87         fname_ = toqstr(filename.absFileName());
88         return load();
89 }
90
91
92 bool GuiImage::load()
93 {
94         if (!original_.load(fname_)) {
95                 LYXERR(Debug::GRAPHICS, "Unable to open image");
96                 return false;
97         }
98         return true;
99 }
100
101
102 bool GuiImage::setPixmap(Params const & params)
103 {
104         if (!params.display)
105                 return false;
106
107         if (original_.isNull()) {
108                 if (!load())
109                         return false;
110         }
111
112         original_.setDevicePixelRatio(params.pixel_ratio);
113
114         is_transformed_ = clip(params);
115         is_transformed_ |= rotate(params);
116         is_transformed_ |= scale(params);
117
118         // Clear the pixmap to save some memory.
119         if (is_transformed_)
120                 original_ = QImage();
121         else
122                 transformed_ = QImage();
123
124         return true;
125 }
126
127
128 bool GuiImage::clip(Params const & params)
129 {
130         if (params.bb.empty())
131                 // No clipping is necessary.
132                 return false;
133
134         double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
135         int const new_width  = static_cast<int>((params.bb.xr.inBP() - params.bb.xl.inBP()) * pixelRatio);
136         int const new_height = static_cast<int>((params.bb.yt.inBP() - params.bb.yb.inBP()) * pixelRatio);
137
138         QImage const & image = is_transformed_ ? transformed_ : original_;
139
140         // No need to check if the width, height are > 0 because the
141         // Bounding Box would be empty() in this case.
142         if (new_width > image.width() || new_height > image.height()) {
143                 // Bounds are invalid.
144                 return false;
145         }
146
147         if (new_width == image.width() && new_height == image.height())
148                 return false;
149
150         int const xoffset_l = params.bb.xl.inBP();
151         int const yoffset_t = (image.height() > params.bb.yt.inBP())
152                 ? image.height() - params.bb.yt.inBP() : 0;
153
154         transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
155         return true;
156 }
157
158
159 bool GuiImage::rotate(Params const & params)
160 {
161         if (params.angle == 0)
162                 return false;
163
164         QImage const & image = is_transformed_ ? transformed_ : original_;
165         QTransform m;
166         m.rotate(- params.angle);
167         transformed_ = image.transformed(m);
168         return true;
169 }
170
171
172 bool GuiImage::scale(Params const & params)
173 {
174         QImage const & image = is_transformed_ ? transformed_ : original_;
175
176         if (params.scale == 100)
177                 return false;
178
179         double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
180         qreal const scale = qreal(params.scale) / 100.0 * pixelRatio;
181
182         QTransform m;
183         m.scale(scale, scale);
184         // Bilinear filtering is used to scale graphics preview
185         transformed_ = image.transformed(m, Qt::SmoothTransformation);
186         return true;
187 }
188
189 } // namespace graphics
190 } // namespace lyx