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 #include <QHeaderView>
36 #include <QScrollBar>
37 
39 
40 namespace rviz
41 {
43  : QWidget(parent), parent_(parent), first_column_size_ratio_(0.5f), color_(128, 128, 128, 64)
44 {
45  setCursor(Qt::SplitHCursor);
46  parent_->header()->setStretchLastSection(false);
47  parent_->viewport()->installEventFilter(this);
49 }
50 
51 bool SplitterHandle::eventFilter(QObject* /*event_target*/, QEvent* event)
52 {
53  if (event->type() == QEvent::Resize)
54  {
56  }
57  return false; // Return false regardless so resize event also does its normal job.
58 }
59 
61 {
62  auto* header = parent_->header();
63  const int sbw = parent_->verticalScrollBar()->isVisible() ? 0 : parent_->verticalScrollBar()->width();
64  const int w = 7;
65  const int min = header->minimumSectionSize();
66  const int available = parent_->viewport()->contentsRect().width();
67  const auto& content = parent_->contentsRect();
68 
69  // determine new width of first column (w.r.t. overall content width!)
70  int new_column_width = int(first_column_size_ratio_ * content.width());
71  if (new_column_width <= 0)
72  new_column_width = 0;
73  else if (new_column_width >= available)
74  new_column_width = available;
75  else
76  new_column_width = qBound(min, // minimum
77  new_column_width, // desired
78  available - min - sbw); // maximum
79 
80  if (new_column_width > header->sectionSize(0))
81  { // decrease 2nd column before increasing 1st one
82  header->resizeSection(1, available - new_column_width);
83  header->resizeSection(0, new_column_width);
84  }
85  else
86  { // decrease 1st column before increasing 2nd one
87  header->resizeSection(0, new_column_width);
88  header->resizeSection(1, available - new_column_width);
89  }
90 
91  // update geometry + position of splitter itself
92  int new_x = content.x() + new_column_width - w / 2;
93  if (new_x != x() || content.height() != height())
94  setGeometry(new_x, content.y(), w, content.height());
95 }
96 
97 void SplitterHandle::setRatio(float ratio)
98 {
100  updateGeometry();
101 }
102 
104 {
106 }
107 
109 {
110  first_column_size_ratio_ = width / (float)parent_->contentsRect().width();
111  updateGeometry();
112 }
113 
114 void SplitterHandle::mousePressEvent(QMouseEvent* event)
115 {
116  if (event->button() == Qt::LeftButton)
117  {
118  // position of mouse press relative to splitter line / the center of the widget
119  x_press_offset_ = event->x() - width() / 2;
120  }
121 }
122 
123 void SplitterHandle::mouseMoveEvent(QMouseEvent* event)
124 {
125  if (event->buttons() & Qt::LeftButton)
126  {
127  QPoint pos_rel_parent = parent_->mapFromGlobal(event->globalPos());
128  setDesiredWidth(pos_rel_parent.x() - parent_->contentsRect().x() - x_press_offset_);
129  }
130 }
131 
132 // adjust splitter position to optimally fit content
133 void SplitterHandle::mouseDoubleClickEvent(QMouseEvent* /*event*/)
134 {
135  int available_width = parent_->contentsRect().width();
136  int default_width = 0.5f * available_width;
137  // missing width to default
138  int col0 = static_cast<QAbstractItemView*>(parent_)->sizeHintForColumn(0) - default_width;
139  int col1 = static_cast<QAbstractItemView*>(parent_)->sizeHintForColumn(1) - default_width;
140 
141  if (col0 <= 0 && col1 <= 0) // each column fits
142  setDesiredWidth(default_width);
143  else if (col0 + col1 <= 0) // both columns fit together, but require a non-default splitting
144  setDesiredWidth(default_width + col0 + 0.5f * std::abs(col0 + col1)); // uniformly split extra space
145  else
146  setDesiredWidth(default_width + col0 - 0.5f * (col0 + col1)); // uniformly cut missing space
147 }
148 
149 void SplitterHandle::paintEvent(QPaintEvent* /*event*/)
150 {
151  QPainter painter(this);
152  painter.setPen(color_);
153  painter.drawLine(width() / 2, 0, width() / 2, height());
154 }
155 
156 } // end namespace rviz
void mouseDoubleClickEvent(QMouseEvent *event) override
f
float getRatio()
Get the ratio of the parent&#39;s left column to the parent widget width.
void paintEvent(QPaintEvent *event) override
void mousePressEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void updateGeometry()
Update the parent&#39;s column widths and this splitter&#39;s geometry based on first_column_size_ratio_.
void setDesiredWidth(int width)
Set desired width of first column - subject to clamping.
double min(double a, double b)
bool eventFilter(QObject *event_target, QEvent *event) override
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.
const std::string header
SplitterHandle(QTreeView *parent=nullptr)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25