warnings for parser errors in odf editor
authorKonrad Rosenbaum <konrad@silmor.de>
Wed, 29 Feb 2012 20:28:10 +0000 (21:28 +0100)
committerKonrad Rosenbaum <konrad@silmor.de>
Wed, 29 Feb 2012 20:28:10 +0000 (21:28 +0100)
src/templates/odfedit.cpp
src/templates/odfedit.h
src/templates/odtrender.cpp
src/templates/odtrender.h

index 0fceebb..a6ce47d 100644 (file)
@@ -78,6 +78,7 @@ class DPTR_CLASS_NAME(MOdfEditor)
                QTextEdit*mText,*mComment;
                QTableView*mTagAttrs;
                QStandardItemModel*mTagAttrModel;
+               bool mChanged;
                //tree menu
                QAction*maAddIntoCalc,*maWrapInCond,*maAddIntoComment,*maInsBehindElse,*maWrapInLoop;
                QAction*maInsBehindCalc,*maInsBehindComment,*maUnwrap,*maDelItem;
@@ -95,6 +96,7 @@ class DPTR_CLASS_NAME(MOdfEditor)
                        File(QString n,Type t,const QByteArray&c):name(n),type(t),content(c){}
                };
                QList<File> mFiles;
+               Private():mChanged(false){st_none=st_tag=st_text=st_loop=st_calc=st_comment=st_cond=-1;}
 };
 
 DEFINE_DPTR(MOdfEditor);
