/********************************* CMDI_GroupedView.cpp **************************
 *                                                                                *
 *   #include "CMDI_GroupedView.h"                                                *
 *   Project: MedinTux                                                            *
 *   Copyright (C) 2003 2004 2005 by Sevin Roland  and Data Medical Design        *
 *   E-Mail: data.medical.design@club-internet.fr                                 *
 *                                                                                *
 *   This program is free software; you can redistribute it and/or modify         *
 *   it under the terms of the CeCILL Version 1 du 21/06/2004                     *
 *   License (GPL compatible)                                                     *
 *                              http://www.cecill.info/                           *
 *   as published by :                                                            *
 *                                                                                *
 *   Commissariat à l'Energie Atomique                                            *
 *   - CEA,                                                                       *
 *                            31-33 rue de la Fédération, 75752 PARIS cedex 15.   *
 *                            FRANCE                                              *
 *   Centre National de la Recherche Scientifique                                 *
 *   - CNRS,                                                                      *
 *                            3 rue Michel-Ange, 75794 Paris cedex 16.            *
 *                            FRANCE                                              *
 *   Institut National de Recherche en Informatique et en Automatique             *
 *   - INRIA,                                                                     *
 *                            Domaine de Voluceau, Rocquencourt, BP 105, 78153    *
 *                            FRANCE                                              *
 *                                                                                *
 *         This program is distributed in the hope that it will be useful,        *
 *         but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   *
 *         CeCILL  License (GPL compatible) for more details.                     *
 *                                                                                *
 **********************************************************************************/
#include <qfile.h>
#include <qfileinfo.h>
#include <qfiledialog.h> 

#include "CMDI_GroupedView.h"
#include <qlabel.h>
#include "CApp.h"
#include "drtux.h"
#include "../../MedinTuxTools/CHtmlTools.h"
#include "../../MedinTuxTools/CPrtQListBoxItem.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*! \class CMDI_GroupedView
 *  \brief Classe CMDI_GroupedView.
*/
//---------------------------------- CMDI_GroupedView -----------------------------------------------------------------------------
CMDI_GroupedView::CMDI_GroupedView( QWidget* parent, const char* name, int wflags,  
				    CMoteurBase *pCMoteurBase, RUBREC_LIST *pRubList,
                                    const char* num_GUID, const char* id_doss, 
				    const char* dossNom, const char* dossPrenom, const char* user)
    :CMDI_Generic( parent, name, wflags , pCMoteurBase, pRubList) //: QMainWindow( parent, name, wflags )
{ SetType(TYP_GROUPEDVIEW);
  SetPrintableState(TRUE);
  m_CustomMenuRequestState = 0;
  m_pMyEditText = 0;

  m_splitterHorizontal = new QSplitter( this, "splitterHorizontal" );
  m_splitterHorizontal->setOrientation( QSplitter::Horizontal );

  m_splitterVertical  = new QSplitter( m_splitterHorizontal, "splitterVertical" );
  m_splitterVertical->setOrientation( QSplitter::Vertical );

  m_splitterVerticalImbrique  = new QSplitter( m_splitterHorizontal, "splitterVertical" );
  m_splitterVerticalImbrique->setOrientation( QSplitter::Vertical );

  QLabel* lbl = new QLabel(TR("Zone pour : "), m_splitterVerticalImbrique);
//  QLabel* lbl2 = new QLabel(TR("Zone pour le traitement / et futurs vaccinations"), m_splitterVerticalImbrique);

  m_pRub_Centrale = new Rub_Centrale(m_splitterVertical);
  if (m_pRub_Centrale)
  { m_pRub_Centrale->setParams(m_pCMoteurBase,
			     m_pRubList,
			     num_GUID,
			     id_doss,
			     dossNom,
			     dossPrenom,
			     user,
			     m_splitterVertical);

    m_pMyEditText = m_pRub_Centrale->m_pMyEditText ;
    m_splitterVertical->setResizeMode ( m_pRub_Centrale, QSplitter::Auto);
    m_splitterHorizontal->setResizeMode ( m_splitterVertical, QSplitter::Auto);

    Rub_Centrale_TTT* pRub_Centrale_TTT = new Rub_Centrale_TTT(m_splitterVerticalImbrique, "Rub_Centrale_TTT");
    

//   m_splitterVerticalImbrique->setResizeMode ( , QSplitter::Auto);

        //..................... connecter impression ......................................................
//        connect( m_pFormRubObservation->pushButtonPrint,     SIGNAL( clicked()),
//                 this,                                       SLOT  ( OnPrint())
//               );
        //.................. connecter les plugin de l'editeur de texte.............................................
        //                   au systeme de menu Custom

        connect( m_pMyEditText,   SIGNAL( Sign_ExeCommand(QString&)),
                this,             SLOT  ( Slot_ExeCommand(QString&))
               );
        connect( m_pMyEditText,   SIGNAL( Sign_Get_Custom_Menu(const QString &, QString&, QString &, QKeySequence &)),
                 this,            SLOT  ( Slot_Get_Custom_Menu(const QString &, QString&, QString &, QKeySequence &))
               );

       } // Fin if m_pRubCentrale

    setFocusProxy( m_pRub_Centrale );
    setCentralWidget( m_splitterHorizontal );
// qWarning("fin");
}

