/***************************************************************************
 *   Copyright (C) 2003 by liuspider                                       *
 *   liuspider@users.sourceforge.net                                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "scimlookuptable.h"
#include "scimmovehandle.h"

#include <kiconloader.h>

ScimLookupTable::ScimLookupTable(QWidget *parent, QVBoxLayout* _dockLayout, const char *name, bool _isVertical)
 : ScimListBox(parent, name, _isVertical), attachedParent(parent), dockWindowAreaLayout(_dockLayout)
{
    setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0) );
    
    PrePageBtn = new QToolButton( this, "PrePageBtn" );
    NextPageBtn = new QToolButton( this, "NextPageBtn" );
    
    ModeSwitchBtn = new ScimMoveHandle( attachedParent, this );
    ModeSwitchBtn->setKeepVisible( false );
    
    bottomLineLayout = new QHBoxLayout(0,"bottomLineLayout");
    BottomSpacer = new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum );
    bottomLineLayout->addItem( BottomSpacer );
    bottomLineLayout->addWidget( PrePageBtn, 0, Qt::AlignRight);
    bottomLineLayout->addWidget( NextPageBtn, 0, Qt::AlignRight);
    bottomLineLayout->addWidget( ModeSwitchBtn, 0, Qt::AlignRight);
    
    QIconSet iconSet;
    if( layout->direction() == QBoxLayout::TopToBottom )
    {
      iconSet = SmallIconSet( "up" );
      PrePageBtn->setIconSet( iconSet );
      iconSet = SmallIconSet( "down" );
      NextPageBtn->setIconSet( iconSet );
    }
    else
    {
      iconSet = SmallIconSet( "back" );
      PrePageBtn->setIconSet( iconSet );
      iconSet = SmallIconSet( "forward" );
      NextPageBtn->setIconSet( iconSet );
    }
    connect( PrePageBtn, SIGNAL( clicked() ), this, SIGNAL( previousPageRequested() ) );
    connect( NextPageBtn, SIGNAL( clicked() ), this, SIGNAL( nextPageRequested() ) );
    connect( ModeSwitchBtn, SIGNAL( doubleClicked() ), this, SLOT( switchMode() ) );
}


ScimLookupTable::~ScimLookupTable(){}
void ScimLookupTable::setEnableMouse(bool b)
{
    setUpdatesEnabled(FALSE);
    layout->removeItem(bottomLineLayout);
    ScimListBox::setEnableMouse(b);
    layout->addItem(bottomLineLayout);
    setUpdatesEnabled(TRUE);
}
void ScimLookupTable::switchMode()
{
  if(attachedParent == parentWidget())
  {
    reparent(0,
      Qt::WStyle_Customize|Qt::WStyle_NoBorder|Qt::WStyle_StaysOnTop|Qt::WX11BypassWM|Qt::WStyle_Tool,
      mapToGlobal(geometry().topLeft()), true);
    reTopParent(this);
    setKeepVisible( true );
    ModeSwitchBtn->reTopParent(this);
    dockWindowAreaLayout->remove(this);
    attachedParent->adjustSize();
    adjustSize();
  }
  else
  {
    reparent(attachedParent,
      Qt::WStyle_Customize|Qt::WStyle_NoBorder|Qt::WStyle_StaysOnTop|Qt::WX11BypassWM|Qt::WStyle_Tool,
      geometry().topLeft(), true);
    reTopParent(attachedParent);
    setKeepVisible( false );
    ModeSwitchBtn->reTopParent(attachedParent);
    dockWindowAreaLayout->addWidget(this);
    attachedParent->adjustSize();
  }
}
void ScimLookupTable::enablePreviousPage( bool b )
{
  PrePageBtn->setEnabled(b);
}

void ScimLookupTable::enableNextPage( bool b )
{
  NextPageBtn->setEnabled(b);
}
#include "scimlookuptable.moc"