@@ -198,10 +200,14 @@ MOdfEditor::MOdfEditor(QWidget* parent, Qt::WindowFlags f): QMainWindow(parent,
        vl->addLayout(hl=new QHBoxLayout);
        hl->addWidget(new QLabel(tr("Tag Name:")));
        hl->addWidget(d->mTagName=new QLineEdit,1);
+       connect(d->mTagName,SIGNAL(textEdited(QString)),this,SLOT(setChanged()));
        vl->addSpacing(15);
        vl->addWidget(new QLabel(tr("Attributes:")));
        vl->addWidget(d->mTagAttrs=new QTableView,1);
        d->mTagAttrs->setModel(d->mTagAttrModel=new QStandardItemModel);
+       connect(d->mTagAttrModel,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this,SLOT(setChanged()));
+       connect(d->mTagAttrModel,SIGNAL(rowsRemoved(const QModelIndex&,int,int)), this,SLOT(setChanged()));
+       connect(d->mTagAttrModel,SIGNAL(rowsInserted(const QModelIndex&,int,int)), this,SLOT(setChanged()));
        //loop editor
        d->st_loop=stack->addWidget(w=new QWidget);
        w->setLayout(vl=new QVBoxLayout);
@@ -209,6 +215,7 @@ MOdfEditor::MOdfEditor(QWidget* parent, Qt::WindowFlags f): QMainWindow(parent,
        vl->addLayout(hl=new QHBoxLayout);
        hl->addWidget(new QLabel(tr("Loop Variable")));
        hl->addWidget(d->mLoopVar=new QLineEdit);
+       connect(d->mLoopVar,SIGNAL(textEdited(QString)), this,SLOT(setChanged()));
        vl->addStretch(1);
        //calc editor
        d->st_calc=stack->addWidget(w=new QWidget);
@@ -217,6 +224,7 @@ MOdfEditor::MOdfEditor(QWidget* parent, Qt::WindowFlags f): QMainWindow(parent,
        vl->addLayout(hl=new QHBoxLayout);
        hl->addWidget(new QLabel(tr("Expression")));
        hl->addWidget(d->mCalcExpr=new QLineEdit);
+       connect(d->mCalcExpr,SIGNAL(textEdited(QString)), this,SLOT(setChanged()));
        vl->addStretch(1);
        //calc editor
        d->st_cond=stack->addWidget(w=new QWidget);
@@ -225,21 +233,26 @@ MOdfEditor::MOdfEditor(QWidget* parent, Qt::WindowFlags f): QMainWindow(parent,
        vl->addLayout(hl=new QHBoxLayout);
        hl->addWidget(new QLabel(tr("Expression")));
        hl->addWidget(d->mCondExpr=new QLineEdit);
+       connect(d->mCondExpr,SIGNAL(textEdited(QString)), this,SLOT(setChanged()));
        vl->addStretch(1);
        //comment editor
        d->st_comment=stack->addWidget(w=new QWidget);
        w->setLayout(vl=new QVBoxLayout);
        vl->addWidget(new QLabel(tr("<h1>Comment</h1>")));
        vl->addWidget(d->mComment=new QTextEdit,1);
-       
+       connect(d->mComment,SIGNAL(textChanged()),this,SLOT(setChanged()));
        //statusbar
        statusBar()->setSizeGripEnabled(true);
        setAttribute(Qt::WA_DeleteOnClose);
        updateDisplay();
 }
 
+void MOdfEditor::setChanged(bool ch)
+{
+       d->mChanged=ch;
+}
 
-void MOdfEditor::loadFile(QString fn)
+void MOdfEditor::loadFile(QString fn,bool istempl)
 {
        qDebug()<<"loading ODF template file"<<fn;
        //try to open the file
@@ -251,7 +264,7 @@ void MOdfEditor::loadFile(QString fn)
        }
        QUnZip unz;
        if(!unz.open(&fd)){
-               QMessageBox::warning(this, tr("Error"), tr("Unable to interpret file '%1'.").arg(fn));
+               QMessageBox::warning(this, tr("Error"), tr("Unable to interpret file '%1'. It is not an ODF container (PKZip format).").arg(fn));
                return;
        }
        //clear cache
@@ -265,7 +278,7 @@ void MOdfEditor::loadFile(QString fn)
                QBuffer buf;buf.open(QIODevice::ReadWrite);
                unz.getCurrentFile(buf);
                if(ifn=="content.xml")
-                       parseTemplate(buf.data());
+                       parseTemplate(buf.data(),istempl);
                else
                        d->mFiles<<Private::File(ifn,Private::File::Type::FlatFile,buf.data());
        }while(unz.gotoNextFile());
@@ -274,7 +287,7 @@ void MOdfEditor::loadFile(QString fn)
 }
 
 
-void MOdfEditor::parseTemplate(QByteArray bytes)
+void MOdfEditor::parseTemplate(QByteArray bytes,bool istempl)
 {
        d->mContent.clear();
        d->mDomModel->clear();
@@ -306,10 +319,13 @@ void MOdfEditor::parseTemplate(QByteArray bytes)
        if(!d->mContent.setContent(MOdtRenderer::convertV1toV2(bytes),false,&err,&errln,&errcl)){
                qDebug()<<"Hmm, definitely not XML - even after conversion, aborting...";
                qDebug()<<" Info: line ="<<errln<<"column ="<<errcl<<"error ="<<err;
+               QMessageBox::warning(this,tr("Warning"),tr("The file '%1' does not contain a valid ODF file or template of any version.").arg(d->mFileName));
                return;
        }else{
                qDebug()<<"Successfully converted the template.";
 //             qDebug()<<"document has"<<d->mContent.documentElement().childNodes().size()<<"children and"<<d->mContent.documentElement().attributes().size()<<"attribs";
+               if(istempl)
+                       QMessageBox::information(this,tr("Conversion Info"),tr("The file '%1' did contain a version 1 template. It has been converted to version 2.\nPlease correct all formulas.").arg(d->mFileName));
        }
 //     qDebug()<<"dump\n"<<d->mContent.toByteArray();
        d->mDomModel->setDomDocument(d->mContent);
@@ -317,15 +333,23 @@ void MOdfEditor::parseTemplate(QByteArray bytes)
        qDebug()<<"done converting and parsing template";
 }
 
-void MOdfEditor::openFile()
+void MOdfEditor::openFile(bool istemp)
 {
-       QString fn=QFileDialog::getOpenFileName(this,tr("Open Ticket Template"));
+       QString title=istemp?tr("Open ODF Template"):tr("Open ODF File");
+       QString flt=istemp?tr("ODF Template File (*.od?t);;All Files (*)"):tr("ODF File (*.od?);;All Files (*)");
+       QString fn=QFileDialog::getOpenFileName(this,title,currentDir(),flt);
        if(fn!=""){
+               setCurrentDir(fn);
                d->mFileName=fn;
-               loadFile(fn);
+               loadFile(fn,istemp);
        }
 }
 
+void MOdfEditor::openFile2()
+{
+       openFile(false);
+}
+
 void MOdfEditor::saveFile()
 {
        if(d->mFileName=="" || d->mFileName.left(2)==":/")
@@ -449,6 +473,11 @@ void MOdfEditor::selectionChange()
                        d->setActions(QList<QAction*>()<<d->maInsBehindCalc<<d->maInsBehindComment<<d->maAddIntoCalc<<d->maAddIntoComment<<d->maWrapInCond<<d->maWrapInLoop<<d->maUnwrap<<d->maDelItem<<(allowElse?d->maInsBehindElse:nullptr));
                        return;
                }
+               if(nn[1]=="template"){
+                       emit switchStack(d->st_none);
+                       d->setActions(QList<QAction*>());
+                       return;
+               }
                //else: fall through to normal tags
        }
        //else: other tags
