/****************************************************************************DlgPrint
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** Qt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/

#include <stdlib.h>
extern QString Global_ParamData;
extern QString Global_PathIni;
//-------------------------------------- init -------------------------------------------
void DlgPrint::init()
{   m_pCViewPageWidget = new CViewPageWidget(this);

    if (m_pCViewPageWidget )
       {m_pCViewPageWidget->setGeometry (QRect (10,10, m_pCViewPageWidget->GetWidgetWidth(), m_pCViewPageWidget->GetWidgetHeight()));
       }
    m_pHeadRichText       = 0;
    m_pBodyRichText       = 0;
    m_pFootRichText       = 0;

    //....................... ajuster la geometrie du dialogue et des widgets .............................................
    QRect rectDlg    = this->geometry();
    QRect rectTool   = frameToolBar->geometry();
    QRect rectPage   = m_pCViewPageWidget->geometry();
    QRect rectscreen = QApplication::desktop()->rect();

    rectDlg.setHeight(Max (rectPage.height()+20, rectTool.height() +20));
    rectDlg.setWidth(rectPage.width()+10+rectTool.width()+10);
    rectTool.moveTop (10);
    rectTool.moveLeft (rectPage.width()+15);
    frameToolBar->setGeometry(rectTool);
    //.......................centrer le dialogue sur l'écran ..............................................................
    rectDlg.moveLeft( (rectscreen.width() -rectDlg.width()  ) / 2);
    rectDlg.moveTop ( (rectscreen.height()-rectDlg.height() ) / 2 );
    this->setGeometry(rectDlg);

    m_PageW = m_pCViewPageWidget->GetPageW();
    m_PageH = m_pCViewPageWidget->GetPageH();

    lineEdit_PageDeb->setText("1");
    //.................... declarer et instancier l'imprimante.................................
    m_pQPrinter = new QPrinter(QPrinter::ScreenResolution );  // PrinterResolution ScreenResolution HighResolution
    if (m_pQPrinter)
    {   m_pQPrinter->setFullPage(TRUE);
	// QPaintDeviceMetrics screen( this );
	// m_pQPrinter->setResolution( Max(screen.logicalDpiX(),screen.logicalDpiY()) );
	// m_pQPrinter->setResolution( screen.logicalDpiY() );
	m_pQPrinter->setResolution(96); // par defaut
        lineEditResoX->setText("96");
        lineEditResoY->setText("96");
    }
}

//-------------------------------------- initDialog ----------------------------------------------------------

void DlgPrint::initDialog(QTextEdit*        pQTextEdit,
                          QString*          pHeadText,
                          QString*          pBodyText,
                          QString*          pFootText
			 )
{   //............................. variables permettant la reinitialisation du dialogue ................
    //                              sur de nouvelles entêtes et pied de page
    QString val1         = "";
    QString val2         = "";
    m_pBodyTextEdit      = pQTextEdit;
    m_pBodyText          = pBodyText; //CreateBody();
    m_pHeadText          = pHeadText; //Get_Head_Foot (&m_FootText);
    m_pFootText          = pFootText;

#ifdef Q_WS_X11
    //.................... ajuster resolution d'impression .........................................
    if (CGestIni::Param_ReadParam( Global_ParamData, "Impression", "Resolution", &val1, &val2)==0)  // zero = pas d'erreur
       {if (val2=="") val2=val1;
	lineEditResoX->setText(val1);
        lineEditResoY->setText(val2);
        m_pQPrinter->setResolution(atoi(val2));
	QPaintDevice::x11SetAppDpiX(atoi(val1)-1);
        QPaintDevice::x11SetAppDpiY(atoi(val2)-1);
       }
#endif

    SetDialogOnHeadAndFoot(1);

    //.................... ajuster resolution d'impression .........................................
    if (CGestIni::Param_ReadParam( Global_ParamData, "Impression", "MargTop",     &val1)==0)      m_pCViewPageWidget->SetTopMarg(atof(val1)    / 10);
    if (CGestIni::Param_ReadParam( Global_ParamData, "Impression", "Marg_Left",   &val1)==0)      m_pCViewPageWidget->SetLeftMarg(atof(val1)   / 10);
    if (CGestIni::Param_ReadParam( Global_ParamData, "Impression", "Marg_Right",  &val1)==0)      m_pCViewPageWidget->SetRightMarg(atof(val1)  / 10);
    if (CGestIni::Param_ReadParam( Global_ParamData, "Impression", "Marg_Bottom", &val1)==0)      m_pCViewPageWidget->SetBottomMarg(atof(val1) / 10);
    if (CGestIni::Param_ReadParam( Global_ParamData, "Impression", "Zoom",        &val1)==0)      m_pCViewPageWidget->SetPrintScale(atof(val1) / 100 );

    slider_Marg_Top->setValue    ( (int)(m_pCViewPageWidget->GetTopMarg()     *100/m_PageH ) );
    slider_Marg_Left->setValue   ( (int)(m_pCViewPageWidget->GetLeftMarg()    *100/m_PageW ) );
    slider_Marg_Right->setValue  ( (int)(m_pCViewPageWidget->GetRightMarg()   *100/m_PageW ) );
    slider_Marg_Bottom->setValue ( (int)(m_pCViewPageWidget->GetBottomMarg()  *100/m_PageH ) );
    lineEditZoom->setText(QString::number(m_pCViewPageWidget->GetPrintScale() *100));


    lineEdit_Marg_Top->setText   (QString::number(m_pCViewPageWidget->GetTopMarg()    *10));
    lineEdit_Head_H->setText     (QString::number(m_pCViewPageWidget->GetHead_H()     *10));
    lineEdit_Marg_Left->setText  (QString::number(m_pCViewPageWidget->GetLeftMarg()   *10));
    lineEdit_Marg_Right->setText (QString::number(m_pCViewPageWidget->GetRightMarg()  *10));
    lineEdit_Marg_Bottom->setText(QString::number(m_pCViewPageWidget->GetBottomMarg() *10));
    lineEdit_Foot_H->setText     (QString::number(m_pCViewPageWidget->GetFoot_H()     *10));


    slider_Marg_Top->setValue    ( (int)(m_pCViewPageWidget->GetTopMarg()     *100 / m_PageH ) );
    slider_Head_H->setValue      ( (int)(m_pCViewPageWidget->GetHead_H()      *100 / m_PageH ) );
    slider_Marg_Left->setValue   ( (int)(m_pCViewPageWidget->GetLeftMarg()    *100 / m_PageW ) );
    slider_Marg_Right->setValue  ( (int)(m_pCViewPageWidget->GetRightMarg()   *100 / m_PageW ) );
    slider_Foot_H->setValue      ( (int)(m_pCViewPageWidget->GetFoot_H()      *100 / m_PageH ) );
    slider_Marg_Bottom->setValue ( (int)(m_pCViewPageWidget->GetBottomMarg()  *100 / m_PageH ) );
    lineEditZoom->setText(QString::number(m_pCViewPageWidget->GetPrintScale() *100));

    //............................ positionner le nombre de copies ...........................
    if (CGestIni::Param_ReadParam( Global_ParamData, "Impression", "NumCopies",  &val1)==0)
       {m_pQPrinter->setNumCopies(atoi(val1));
       }

    //.................... ajuster fichier de sortie .........................................
    if (CGestIni::Param_ReadParam( Global_ParamData, "Impression", "OutpuFileName", &val1)==0)  // zero = pas d'erreur
       {m_pQPrinter->setOutputFileName(val1);
       }
    //.................... ajuster sortie fichier ou papier.........................................
    //                     à executer toujours apres: setOutputFileName();  car setOutputFileName()
    //                     replace sur TRUE !!!
    if (CGestIni::Param_ReadParam( Global_ParamData, "Impression", "IsOutToFile", &val1)==0)  // zero = pas d'erreur
       { m_pQPrinter->setOutputToFile(atoi(val1));
       }

    //...................... titre de laboite de dialogue ..........................................
    setCaption(QString(tr("Imprimer  Rx :")) + QString::number(m_pCViewPageWidget->GetResolutionX()) +
      	       QString(tr("  Ry :")) + QString::number(m_pCViewPageWidget->GetResolutionY())
              );
}


//-------------------------------------- destroy -------------------------------------------
void DlgPrint::destroy()
{   if (m_pQPrinter)     delete  m_pQPrinter ;     m_pQPrinter        = 0;
    if (m_pHeadRichText) delete  m_pHeadRichText;  m_pHeadRichText    = 0;
    if (m_pBodyRichText) delete  m_pBodyRichText;  m_pBodyRichText    = 0;
    if (m_pFootRichText) delete  m_pFootRichText;  m_pFootRichText    = 0;
}

//-------------------------------------- slider_Marg_Top_sliderMoved -------------------------------------------------------------
void DlgPrint::slider_Marg_Top_sliderMoved( int value)
{float fval = (m_PageH * value )/ 100.0;
    m_pCViewPageWidget->SetTopMarg(fval);
    lineEdit_Marg_Top->setText(QString::number( (fval *10.0) ));
    m_pCViewPageWidget->update ();
    UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
}

//-------------------------------------- slider_Head_H_sliderMoved -------------------------------------------
void DlgPrint::slider_Head_H_sliderMoved( int value)
{float fval = (m_PageH * value )/ 100.0;
    m_pCViewPageWidget->SetHead_H(fval);
    lineEdit_Head_H->setText(QString::number( (fval *10.0) ));
    m_pCViewPageWidget->update ();
    UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
}

//-------------------------------------- slider_Marg_Left_sliderMoved -------------------------------------------
void DlgPrint::slider_Marg_Left_sliderMoved( int value)
{float fval = (m_PageW * value )/ 100.0;
    m_pCViewPageWidget->SetLeftMarg(fval);
    lineEdit_Marg_Left->setText(QString::number( (fval *10.0) ));
    m_pCViewPageWidget->update ();
    UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
}

//-------------------------------------- slider_Marg_Right_sliderMoved -------------------------------------------
void DlgPrint::slider_Marg_Right_sliderMoved( int value)
{float fval = (m_PageW * value )/ 100.0;
    m_pCViewPageWidget->SetRightMarg(fval);
    lineEdit_Marg_Right->setText(QString::number( (fval *10.0) ));
    m_pCViewPageWidget->update ();
    UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
}

//-------------------------------------- slider_Marg_Bottom_sliderMoved -------------------------------------------
void DlgPrint::slider_Marg_Bottom_sliderMoved( int value)
{float fval = (m_PageH * value )/ 100.0;
    m_pCViewPageWidget->SetBottomMarg(fval);
    lineEdit_Marg_Bottom->setText(QString::number( (fval *10.0) ));
    m_pCViewPageWidget->update ();
    UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
}

//-------------------------------------- slider_Foot_H_sliderMoved -------------------------------------------
void DlgPrint::slider_Foot_H_sliderMoved( int value)
{float fval = (m_PageH * value )/ 100.0;
    m_pCViewPageWidget->SetFoot_H(fval);
    lineEdit_Foot_H->setText(QString::number( (fval *10.0) ));
    m_pCViewPageWidget->update ();
    UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
}
//-------------------------------------- UpdateTextePageInfo ---------------------------------------------------
void DlgPrint::UpdateTextePageInfo( int nb_page)
{QString qtext;
    qtext.sprintf(tr("Page %i sur %i"),m_pCViewPageWidget->GetCurrentPage(), nb_page);
    textLabel_NbPage->setText(qtext);
    lineEdit_PageFin->setText(QString::number(nb_page));
}


//-------------------------------------- pushButtonSave_clicked ----------------------------------------------------------
// Sauver les reglages
void DlgPrint::pushButtonSave_clicked()
{
 //................... modifier et updater ces parametres selon les reglages ...........................................
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "Resolution",       lineEditResoX->text(), lineEditResoY->text());
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "MargTop",          lineEdit_Marg_Top->text());
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "Marg_Left",        lineEdit_Marg_Left->text());
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "Marg_Right",       lineEdit_Marg_Right->text());
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "Marg_Bottom",      lineEdit_Marg_Bottom->text());
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "Zoom",             lineEditZoom->text());
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "IsOutToFile",      QString::number((unsigned int) m_pQPrinter->outputToFile ()));
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "OutpuFileName",    m_pQPrinter->outputFileName ());

 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "Head_H",           lineEdit_Head_H->text());
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "Foot_H",           lineEdit_Foot_H->text());
 CGestIni::Param_WriteParam( &Global_ParamData, "Impression", "NumCopies",        QString::number(m_pQPrinter->numCopies ()));
 Global_pCGestIni->Param_UpdateToDisk(Global_PathIni, Global_ParamData);
}

//-------------------------------------- SetDialogOnHeadAndFoot ----------------------------------------------------------
// mode = 1  on reajuste les hauteurs des header et Foot sur leur contenu
//      = 0  on garde les valeurs deja presentes
void DlgPrint::SetDialogOnHeadAndFoot(int mode)
{   QFont font( "Arial Narrow", 10 );
    if (m_pHeadRichText) delete m_pHeadRichText; m_pHeadRichText = 0;
    if (m_pBodyRichText) delete m_pBodyRichText; m_pBodyRichText = 0;
    if (m_pFootRichText) delete m_pFootRichText; m_pFootRichText = 0;

    //......................................... creer les QSimpleRichText ......................................
    //                                          d'affichage et d'impression

    m_pBodyRichText = new QSimpleRichText( *m_pBodyText, font, m_pBodyTextEdit->context(), m_pBodyTextEdit->styleSheet(),
					    m_pBodyTextEdit->mimeSourceFactory(), -1 );
    m_pHeadRichText = new QSimpleRichText( *m_pHeadText, font, m_pBodyTextEdit->context(), m_pBodyTextEdit->styleSheet(),
					    m_pBodyTextEdit->mimeSourceFactory(), -1 );
    m_pFootRichText = new QSimpleRichText( *m_pFootText, font, m_pBodyTextEdit->context(), m_pBodyTextEdit->styleSheet(),
					    m_pBodyTextEdit->mimeSourceFactory(), -1 );

    m_pCViewPageWidget->SetHead(m_pHeadRichText);
    m_pCViewPageWidget->SetBody(m_pBodyRichText);
    m_pCViewPageWidget->SetFoot(m_pFootRichText);
    AdjustHeightFootAndHead();
    UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
}

//-------------------------------------- AdjustHeightFootAndHead ----------------------------------------------
// soit il existe un parametre fixé et sauvegardé dans la base de données et alors on utilise ce parametre
// soit il n'existe pas et alors on prend la hauteur des documents entete et pieds de page

void DlgPrint::AdjustHeightFootAndHead()
{QString head_H  = "";
 QString foot_H  = "";
 CGestIni::Param_ReadParam( Global_ParamData, "Impression" , "Head_H",    &head_H);
 CGestIni::Param_ReadParam( Global_ParamData, "Impression" , "Foot_H",    &foot_H);

    if (m_pHeadRichText) //reajuster hauteur header à la taille de son contenu
    {   if (head_H != "")  m_pCViewPageWidget->SetHead_H(atof(head_H) / 10);
        else               m_pCViewPageWidget->SetHead_H(m_pCViewPageWidget->GetRichHead_H());
	lineEdit_Head_H->setText(QString::number(m_pCViewPageWidget->GetHead_H()*10));
	slider_Head_H->setValue ( (int)(m_pCViewPageWidget->GetHead_H()*100/m_PageH ) );
    }
    if (m_pFootRichText) //reajuster hauteur pied de page à la taille de son contenu
    {   if (foot_H != "")  m_pCViewPageWidget->SetFoot_H(atof(foot_H ) / 10);
        else               m_pCViewPageWidget->SetFoot_H(m_pCViewPageWidget->GetRichFoot_H());
	lineEdit_Foot_H->setText(QString::number(m_pCViewPageWidget->GetFoot_H()*10));
	slider_Foot_H->setValue ( (int)(m_pCViewPageWidget->GetFoot_H()*100/m_PageH ) );
    }
}

//-------------------------------------- initRichBodyText ----------------------------------------------------
void DlgPrint::initRichBodyText(QSimpleRichText* pRichText)
{   m_pBodyRichText        = pRichText;
    m_pCViewPageWidget->SetBody(pRichText);
}

//-------------------------------------- initRichHeadText ----------------------------------------------------
void DlgPrint::initRichHeadText(QSimpleRichText* pRichText)
{
    m_pHeadRichText        = pRichText;
    m_pCViewPageWidget->SetHead(pRichText);
    if (pRichText) //reajuster hauteur header à la taille de son contenu
    {m_pCViewPageWidget->SetHead_H(m_pCViewPageWidget->GetRichHead_H());
	lineEdit_Head_H->setText(QString::number(m_pCViewPageWidget->GetHead_H()*10));
	slider_Head_H->setValue ( (int)(m_pCViewPageWidget->GetHead_H()*100/m_PageH ) );
    }
}

//-------------------------------------- initRichFootText -------------------------------------------
void DlgPrint::initRichFootText(QSimpleRichText* pRichText)
{   m_pFootRichText        = pRichText;
    m_pCViewPageWidget->SetFoot(pRichText);
    if (pRichText) //reajuster hauteur pied de page à la taille de son contenu
    {   m_pCViewPageWidget->SetFoot_H(m_pCViewPageWidget->GetRichFoot_H());
	lineEdit_Foot_H->setText(QString::number(m_pCViewPageWidget->GetFoot_H()*10));
	slider_Foot_H->setValue ( (int)(m_pCViewPageWidget->GetFoot_H()*100/m_PageH ) );
	UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
    }
}

//-------------------------------------- pushButtonOk_clicked --------------------------------------------------
void DlgPrint::pushButtonOk_clicked()
{accept();
}

//-------------------------------------- pushButtonPrint_clicked ----------------------------------------------
void DlgPrint::pushButtonPrint_clicked()
{
    float      zoom = atof(lineEditZoom->text())/100;
    int    page_deb = atoi(lineEdit_PageDeb->text());
    int    page_fin = atoi(lineEdit_PageFin->text());
    //................ recuperer resolution affichée ............................
    int        resoX = atoi(lineEditResoX->text());
    int        resoY = atoi(lineEditResoY->text());

#ifdef Q_WS_X11
	QPaintDevice::x11SetAppDpiX (resoX-1);
        QPaintDevice::x11SetAppDpiY (resoY-1);
#endif
    m_pQPrinter->setResolution(resoY);
    //.............. borner page_fin au max possible ........................
    int nb_page = m_pCViewPageWidget->GetNbPage();
    if (page_fin>nb_page)
       {page_fin = nb_page;
	lineEdit_PageFin->setText(QString::number(page_fin));
       }

    //.............. borner page_deb au max et min possible ..................
    //
    QPainter* p = 0;
    if (page_deb<1)         page_deb = 1;
    if (page_deb>page_fin)  page_deb = page_fin;           //
    int page = page_deb;
    while(page <= page_fin )
    {   p = new QPainter( m_pQPrinter ); // logicalDpiX() and logicalDpiY()).
	m_pCViewPageWidget->PrintDoc(p, page, zoom ,zoom , 0, resoX, resoY);
	delete p;
	++page;
	if (page > page_fin) break;
	m_pQPrinter->newPage();
    }
    //delete pQPrinter;
}

//-------------------------------------- pushButtonQuitter_clicked ------------------------------------------------------
void DlgPrint::pushButtonQuitter_clicked()
{reject();
}

//-------------------------------------- pushButtonQuitter_clicked ------------------------------------------------------
void DlgPrint::pushButtonConfigurer_clicked()
{if ( m_pQPrinter->setup( this ) )
    {
    }
}

//-------------------------------------- pushButtonPrevious_clicked -----------------------------------------------------
void DlgPrint::pushButtonPrevious_clicked()
{m_pCViewPageWidget->SetCurrentPage(m_pCViewPageWidget->GetCurrentPage()-1);
    m_pCViewPageWidget->update ();
    UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
}

//-------------------------------------- pushButtonNext_clicked ---------------------------------------------------------
void DlgPrint::pushButtonNext_clicked()
{m_pCViewPageWidget->SetCurrentPage(m_pCViewPageWidget->GetCurrentPage()+1);
    m_pCViewPageWidget->update ();
    UpdateTextePageInfo(m_pCViewPageWidget->GetNbPage());
}





