00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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;
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 }