//---------------------------------- ~CMDI_GroupedView -----------------------------------------------------------------------------
CMDI_GroupedView::~CMDI_GroupedView()
{emit Sign_RubIsDeleted(CMDI_GroupedView::S_GetRubName());
 if (G_pCApp->m_pDrTux) G_pCApp->m_pDrTux->m_pCMDI_GroupedView = 0;
}

//---------------------------------- GetCurrent_RubList_ID -----------------------------------------------------------------------------
int     CMDI_GroupedView::GetCurrent_RubList_ID()
{IfModified_SaveInRubList();
 return m_pRub_Centrale->Current_RubList_ID();   // noter rubrique affichée;
}

//---------------------------------- Current_RubList_Iterator -----------------------------------------------------------------------------
RUBREC_LIST::iterator CMDI_GroupedView::Current_RubList_Iterator()
{return m_pRub_Centrale->Current_RubList_Iterator();
}

 
//----------------------------- initData -----------------------------------
/*! \brief initialise la rubrique avec les données la concernant */
int  CMDI_GroupedView::initData()
{// m_pFormRubObservation->m_LastRub    = -1;   // tres tres important 
  m_pRub_Centrale->setData();
  return 0;
}

//----------------------------- SetCurentDocByPrimkey -----------------------------------
/*! \brief initialise l'affichage sur le document désigné par const QString &pk */
int  CMDI_GroupedView::SetCurentDocByPrimkey(const QString &pk)
{return m_pRub_Centrale->SetCurentDocByPrimkey(pk);
}

//----------------------------- ChangeCurrentComboBoxItemName -----------------------------------
/*! \brief SLOT qui change le nom de l'item courant de la comboBox 
 * \todo A CODER
*/
void CMDI_GroupedView::ChangeCurrentComboBoxItemName(const QString &name)
{
//  if (name.length()==0)               return;
//  CPrtQListBoxItem* pCPrtQListBoxItem  = (CPrtQListBoxItem*) m_pFormRubObservation->comboBox_RubName->listBox()->item (  m_pFormRubObservation->comboBox_RubName->currentItem ()  );
//  if (!pCPrtQListBoxItem)             return;
//  pCPrtQListBoxItem->setText ( name );
}

//----------------------------- GetCurrentEditor -----------------------------------
/*! \brief retourne l'éditeur de texte courant avancé type MyEditText */
MyEditText  *CMDI_GroupedView::GetCurrentEditor()
{return m_pMyEditText;
}

//---------------------------------- SetInterfaceOnDroits ----------------------------------------------------------------------------------------
/*! \brief positionne l'interface sur les droits */
void CMDI_GroupedView::SetInterfaceOnDroits(const QString &/*droits*/)
{//m_pFormRubObservation->comboBox_RubName_activated( m_pFormRubObservation->comboBox_RubName->currentItem() );
}

//---------------------------------- AddNewDocument -----------------------------------------------------------------------------
/*! \brief replace le document en cours en replaçant le contenu dans la liste des rubriques, et ajoute un nouveau document à la liste */
void CMDI_GroupedView::AddNewDocument(const QString &/*strData*/, int /*typ*/, const char */*path =0 */, const QDateTime */*pQDateTime=0*/, const QString */*libelle=0*/, const QString */*subType=0*/)
{
}

//---------------------------------- IfModified_SaveInRubList -----------------------------------------------------------------------------
void CMDI_GroupedView::IfModified_SaveInRubList()
{m_pRub_Centrale->IfModified_SaveInRubList();
}

//---------------------------------- OnPrint -----------------------------------------------------------------------------
void CMDI_GroupedView::OnPrint()
{m_pRub_Centrale->IfModified_SaveInRubList();
 emit Sign_Print();
}

//---------------------------------- RubOnUserChange -----------------------------------------------------------------------------
void CMDI_GroupedView::RubOnUserChange(const char*user, const char*/*sign_user*/)
{m_pRub_Centrale->m_User       = user;
}

