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   updateGeometry();
00049   parent_->installEventFilter( this );
00050 }
00051 
00052 bool SplitterHandle::eventFilter( QObject* event_target, QEvent* event )
00053 {
00054   if( event_target == parent_ && (event->type() == QEvent::Resize ||
00055                                   event->type() == QEvent::Paint))
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 w = 7;
00065   int content_width = parent_->contentsRect().width();
00066   int new_column_width = int( first_column_size_ratio_ * content_width );
00067   if ( new_column_width != parent_->columnWidth(0) ) {
00068     parent_->setColumnWidth( 0, new_column_width );
00069     parent_->setColumnWidth( 1, content_width - new_column_width );
00070   }
00071   int new_x = new_column_width - w / 2 + parent_->columnViewportPosition(0);
00072   if ( new_x != x() || parent_->height() != height() )
00073     setGeometry( new_x, 0, w, parent_->height() );
00074 }
00075 
00076 void SplitterHandle::setRatio( float ratio )
00077 {
00078   first_column_size_ratio_ = ratio;
00079   updateGeometry();
00080 }
00081 
00082 float SplitterHandle::getRatio()
00083 {
00084   return first_column_size_ratio_;
00085 }
00086 
00087 void SplitterHandle::mousePressEvent( QMouseEvent* event )
00088 {
00089   if( event->button() == Qt::LeftButton )
00090   {
00091     // position of mouse press inside this QWidget
00092     x_press_offset_ = event->x();
00093   }
00094 }
00095 
00096 void SplitterHandle::mouseMoveEvent( QMouseEvent* event )
00097 {
00098   int padding = 55;
00099 
00100   if( event->buttons() & Qt::LeftButton )
00101   {
00102     QPoint pos_rel_parent = parent_->mapFromGlobal( event->globalPos() );
00103 
00104     int new_x = pos_rel_parent.x() - x_press_offset_ - parent_->columnViewportPosition(0);
00105 
00106     if( new_x > parent_->width() - width() - padding )
00107     {
00108       new_x = parent_->width() - width() - padding;
00109     }
00110 
00111     if( new_x < padding )
00112     {
00113       new_x = padding;
00114     }
00115 
00116     if( new_x != x() )
00117     {
00118       int new_column_width = new_x + width() / 2 - parent_->contentsRect().x();
00119       first_column_size_ratio_ = new_column_width / (float) parent_->contentsRect().width();
00120       updateGeometry();
00121     }
00122   }
00123 }
00124 
00125 void SplitterHandle::paintEvent( QPaintEvent* event )
00126 {
00127   QPainter painter( this );
00128   painter.setPen( color_ );
00129   painter.drawLine( 1+width() / 2, 0, 1+width() / 2, height() );
00130 }
00131 
00132 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Tue Oct 3 2017 03:19:31