EnhancedTreeWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  *
19  * @package ArmarX::RobotAPI
20  * @author Raphael Grimm <raphael dot grimm at kit dot edu>
21  * @date 2017
22  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
23  * GNU General Public License
24  */
25 
26 #include "EnhancedTreeWidget.h"
27 
28 #include <QHeaderView>
29 #include <QWheelEvent>
30 #include <QScrollBar>
31 
32 namespace armarx
33 {
34 
36  {
37  int sz = 0;
38  for (int i = 0; i < topLevelItemCount(); ++i)
39  {
40  sz += calcHeight(topLevelItem(i));
41  }
42  if (!isHeaderHidden())
43  {
44  sz += header()->height();
45  }
46  return sz + 2;
47  }
48 
49  void EnhancedTreeWidget::setWheelTicksPerScrollTick(int wheelTicksPerScrollTick)
50  {
51  this->wheelTicksPerScrollTick = wheelTicksPerScrollTick;
52  }
53 
54  void EnhancedTreeWidget::wheelEvent(QWheelEvent* event)
55  {
56  if (event->orientation() == Qt::Horizontal)
57  {
58  if (horizontalScrollMode() == QAbstractItemView::ScrollPerPixel)
59  {
60  hdelta += event->delta();
61  horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (hdelta / wheelTicksPerScrollTick));
62  hdelta = hdelta % wheelTicksPerScrollTick;
63  }
64  else
65  {
66  QTreeWidget::wheelEvent(event);
67  }
68  }
69  else
70  {
71  if (verticalScrollMode() == QAbstractItemView::ScrollPerPixel)
72  {
73  vdelta += event->delta();
74  verticalScrollBar()->setValue(verticalScrollBar()->value() - (vdelta / wheelTicksPerScrollTick));
75  vdelta = hdelta % wheelTicksPerScrollTick;
76  }
77  else
78  {
79  QTreeWidget::wheelEvent(event);
80  }
81  }
82  event->accept();
83  }
84 
86  {
87  setSizePolicy(sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
88  setFixedHeight(getHeight());
89  }
90 
92  {
93  if (active)
94  {
95  expand();
96  connect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(expand()));
97  connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(expand()));
98  }
99  else
100  {
101  disconnect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(expand()));
102  disconnect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(expand()));
103  }
104  }
105 
106  int EnhancedTreeWidget::calcHeight(QTreeWidgetItem* it)
107  {
108  int sz = 0;
109  if (it->isHidden())
110  {
111  return sz;
112  }
113  for (int i = 0; i < it->columnCount(); ++i)
114  {
115  sz = std::max(sz, rowHeight(indexFromItem(it, i)));
116  }
117  if (it->isExpanded())
118  {
119  for (int i = 0; i < it->childCount(); ++i)
120  {
121  sz += calcHeight(it->child(i));
122  }
123  }
124  return sz;
125  }
126 
127 }
armarx::EnhancedTreeWidget::setWheelTicksPerScrollTick
void setWheelTicksPerScrollTick(int wheelTicksPerScrollTick)
Definition: EnhancedTreeWidget.cpp:49
EnhancedTreeWidget.h
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::EnhancedTreeWidget::getHeight
int getHeight()
Definition: EnhancedTreeWidget.cpp:35
armarx::EnhancedTreeWidget::setAutoExpand
void setAutoExpand(bool active)
Definition: EnhancedTreeWidget.cpp:91
armarx::EnhancedTreeWidget::expand
void expand()
Definition: EnhancedTreeWidget.cpp:85
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::EnhancedTreeWidget::wheelEvent
void wheelEvent(QWheelEvent *event) override
Definition: EnhancedTreeWidget.cpp:54