]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiImage.cpp
General cleanup of the Outline/Navigator:
[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         if (!original_.load(toqstr(filename.absFilename()))) {
74                 LYXERR(Debug::GRAPHICS, "Unable to open image");
75                 return false;
76         }
77         return true;
78 }
79
80
81 bool GuiImage::setPixmap(Params const & params)
82 {
83         if (original_.isNull() || !params.display)
84                 return false;
85
86         is_transformed_ = clip(params);
87         is_transformed_ |= rotate(params);
88         is_transformed_ |= scale(params);
89
90         if (!is_transformed_)
91                 // Clear it out to save some memory.
92                 transformed_ = QPixmap();
93
94         return true;
95 }
96
97
98 bool GuiImage::clip(Params const & params)
99 {
100         if (params.bb.empty())
101                 // No clipping is necessary.
102                 return false;
103
104         int const new_width  = params.bb.xr - params.bb.xl;
105         int const new_height = params.bb.yt - params.bb.yb;
106
107         // No need to check if the width, height are > 0 because the
108         // Bounding Box would be empty() in this case.
109         if (new_width > original_.width() || new_height > original_.height()) {
110                 // Bounds are invalid.
111                 return false;
112         }
113
114         if (new_width == original_.width() && new_height == original_.height())
115                 return false;
116
117         int const xoffset_l = params.bb.xl;
118         int const yoffset_t = (original_.height() > int(params.bb.yt) ?
119                                original_.height() - params.bb.yt : 0);
120
121         transformed_ = original_.copy(xoffset_l, yoffset_t,
122                                       new_width, new_height);
123         return true;
124 }
125
126
127 bool GuiImage::rotate(Params const & params)
128 {
129         if (!params.angle)
130                 return false;
131
132         if (!is_transformed_)
133                 transformed_ = original_;
134
135         QMatrix m;
136         m.rotate(-params.angle);
137         transformed_ = transformed_.transformed(m);
138         return true;
139 }
140
141
142 bool GuiImage::scale(Params const & params)
143 {
144         Dimension dim = scaledDimension(params);
145
146         if (dim.width() == width() && dim.height() == height())
147                 return false;
148
149         if (!is_transformed_)
150                 transformed_ = original_;
151
152         QMatrix m;
153         m.scale(double(dim.width()) / width(), double(dim.height()) / height());
154         transformed_ = transformed_.transformed(m);
155
156         return true;
157 }
158
159 } // namespace graphics
160 } // lyx