/********************************* CPrtQListViewItem.cpp **************************
 *                                                                                *
 *   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.                     *
 *                                                                                *
 **********************************************************************************/
// C++ Implementation: CPrtQListViewItem.cpp
//
// Description:
//
//
// Author: Eric MAEKER <eric.maeker.fr>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "CPrtQListViewItem.h"
#include <qfontmetrics.h>

/*! \class CPrtQListViewItem
 * \brief réimplémentation de la classe QListViewItem dont elle hérite pour améliorer l'affichage. Une réécriture de la fonction compare permet de trier la colonne des date correctement.
 * Attention cette fonction est dépendante de la classe Theme pour les couleurs.
*/


//-------------------------------- CPrtQListViewItem ----------------------------------------------------
CPrtQListViewItem::CPrtQListViewItem(QListViewItem * listview ,
				     QListViewItem * after ,
				     const QString libelle )
    : QListViewItem(listview, after, libelle)
{ // initialise les valeurs
  m_Libelle = libelle;
  m_signUser="";
  m_User="";
  m_LightBackground=FALSE;
  m_ID_Rublist="";
  m_SubType="";
  m_Type="";
  m_pkDoc="";
  m_GUID = "";
  m_NomPatient = "";
  m_PrenomPatient = "";
  m_Modified=FALSE;
  columnStyleInitialise( listview->listView()->columns() );
}

CPrtQListViewItem::CPrtQListViewItem(QListView     * listview ,
				     QListViewItem * after ,
				     const QString libelle )
    : QListViewItem(listview, after, libelle), m_Libelle(libelle)
{ // initialise les valeurs
  //m_Libelle = libelle;
  m_signUser="";
  m_User="";
  m_LightBackground=FALSE;
  m_ID_Rublist="";
  m_SubType="";
  m_Type="";
  m_pkDoc="";
  m_GUID = "";
  m_NomPatient = "";
  m_PrenomPatient = "";
  m_Modified=FALSE;
  columnStyleInitialise( listview->columns() );
}


//-------------------------------- CPrtQListViewItem ----------------------------------------------------
CPrtQListViewItem::CPrtQListViewItem(QListView * listview, 
				     const QString &libelle ,
 				     const QString &iD_Rublist,
				     const QString &date, 
				     const QString &pkDoc, 
				     const QString &subType,
				     const QString &type,
				     const QString &signUser,
				     const QString &user)
    : QListViewItem(listview), m_Type(type), m_ID_Rublist(iD_Rublist), m_User(user), m_SubType(subType), m_Libelle(libelle)
{ QString tmp = "";
  if (date != "" ) m_Date = QDateTime::fromString(date, Qt::ISODate);
  m_pkDoc = pkDoc;
  m_signUser = signUser;
  m_GUID = "";
  m_NomPatient = "";
  m_PrenomPatient = "";

  if (m_Type.left(4) == "2003") tmp = "O";
  else if (m_Type.left(4) == "2002") tmp = "P";
  else if (m_Type.left(4) == "2008") tmp = "D";

  // Elimine les terrains
  if (m_Type.left(4) != "2006")
  { switch (listview->columns())
    { case 1 : { QListViewItem::setText(0, m_Libelle );
		 break ;
	       }
      case 3 : { QListViewItem::setText(0, tmp );
		 QListViewItem::setText(1,m_Date.toString("dd MMM yyyy") );
		 QListViewItem::setText(2,m_Libelle );
		 break ;
	       }
      case 5 : { //QListViewItem::setText(TYPE_COLUMN, tmp );
		 QListViewItem::setText(LISTVIEW_DATE_COLUMN,m_Date.toString("dd MMM yyyy") );
		 QListViewItem::setText(LISTVIEW_LIBELLE_COLUMN,m_Libelle );
		 break ;
	       }

    } // end switch
  }
  m_Modified=FALSE;
  columnStyleInitialise( listview->columns() );
}


//-------------------------------- CPrtQListViewItem ----------------------------------------------------
CPrtQListViewItem::CPrtQListViewItem(QListViewItem * listview, 
				     const QString &libelle ,
 				     const QString &iD_Rublist,
				     const QString &date, 
				     const QString &pkDoc, 
				     const QString &subType,
				     const QString &type,
				     const QString &signUser,
				     const QString &user)
    : QListViewItem(listview), m_Type(type), m_ID_Rublist(iD_Rublist), m_User(user), m_SubType(subType), m_Libelle(libelle)
{ QString tmp = "";
  if (date != "" ) m_Date = QDateTime::fromString(date, Qt::ISODate);
  m_pkDoc    = pkDoc;
  m_signUser = signUser;
  m_GUID = "";
  m_NomPatient = "";
  m_PrenomPatient = "";

  if (m_Type.left(4) == "2003") tmp = "O";
  else if (m_Type.left(4) == "2002") tmp = "P";
  else if (m_Type.left(4) == "2008") tmp = "D";

  // Elimine les terrains
  if (m_Type.left(4) != "2006")
  { switch (listview->listView()->columns())
    { case 1 : { QListViewItem::setText(0, m_Libelle );
		 break ;
	       }
      case 3 : { QListViewItem::setText(0, tmp );
		 QListViewItem::setText(1,m_Date.toString("dd MMM yyyy") );
		 QListViewItem::setText(2,m_Libelle );
		 break ;
	       }
    } // end switch
  }
  m_Modified=FALSE;
  columnStyleInitialise( listview->listView()->columns() );
}

