moveit2
The MoveIt Motion Planning Framework for ROS 2.
rotated_header_view.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016, CITEC, Bielefeld University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Robert Haschke */
36 
38 #include <QPainter>
39 
40 namespace moveit_setup
41 {
42 namespace srdf_setup
43 {
44 RotatedHeaderView::RotatedHeaderView(Qt::Orientation orientation, QWidget* parent) : QHeaderView(orientation, parent)
45 {
46 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
47  setSectionsClickable(true);
48  setSectionResizeMode(Fixed);
49 #else
50  setClickable(true);
51  setResizeMode(Fixed);
52 #endif
53  setDefaultSectionSize(27);
54 }
55 
56 void RotatedHeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
57 {
58  if (orientation() == Qt::Vertical)
59  {
60  QHeaderView::paintSection(painter, rect, logicalIndex);
61  return;
62  }
63 
64  painter->save();
65  // rotate about (x,y)
66  painter->translate(rect.x(), rect.y());
67  painter->rotate(-90);
68  painter->translate(-rect.height(), 0);
69  QHeaderView::paintSection(painter, QRect(0, 0, rect.height(), rect.width()), logicalIndex);
70  painter->restore();
71 }
72 
73 QSize RotatedHeaderView::sectionSizeFromContents(int logicalIndex) const
74 {
75  if (orientation() == Qt::Vertical)
76  return QHeaderView::sectionSizeFromContents(logicalIndex);
77 
78  Q_ASSERT(logicalIndex >= 0);
79 
80  ensurePolished();
81 
82  // use SizeHintRole
83  QVariant variant = model()->headerData(logicalIndex, Qt::Vertical, Qt::SizeHintRole);
84  if (variant.isValid())
85  return qvariant_cast<QSize>(variant);
86 
87  // otherwise use the contents
88  QStyleOptionHeader opt;
89  initStyleOption(&opt);
90  opt.section = logicalIndex;
91  QVariant var = model()->headerData(logicalIndex, orientation(), Qt::FontRole);
92  QFont fnt;
93  if (var.isValid() && var.canConvert<QFont>())
94  fnt = qvariant_cast<QFont>(var);
95  else
96  fnt = font();
97  fnt.setBold(true);
98  opt.fontMetrics = QFontMetrics(fnt);
99  opt.text = model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString();
100  variant = model()->headerData(logicalIndex, orientation(), Qt::DecorationRole);
101  opt.icon = qvariant_cast<QIcon>(variant);
102  if (opt.icon.isNull())
103  opt.icon = qvariant_cast<QPixmap>(variant);
104  QSize size = style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize(), this);
105  if (isSortIndicatorShown())
106  {
107  int margin = style()->pixelMetric(QStyle::PM_HeaderMargin, &opt, this);
108  if (orientation() == Qt::Horizontal)
109  size.rwidth() += size.height() + margin;
110  else
111  size.rheight() += size.width() + margin;
112  }
113  return QSize(size.height(), size.width());
114 }
115 
116 int RotatedHeaderView::sectionSizeHint(int logicalIndex) const
117 {
118  if (isSectionHidden(logicalIndex))
119  return 0;
120  if (logicalIndex < 0 || logicalIndex >= count())
121  return -1;
122  QSize size;
123  QVariant value = model()->headerData(logicalIndex, orientation(), Qt::SizeHintRole);
124  if (value.isValid())
125  size = qvariant_cast<QSize>(value);
126  else
127  size = sectionSizeFromContents(logicalIndex);
128  int hint = size.height();
129  return qMax(minimumSectionSize(), hint);
130 }
131 } // namespace srdf_setup
132 } // namespace moveit_setup
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override
RotatedHeaderView(Qt::Orientation orientation, QWidget *parent=nullptr)
QSize sectionSizeFromContents(int logicalIndex) const override