KWEFBaseWorker.cpp
1/*
2 This file is part of the KDE project
3 Copyright 2001, 2002, 2003, 2004 Nicolas GOUTTE <goutte@kde.org>
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 <tqbuffer.h>
22#include <tqimage.h>
23
24#include <kdebug.h>
25
26#include <KoPicture.h>
27
28#include "KWEFStructures.h"
29#include "KWEFBaseWorker.h"
30#include "KWEFKWordLeader.h"
31
32void KWEFBaseWorker::registerKWordLeader(KWEFKWordLeader* leader)
33{
34 m_kwordLeader=leader;
35}
36
37//
38// At first, define all methods that do something real!
39//
40
41bool KWEFBaseWorker::doAbortFile(void)
42{
43 // Mostly, aborting is the same than closing the file!
44 return doCloseFile();
45}
46
47bool KWEFBaseWorker::doFullDocument (const TQValueList<ParaData>& paraList)
48{
49 if (!doOpenTextFrameSet())
50 return false;
51 if (!doFullAllParagraphs(paraList))
52 return false;
53 if (!doCloseTextFrameSet())
54 return false;
55
56 return true;
57}
58
59bool KWEFBaseWorker::doFullAllParagraphs (const TQValueList<ParaData>& paraList)
60{
61 TQValueList<ParaData>::ConstIterator it;
62 TQValueList<ParaData>::ConstIterator end(paraList.end());
63 for (it=paraList.begin();it!=end;++it)
64 {
65 if (!doFullParagraph((*it).text,(*it).layout,(*it).formattingList))
66 return false;
67 }
68 return true;
69}
70
71bool KWEFBaseWorker::loadSubFile(const TQString& fileName, TQByteArray& array) const
72// return value:
73// true if the file is not empty
74// false if the file is empty or if an error occurred
75{
76 bool flag=false;
77 if (m_kwordLeader)
78 {
79 flag=m_kwordLeader->loadSubFile(fileName,array);
80 }
81 else
82 {
83 kdWarning(30508) << "Leader is unknown! (KWEFBaseWorker::loadSubFile)" << endl;
84 }
85 return flag;
86}
87
88TQIODevice* KWEFBaseWorker::getSubFileDevice(const TQString& fileName) const
89{
90 if (!m_kwordLeader)
91 {
92 kdWarning(30508) << "Leader is unknown! (KWEFBaseWorker::getSubFileDevice)" << endl;
93 return NULL;
94 }
95 return m_kwordLeader->getSubFileDevice(fileName);
96}
97
98TQImage KWEFBaseWorker::loadAndConvertToImage(const TQString& strName, const TQString& inExtension) const
99{
100 TQIODevice* io=getSubFileDevice(strName);
101 if (!io)
102 {
103 // NO message error, as there must be already one
104 return TQImage();
105 }
106
107 kdDebug(30508) << "Picture " << strName << " has size: " << io->size() << endl;
108
109 KoPicture picture;
110 if (!picture.load(io, inExtension)) // we do not care about KoPictureKey
111 {
112 kdWarning(30508) << "Could not read picture: " << strName << " (KWEFBaseWorker::loadAndConvertToImage)" << endl;
113 return TQImage();
114 }
115
116 return picture.generateImage(picture.getOriginalSize()); // ### TODO: KoPicture::getOriginalSize is bad for cliparts
117}
118
119bool KWEFBaseWorker::loadAndConvertToImage(const TQString& strName, const TQString& inExtension, const TQString& outExtension, TQByteArray& image) const
120{
121 TQImage qimage(loadAndConvertToImage(strName,inExtension));
122
123 if (qimage.isNull())
124 {
125 kdWarning(30508) << "Could not load image (KWEFBaseWorker::loadAndConvertToImage)" <<endl;
126 return false;
127 }
128
129 TQImageIO imageIO;
130 imageIO.setImage(qimage);
131
132 TQBuffer buffer(image); // A TQBuffer is a TQIODevice
133 if (!buffer.open(IO_WriteOnly))
134 {
135 kdWarning(30508) << "Could not open buffer! (KWEFBaseWorker::loadAndConvertToImage)" << endl;
136 return false;
137 }
138
139 imageIO.setIODevice(&buffer);
140 imageIO.setFormat(outExtension.utf8());
141
142 if (!imageIO.write())
143 {
144 kdWarning(30508) << "Could not write converted image! (KWEFBaseWorker::loadAndConvertToImage)" << endl;
145 return false;
146 }
147 buffer.close();
148
149 return true;
150}
151
152
153//
154// Secondly, define all methods returning false
155//
156
157#define DO_FALSE_DEFINITION(string) \
158 bool KWEFBaseWorker::string \
159 {\
160 kdWarning(30508) << "KWEFBaseWorker::" << #string << " was called (Worker not correctly defined?)" << endl; \
161 return false;\
162 }
163
164DO_FALSE_DEFINITION (doOpenFile (const TQString& , const TQString& ))
165DO_FALSE_DEFINITION (doCloseFile (void))
166DO_FALSE_DEFINITION (doOpenDocument (void))
167DO_FALSE_DEFINITION (doCloseDocument (void))
168
169// The following is not generated by the leader
170DO_FALSE_DEFINITION (doFullParagraph(const TQString&, const LayoutData&, const ValueListFormatData&))
171
172//
173// Thirdly, define all methods returning true
174//
175
176#define DO_TRUE_DEFINITION(string) \
177 bool KWEFBaseWorker::string \
178 {\
179 return true;\
180 }
181
182DO_TRUE_DEFINITION (doFullDocumentInfo (const KWEFDocumentInfo&))
183DO_TRUE_DEFINITION (doVariableSettings (const VariableSettingsData &))
184DO_TRUE_DEFINITION (doFullPaperFormat (const int, const double, const double, const int))
185DO_TRUE_DEFINITION (doFullPaperBorders (const double, const double, const double, const double))
186DO_TRUE_DEFINITION (doFullPaperFormatOther ( const int, const double, const int ) )
187DO_TRUE_DEFINITION (doPageInfo(int,int))
188DO_TRUE_DEFINITION (doOpenHead (void))
189DO_TRUE_DEFINITION (doCloseHead (void))
190DO_TRUE_DEFINITION (doOpenBody (void))
191DO_TRUE_DEFINITION (doCloseBody (void))
192DO_TRUE_DEFINITION (doOpenStyles (void))
193DO_TRUE_DEFINITION (doCloseStyles (void))
194DO_TRUE_DEFINITION (doFullDefineStyle (LayoutData&))
195DO_TRUE_DEFINITION (doOpenSpellCheckIgnoreList (void))
196DO_TRUE_DEFINITION (doCloseSpellCheckIgnoreList (void))
197DO_TRUE_DEFINITION (doFullSpellCheckIgnoreWord (const TQString&))
198DO_TRUE_DEFINITION (doHeader(const HeaderData&))
199DO_TRUE_DEFINITION (doFooter(const FooterData&))
200DO_TRUE_DEFINITION ( doDeclareNonInlinedFramesets( TQValueList<FrameAnchor>&, TQValueList<FrameAnchor>& ) )
201
202// The following are not generated by the leader
203DO_TRUE_DEFINITION (doOpenTextFrameSet (void))
204DO_TRUE_DEFINITION (doCloseTextFrameSet (void))
Paragraph layout.
see <VARIABLESETTINGS> in the KWord DTD