splitter_handle.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <QEvent>
00031 #include <QMouseEvent>
00032 #include <QPaintEvent>
00033 #include <QPainter>
00034 #include <QTreeView>
00035 
00036 #include "rviz/properties/splitter_handle.h"
00037 
00038 namespace rviz
00039 {
00040 
00041 SplitterHandle::SplitterHandle( QTreeView* parent )
00042   : QWidget( parent )
00043   , parent_( parent )
00044   , first_column_size_ratio_( 0.5f )
00045   , color_( 128, 128, 128, 64 )
00046 {
00047   setCursor( Qt::SplitHCursor );
00048   int w = 7;
00049   setGeometry( parent_->width() / 2 - w/2, 0, w, parent_->height() );
00050   parent_->installEventFilter( this );
00051 }
00052 
00053 bool SplitterHandle::eventFilter( QObject* event_target, QEvent* event )
00054 {
00055   if( event_target == parent_ && event->type() == QEvent::Resize )
00056   {
00057     updateGeometry();
00058   }
00059   return false; // Return false regardless so resize event also does its normal job.
00060 }
00061 
00062 void SplitterHandle::updateGeometry()
00063 {
00064   int content_width = parent_->contentsRect().width();
00065   int new_column_width = int( first_column_size_ratio_ * content_width );
00066   parent_->setColumnWidth( 0, new_column_width );
00067   parent_->setColumnWidth( 1, content_width - new_column_width );
00068   setGeometry( new_column_width - width() / 2, 0, width(), parent_->height() );
00069 }
00070 
00071 void SplitterHandle::setRatio( float ratio )
00072 {
00073   first_column_size_ratio_ = ratio;
00074   updateGeometry();
00075 }
00076 
00077 float SplitterHandle::getRatio()
00078 {
00079   return first_column_size_ratio_;
00080 }
00081 
00082 void SplitterHandle::mousePressEvent( QMouseEvent* event )
00083 {
00084   if( event->button() == Qt::LeftButton )
00085   {
00086     x_press_offset_ = event->x();
00087   }
00088 }
00089 
00090 void SplitterHandle::mouseMoveEvent( QMouseEvent* event )
00091 {
00092   int padding = 55;
00093 
00094   if( event->buttons() & Qt::LeftButton )
00095   {
00096     QPoint pos_rel_parent = parent_->mapFromGlobal( event->globalPos() );
00097 
00098     int new_x = pos_rel_parent.x() - x_press_offset_;
00099 
00100     if( new_x > parent_->width() - width() - padding )
00101     {
00102       new_x = parent_->width() - width() - padding;
00103     }
00104 
00105     if( new_x < padding )
00106     {
00107       new_x = padding;
00108     }
00109 
00110     if( new_x != x() )
00111     {
00112       int new_column_width = new_x + width() / 2 - parent_->contentsRect().x();
00113       first_column_size_ratio_ = new_column_width / (float) parent_->contentsRect().width();
00114       updateGeometry();
00115     }
00116   }
00117 }
00118 
00119 void SplitterHandle::paintEvent( QPaintEvent* event )
00120 {
00121   QPainter painter( this );
00122   painter.setPen( color_ );
00123   painter.drawLine( width() / 2, 0, width() / 2, height() );
00124 }
00125 
00126 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:28