]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiImage.cpp
better naming for encoding default, as discussed in bug 4971.
[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 <QImage>
27 #include <QImageReader>
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33 namespace graphics {
34
35 /// Access to this class is through this static method.
36 Image * GuiImage::newImage()
37 {
38         return new GuiImage;
39 }
40
41
42 GuiImage::GuiImage(GuiImage const & other)
43         : Image(other), original_(other.original_),
44           transformed_(other.transformed_), is_transformed_(other.is_transformed_)
45 {}
46
47
48 Image * GuiImage::clone() const
49 {
50         return new GuiImage(*this);
51 }
52
53
54 unsigned int GuiImage::width() const
55 {
56         return is_transformed_ ? transformed_.width() : original_.width();
57 }
58
59
60 unsigned int GuiImage::height() const
61 {
62         return is_transformed_ ? transformed_.height() : original_.height();
63 }
64
65
66 bool GuiImage::load(FileName const & filename)
67 {
68         if (!original_.isNull()) {
69                 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
70                 return false;
71         }
72
73         fname_ = toqstr(filename.absFilename());
74
75         if (!original_.load(fname_)) {
76                 LYXERR(Debug::GRAPHICS, "Unable to open image");
77                 return false;
78         }
79         return true;
80 }
81
82
83 bool GuiImage::setPixmap(Params const & params)
84 {
85         if (!params.display)
86                 return false;
87
88         if (original_.isNull()) {
89                 if (!original_.load(fname_))
90                         return false;
91         }
92                 
93         is_transformed_ = clip(params);
94         is_transformed_ |= rotate(params);
95         is_transformed_ |= scale(params);
96
97         // Clear the pixmap to save some memory.
98         if (is_transformed_)
99                 original_ = QPixmap();
100         else
101                 transformed_ = QPixmap();
102
103         return true;
104 }
105
106
107 bool GuiImage::clip(Params const & params)
108 {
109         if (params.bb.empty())
110                 // No clipping is necessary.
111                 return false;
112
113         int const new_width  = params.bb.xr - params.bb.xl;
114         int const new_height = params.bb.yt - params.bb.yb;
115
116         // No need to check if the width, height are > 0 because the
117         // Bounding Box would be empty() in this case.
118         if (new_width > original_.width() || new_height > original_.height()) {
119                 // Bounds are invalid.
120                 return false;
121         }
122
123         if (new_width == original_.width() && new_height == original_.height())
124                 return false;
125
126         int const xoffset_l = params.bb.xl;
127         int const yoffset_t = (original_.height() > int(params.bb.yt) ?
128                                original_.height() - params.bb.yt : 0);
129
130         transformed_ = original_.copy(xoffset_l, yoffset_t,
131                                       new_width, new_height);
132         return true;
133 }
134
135
136 bool GuiImage::rotate(Params const & params)
137 {
138         if (!params.angle)
139                 return false;
140
141         if (!is_transformed_)
142                 transformed_ = original_;
143
144         QMatrix m;
145         m.rotate(-params.angle);
146         transformed_ = transformed_.transformed(m);
147         return true;
148 }
149
150
151 bool GuiImage::scale(Params const & params)
152 {
153         Dimension dim = scaledDimension(params);
154
155         if (dim.width() == width() && dim.height() == height())
156                 return false;
157
158         if (!is_transformed_)
159                 transformed_ = original_;
160
161         QMatrix m;
162         m.scale(double(dim.width()) / width(), double(dim.height()) / height());
163         transformed_ = transformed_.transformed(m);
164
165         return true;
166 }
167
168 } // namespace graphics
169 } // lyx