/***************************************************************************
 *   Copyright (C) 2003-2005 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"
#include <qtimer.h>

ScimDragableFrame::ScimDragableFrame( QWidget *body, QWidget *parent, const char *name, WFlags f )
    : QFrame( parent, name, f), mousePressed( FALSE ), keepVisible( FALSE ), bodyWidget( body ), timer( 0 )
{
//     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;
}

bool ScimDragableFrame::scheduleAdjustSize()
{
    if(!timer)
    {
        timer = new QTimer(this);
        connect( timer, SIGNAL(timeout()), SLOT(adjustSize()) );
    }
    if(!timer->isActive()) {
        timer->start( 0, true );
        return true;    //another is waiting
    } else
        return false;
}

void ScimDragableFrame::adjustSize()
{
    if(isKeepVisible())
    {
      QFrame::adjustSize();
      QRect visiblerect = frameGeometry();

      if( !screenContainsRect(visiblerect) )
      {
          move( visiblerect.topLeft() );
      }
    }
    else if(bodyWidget) //otherwise if this widget is not keepvisible in screen,
        bodyWidget->adjustSize();  //then adjustSize() of bodyWidget should be called
    else
        QFrame::adjustSize();
}

bool ScimDragableFrame::screenContainsRect(QRect & visiblerect)
{
    if( !screen.contains( visiblerect ) )
    {
        QRect intersect = screen & visiblerect;
        if ( intersect.isEmpty() )
        {
            visiblerect.moveCenter( screen.center() );
        }
        else
        {
            if( intersect.contains( visiblerect.topLeft() ) )
            {
                visiblerect.moveBottomRight( intersect.bottomRight() );
            }
            else
            {
                visiblerect = intersect;
            }
        }
        return false;
    }

    return true;
}
#include "scimdragableframe.moc"
