filters

KWEFBaseWorker.cc

00001 /*
00002    This file is part of the KDE project
00003    Copyright 2001, 2002, 2003, 2004 Nicolas GOUTTE <goutte@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qbuffer.h>
00022 #include <qimage.h>
00023 
00024 #include <kdebug.h>
00025 
00026 #include <KoPicture.h>
00027 
00028 #include "KWEFStructures.h"
00029 #include "KWEFBaseWorker.h"
00030 #include "KWEFKWordLeader.h"
00031 
00032 void KWEFBaseWorker::registerKWordLeader(KWEFKWordLeader* leader)
00033 {
00034     m_kwordLeader=leader;
00035 }
00036 
00037 //
00038 // At first, define all methods that do something real!
00039 //
00040 
00041 bool KWEFBaseWorker::doAbortFile(void)
00042 {
00043     // Mostly, aborting is the same than closing the file!
00044     return doCloseFile();
00045 }
00046 
00047 bool KWEFBaseWorker::doFullDocument (const QValueList<ParaData>& paraList)
00048 {
00049     if (!doOpenTextFrameSet())
00050         return false;
00051     if (!doFullAllParagraphs(paraList))
00052         return false;
00053     if (!doCloseTextFrameSet())
00054         return false;
00055 
00056     return true;
00057 }
00058 
00059 bool KWEFBaseWorker::doFullAllParagraphs (const QValueList<ParaData>& paraList)
00060 {
00061     QValueList<ParaData>::ConstIterator it;
00062     QValueList<ParaData>::ConstIterator end(paraList.end());
00063     for (it=paraList.begin();it!=end;++it)
00064     {
00065         if (!doFullParagraph((*it).text,(*it).layout,(*it).formattingList))
00066             return false;
00067     }
00068     return true;
00069 }
00070 
00071 bool KWEFBaseWorker::loadSubFile(const QString& fileName, QByteArray& array) const
00072 // return value:
00073 //   true if the file is not empty
00074 //   false if the file is empty or if an error occurred
00075 {
00076     bool flag=false;
00077     if (m_kwordLeader)
00078     {
00079         flag=m_kwordLeader->loadSubFile(fileName,array);
00080     }
00081     else
00082     {
00083         kdWarning(30508) << "Leader is unknown! (KWEFBaseWorker::loadSubFile)" << endl;
00084     }
00085     return flag;
00086 }
00087 
00088 QIODevice* KWEFBaseWorker::getSubFileDevice(const QString& fileName) const
00089 {
00090     if (!m_kwordLeader)
00091     {
00092         kdWarning(30508) << "Leader is unknown! (KWEFBaseWorker::getSubFileDevice)" << endl;
00093         return NULL;
00094     }
00095     return m_kwordLeader->getSubFileDevice(fileName);
00096 }
00097 
00098 QImage KWEFBaseWorker::loadAndConvertToImage(const QString& strName, const QString& inExtension) const
00099 {
00100     QIODevice* io=getSubFileDevice(strName);
00101     if (!io)
00102     {
00103         // NO message error, as there must be already one
00104         return QImage();
00105     }
00106 
00107     kdDebug(30508) << "Picture " << strName << " has size: " << io->size() << endl;
00108     
00109     KoPicture picture;
00110     if (!picture.load(io, inExtension)) // we do not care about KoPictureKey
00111     {
00112         kdWarning(30508) << "Could not read picture: " << strName << " (KWEFBaseWorker::loadAndConvertToImage)" << endl;
00113         return QImage();
00114     }
00115     
00116     return picture.generateImage(picture.getOriginalSize()); // ### TODO: KoPicture::getOriginalSize is bad for cliparts
00117 }
00118 
00119 bool KWEFBaseWorker::loadAndConvertToImage(const QString& strName, const QString& inExtension, const QString& outExtension, QByteArray& image) const
00120 {
00121     QImage qimage(loadAndConvertToImage(strName,inExtension));
00122     
00123     if (qimage.isNull())
00124     {
00125         kdWarning(30508) << "Could not load image (KWEFBaseWorker::loadAndConvertToImage)" <<endl;
00126         return false;
00127     }
00128     
00129     QImageIO imageIO;
00130     imageIO.setImage(qimage);
00131 
00132     QBuffer buffer(image); // A QBuffer is a QIODevice
00133     if (!buffer.open(IO_WriteOnly))
00134     {
00135         kdWarning(30508) << "Could not open buffer! (KWEFBaseWorker::loadAndConvertToImage)" << endl;
00136         return false;
00137     }
00138 
00139     imageIO.setIODevice(&buffer);
00140     imageIO.setFormat(outExtension.utf8());
00141 
00142     if (!imageIO.write())
00143     {
00144         kdWarning(30508) << "Could not write converted image! (KWEFBaseWorker::loadAndConvertToImage)" << endl;
00145         return false;
00146     }
00147     buffer.close();
00148 
00149     return true;
00150 }
00151 
00152 
00153 //
00154 // Secondly, define all methods returning false
00155 //
00156 
00157 #define DO_FALSE_DEFINITION(string) \
00158     bool KWEFBaseWorker::string \
00159     {\
00160         kdWarning(30508) << "KWEFBaseWorker::" << #string << " was called (Worker not correctly defined?)" << endl; \
00161         return false;\
00162     }
00163 
00164 DO_FALSE_DEFINITION (doOpenFile (const QString& , const QString& ))
00165 DO_FALSE_DEFINITION (doCloseFile (void))
00166 DO_FALSE_DEFINITION (doOpenDocument (void))
00167 DO_FALSE_DEFINITION (doCloseDocument (void))
00168 
00169 // The following is not generated by the leader
00170 DO_FALSE_DEFINITION (doFullParagraph(const QString&, const LayoutData&, const ValueListFormatData&))
00171 
00172 //
00173 // Thirdly, define all methods returning true
00174 //
00175 
00176 #define DO_TRUE_DEFINITION(string) \
00177     bool KWEFBaseWorker::string \
00178     {\
00179         return true;\
00180     }
00181 
00182 DO_TRUE_DEFINITION (doFullDocumentInfo (const KWEFDocumentInfo&))
00183 DO_TRUE_DEFINITION (doVariableSettings (const VariableSettingsData &))
00184 DO_TRUE_DEFINITION (doFullPaperFormat (const int, const double, const double, const int))
00185 DO_TRUE_DEFINITION (doFullPaperBorders (const double, const double, const double, const double))
00186 DO_TRUE_DEFINITION (doFullPaperFormatOther ( const int, const double, const int ) )
00187 DO_TRUE_DEFINITION (doPageInfo(int,int))
00188 DO_TRUE_DEFINITION (doOpenHead (void))
00189 DO_TRUE_DEFINITION (doCloseHead (void))
00190 DO_TRUE_DEFINITION (doOpenBody (void))
00191 DO_TRUE_DEFINITION (doCloseBody (void))
00192 DO_TRUE_DEFINITION (doOpenStyles (void))
00193 DO_TRUE_DEFINITION (doCloseStyles (void))
00194 DO_TRUE_DEFINITION (doFullDefineStyle (LayoutData&))
00195 DO_TRUE_DEFINITION (doOpenSpellCheckIgnoreList (void))
00196 DO_TRUE_DEFINITION (doCloseSpellCheckIgnoreList (void))
00197 DO_TRUE_DEFINITION (doFullSpellCheckIgnoreWord (const QString&))
00198 DO_TRUE_DEFINITION (doHeader(const HeaderData&))
00199 DO_TRUE_DEFINITION (doFooter(const FooterData&))
00200 DO_TRUE_DEFINITION ( doDeclareNonInlinedFramesets( QValueList<FrameAnchor>&, QValueList<FrameAnchor>& ) )
00201 
00202 //  The following are not generated by the leader
00203 DO_TRUE_DEFINITION (doOpenTextFrameSet (void))
00204 DO_TRUE_DEFINITION (doCloseTextFrameSet (void))
KDE Home | KDE Accessibility Home | Description of Access Keys