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 
37 #include "rotated_header_view.h"
38 #include <QPainter>
39 #include <QDebug>
40 
41 namespace moveit_setup_assistant
42 {
43 RotatedHeaderView::RotatedHeaderView(Qt::Orientation orientation, QWidget* parent) : QHeaderView(orientation, parent)
44 {
45 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
46  setSectionsClickable(true);
47  setSectionResizeMode(Fixed);
48 #else
49  setClickable(true);
50  setResizeMode(Fixed);
51 #endif
52  setDefaultSectionSize(27);
53 }
54 
55 void RotatedHeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
56 {
57  if (orientation() == Qt::Vertical)
58  {
59  QHeaderView::paintSection(painter, rect, logicalIndex);
60  return;
61  }
62 
63  painter->save();
64  // rotate about (x,y)
65  painter->translate(rect.x(), rect.y());
66  painter->rotate(-90);
67  painter->translate(-rect.height(), 0);
68  QHeaderView::paintSection(painter, QRect(0, 0, rect.height(), rect.width()), logicalIndex);
69  painter->restore();
70 }
71 
72 QSize RotatedHeaderView::sectionSizeFromContents(int logicalIndex) const
73 {
74  if (orientation() == Qt::Vertical)
75  return QHeaderView::sectionSizeFromContents(logicalIndex);
76 
77  Q_ASSERT(logicalIndex >= 0);
78 
79  ensurePolished();
80 
81  // use SizeHintRole
82  QVariant variant = model()->headerData(logicalIndex, Qt::Vertical, Qt::SizeHintRole);
83  if (variant.isValid())
84  return qvariant_cast<QSize>(variant);
85 
86  // otherwise use the contents
87  QStyleOptionHeader opt;
88  initStyleOption(&opt);
89  opt.section = logicalIndex;
90  QVariant var = model()->headerData(logicalIndex, orientation(), Qt::FontRole);
91  QFont fnt;
92  if (var.isValid() && var.canConvert<QFont>())
93  fnt = qvariant_cast<QFont>(var);
94  else
95  fnt = font();
96  fnt.setBold(true);
97  opt.fontMetrics = QFontMetrics(fnt);
98  opt.text = model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString();
99  variant = model()->headerData(logicalIndex, orientation(), Qt::DecorationRole);
100  opt.icon = qvariant_cast<QIcon>(variant);
101  if (opt.icon.isNull())
102  opt.icon = qvariant_cast<QPixmap>(variant);
103  QSize size = style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize(), this);
104  if (isSortIndicatorShown())
105  {
106  int margin = style()->pixelMetric(QStyle::PM_HeaderMargin, &opt, this);
107  if (orientation() == Qt::Horizontal)
108  size.rwidth() += size.height() + margin;
109  else
110  size.rheight() += size.width() + margin;
111  }
112  return QSize(size.height(), size.width());
113 }
114 
115 int RotatedHeaderView::sectionSizeHint(int logicalIndex) const
116 {
117  if (isSectionHidden(logicalIndex))
118  return 0;
119  if (logicalIndex < 0 || logicalIndex >= count())
120  return -1;
121  QSize size;
122  QVariant value = model()->headerData(logicalIndex, orientation(), Qt::SizeHintRole);
123  if (value.isValid())
124  size = qvariant_cast<QSize>(value);
125  else
126  size = sectionSizeFromContents(logicalIndex);
127  int hint = size.height();
128  qDebug() << logicalIndex << size << hint;
129  return qMax(minimumSectionSize(), hint);
130 }
131 }
std::size_t size(custom_string const &s)
QSize sectionSizeFromContents(int logicalIndex) const
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
RotatedHeaderView(Qt::Orientation orientation, QWidget *parent=NULL)
int value


moveit_setup_assistant
Author(s): Dave Coleman
autogenerated on Sun Oct 18 2020 13:19:28