//---------------------------------- OnIdentChange ----------------------------------------------------------------------------------
// ACTION: ce slot est appelé par qui veut, pour signaler un changement d'identité à cette fenetre MDI
//         qui doit donc adapter son contenu à un nouveau malade.

void CMDI_GroupedView::OnIdentChange(const char* num_GUID,  const char* id_doss,
                                     const char* dossNom ,  const char* dossPrenom )
{ m_pRub_Centrale->m_NumGUID    = num_GUID;
  m_pRub_Centrale->m_ID_Doss    = id_doss;
  m_pRub_Centrale->m_DossNom    = dossNom;
  m_pRub_Centrale->m_DossPrenom = dossPrenom;
//  m_pFormRubObservation->m_LastRub    = -1;          // tres tres important
  m_pRub_Centrale->setData();
}
//---------------------------------- SetZoom ----------------------------------------------------------------------------------------
void CMDI_GroupedView::SetZoom(int /*zoom*/)
{//m_pMyEditText->SetZoom (zoom);
}
//---------------------------------- Slot_Get_Custom_Menu -----------------------------------------------------------------------------
void CMDI_GroupedView::Slot_Get_Custom_Menu(const QString &state, QString &menuIcon, QString &menuItem, QKeySequence &accel)
{if (state == "$StartRequest")    m_CustomMenuRequestState = 0;
 else                           ++m_CustomMenuRequestState;
 switch(m_CustomMenuRequestState)
   {case 0:
         menuIcon = G_pCApp->m_PathAppli + "Ressources/icones/backup_section.png";
         menuItem = tr("Enregi&strer comme document type");
         accel    = CTRL+Key_S;
         break;
    default:
         menuIcon = "";
         menuItem = "";
   }
}
//---------------------------------- Slot_ExeCommand -----------------------------------------------------------------------------
void CMDI_GroupedView::Slot_ExeCommand(QString &command)
{  RUBREC_LIST::iterator it = Current_RubList_Iterator();
   if (it ==  m_pRub_Centrale->m_pRubList->end() )  return;

   if (command==tr("Enregi&strer comme document type"))
      { QString   path    = G_pCApp->m_PathGlossaire + S_GetRubName();
        //int        typ    = (*it).m_Type.toInt();
        QString    ext    = ".html";
        QString    txt    = m_pMyEditText->text();
        QString  filtre   = tr( " Fichiers HTML (*.htm *.html);;  Tous les fichiers (*)");
         //.................. selectionner le fichier .......................................
        QFileDialog fd (path,  filtre,  this, "SaveDlg" ) ;
        fd.setMode(QFileDialog::AnyFile );    // selection multiple
        if ( fd.exec() != QDialog::Accepted )           return;
        QString fn = fd.selectedFile();
        if ( fn.isEmpty() )                             return;
        fn         = QFileInfo(fn).dirPath() + "/" + QFileInfo(fn).baseName() + ext;
        QFile file( fn);
        if ( !file.open( IO_WriteOnly ) )               return;
        QTextStream ts( &file );
        CHtmlTools::ElimineAncresRedondantes(txt);
        CHtmlTools::setTextDefaultAtribut(txt, "text",          m_pMyEditText->m_DefaultTextColor);
        CHtmlTools::setTextDefaultAtribut(txt, "bgcolor",       m_pMyEditText->m_DefaultBackgroundColor);
        CHtmlTools::QRichTextToHtml(txt);
        ts << txt;
      }
}
//---------------------------------- setWordWrap ----------------------------------------------------------------------------------------
void CMDI_GroupedView::setWordWrap(int width)
{if (m_pMyEditText==0) return;
 if (width == -1)
    {m_pMyEditText->setWordWrap(QTextEdit::WidgetWidth);   //NoWrap, WidgetWidth, FixedPixelWidth, FixedColumnWidth
     //setWrapColumnOrWidth (793-37 );
    }
 else
    {m_pMyEditText->setWordWrap (QTextEdit::FixedPixelWidth);
     m_pMyEditText->setWrapColumnOrWidth (width );  //   (793-37 );   //( corresponds A4 width  21*72/2.54 )
    }
}
//---------------------------------- SetModifiableState -----------------------------------------------------------------------------
void CMDI_GroupedView::SetModifiableState(int state)
{// m_pRub_Centrale->SetModifiableState(state);
}


/*! \brief Slot appelé pour changer de rubrique courante. 
 * Cette fonction place l'item courant sur la rubrique demandée, et met à jour le texte.
*/
void CMDI_GroupedView::Slot_ChangeViewToRub( QString idRubList )
{ m_pRub_Centrale->changeViewToRub( idRubList.toInt() );

}


