]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiImage.cpp
cf580b12530150370c2bbdb3b121602c35dec3b4
[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 /// Access to this class is through this static method.
35 Image * GuiImage::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         original_.detach();
94         return true;
95 }
96
97
98 bool GuiImage::setPixmap(Params const & params)
99 {
100         if (!params.display)
101                 return false;
102
103         if (original_.isNull()) {
104                 if (!load())
105                         return false;
106         }
107                 
108         is_transformed_ = clip(params);
109         is_transformed_ |= rotate(params);
110         is_transformed_ |= scale(params);
111
112         // Clear the pixmap to save some memory.
113         if (is_transformed_) {
114                 original_.detach();
115                 original_ = QImage();
116         } else {
117                 transformed_.detach();
118                 transformed_ = QImage();
119         }
120
121         return true;
122 }
123
124
125 bool GuiImage::clip(Params const & params)
126 {
127         if (params.bb.empty())
128                 // No clipping is necessary.
129                 return false;
130
131         int const new_width  = params.bb.xr - params.bb.xl;
132         int const new_height = params.bb.yt - params.bb.yb;
133
134         QImage const & image = is_transformed_ ? transformed_ : original_;
135
136         // No need to check if the width, height are > 0 because the
137         // Bounding Box would be empty() in this case.
138         if (new_width > image.width() || new_height > image.height()) {
139                 // Bounds are invalid.
140                 return false;
141         }
142
143         if (new_width == image.width() && new_height == image.height())
144                 return false;
145
146         int const xoffset_l = params.bb.xl;
147         int const yoffset_t = (image.height() > int(params.bb.yt))
148                 ? image.height() - params.bb.yt : 0;
149
150         transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
151         return true;
152 }
153
154
155 bool GuiImage::rotate(Params const & params)
156 {
157         if (!params.angle)
158                 return false;
159
160         QImage const & image = is_transformed_ ? transformed_ : original_;
161         QMatrix m;
162         m.rotate(- params.angle);
163         transformed_ = image.transformed(m);
164         return true;
165 }
166
167
168 bool GuiImage::scale(Params const & params)
169 {
170         QImage const & image = is_transformed_ ? transformed_ : original_;
171
172         unsigned int w = image.width();
173         unsigned int h = image.height();
174
175         // scale only when value > 0
176         if (params.scale > 0) {
177                 w = (w * params.scale) / 100;
178                 h = (h * params.scale) / 100;
179         }
180
181         LYXERR(Debug::GRAPHICS, "\n\tparams.scale  : " << params.scale
182                                  << "\n\twidth         : " << w
183                                  << "\n\theight        : " << h);
184
185         if (w == image.width() && h == image.height())
186                 return false;
187         
188         QMatrix m;
189         m.scale(double(w) / image.width(), double(h) / image.height());
190         transformed_ = image.transformed(m);
191         return true;
192 }
193
194 } // namespace graphics
195 } // lyx