/***************************************************************************
 *   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 "scimdragableframe.h"

ScimDragableFrame::ScimDragableFrame( QWidget *body, QWidget *parent, const char *name, WFlags f )
    : QFrame( parent, name, f), mousePressed( FALSE ), keepVisible( TRUE ), bodyWidget( body ) 
{
    ctrlDown = FALSE;
    screen = QApplication::desktop()->screenGeometry();
}
void ScimDragableFrame::reTopParent(QWidget *body)
{
  bodyWidget = body;
}

void ScimDragableFrame::mousePressEvent( QMouseEvent *e )
{
    ctrlDown = ( e->state() & ControlButton ) == ControlButton;
    e->ignore();
    if ( e->button() != LeftButton )
        return;
    e->accept();
    hadDblClick = FALSE;
    mousePressed = TRUE;
    if(bodyWidget)
      offset = mapTo(bodyWidget, e->pos());
}
void ScimDragableFrame::mouseMoveEvent( QMouseEvent *e )
{
    if ( !mousePressed || e->pos() == offset )
        return;
    ctrlDown = ( e->state() & ControlButton ) == ControlButton;
    if(bodyWidget)
      bodyWidget->move(e->globalPos()-offset);
}
void ScimDragableFrame::mouseReleaseEvent( QMouseEvent * )
{
    ctrlDown = FALSE;
    if ( !mousePressed )
        return;
    mousePressed = FALSE;
    adjustSize();
}

void ScimDragableFrame::mouseDoubleClickEvent( QMouseEvent *e )
{
    e->ignore();
    if ( e->button() != LeftButton )
        return;
    e->accept();
    emit doubleClicked();
    hadDblClick = TRUE;
}
void ScimDragableFrame::adjustSize()
{
    if(isKeepVisible())
    {
      QFrame::adjustSize();
      QRect visiblerect = frameGeometry();
  
      if( !screen.contains( visiblerect ) )
      {
          QRect intersect = screen & visiblerect;
          if ( intersect.isEmpty() )
          {
              visiblerect.moveCenter( screen.center() );
              move( visiblerect.topLeft() );
          }
          else
          {
              if( intersect.contains( visiblerect.topLeft() ) )
              {
                  visiblerect.moveBottomRight( intersect.bottomRight() );
                  move( visiblerect.topLeft() );
              }
              else
              {
                  move( intersect.topLeft() );
              }
          }
      }
    }
    else if(bodyWidget) //otherwise if this widget is not keepvisible in screen,
      bodyWidget->adjustSize();  //then adjustSize() of bodyWidget should be called
}
#include "scimdragableframe.moc"
