1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/***************************************************************************
* 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), m_keepVisible( FALSE ), m_bodyWidget( body ), m_timer( 0 )
{
// ctrlDown = FALSE;
m_screen = QApplication::desktop()->screenGeometry();
}
bool ScimDragableFrame::isMouseButtonPressed()
{
return mousePressed;
}
void ScimDragableFrame::reTopParent(QWidget *body)
{
m_bodyWidget = body;
}
void ScimDragableFrame::mousePressEvent( QMouseEvent *e )
{
// ctrlDown = ( e->state() & ControlButton ) == ControlButton;
e->ignore();
if ( e->button() == LeftButton )
{
e->accept();
m_hadDblClick = FALSE;
mousePressed = TRUE;
if(m_bodyWidget)
m_offset = mapTo(m_bodyWidget, e->pos());
}
QFrame::mousePressEvent(e);
}
void ScimDragableFrame::mouseMoveEvent( QMouseEvent *e )
{
if ( mousePressed && e->pos() != m_offset )
{
// ctrlDown = ( e->state() & ControlButton ) == ControlButton;
if(m_bodyWidget)
m_bodyWidget->move(e->globalPos()-m_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();
m_hadDblClick = TRUE;
}
bool ScimDragableFrame::scheduleAdjustSize()
{
if(!m_timer)
{
m_timer = new QTimer(this);
connect( m_timer, SIGNAL(timeout()), SLOT(adjustSize()) );
}
if(!m_timer->isActive()) {
m_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(m_bodyWidget) //otherwise if this widget is not keepvisible in screen,
m_bodyWidget->adjustSize(); //then adjustSize() of m_bodyWidget should be called
else
QFrame::adjustSize();
}
bool ScimDragableFrame::screenContainsRect(QRect & visiblerect)
{
if( !m_screen.contains( visiblerect ) )
{
QRect intersect = m_screen & visiblerect;
if ( intersect.isEmpty() )
{
visiblerect.moveCenter( m_screen.center() );
}
else
{
if( intersect.contains( visiblerect.topLeft() ) )
{
visiblerect.moveBottomRight( intersect.bottomRight() );
}
else
{
visiblerect = intersect;
}
}
return false;
}
return true;
}
#include "scimdragableframe.moc"
|