splitter_handle.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QEvent>
31 #include <QMouseEvent>
32 #include <QPaintEvent>
33 #include <QPainter>
34 #include <QTreeView>
35 
37 
38 namespace rviz
39 {
40 
41 SplitterHandle::SplitterHandle( QTreeView* parent )
42  : QWidget( parent )
43  , parent_( parent )
44  , first_column_size_ratio_( 0.5f )
45  , color_( 128, 128, 128, 64 )
46 {
47  setCursor( Qt::SplitHCursor );
49  parent_->installEventFilter( this );
50 }
51 
52 bool SplitterHandle::eventFilter( QObject* event_target, QEvent* event )
53 {
54  if( event_target == parent_ && (event->type() == QEvent::Resize ||
55  event->type() == QEvent::Paint))
56  {
58  }
59  return false; // Return false regardless so resize event also does its normal job.
60 }
61 
63 {
64  int w = 7;
65  int content_width = parent_->contentsRect().width();
66  int new_column_width = int( first_column_size_ratio_ * content_width );
67  if ( new_column_width != parent_->columnWidth(0) ) {
68  parent_->setColumnWidth( 0, new_column_width );
69  parent_->setColumnWidth( 1, content_width - new_column_width );
70  }
71  int new_x = new_column_width - w / 2 + parent_->columnViewportPosition(0);
72  if ( new_x != x() || parent_->height() != height() )
73  setGeometry( new_x, 0, w, parent_->height() );
74 }
75 
76 void SplitterHandle::setRatio( float ratio )
77 {
80 }
81 
83 {
85 }
86 
87 void SplitterHandle::mousePressEvent( QMouseEvent* event )
88 {
89  if( event->button() == Qt::LeftButton )
90  {
91  // position of mouse press inside this QWidget
92  x_press_offset_ = event->x();
93  }
94 }
95 
96 void SplitterHandle::mouseMoveEvent( QMouseEvent* event )
97 {
98  int padding = 55;
99 
100  if( event->buttons() & Qt::LeftButton )
101  {
102  QPoint pos_rel_parent = parent_->mapFromGlobal( event->globalPos() );
103 
104  int new_x = pos_rel_parent.x() - x_press_offset_ - parent_->columnViewportPosition(0);
105 
106  if( new_x > parent_->width() - width() - padding )
107  {
108  new_x = parent_->width() - width() - padding;
109  }
110 
111  if( new_x < padding )
112  {
113  new_x = padding;
114  }
115 
116  if( new_x != x() )
117  {
118  int new_column_width = new_x + width() / 2 - parent_->contentsRect().x();
119  first_column_size_ratio_ = new_column_width / (float) parent_->contentsRect().width();
120  updateGeometry();
121  }
122  }
123 }
124 
125 void SplitterHandle::paintEvent( QPaintEvent* event )
126 {
127  QPainter painter( this );
128  painter.setPen( color_ );
129  painter.drawLine( 1+width() / 2, 0, 1+width() / 2, height() );
130 }
131 
132 } // end namespace rviz
f
virtual void paintEvent(QPaintEvent *event)
float getRatio()
Get the ratio of the parent&#39;s left column to the parent widget width.
virtual void mousePressEvent(QMouseEvent *event)
SplitterHandle(QTreeView *parent=0)
void updateGeometry()
Update the parent&#39;s column widths and this splitter&#39;s geometry based on first_column_size_ratio_.
virtual void mouseMoveEvent(QMouseEvent *event)
TFSIMD_FORCE_INLINE const tfScalar & x() const
TFSIMD_FORCE_INLINE const tfScalar & w() const
bool eventFilter(QObject *event_target, QEvent *event)
Catch resize events sent to parent to update splitter&#39;s geometry. Always returns false.
void setRatio(float ratio)
Set the ratio of the parent&#39;s left column to the parent widget width.


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51