/****************************************************************************
*
*  Copyright (C) 2002-2008 Helta Kft. / NociSoft Software Solutions
*  All rights reserved.
*  Author: Norbert Szabo
*  E-mail: nszabo@helta.hu, info@nocisoft.com
*  Web: www.nocisoft.com
*
*  This file is part of the NCReport reporting software
*
*  Licensees holding a valid NCReport License Agreement may use this
*  file in accordance with the rights, responsibilities, and obligations
*  contained therein. Please consult your licensing agreement or contact
*  nszabo@helta.hu if any conditions of this licensing are not clear
*  to you.
*
*  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
*  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
****************************************************************************/
#ifndef NCREPORTTEXTITEM_H
#define NCREPORTTEXTITEM_H

#include "ncreportitem.h"
#include "ncreport_global.h"

#include <QString>
#include <QColor>
#include <QFont>

#define NCRD_DEFAULT_TEXT 	"Html text"
QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE

/*!
Html/Rich text item class
 */
class NCREPORTSHARED_EXPORT NCReportTextItem : public NCReportItem
{
public:
    NCReportTextItem( NCReportDef* rdef, QGraphicsItem * parent=0 );
    ~NCReportTextItem();

    enum EditorMode {
        RichTextEdit = 0,
        SourceCodeEdit
    };

    bool read( NCReportXMLReader* );
    bool write( NCReportXMLWriter* );
    void setHtml( const QString& content);
    QString toHtml() const;
    void setDefaultForEditor();
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    void paint( NCReportOutput*, const QPointF& );
    void dummyPaint(NCReportOutput *, const QPointF &);
    void adjustSize();
    void adjustSize(NCReportOutput* =0);
    void resize( const QSizeF& );
    bool isEmpty() const;
    void updateValue(NCReportOutput* output, NCReportEvaluator* evaluator);

    QTextDocument * document();
    bool isDefaultPosAndSize() const;
    void setDefaultPosAndSize( bool set );
    QColor foreColor() const;
    void setForeColor( const QColor& color );
    QFont font() const;
    void setFont( const QFont& font );
    Qt::Alignment alignment() const;
    void setAlignment( Qt::Alignment al );
    QString fileName() const;
    void setFileName( const QString& fname );
    bool isSizeFix() const;
    void setSizeFix( bool set );
    bool isUsedDefaultFont() const;
    void setPlainText( bool set );
    bool isPlainText() const;
    void setUseDefaultFont( bool set );
    QString documentTemplate() const;
    void setDocumentTemplate( const QString& dt );
    bool isMarkdown() const;
    void setMarkdown( bool set );

    EditorMode editorMode() const;
    void setEditorMode( EditorMode mode );

    qreal breakTo( qreal heightMM, NCReportOutput* );
    bool isBroken() const;
    QString convertPlainTextToHtml( const QString& text) const;
    QList<int> availableSourceTypes() const;
    QString htmlContent() const;

    void setUseDatasourceAppearanceRoles( bool set );
    bool useDataSourceAppearanceRoles() const;

    QString& documentTemplateRef();
    void restoreDefaultTextStyle();

    virtual void adjustFontF2(QString &htmlContent, NCReportOutput* output = Q_NULLPTR );
    virtual void adjustFontF3(QString &htmlContent, NCReportOutput* output = Q_NULLPTR );

protected:
    void readProps( NCReportXMLReader* );
    void writeProps( NCReportXMLWriter* );
    /*! Font adjustment for MAC users */

private:

    struct TextPart {
        TextPart() : blockNo(-1), lineNo(-1) {}
        int blockNo;
        int lineNo;
    };

    TextPart partFrom, partTo;
    void partialPaint(NCReportOutput *o, QPointF &pos, bool dummyPaint );
};

#endif
