source: visorpdf/sources/psmodel.cpp @ 1c100ae

Last change on this file since 1c100ae was 1c100ae, checked in by Pedro Buitrago <pbuitrago@…>, 8 years ago

cambio el nombre de murrupuy por terepayma

  • Property mode set to 100644
File size: 8.4 KB
Line 
1/*
2
3Copyright 2013 Alexander Volkov
4Copyright 2013 Adam Reichold
5
6This file is part of qpdfview.
7
8qpdfview is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 2 of the License, or
11(at your option) any later version.
12
13qpdfview is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
20
21*/
22
23#include "psmodel.h"
24
25#include <QFile>
26#include <QFormLayout>
27#include <qmath.h>
28#include <QSettings>
29#include <QSpinBox>
30
31#include <libspectre/spectre-document.h>
32
33namespace
34{
35
36using namespace qpdfview;
37using namespace qpdfview::Model;
38
39inline void appendRow(QStandardItemModel* model, const QString& key, const QString& value)
40{
41    model->appendRow(QList< QStandardItem* >() << new QStandardItem(key) << new QStandardItem(value));
42}
43
44namespace Defaults
45{
46
47const int graphicsAntialiasBits = 4;
48const int textAntialiasBits = 2;
49
50} // Defaults
51
52} // anonymous
53
54namespace qpdfview
55{
56
57namespace Model
58{
59
60PsPage::PsPage(QMutex* mutex, SpectrePage* page, SpectreRenderContext* renderContext) :
61    m_mutex(mutex),
62    m_page(page),
63    m_renderContext(renderContext)
64{
65}
66
67PsPage::~PsPage()
68{
69    spectre_page_free(m_page);
70    m_page = 0;
71}
72
73QSizeF PsPage::size() const
74{
75    QMutexLocker mutexLocker(m_mutex);
76
77    int w;
78    int h;
79
80    spectre_page_get_size(m_page, &w, &h);
81
82    return QSizeF(w, h);
83}
84
85QImage PsPage::render(qreal horizontalResolution, qreal verticalResolution, Rotation rotation, const QRect& boundingRect) const
86{
87    QMutexLocker mutexLocker(m_mutex);
88
89    double xscale;
90    double yscale;
91
92    switch(rotation)
93    {
94    default:
95    case RotateBy0:
96    case RotateBy180:
97        xscale = horizontalResolution / 72.0;
98        yscale = verticalResolution / 72.0;
99        break;
100    case RotateBy90:
101    case RotateBy270:
102        xscale = verticalResolution / 72.0;
103        yscale = horizontalResolution / 72.0;
104        break;
105    }
106
107    spectre_render_context_set_scale(m_renderContext, xscale, yscale);
108
109    switch(rotation)
110    {
111    default:
112    case RotateBy0:
113        spectre_render_context_set_rotation(m_renderContext, 0);
114        break;
115    case RotateBy90:
116        spectre_render_context_set_rotation(m_renderContext, 90);
117        break;
118    case RotateBy180:
119        spectre_render_context_set_rotation(m_renderContext, 180);
120        break;
121    case RotateBy270:
122        spectre_render_context_set_rotation(m_renderContext, 270);
123        break;
124    }
125
126    int w;
127    int h;
128
129    spectre_page_get_size(m_page, &w, &h);
130
131    w = qRound(w * xscale);
132    h = qRound(h * yscale);
133
134    if(rotation == RotateBy90 || rotation == RotateBy270)
135    {
136        qSwap(w, h);
137    }
138
139    unsigned char* pageData = 0;
140    int rowLength = 0;
141
142    spectre_page_render(m_page, m_renderContext, &pageData, &rowLength);
143
144    if (spectre_page_status(m_page) != SPECTRE_STATUS_SUCCESS)
145    {
146        free(pageData);
147        pageData = 0;
148
149        return QImage();
150    }
151
152    QImage auxiliaryImage(pageData, rowLength / 4, h, QImage::Format_RGB32);
153    QImage image(boundingRect.isNull() ? auxiliaryImage.copy(0, 0, w, h) : auxiliaryImage.copy(boundingRect));
154
155    free(pageData);
156    pageData = 0;
157
158    return image;
159}
160
161PsDocument::PsDocument(SpectreDocument* document, SpectreRenderContext* renderContext) :
162    m_mutex(),
163    m_document(document),
164    m_renderContext(renderContext)
165{
166}
167
168PsDocument::~PsDocument()
169{
170    spectre_render_context_free(m_renderContext);
171    m_renderContext = 0;
172
173    spectre_document_free(m_document);
174    m_document = 0;
175}
176
177int PsDocument::numberOfPages() const
178{
179    QMutexLocker mutexLocker(&m_mutex);
180
181    return spectre_document_get_n_pages(m_document);
182}
183
184Page* PsDocument::page(int index) const
185{
186    QMutexLocker mutexLocker(&m_mutex);
187
188    SpectrePage* page = spectre_document_get_page(m_document, index);
189
190    return page != 0 ? new PsPage(&m_mutex, page, m_renderContext) : 0;
191}
192
193QStringList PsDocument::saveFilter() const
194{
195    QMutexLocker mutexLocker(&m_mutex);
196
197    if(spectre_document_is_eps(m_document))
198    {
199        return QStringList() << QLatin1String("Encapsulated PostScript (*.eps)");
200    }
201    else
202    {
203        return QStringList() << QLatin1String("PostScript (*.ps)");
204    }
205}
206
207bool PsDocument::canSave() const
208{
209    return true;
210}
211
212bool PsDocument::save(const QString& filePath, bool withChanges) const
213{
214    Q_UNUSED(withChanges)
215
216    QMutexLocker mutexLocker(&m_mutex);
217
218    spectre_document_save(m_document, QFile::encodeName(filePath));
219
220    return spectre_document_status(m_document) == SPECTRE_STATUS_SUCCESS;
221}
222
223bool PsDocument::canBePrintedUsingCUPS() const
224{
225    return true;
226}
227
228void PsDocument::loadProperties(QStandardItemModel* propertiesModel) const
229{
230    Document::loadProperties(propertiesModel);
231
232    QMutexLocker mutexLocker(&m_mutex);
233
234    const QString title = QString::fromLocal8Bit(spectre_document_get_title(m_document));
235    const QString createdFor = QString::fromLocal8Bit(spectre_document_get_for(m_document));
236    const QString creator = QString::fromLocal8Bit(spectre_document_get_creator(m_document));
237    const QString creationDate = QString::fromLocal8Bit(spectre_document_get_creation_date(m_document));
238    const QString format = QString::fromLocal8Bit(spectre_document_get_format(m_document));
239    const QString languageLevel = QString::number(spectre_document_get_language_level(m_document));
240
241    appendRow(propertiesModel, tr("Title"), title);
242    appendRow(propertiesModel, tr("Created for"), createdFor);
243    appendRow(propertiesModel, tr("Creator"), creator);
244    appendRow(propertiesModel, tr("Creation date"), creationDate);
245    appendRow(propertiesModel, tr("Format"), format);
246    appendRow(propertiesModel, tr("Language level"), languageLevel);
247}
248
249} // Model
250
251PsSettingsWidget::PsSettingsWidget(QSettings* settings, QWidget* parent) : SettingsWidget(parent),
252    m_settings(settings)
253{
254    m_layout = new QFormLayout(this);
255
256    // graphics antialias bits
257
258    m_graphicsAntialiasBitsSpinBox = new QSpinBox(this);
259    m_graphicsAntialiasBitsSpinBox->setRange(1, 4);
260    m_graphicsAntialiasBitsSpinBox->setValue(m_settings->value("graphicsAntialiasBits", Defaults::graphicsAntialiasBits).toInt());
261
262    m_layout->addRow(tr("Graphics antialias bits:"), m_graphicsAntialiasBitsSpinBox);
263
264    // text antialias bits
265
266    m_textAntialiasBitsSpinBox = new QSpinBox(this);
267    m_textAntialiasBitsSpinBox->setRange(1, 2);
268    m_textAntialiasBitsSpinBox->setValue(m_settings->value("textAntialiasBits", Defaults::textAntialiasBits).toInt());
269
270    m_layout->addRow(tr("Text antialias bits:"), m_textAntialiasBitsSpinBox);
271}
272
273void PsSettingsWidget::accept()
274{
275    m_settings->setValue("graphicsAntialiasBits", m_graphicsAntialiasBitsSpinBox->value());
276    m_settings->setValue("textAntialiasBits", m_textAntialiasBitsSpinBox->value());
277}
278
279void PsSettingsWidget::reset()
280{
281    m_graphicsAntialiasBitsSpinBox->setValue(Defaults::graphicsAntialiasBits);
282    m_textAntialiasBitsSpinBox->setValue(Defaults::textAntialiasBits);
283}
284
285PsPlugin::PsPlugin(QObject* parent) : QObject(parent)
286{
287    setObjectName("PsPlugin");
288
289//    m_settings = new QSettings("qpdfview", "ps-plugin", this);
290    m_settings = new QSettings("terepayma", "ps-plugin", this);
291
292}
293
294Model::Document* PsPlugin::loadDocument(const QString& filePath) const
295{
296    SpectreDocument* document = spectre_document_new();
297
298    spectre_document_load(document, QFile::encodeName(filePath));
299
300    if (spectre_document_status(document) != SPECTRE_STATUS_SUCCESS)
301    {
302        spectre_document_free(document);
303
304        return 0;
305    }
306
307    SpectreRenderContext* renderContext = spectre_render_context_new();
308
309    spectre_render_context_set_antialias_bits(renderContext,
310                                              m_settings->value("graphicsAntialiasBits", Defaults::graphicsAntialiasBits).toInt(),
311                                              m_settings->value("textAntialiasBits", Defaults::textAntialiasBits).toInt());
312
313    return new Model::PsDocument(document, renderContext);
314}
315
316SettingsWidget* PsPlugin::createSettingsWidget(QWidget* parent) const
317{
318    return new PsSettingsWidget(m_settings, parent);
319}
320
321} // qpdfview
322
323#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
324
325Q_EXPORT_PLUGIN2(qpdfview_ps, qpdfview::PsPlugin)
326
327#endif // QT_VERSION
Note: See TracBrowser for help on using the repository browser.