/********************************* 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: Sevin Roland <upsis@club-internet.fr>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "CPrtQListViewItem.h"
#include <qfontmetrics.h>
//-------------------------------- CPrtQListViewItem ----------------------------------------------------
CPrtQListViewItem::CPrtQListViewItem(QListViewItem * listview, 
				     const QString &libelle ,
 				     const QString &pk,
				     const QString &date, 
				     const QString &pkDoc, 
				     const QString &subType,
				     const QString &type,
				     const QString &signUser,
				     const QString &user)
    : QListViewItem(listview), m_Type(type), m_Pk(pk), m_User(user), m_SubType(subType)
{ QString tmp = "";
  m_Date = QDateTime::fromString(date, Qt::ISODate);
  m_pkDoc = pkDoc;
  m_signUser = signUser;
  m_Libelle = libelle;
  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";
  if (m_Type.left(4) != "2006")
  { QListViewItem::setText(0, tmp );
    QListViewItem::setText(1,m_Date.toString("dd MMM yyyy") );
    QListViewItem::setText(2,m_Libelle );
  }
//  QListViewItem::setItemMargin ( 10 );
}

//-------------------------------- CPrtQListViewItem ----------------------------------------------------
CPrtQListViewItem::CPrtQListViewItem(QListView * listview, 
				     const QString &libelle ,
 				     const QString &pk,
				     const QString &date, 
				     const QString &pkDoc, 
				     const QString &subType,
				     const QString &type,
				     const QString &signUser,
				     const QString &user)
    : QListViewItem(listview), m_Type(type), m_Pk(pk), m_User(user), m_SubType(subType)
{ QString tmp = "";
  m_Date = QDateTime::fromString(date, Qt::ISODate);
  m_pkDoc = pkDoc;
  m_signUser = signUser;
  m_Libelle = libelle;
  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";
  if (m_Type.left(4) != "2006")
  { QListViewItem::setText(0, tmp );
    QListViewItem::setText(1,m_Date.toString("dd MMM yyyy") );
    QListViewItem::setText(2,m_Libelle );
  }
//  QListViewItem::setItemMargin ( 10 );
}

//-------------------------------- compare ----------------------------------------------------
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 == 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 );
}


//-------------------------------- paint ----------------------------------------------------
void CPrtQListViewItem::paintCell( QPainter * p, const QColorGroup & cg, int column, int width, int align )
{ // On déclare une nouvelle couleur
  QColor bk, txt;

  // En fonction de la ligne on change le background
  int pos    = QListViewItem::itemPos();
  int height = QListViewItem::height () ;
  if (((pos / height) % 2) == 0)  bk = LIGHT_COLOR;
  else                            bk = DARK_COLOR;

  // En fonction du type on change la couleur de la police
  if (m_Type.left(4) == "2003") txt = OBSERV_COLOR;
  else if (m_Type.left(4) == "2002") txt = PRESCR_COLOR;
  else if (m_Type.left(4) == "2008") txt = DOCUMENT_COLOR;
  else if (m_Type.left(4) == "2006") txt = TERRAIN_COLOR;

  // 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 );

  // 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 );
  


//   QListViewItem::paintCell( p, _cg, column, width, align );
//   // On rétablit la palette de couleurs "standard"
//   _cg.setColor( QColorGroup::Background, oldBgd );
// 
//   QListView* pContainer = __super::listView();
//    int iPos    = pContainer->itemPos(this);
//    int iHeight = this->totalHeight();
//    QRect theRect(0, 0, width, iHeight);
//    if(width > 0 && iHeight > 0)
//    {
//       if(((int)(iPos/iHeight)) & 0x0001)
//       {// index = odd, don't draw the background.
//          pContainer->setPaletteBackgroundColor(QColor(255, 255, 255));
//       }
//       else
//       {// index = even, draw the background.
//          pContainer->setPaletteBackgroundColor(QColor(211, 228, 252));
//       }
//    }


}


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

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

//-------------------------------- GetPk ----------------------------------------------------
QString CPrtQListViewItem::getPk()
{ return m_Pk; }
//-------------------------------- GetType ----------------------------------------------------
QString CPrtQListViewItem::getType()
{ return m_Type; }
//-------------------------------- GetUser ----------------------------------------------------
QString CPrtQListViewItem::getUser()
{ return m_User; }
//-------------------------------- GetSubType ----------------------------------------------------
QString CPrtQListViewItem::getSubType()
{ return m_SubType; }

//-------------------------------- SetPk ----------------------------------------------------
void CPrtQListViewItem::setPk(QString& n)
{ m_Pk = n; }
//-------------------------------- SetType ----------------------------------------------------
void CPrtQListViewItem::setType(QString& n)
{ m_Type = n; }
//-------------------------------- SetUser ----------------------------------------------------
void CPrtQListViewItem::setUser(QString& n)
{ m_User = n; }
//-------------------------------- SetSubType ----------------------------------------------------
void CPrtQListViewItem::setSubType(QString& n)
{ m_SubType = n; }

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; }

