//######
//## My_TreeView.cpp
//## ===============
//## 30.05.12: Creation
//## 30.05.12: 
//######

#include  <QtCore/QtCore>
#include  <QtGui/QtGui>

#include  "My_TreeView.h"
#include  "MySortFilterProxyModel.h"

//
// <<<< My_TreeView::My_TreeView
//
My_TreeView::My_TreeView( MySortFilterProxyModel *p_ProxyModel, bool p_b_Source )
    : QTreeView()
    , _ProxyModel( p_ProxyModel )
    , _b_Source  ( p_b_Source   )
{
  this->setRootIsDecorated( false );

  this->setAllColumnsShowFocus( true );
  this->sortByColumn( 0, Qt::AscendingOrder );

  connect( this, SIGNAL( SIGNAL_RightClicked( QModelIndex ) )
         , this, SLOT(        slot_PopupMenu( QModelIndex ) ) );
}
// >>>> My_TreeView::My_TreeView


//
// <<<< My_TreeView::~My_TreeView
//
My_TreeView::~My_TreeView()
{
}
// >>>> My_TreeView::~My_TreeView


//
// <<<< My_TreeView::slot_PopupMenu
//
void My_TreeView::slot_PopupMenu( QModelIndex p_Index )
{
  QString Champ, Type; 
  QModelIndex ViewIndex = p_Index;

  if ( ! ViewIndex.isValid() ) {
    return;
  }
  
  if ( _b_Source ) {
    Type  = "Source";
    Champ = _ProxyModel->sourceModel()->data( ViewIndex ).toString();
  } else {
    Type  = "Proxy";
    Champ = _ProxyModel->data( ViewIndex ).toString();
  }
  
  QMenu qPPM( QString( "slot_PopupMenu> ") + Type  );

  QAction * X_A = qPPM.addAction( QString( "Action A : " + Champ ) );
  qPPM.addSeparator();
  QAction * X_B = qPPM.addAction( QString( "Action B : " + Champ ) );
  QAction * X_C = qPPM.addAction( QString( "Action C : " + Champ ) );

  QPoint PM_Point = QCursor::pos() - QPoint( 16, 16 );

  QAction * ACT_x = qPPM.exec( PM_Point );

  if ( ACT_x == NULL ) return;

  QString Title = QString( "slot_PopupMenu> ") + Type;
  QString MsgTxt;

  if ( ACT_x == X_A ) {
      MsgTxt = QString( "Action A : " + Champ );
  } else   if ( ACT_x == X_B ) {
      MsgTxt = QString( "Action B : " + Champ );
  } else   if ( ACT_x == X_C ) {
      MsgTxt = QString( "Action C : " + Champ );
  } else {
    MsgTxt = "Hors Item";
  }

  QMessageBox::information( this, Title, MsgTxt );
}
// >>>>> My_TreeView::slot_PopupMenu


//
// <<<< My_TreeView::keyPressEvent
//
void My_TreeView::keyPressEvent( QKeyEvent * p_Event )
{
  bool b_IGNORE = false;

  QString     Champ, Type;
  QModelIndex ViewIndex = this->currentIndex();

  if ( ViewIndex.isValid() ) {

    if ( _b_Source ) {
      Type  = "Source";
      Champ = _ProxyModel->sourceModel()->data( ViewIndex ).toString();
    } else {
      Type  = "Proxy";
      Champ = _ProxyModel->data( ViewIndex ).toString();
    }

    QString Title = QString( "CTRL+G : ") + Type;
    QString MsgTxt;

    int e_key   = p_Event->key();
    int e_state = (int) p_Event->modifiers();
    
    switch ( e_key ) {
    case Qt::Key_G:

      if ( e_state & Qt::ControlModifier ) {
        MsgTxt = QString( "CTRL+G : " ) + Champ;
        QMessageBox::information( this, Title, MsgTxt );
      }
      break;
    default:
      break;
    }
  }

  if ( b_IGNORE ) {
    p_Event->ignore();
  } else {
    QTreeView::keyPressEvent( p_Event );
  }
}
// >>>> My_TreeView::keyPressEvent


//
// <<<< My_TreeView::mousePressEvent
//
void My_TreeView::mousePressEvent( QMouseEvent* p_Event )
{
  bool b_IGNORE = false;

  QString     Champ, Type;
  QModelIndex ViewIndex = this->currentIndex();

  if ( ViewIndex.isValid() ) {

    if ( _b_Source ) {
      Type  = "Source";
      Champ = _ProxyModel->sourceModel()->data( ViewIndex ).toString();
    } else {
      Type  = "Proxy";
      Champ = _ProxyModel->data( ViewIndex ).toString();
    }

    if ( p_Event->button() == Qt::RightButton ) {

      QPoint qPoint = QPoint( p_Event->x(), p_Event->y() ); // et non globalX

      emit SIGNAL_RightClicked( ViewIndex );
    
      b_IGNORE = true;
    }
  }

  if ( b_IGNORE ) {
    p_Event->ignore();
  } else {
    QTreeView::mousePressEvent( p_Event );
  }
}
// >>>> My_TreeView::mousePressEvent
