filters
qproimport.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <kdebug.h>
00022 #include <kmessagebox.h>
00023 #include <kgenericfactory.h>
00024 #include <KoFilterChain.h>
00025
00026 #include <kspread_doc.h>
00027 #include <kspread_sheet.h>
00028 #include <kspread_cell.h>
00029 #include <kspread_map.h>
00030
00031 #include <qproimport.h>
00032
00033 #include <qproformula.h>
00034 #include <qpro/stream.h>
00035 #include <qpro/record_factory.h>
00036 #include <qfile.h>
00037
00038 using namespace KSpread;
00039
00040 typedef KGenericFactory<QpImport, KoFilter> QPROImportFactory;
00041 K_EXPORT_COMPONENT_FACTORY( libqproimport, QPROImportFactory( "kofficefilters" ) )
00042
00043
00044
00045 QpTableList::QpTableList()
00046 {
00047 for( int lIdx=0; lIdx<cNameCnt; ++lIdx )
00048 {
00049 cTable[lIdx] = 0;
00050 }
00051 }
00052
00053 QpTableList::~QpTableList()
00054 {
00055
00056 }
00057
00058
00059 void
00060 QpTableList::table(unsigned pIdx, Sheet* pTable)
00061 {
00062 if(pIdx < cNameCnt)
00063 {
00064 cTable[pIdx] = pTable;
00065 }
00066 }
00067
00068 Sheet*
00069 QpTableList::table(unsigned pIdx)
00070 {
00071 return (pIdx < cNameCnt ? cTable[pIdx] : 0);
00072 }
00073
00074
00075
00076
00077 QpImport::QpImport( KoFilter*, const char*, const QStringList& )
00078 : KoFilter()
00079 {
00080
00081 }
00082
00083 void
00084 QpImport::InitTableName(int pIdx, QString& pResult)
00085 {
00086 if( pIdx < 26 )
00087 {
00088 pResult = (char)('A' + pIdx);
00089 }
00090 else
00091 {
00092 pResult = (char)('A' -1 + pIdx / 26);
00093 pResult += (char)('A' + pIdx % 26);
00094 }
00095 }
00096
00097 KoFilter::ConversionStatus QpImport::convert( const QCString& from, const QCString& to )
00098 {
00099 bool bSuccess=true;
00100
00101 KoDocument* document = m_chain->outputDocument();
00102 if ( !document )
00103 return KoFilter::StupidError;
00104
00105 kdDebug(30523) << "here we go... " << document->className() << endl;
00106
00107 if( !::qt_cast<const KSpread::Doc *>( document ) )
00108 {
00109 kdWarning(30501) << "document isn't a KSpread::Doc but a " << document->className() << endl;
00110 return KoFilter::NotImplemented;
00111 }
00112 if(from!="application/x-quattropro" || to!="application/x-kspread")
00113 {
00114 kdWarning(30501) << "Invalid mimetypes " << from << " " << to << endl;
00115 return KoFilter::NotImplemented;
00116 }
00117
00118 kdDebug(30523) << "...still here..." << endl;
00119
00120
00121 Doc *ksdoc=(Doc*)document;
00122
00123 if(ksdoc->mimeType()!="application/x-kspread")
00124 {
00125 kdWarning(30501) << "Invalid document mimetype " << ksdoc->mimeType() << endl;
00126 return KoFilter::NotImplemented;
00127 }
00128
00129 QpIStream lIn( QFile::encodeName(m_chain->inputFile()) );
00130
00131 if( !lIn )
00132 {
00133 KMessageBox::sorry( 0L, i18n("QPRO filter cannot open input file - please report.") );
00134 return KoFilter::FileNotFound;
00135 }
00136
00137 Sheet *table=0;
00138
00139 QString field;
00140 int value=0;
00141 emit sigProgress(value);
00142
00143 QpRecFactory lFactory(lIn);
00144 QpTableList lTableNames;
00145 QP_UINT8 lPageIdx = 0;
00146
00147 QpRec* lRec = 0;
00148 QpRecBop* lRecBop = 0;
00149 QpRecIntegerCell* lRecInt = 0;
00150 QpRecFloatingPointCell* lRecFloat = 0;
00151 QpRecFormulaCell* lRecFormula = 0;
00152 QpRecLabelCell* lRecLabel = 0;
00153 QpRecPageName* lRecPageName = 0;
00154
00155 do
00156 {
00157 field = "";
00158 lRec = lFactory.nextRecord();
00159
00160 switch( lRec->type() )
00161 {
00162 case QpBop:
00163 lRecBop = (QpRecBop*)lRec;
00164 lPageIdx = lRecBop->pageIndex();
00165
00166
00167 table=lTableNames.table(lPageIdx);
00168
00169 if( table == 0 )
00170 {
00171 table=ksdoc->map()->addNewSheet();
00172
00173 table->setSheetName( lTableNames.name(lPageIdx)
00174 , TRUE
00175 );
00176 lTableNames.table(lPageIdx, table);
00177 }
00178 break;
00179
00180 case QpIntegerCell:
00181 lRecInt = (QpRecIntegerCell*)lRec;
00182 field.setNum( lRecInt->integer() );
00183
00184 table->setText( lRecInt->row()+1, ((unsigned)lRecInt->column())+1, field, false );
00185 break;
00186
00187 case QpFormulaCell:
00188 lRecFormula = (QpRecFormulaCell*)lRec;
00189 {
00190 Formula lAnswer(*lRecFormula, lTableNames);
00191
00192 char* lFormula = lAnswer.formula();
00193
00194 field = lFormula;
00195
00196 delete [] lFormula;
00197 }
00198
00199
00200 for(unsigned lIdx=0; lIdx<lTableNames.cNameCnt; ++lIdx)
00201 {
00202 if(lTableNames.allocated(lIdx) && (lTableNames.table(lIdx) == 0) )
00203 {
00204
00205
00206
00207 Sheet* lNewTable=ksdoc->map()->addNewSheet();
00208
00209
00210 lNewTable->setSheetName( lTableNames.name(lIdx)
00211 , TRUE
00212 );
00213 lTableNames.table(lIdx, lNewTable);
00214 }
00215 }
00216
00217 table->setText( lRecFormula->row()+1, lRecFormula->column()+1, field, false );
00218 break;
00219
00220 case QpFloatingPointCell:
00221 lRecFloat = (QpRecFloatingPointCell*)lRec;
00222 field.setNum( lRecFloat->value() );
00223 table->setText( lRecFloat->row()+1, lRecFloat->column()+1, field, false );
00224 break;
00225
00226 case QpLabelCell:
00227 lRecLabel = (QpRecLabelCell*)lRec;
00228 field = "'";
00229 field += lRecLabel->label();
00230 table->setText( lRecLabel->row()+1, lRecLabel->column()+1, field, false );
00231 break;
00232
00233 case QpPageName:
00234 lRecPageName = (QpRecPageName*)lRec;
00235
00236 if( lTableNames.allocated(lPageIdx) && lTableNames.table(lPageIdx) )
00237 {
00238 lTableNames.table(lPageIdx)->setSheetName( lRecPageName->pageName()
00239
00240 );
00241 lTableNames.name(lPageIdx, lRecPageName->pageName());
00242 }
00243 break;
00244
00245 case QpPassword:
00246 KMessageBox::sorry( 0L, i18n("Unable to open password protected files.\n"
00247 "The password algorithm has not been published")
00248 );
00249 return KoFilter::NotImplemented;
00250 }
00251
00252 delete lRec;
00253 lRec = 0;
00254 } while( lIn );
00255
00256 emit sigProgress(100);
00257 if ( bSuccess )
00258 return KoFilter::OK;
00259 else
00260 return KoFilter::StupidError;
00261 }
00262
00263 #include <qproimport.moc>
|