/****************************************************************************
*
*  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 NCREPORTPREVIEWWINDOW_H
#define NCREPORTPREVIEWWINDOW_H

#include "ncreportpageoption.h"
#include "ncreportpreviewpagecontainer.h"
#include "ncreport_global.h"

#include <QMainWindow>
#include <QCloseEvent>
#include <QScrollArea>

class NCReportPreviewPageContainer;
class NCReportPreviewOutput;
class NCReportPreviewScrollArea;
class NCReportPreviewWindowPrivate;

QT_BEGIN_NAMESPACE
class QWidget;
class QAction;
class QActionGroup;
class QComboBox;
class QMenu;
class QTextEdit;
class QSpinBox;
class QProgressDialog;
class QSettings;
class QPrinter;
QT_END_NAMESPACE


/*!
Main window class for Print preview. Represents the contents of an NCReportPreviewOutput object
 */
class NCREPORTSHARED_EXPORT NCReportPreviewWindow : public QMainWindow
{
      Q_OBJECT
public:
    NCReportPreviewWindow( QWidget *parent = Q_NULLPTR, bool handleSettings = true );
    NCReportPreviewWindow( const QString &orgName, const QString &appName, QWidget *parent = Q_NULLPTR, bool handleSettings = true );
    NCReportPreviewWindow( bool createMenuBar, bool createStatusBar, QWidget *parent = Q_NULLPTR, bool handleSettings = true );
    ~NCReportPreviewWindow();

    enum PagePosition { Top, Bottom, Off };

    void setOutput( NCReportPreviewOutput* );
    void setReport( NCReport* );
    NCReport* report() const;
    void setDefaultOutputDir( const QString& dir );
    QString defaultOutputDir() const;

    void setPrinterName( const QString& printer );
    QString printerName() const;
    void setUsePrintDialog( bool set );
    bool usePrintDialog() const;

    //bool setPageIMFData(QIODevice *device, const QString& imfdata);
    int pageCount() const;
    int reportCount() const;
    NCReportPreviewPageContainer::ShowType showType() const;
    void setCurrentPage( int pageno, NCReportPreviewPageContainer::ShowType type = NCReportPreviewPageContainer::Single );

    QAction* actionPrint() const;
    QAction* actionPdf() const;
    QAction* actionSvg() const;
    QAction* actionXls() const;

    QAction* actionClose() const;

    QAction* actionFirstPage() const;
    QAction* actionNextPage() const;
    QAction* actionPreviousPage() const;
    QAction* actionLastPage() const;
    QAction* actionGoToPage() const;
    QAction* actionZoomIn() const;
    QAction* actionZoomOut() const;
    QAction* actionZoomOff() const;
    QAction* actionNextReport() const;
    QAction* actionPreviousReport() const;
    QAction* actionViewSingleMode() const;
    QAction* actionViewDoubleMode() const;
    QAction* actionViewContinousMode() const;
    bool donePrint() const;
    bool donePDF() const;
    bool doneSVG() const;
    bool doneXLS() const;

    QToolBar *toolBarFile();
    QToolBar *toolBarView();
    QToolBar *toolBarNavigation();

    void exec( bool autoAdjustSize = false );

    void setDefaultOutputFileName( const QString& fileName );
    QString defaultOutputFileName() const;

    void setSettingsData( const QString& orgName, const QString& appName );
    void setDefaultPdfDpi( int dpi );
    int defaultPdfDpi() const;

    bool isInternalPrinting();
    void setInternalPrinting( bool set );
    qreal currentScale() const;
    void setScale( qreal zoomFactor );
    void setPdfVersion( NCReport::PdfVersion version );
    void setPdfCreator( const QString creator);

signals:
    void runPrinterDone();
    void runPdfDone();
    void runSvgDone();
    void runXlsDone();
    void elementClicked(const QString& id);
    void customizePrinter(QPrinter* printer);

public slots:
    virtual void nextPage( const PagePosition = Top );
    virtual void prevPage( const PagePosition = Top );
    virtual void firstPage( const PagePosition = Top );
    virtual void lastPage( const PagePosition = Top );

    virtual void open();
    virtual bool save();
    virtual bool saveAs();
    virtual void about();
    virtual void documentWasModified();
    virtual void activatePrint();
    virtual void activatePdf();
    virtual void activateSvg();
    virtual void activateXls();

    virtual void zoomChangedStr( const QString & s);
    virtual void zoomChanged();
    virtual void zoomChangedInt( int scaleFactor );
    virtual void zoomPlus();
    virtual void zoomMinus();
    virtual void zoom1();

    virtual void gotoPage();
    virtual void gotoPage( int pageno );

    virtual void nextReport();
    virtual void prevReport();
    virtual void sliderMoved(int value);
    virtual void reRun();
    virtual void adjustSizeToOptimalWidth();

    virtual void setViewSingle();
    virtual void setViewDouble();
    virtual void setViewContinous();

protected slots:
    virtual void loadPage( int pageno );
    virtual void viewChange(QAction*);

    //TESTS
    virtual void testPrint1();
    virtual void testPrint2();
    virtual void addTestItems();
    virtual void clearPage();

protected:
    virtual void showEvent( QShowEvent* event);
    virtual void closeEvent(QCloseEvent *event);
    virtual void loadPagePrivate( int pageno, const PagePosition );

    virtual void readSettings( QSettings* settings );
    virtual void writeSettings( QSettings* settings );

private:
    Q_DECLARE_PRIVATE(NCReportPreviewWindow)
    NCReportPreviewWindowPrivate* const d_ptr;

private:
    void initialize();
    void createActions();
    void createMenus();
    void createToolBars();
    void createStatusBar();

    void writeSettings();
    void readSettings();

    bool maybeSave();
    void loadFile(const QString &fileName);
    bool saveFile(const QString &fileName);
    void refreshActions();
    void updateZoomInfo();
    void initPrinter( QPrinter* );
    void initProgress( QProgressDialog* );
    void printPicturePages( QPrinter* );
    void zoom( qreal );

    void setCurrentFile(const QString &fileName);
    QString strippedName(const QString &fullFileName);
    QString genOutputFileName(const QString &extension) const;
};


class NCReportPreviewScrollArea : public QScrollArea
{
    Q_OBJECT
public:
    NCReportPreviewScrollArea( NCReportPreviewWindow * parent = Q_NULLPTR );
    ~NCReportPreviewScrollArea();

protected:
    void keyPressEvent( QKeyEvent* );

private:
    NCReportPreviewWindow* pWindow;
};


#endif