/*! \brief Destructeur de la classe qu'il faut réimplémenter sinon erreur lors des deletes d'items.
*/
CPrtQListViewItem::~CPrtQListViewItem()
{/* while (QListViewItem::childCount() != 0)
  { CPrtQListViewItem* pCprt = (CPrtQListViewItem*) QListViewItem::firstChild();
  delete pCprt;
  }*/
}

/*! \brief Prépare un tableau de booléen qui contiendront le style à utiliser : bold, italic.
 *  \sa columnIsBold , columnIsItalic
*/
void CPrtQListViewItem::columnStyleInitialise(int nbColumns)
{ columnIsBold.resize(nbColumns);
  columnIsItalic.resize(nbColumns);
  columnIsUnderline.resize(nbColumns);
  for (int i = 0; i< nbColumns;++i)
  { columnIsBold[i] = FALSE; 
    columnIsItalic[i] = FALSE;
    columnIsUnderline[i] = FALSE;
  }
}

//-------------------------------- compare ----------------------------------------------------
/*! \brief Réimplentation de la fonction pour permettre le trie par date.
*/
int CPrtQListViewItem::compare( QListViewItem *i, int col, bool asc ) const
// Returns < 0 if this item is less than i, 0 if they are equal and > 0 if this item is greater than i.
{  CPrtQListViewItem* pCPrtQListViewItem = (CPrtQListViewItem*)i;
   int retour = 0;
   if( col == LISTVIEW_DATE_COLUMN ) // Si colonne de la date
   { if (m_Date > pCPrtQListViewItem->m_Date)  retour = 1;
     else if (m_Date == pCPrtQListViewItem->m_Date) retour = 0;
     else retour = -1;
     return retour;
   }
   else
      return QListViewItem::compare( i, col, asc );
}


//-------------------------------- setModified ----------------------------------------------------
/*! \brief définit l'item courant comme modifié. Sera utilisé pour gérer les rubrecords à sauvegarder dans la base par CMDI_GroupedView. 
*/
void CPrtQListViewItem::setModified( bool m )
{ m_Modified = m; }


//-------------------------------- paint ----------------------------------------------------
/*! \brief Réimplémentation de la fonction paint du ListViewItem pour l'esthétisme du bestiaux.
 *  Permet d'utiliser les tags HTML : <b></b> , <i></i>  et <u></u>  pour l'ensemble de la colonne uniquement.
*/
void CPrtQListViewItem::paintCell( QPainter * p, const QColorGroup & cg, int column, int width, int align )
{ QString tmp = "";
  // On déclare une nouvelle couleur
  QColor bk, txt;
  QFont font = p->font();

  // Mettre en gras la ligne si le document à été modifié et reste à sauvegarder
  if (m_Modified)
  { font.setBold( TRUE );
    p->setFont( font );
  }

  // Recherche les <b> et <i> dans le libelle de la colonne
  tmp = QListViewItem::text(column);
  if (tmp.find("<b>") != -1)
  { // sauvegarde le style à utiliser
    columnIsBold[column] = TRUE;
    // enleve les tags html
    tmp.replace("<b>","");
    tmp.replace("</b>","");
    QListViewItem::setText(column, tmp);
  }
  if (QListViewItem::text(column).find("<i>") != -1)
  { // sauvegarde le style à utiliser
    columnIsItalic[column] = TRUE;
    // enleve les tags html
    tmp.replace("<i>","");
    tmp.replace("</i>","");
    QListViewItem::setText(column, tmp);
  }
  if (QListViewItem::text(column).find("<u>") != -1)
  { // sauvegarde le style à utiliser
    columnIsUnderline[column] = TRUE;
    // enleve les tags html
    tmp.replace("<u>","");
    tmp.replace("</u>","");
    QListViewItem::setText(column, tmp);
  }

  if (columnIsBold[column])
  { font.setBold( TRUE );
    p->setFont( font );
  }
  if (columnIsItalic[column])
  { font.setItalic( TRUE );
    p->setFont( font );
  }
  if (columnIsUnderline[column])
  { font.setUnderline( TRUE );
    p->setFont( font );
  }

  // En fonction de la ligne on change le background
  int pos    = QListViewItem::itemPos();
  int height = QListViewItem::height () ;
  if (((pos / height) % 2) == 0)  
  { bk = Theme::getRapidAccess_LighterBackground(); 
    m_LightBackground=TRUE;
  }
  else
  { bk = Theme::getRapidAccess_DarkerBackground(); 
    m_LightBackground=FALSE;
  }

  // En fonction du type on change la couleur de la police
  if (m_Type.left(4) == "2003")      txt = Theme::getRapidAccess_FontColor_Obs();
  else if (m_Type.left(4) == "2002") txt = Theme::getRapidAccess_FontColor_Prescr();
  else if (m_Type.left(4) == "2008") txt = Theme::getRapidAccess_FontColor_Doc();
  else if (m_Type.left(4) == "2006") txt = Theme::getRapidAccess_FontColor_Terrain();

  // On sauvegarde la palette de couleurs
  QColorGroup _cg( cg );
  QColor old_bk  = _cg.background();
  QColor old_txt = _cg.text();

  // On modifie la palette de couleurs en définissant
  // notre nouvelle couleur comme couleur de texte

  _cg.setColor( QColorGroup::Base, bk );
  _cg.setColor( QColorGroup::Text, txt );
  _cg.setColor( QColorGroup::HighlightedText, txt );
  _cg.setColor( QColorGroup::Highlight, Theme::getRapidAccess_HighLightColor() );



  // On appelle la méthode virtuelle de l'ancêtre avec la nouvelle palette
  QListViewItem::paintCell( p, _cg, column, width, align );

  _cg.setColor( QColorGroup::Base, old_bk );

}