@@ -483,7 +512,13 @@ void MOdfEditor::displayTag()
 
 void MOdfEditor::saveCurrentNode()
 {
+       //nothing to do?
+       if(!d->mChanged)return;
 #warning implement
+
+       qDebug()<<"fake saving item";
+       //done!
+       d->mChanged=false;
 }
 
 void MOdfEditor::Private::setActions(const QList< QAction* >& act)
index 96de8f7..5989439 100644 (file)
@@ -33,18 +33,21 @@ class MOdfEditor:public QMainWindow
                
        public slots:
                ///loads a template file, this is a helper for openFile and download
-               void loadFile(QString);
+               void loadFile(QString,bool isTemplate=true);
                ///shows a file open dialog and then opens the selected file
-               void openFile();
+               void openFile(bool isTemplate=true);
+               ///shows a file open dialog and then opens the selected file
+               void openFile2();
                ///saves the file - if it is local, otherwise calls saveFileAs
                void saveFile();
                ///asks for a new file name
                void saveFileAs();
                ///helper for saveFile and upload
                void saveFile(QString);
+
        private slots:
                ///parses the XML part of the template file and fills internal ressources
-               void parseTemplate(QByteArray);
+               void parseTemplate(QByteArray,bool isTemplate=true);
                ///pushes all data from internal ressources to the display
                void updateDisplay();
                ///saves all data to the file (must be open ReadWrite)
@@ -53,6 +56,8 @@ class MOdfEditor:public QMainWindow
                void saveXmlPart(QIODevice&);
                ///helper: react on selection changes in the DOM tree
                void selectionChange();
+               ///helper: remember that an editor element changed
+               void setChanged(bool ch=true);
        signals:
                ///used to switch to the correct editor widget
                void switchStack(int);
index 086c359..adc659f 100644 (file)
@@ -64,6 +64,7 @@ class MOdtRendererPrivate
                
                //data the parent uses
                QString extension;
+               MOdtRenderer::FileFormat fileformat;
                
        private:
                //methods that the parent does not call
@@ -103,7 +104,7 @@ MOdtRenderer::MOdtRenderer(MTemplate file)
 const QString OdfTemplateNS("http://smoke.silmor.de/odftemplate/namespace");
 const QString OdfTemplatePrefix("msmoketpl");
 MOdtRendererPrivate::MOdtRendererPrivate(QString file,MOdtRenderer*p)
-       :tfile(file)
+       :fileformat(MOdtRenderer::FileFormat::None),tfile(file)
 {
        parent=p;
        //open ZIP
@@ -139,14 +140,19 @@ MOdtRendererPrivate::MOdtRendererPrivate(QString file,MOdtRenderer*p)
                }
        }
        //conversion process
-       if(!dov1)return;
+       if(!dov1){
+               fileformat=MOdtRenderer::FileFormat::Version2;
+               return;
+       }
        //try again
        if(!cdoc.setContent(MOdtRenderer::convertV1toV2(buffer.data()),false,&err,&errln,&errcl)){
                qDebug()<<"Hmm, definitely not XML - even after conversion, aborting...";
                qDebug()<<" Info: line ="<<errln<<"column ="<<errcl<<"error ="<<err;
                return;
-       }else
+       }else{
+               fileformat=MOdtRenderer::FileFormat::ConvertedV1;
                qDebug()<<"Successfully converted the template.";
+       }
 }
 
 MOdtRenderer::~MOdtRenderer()
@@ -162,6 +168,11 @@ MOdtRendererPrivate::~MOdtRendererPrivate()
        tfile.close();
 }
 
+MOdtRenderer::FileFormat MOdtRenderer::fileFormat()const
+{
+       return d->fileformat;
+}
+
 void MOdtRenderer::renderToFile(QString file)
 {
        QFile f(file);
index ef19414..cf99d14 100644 (file)
@@ -43,8 +43,21 @@ class MOdtRenderer
                virtual void renderToPrinter();
                
                ///helper routine: converts a V1 template to V2
+               ///you have to make sure to not do a double conversion on an already converted document
                static QByteArray convertV1toV2(const QByteArray&);
                
+               ///format of the file represented by this renderer
+               enum class FileFormat{
+                       ///no file loaded or loading was not successful
+                       None,
+                       ///file was a valid v2 format
+                       Version2,
+                       ///file was probably v1 or plain ODF, it has been converted
+                       ConvertedV1,
+               };
+               ///returns the format of the file
+               FileFormat fileFormat()const;
+               
        protected:
                friend class MOdtRendererPrivate;
                friend class MOdfEngine;