/***************************************************************************
 *   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>

//this variable should be shared by all instances of this class
static bool mousePressed = false;

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

bool ScimDragableFrame::isMouseButtonPressed()
{
    return mousePressed;
}

void ScimDragableFrame::reTopParent(QWidget *body)
{
    bodyWidget = body;
}

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

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"
