Go to the documentation of this file.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 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;
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
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 }