Code:
void GraphicsViewMesh::mousePressEvent ( QMouseEvent * me )
{
if(me->button()==Qt::LeftButton) {
onLButtonDown( ( me->buttons() | me->modifiers() ),me->pos() );
}
}
void GraphicsViewMesh::mouseMoveEvent ( QMouseEvent * me )
{
onMouseMove( me->buttons(), me->pos() );
}
void GraphicsViewMesh::mouseReleaseEvent ( QMouseEvent * me )
{
if(me->button()==Qt::LeftButton) {
onLButtonUp( me->buttons(), me->pos() );
}
}
void GraphicsViewMesh::onLButtonDown( const int/*Qt::MouseButtons*/ nFlags, const QPoint point )
{
// save the current mouse coordinate in min
myXmin = point.x();
myYmin = point.y();
myXmax = point.x();
myYmax = point.y();
}
void GraphicsViewMesh::onLButtonUp( Qt::MouseButtons nFlags, const QPoint point )
{
DrawRectangle( myXmin, myYmin, myXmax, myYmax, false );//,LongDash);
myXmax = point.x();
myYmax = point.y();
fitArea( myXmin, myYmin, myXmax, myYmax );
myCurrentMode = CurAction_Nothing;
updateGL();
}
void GraphicsViewMesh::onMouseMove( Qt::MouseButtons nFlags, const QPoint point )
{
if ( nFlags & Qt::LeftButton )
{
myXmax = point.x();
myYmax = point.y();
DrawRectangle( myXmin, myYmin, myXmax, myYmax, false );
DrawRectangle( myXmin, myYmin, myXmax, myYmax, true );
}
}
void GraphicsViewMesh::DrawRectangle(const int MinX, const int MinY,
const int MaxX, const int MaxY, const bool Draw)
{
static double StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
static bool m_IsVisible;
StoredMinX = (MinX < MaxX) ? MinX: MaxX ;
StoredMinY = (MinY < MaxY) ? MinY: MaxY ;
StoredMaxX = (MinX > MaxX) ? MinX: MaxX ;
StoredMaxY = (MinY > MaxY) ? MinY: MaxY ;
QRect aRect;
aRect.setRect( StoredMinX, StoredMinY, abs(StoredMaxX-StoredMinX), abs(StoredMaxY-StoredMinY));
if ( !myRectBand )
{
myRectBand = new QRubberBand( QRubberBand::Rectangle, this );
myRectBand->setStyle(new QWindowsStyle);
myRectBand->setGeometry( aRect );
myRectBand->show();
QPalette palette;
palette.setColor(myRectBand->foregroundRole(), Qt::white);
myRectBand->setPalette(palette);
}
if ( m_IsVisible && !Draw ) // move or up : erase at the old position
{
myRectBand->hide();
delete myRectBand;
myRectBand = 0;
m_IsVisible = false;
}
if (Draw) // move : draw
{
//aRect.setRect( StoredMinX, StoredMinY, abs(StoredMaxX-StoredMinX), abs(StoredMaxY-StoredMinY));
m_IsVisible = true;
myRectBand->setGeometry( aRect );
// myRectBand->show();
}
}
void GraphicsViewMesh::fitArea(double myXmin,double myXmax,double myYmin,double myYmax )
{
double w,h;
w = (myXmax-myXmin);
h = (myYmax-myYmin);
glViewport(GLint(myXmin),GLint(myYmin),(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(myXmin, myXmax, myYmax, myYmin, -1, -1);
glMatrixMode(GL_MODELVIEW);
updateGL();
}
while doing the box zoom... rubberband rectangle is visible... but the problem is ... it is not correctly zooming the captured rectangle...