1 | /* |
---|
2 | |
---|
3 | Copyright 2015 Adam Reichold |
---|
4 | |
---|
5 | This file is part of qpdfview. |
---|
6 | |
---|
7 | qpdfview is free software: you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation, either version 2 of the License, or |
---|
10 | (at your option) any later version. |
---|
11 | |
---|
12 | qpdfview is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | |
---|
20 | */ |
---|
21 | |
---|
22 | #include "imagemodel.h" |
---|
23 | |
---|
24 | #include <QDebug> |
---|
25 | #include <QImageWriter> |
---|
26 | |
---|
27 | namespace |
---|
28 | { |
---|
29 | |
---|
30 | using namespace qpdfview; |
---|
31 | using namespace qpdfview::Model; |
---|
32 | |
---|
33 | inline qreal dotsPerInchX(const QImage& image) |
---|
34 | { |
---|
35 | return 0.0254 * image.dotsPerMeterX(); |
---|
36 | } |
---|
37 | |
---|
38 | inline qreal dotsPerInchY(const QImage& image) |
---|
39 | { |
---|
40 | return 0.0254 * image.dotsPerMeterY(); |
---|
41 | } |
---|
42 | |
---|
43 | inline void appendRow(QStandardItemModel* model, const QString& key, const QString& value) |
---|
44 | { |
---|
45 | model->appendRow(QList< QStandardItem* >() << new QStandardItem(key) << new QStandardItem(value)); |
---|
46 | } |
---|
47 | |
---|
48 | } // anonymous |
---|
49 | |
---|
50 | namespace qpdfview |
---|
51 | { |
---|
52 | |
---|
53 | namespace Model |
---|
54 | { |
---|
55 | |
---|
56 | ImagePage::ImagePage(QImage image) : |
---|
57 | m_image(image) |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | QSizeF ImagePage::size() const |
---|
62 | { |
---|
63 | return QSizeF(m_image.width() * 72.0 / dotsPerInchX(m_image), |
---|
64 | m_image.height() * 72.0 / dotsPerInchY(m_image)); |
---|
65 | } |
---|
66 | |
---|
67 | QImage ImagePage::render(qreal horizontalResolution, qreal verticalResolution, Rotation rotation, const QRect& boundingRect) const |
---|
68 | { |
---|
69 | QTransform transform; |
---|
70 | |
---|
71 | transform.scale(horizontalResolution / dotsPerInchX(m_image), |
---|
72 | verticalResolution / dotsPerInchY(m_image)); |
---|
73 | |
---|
74 | switch(rotation) |
---|
75 | { |
---|
76 | default: |
---|
77 | case RotateBy0: |
---|
78 | break; |
---|
79 | case RotateBy90: |
---|
80 | transform.rotate(90.0); |
---|
81 | break; |
---|
82 | case RotateBy180: |
---|
83 | transform.rotate(180.0); |
---|
84 | break; |
---|
85 | case RotateBy270: |
---|
86 | transform.rotate(270.0); |
---|
87 | break; |
---|
88 | } |
---|
89 | |
---|
90 | QImage image = m_image.transformed(transform, Qt::SmoothTransformation); |
---|
91 | |
---|
92 | if(!boundingRect.isNull()) |
---|
93 | { |
---|
94 | image = image.copy(boundingRect); |
---|
95 | } |
---|
96 | |
---|
97 | return image; |
---|
98 | } |
---|
99 | |
---|
100 | ImageDocument::ImageDocument(QImage image) : |
---|
101 | m_image(image) |
---|
102 | { |
---|
103 | } |
---|
104 | |
---|
105 | int ImageDocument::numberOfPages() const |
---|
106 | { |
---|
107 | return 1; |
---|
108 | } |
---|
109 | |
---|
110 | Page* ImageDocument::page(int index) const |
---|
111 | { |
---|
112 | return index == 0 ? new ImagePage(m_image) : 0; |
---|
113 | } |
---|
114 | |
---|
115 | QStringList ImageDocument::saveFilter() const |
---|
116 | { |
---|
117 | QStringList formats; |
---|
118 | |
---|
119 | foreach(const QByteArray& format, QImageWriter::supportedImageFormats()) |
---|
120 | { |
---|
121 | const QString name = QString::fromLocal8Bit(format); |
---|
122 | |
---|
123 | formats.append(QLatin1String("*.") + name); |
---|
124 | } |
---|
125 | |
---|
126 | return QStringList() << tr("Image (%1)").arg(formats.join(QLatin1String(" "))); |
---|
127 | } |
---|
128 | |
---|
129 | bool ImageDocument::canSave() const |
---|
130 | { |
---|
131 | return true; |
---|
132 | } |
---|
133 | |
---|
134 | bool ImageDocument::save(const QString& filePath, bool withChanges) const |
---|
135 | { |
---|
136 | Q_UNUSED(withChanges); |
---|
137 | |
---|
138 | QImageWriter writer(filePath); |
---|
139 | |
---|
140 | if(!writer.write(m_image)) |
---|
141 | { |
---|
142 | qWarning() << writer.errorString(); |
---|
143 | |
---|
144 | return false; |
---|
145 | } |
---|
146 | |
---|
147 | return true; |
---|
148 | } |
---|
149 | |
---|
150 | void ImageDocument::loadProperties(QStandardItemModel *propertiesModel) const |
---|
151 | { |
---|
152 | Document::loadProperties(propertiesModel); |
---|
153 | |
---|
154 | appendRow(propertiesModel, tr("Size"), QString("%1 px x %2 px").arg(m_image.width()).arg(m_image.height())); |
---|
155 | appendRow(propertiesModel, tr("Resolution"), QString("%1 dpi x %2 dpi").arg(dotsPerInchX(m_image), 0, 'f', 1).arg(dotsPerInchY(m_image), 0, 'f', 1)); |
---|
156 | appendRow(propertiesModel, tr("Depth"), QString("%1 bits").arg(m_image.depth())); |
---|
157 | |
---|
158 | switch(m_image.format()) |
---|
159 | { |
---|
160 | default: |
---|
161 | break; |
---|
162 | case QImage::Format_Mono: |
---|
163 | case QImage::Format_MonoLSB: |
---|
164 | appendRow(propertiesModel, tr("Format"), tr("Monochrome")); |
---|
165 | break; |
---|
166 | case QImage::Format_Indexed8: |
---|
167 | appendRow(propertiesModel, tr("Format"), tr("Indexed")); |
---|
168 | break; |
---|
169 | case QImage::Format_RGB32: |
---|
170 | appendRow(propertiesModel, tr("Format"), tr("32 bits RGB")); |
---|
171 | break; |
---|
172 | case QImage::Format_ARGB32: |
---|
173 | appendRow(propertiesModel, tr("Format"), tr("32 bits ARGB")); |
---|
174 | break; |
---|
175 | case QImage::Format_RGB16: |
---|
176 | case QImage::Format_RGB555: |
---|
177 | case QImage::Format_RGB444: |
---|
178 | appendRow(propertiesModel, tr("Format"), tr("16 bits RGB")); |
---|
179 | break; |
---|
180 | case QImage::Format_RGB666: |
---|
181 | case QImage::Format_RGB888: |
---|
182 | appendRow(propertiesModel, tr("Format"), tr("24 bits RGB")); |
---|
183 | break; |
---|
184 | } |
---|
185 | |
---|
186 | foreach(const QString& key, m_image.textKeys()) |
---|
187 | { |
---|
188 | appendRow(propertiesModel, key, m_image.text(key)); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | } // Model |
---|
193 | |
---|
194 | ImagePlugin::ImagePlugin(QObject* parent) : QObject(parent) |
---|
195 | { |
---|
196 | setObjectName("ImagePlugin"); |
---|
197 | } |
---|
198 | |
---|
199 | Model::Document* ImagePlugin::loadDocument(const QString& filePath) const |
---|
200 | { |
---|
201 | QImage image(filePath); |
---|
202 | |
---|
203 | return !image.isNull() ? new Model::ImageDocument(image) : 0; |
---|
204 | } |
---|
205 | |
---|
206 | } // qpdfview |
---|
207 | |
---|
208 | #if QT_VERSION < QT_VERSION_CHECK(5,0,0) |
---|
209 | |
---|
210 | Q_EXPORT_PLUGIN2(qpdfview_image, qpdfview::ImagePlugin) |
---|
211 | |
---|
212 | #endif // QT_VERSION |
---|