//-------------------------------- SetValue ----------------------------------------------------
void CPrtQListViewItem::setValue(const QString &iD_Rublist, const QString &type, const QString &user, const QString &subType)
{m_Type              = type;
 m_ID_Rublist        = iD_Rublist;
 m_User              = user;
 m_SubType           = subType;
}

//-------------------------------- GetValue ----------------------------------------------------
void CPrtQListViewItem::getValue(QString &iD_Rublist, QString &type, QString &user, QString &subType)
{type            = m_Type;
 iD_Rublist      = m_ID_Rublist;
 user            = m_User;
 subType         = m_SubType;
}

//-------------------------------- get_ID_Rublist ----------------------------------------------------
QString CPrtQListViewItem::get_ID_Rublist()
{ return m_ID_Rublist; }
//-------------------------------- GetPkDoc ----------------------------------------------------
QString CPrtQListViewItem::getPkDoc()
{ return m_pkDoc; }
//-------------------------------- GetType ----------------------------------------------------
QString CPrtQListViewItem::getType()
{ return m_Type; }
//-------------------------------- GetUser ----------------------------------------------------
QString CPrtQListViewItem::getUser()
{ return m_User; }
//-------------------------------- GetSubType ----------------------------------------------------
QString CPrtQListViewItem::getSubType()
{ return m_SubType; }
//-------------------------------- GetLibelle ----------------------------------------------------
QString CPrtQListViewItem::getLibelle()
{ return m_Libelle; }
//-------------------------------- getGUID ----------------------------------------------------
QString CPrtQListViewItem::getGUID()
{ return m_GUID; }
//-------------------------------- getNom ----------------------------------------------------
QString CPrtQListViewItem::getNom()
{ return m_NomPatient; }
//-------------------------------- getPrenom ----------------------------------------------------
QString CPrtQListViewItem::getPrenom()
{ return m_PrenomPatient; }
//-------------------------------- GetBackgroundColor ----------------------------------------------------
bool CPrtQListViewItem::isLightBackground()
{ return m_LightBackground; }


//-------------------------------- setNom ----------------------------------------------------
void CPrtQListViewItem::setNom(QString n)
{ m_NomPatient = n; }
//-------------------------------- setPrenom ----------------------------------------------------
void CPrtQListViewItem::setPrenom(QString n)
{ m_PrenomPatient = n; }
//-------------------------------- setGUID ----------------------------------------------------
void CPrtQListViewItem::setGUID(QString n)
{ m_GUID = n; }
//-------------------------------- SetPkDoc ----------------------------------------------------
void CPrtQListViewItem::setPkDoc(QString n)
{ m_pkDoc = n; }
//-------------------------------- Set_ID_Rublist ----------------------------------------------------
void CPrtQListViewItem::set_ID_Rublist(QString& n)
{ m_ID_Rublist = n; }
void CPrtQListViewItem::set_ID_Rublist(int n)
{ m_ID_Rublist = QString::number(n); }
//-------------------------------- SetType ----------------------------------------------------
void CPrtQListViewItem::setType(QString& n)
{ m_Type = n; }
//-------------------------------- SetUser ----------------------------------------------------
void CPrtQListViewItem::setUser(QString& n)
{ m_User = n; }

void CPrtQListViewItem::setSignUser(QString& n)
{ m_signUser = n; }

//-------------------------------- SetSubType ----------------------------------------------------
void CPrtQListViewItem::setSubType(QString& n)
{ m_SubType = n; }
//-------------------------------- GetBackgroundColor ----------------------------------------------------
void CPrtQListViewItem::setLightBackground(bool c)
{ m_LightBackground = c; }

void CPrtQListViewItem::setDate(QString& n)
{ m_Date = QDateTime::fromString(n, Qt::ISODate); }

void CPrtQListViewItem::setDate(QDateTime& dt)
{ m_Date = dt; }

QString CPrtQListViewItem::getDate(QString& format)
{ return m_Date.toString( format ); }

QDateTime CPrtQListViewItem::getDate()
{ return m_Date; }

