Source

AncientDocumentEditor / documentImageCreator / src / framework / DocumentCreator / iomanager / parametricdocumentloader.cpp

#include "parametricdocumentloader.h"
#include <QTextStream>
#include <QString>
#include <QFile>
#include <iostream>
#include <QDebug>

namespace IOManager
{
    ParametricDocumentLoader::ParametricDocumentLoader(QString input) : DocumentLoader(input),
        _pageWidth (2480), // A4 @ 300 ppi
        _pageHeight(3508), // A4 @ 300 ppi
        _margin(50),
        _x(_margin), // same as margin (_x, _y, _w, _y are values for the main text bloc)
        _y(_margin),
        _w(_pageWidth-100),
        _h(_pageHeight-100),
        _fontname("")

    {
        _reader = new CmdLineParser(_input);
    }

    ParametricDocumentLoader::~ParametricDocumentLoader()
    {
        if(_reader != NULL) delete _reader; _reader = NULL;
    }

    void ParametricDocumentLoader::createNewDocument()
    {
        DocumentLoader::createNewDocument();
        _reader = new CmdLineParser(_input);
        _output->setPageWidth(_pageWidth);
        _output->setPageHeight(_pageHeight);
    }

    void ParametricDocumentLoader::buildStyles()
    {
        _output->addStyle(buildStyle());
    }

    void ParametricDocumentLoader::buildContent()
    {
        //Reading document
        _output->add( buildPage() );
    }

    //protected:
    DocStyle * ParametricDocumentLoader::buildStyle ()
    {
        _fontname = _reader->value("font");

        if(_fontname.isEmpty() ) {
            qDebug() << "bad font name";
            return NULL;
        }

        DocStyle * s = new DocStyle(_fontname);
        s->setFontName(_fontname);
        return s;
    }

    Page * ParametricDocumentLoader::buildPage ()
    {
        QString bg = _reader->value("bg");
        QString filename = _reader->value("inputfile");

        if(bg.isEmpty() ) {
            qDebug() << "bad background name";
            return NULL;
        }

        if(filename.isEmpty() ) {
            qDebug() << "bad file name";
            return NULL;
        }

        Page * p = new Page(_output);
        p->setBackgroundFileName(bg);

        //load texte file
        loadTextFile(filename, p);

        return p;
    }


    void ParametricDocumentLoader::loadTextFile(QString filename, Page* p) {
        QFile file(filename);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;

        QTextStream in(&file);
        in.setAutoDetectUnicode(true);
        QStringList paragraph = QStringList();

        while(!in.atEnd())
        {
            QString line = in.readLine();
            //std::cout << line.toStdString() << std::endl;

            if(line.isEmpty() && ! paragraph.isEmpty()) {
                _paragraphs << paragraph;
                paragraph.clear();
            } else {
                paragraph << line.toLocal8Bit() << " "; // convert to locale multi-byte string
            }
        }
        _paragraphs << paragraph;
        paragraph.clear();

        p->add( buildImageBlock());
        p->add( buildTextBlock());
    }

    DocImageBlock * ParametricDocumentLoader::buildImageBlock ()
    {
        // do nothing for now
    }

    DocTextBlock * ParametricDocumentLoader::buildTextBlock ()
    {

        /* default layer : 1 column */
        DocTextBlock * tb = new DocTextBlock(_output, _w, _h, _x, _y);
        tb->setMarginTop( _margin );
        tb->setMarginBottom( _margin );
        tb->setMarginLeft( _margin );
        tb->setMarginRight( _margin );


        foreach (QStringList paragraph, _paragraphs) {
            tb->add( buildParagraph(paragraph) );
        }
        return tb;
    }


    DocParagraph * ParametricDocumentLoader::buildParagraph (){}
    DocParagraph * ParametricDocumentLoader::buildParagraph (QStringList paragraph)
    {
        DocParagraph * p = new DocParagraph(_output);

        /* TODO : default values */
        p->setLineSpacing(80);
        p->setTabulationSize(80);

        foreach (QString str, paragraph) {
            p->add( buildString(str) );
        }

        return p;
    }

    DocString * ParametricDocumentLoader::buildString (){}
    DocString * ParametricDocumentLoader::buildString (QString str)
    {
        // TODO : default style
        DocStyle* style = _output->getStyle(_fontname);
        DocString* s = new DocString(style ,_output);
        foreach (const QChar c, str) {
            s->add( buildCharacter(c) );
        }

        return s;
    }

    DocCharacter * ParametricDocumentLoader::buildCharacter (){}
    DocCharacter * ParametricDocumentLoader::buildCharacter (const QChar ch)
    {
        QAtomicInt id = -1;
        QString display = ch;
        DocCharacter * c = new DocCharacter(display, id, _output);

        return c;
    }



}