filters

kivio_imageexport.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005-2006 Peter Simonsson <psn@linux.se>
3 Copyright (C) 2006 Kåre Särs <kare.sars@kolumbus.fi>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "kivio_imageexport.h"
22
23#include <tqdom.h>
24#include <tqcstring.h>
25#include <tqpixmap.h>
26#include <tqpainter.h>
27#include <tqsize.h>
28#include <tqstringlist.h>
29
30#include <tdemessagebox.h>
31#include <tdelocale.h>
32#include <kgenericfactory.h>
33#include <kdebug.h>
34
35#include <KoStore.h>
36#include <KoFilterChain.h>
37#include <KoZoomHandler.h>
38
39#include "kivio_doc.h"
40#include "kivio_page.h"
41#include "kivio_map.h"
42#include "kivio_screen_painter.h"
43
44#include "kivio_imageexportdialog.h"
45
46typedef KGenericFactory<Kivio::ImageExport, KoFilter> KivioImageExportFactory;
47K_EXPORT_COMPONENT_FACTORY( libkivioimageexport, KivioImageExportFactory("KivioImageExport") )
48
49namespace Kivio
50{
51
52ImageExport::ImageExport(KoFilter *, const char *, const TQStringList&)
53 : KoFilter()
54{
55 TDEGlobal::locale()->insertCatalogue("kofficefilters");
56}
57
58KoFilter::ConversionStatus ImageExport::convert(const TQCString& from, const TQCString& to)
59{
60 if(from != "application/x-kivio") {
61 return KoFilter::BadMimeType;
62 }
63
64 TQString format;
65
66 if(to == "image/png") {
67 format = "PNG";
68 } else if(to == "image/jpeg") {
69 format = "JPEG";
70 } else if(to == "image/x-bmp") {
71 format = "BMP";
72 } else if(to == "image/x-eps") {
73 format = "EPS";
74 } else if(to == "image/x-portable-bitmap") {
75 format = "PBM";
76 } else if(to == "image/x-pcx") {
77 format = "PCX";
78 } else if(to == "image/x-portable-pixmap") {
79 format = "PPM";
80 } else if(to == "image/x-rgb") {
81 format = "RGB";
82 } else if(to == "image/x-xpm") {
83 format = "XPM";
84 } else if(to == "image/jp2") {
85 format = "JP2";
86 } else {
87 return KoFilter::BadMimeType;
88 }
89
90 KoStoreDevice* storeIn = m_chain->storageFile("root", KoStore::Read);
91
92 if (!storeIn) {
93 KMessageBox::error(0, i18n("Failed to read data."), i18n( "Export Error" ));
94 return KoFilter::FileNotFound;
95 }
96
97 // Get the XML tree.
98 TQDomDocument domIn;
99 domIn.setContent(storeIn);
100
101 KivioDoc doc;
102
103 if(!doc.loadXML(0, domIn)) {
104 KMessageBox::error(0, i18n("Malformed XML data."), i18n("Export Error"));
105 return KoFilter::WrongFormat;
106 }
107
108 ImageExportDialog dlg;
109
110 TQStringList pageNames;
111 TQPtrList<KivioPage> pageList = doc.map()->pageList();
112 TQPtrListIterator<KivioPage> it(pageList);
113
114 for(; it.current() != 0; ++it) {
115 pageNames.append(it.current()->pageName());
116 }
117
118 dlg.setPageList(pageNames);
119
120 dlg.setInitialDPI(300);
121 dlg.setInitialmargin(10);
122
123 if(dlg.exec() != TQDialog::Accepted) {
124 return KoFilter::UserCancelled;
125 }
126
127 KivioPage* page = doc.map()->findPage(dlg.selectedPage());
128
129 if(!page) {
130 kdDebug() << "The page named " << dlg.selectedPage() << " wasn't found!!" << endl;
131 return KoFilter::InternalError;
132 }
133
134 float z = (float)dlg.imageDPI()/(float)KoGlobal::dpiX();
135 KoZoomHandler zoom;
136 zoom.setZoomAndResolution(tqRound(z * 100), KoGlobal::dpiX(), KoGlobal::dpiY());
137
138 TQSize size;
139 if(dlg.usePageBorders()) {
140 size = TQSize(zoom.zoomItX(page->paperLayout().ptWidth), zoom.zoomItY(page->paperLayout().ptHeight));
141 } else {
142 size = zoom.zoomSize(page->getRectForAllStencils().size());
143 }
144
145 kdDebug() << "KoGlobal::dpiX() " << KoGlobal::dpiX() << " KoGlobal::dpiY() " << KoGlobal::dpiY() << endl;
146
147 int border = dlg.margin();
148
149 size.setWidth(size.width() + (border * 2));
150 size.setHeight(size.height() + (border * 2));
151
152 TQPixmap pixmap = TQPixmap(size);
153 pixmap.fill(TQt::white);
154 KivioScreenPainter kpainter;
155 kpainter.start(&pixmap);
156
157 float translationX = border;
158 float translationY = border;
159
160 if(!dlg.usePageBorders()) {
161 TQPoint point = zoom.zoomPoint(page->getRectForAllStencils().topLeft());
162 translationX -= point.x();
163 translationY -= point.y();
164 }
165
166 kpainter.setTranslation(translationX, translationY);
167 page->printContent(kpainter, &zoom);
168
169 if(!pixmap.save(m_chain->outputFile(), format.local8Bit())) {
170 return KoFilter::CreationError;
171 }
172
173 return KoFilter::OK;
174}
175
176}
177
178#include "kivio_imageexport.moc"