message_tree_widget.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of the TU Darmstadt nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from python_qt_binding.QtCore import Slot, QMimeData, QModelIndex, Qt, qWarning
34 from python_qt_binding.QtGui import QDrag, QIcon
35 from python_qt_binding.QtWidgets import QAction, QHeaderView, QMenu, QTreeView
36 
37 
38 class MessageTreeWidget(QTreeView):
39 
40  def __init__(self, parent=None):
41  super(MessageTreeWidget, self).__init__(parent)
42  self.setDragEnabled(True)
43  self.sortByColumn(0, Qt.AscendingOrder)
44 
45  try:
46  setSectionResizeMode = self.header().setSectionResizeMode # Qt5
47  except AttributeError:
48  setSectionResizeMode = self.header().setResizeMode # Qt4
49  setSectionResizeMode(QHeaderView.ResizeToContents)
50  self.header().setContextMenuPolicy(Qt.CustomContextMenu)
51  self.header().customContextMenuRequested.connect(
53 
54  self._action_item_expand = QAction(QIcon.fromTheme('zoom-in'), 'Expand Selected', self)
55  self._action_item_expand.triggered.connect(self._handle_action_item_expand)
56  self._action_item_collapse = QAction(QIcon.fromTheme('zoom-out'), 'Collapse Selected', self)
57  self._action_item_collapse.triggered.connect(self._handle_action_item_collapse)
58  self.customContextMenuRequested.connect(self.handle_customContextMenuRequested)
59 
60  def startDrag(self, supportedActions):
61  index = self.currentIndex()
62  if not index.isValid():
63  return
64 
65  item = self.model().itemFromIndex(index)
66  path = getattr(item, '_path', None)
67  if path is None:
68  qWarning('MessageTreeWidget.startDrag(): no _path set on item %s' % item)
69  return
70 
71  data = QMimeData()
72  data.setText(item._path)
73 
74  drag = QDrag(self)
75  drag.setMimeData(data)
76  drag.exec_()
77 
78  @Slot('QPoint')
80  # show context menu
81  menu = QMenu(self)
82  self._context_menu_add_actions(menu, pos)
83  menu.exec_(self.mapToGlobal(pos))
84 
85  def _context_menu_add_actions(self, menu, pos):
86  if self.selectionModel().hasSelection():
87  menu.addAction(self._action_item_expand)
88  menu.addAction(self._action_item_collapse)
89 
91  self._handle_action_set_expanded(False)
92 
95 
96  def _handle_action_set_expanded(self, expanded):
97  def recursive_set_expanded(index):
98  if index != QModelIndex():
99  self.setExpanded(index, expanded)
100  recursive_set_expanded(index.child(0, 0))
101  for index in self.selectedIndexes():
102  recursive_set_expanded(index)
103 
104  @Slot('QPoint')
106 
107  # create context menu
108  menu = QMenu(self)
109 
110  action_toggle_auto_resize = menu.addAction('Auto-Resize')
111  action_toggle_auto_resize.setCheckable(True)
112  auto_resize_flag = (self.header().resizeMode(0) == QHeaderView.ResizeToContents)
113  action_toggle_auto_resize.setChecked(auto_resize_flag)
114 
115  action_toggle_sorting = menu.addAction('Sorting')
116  action_toggle_sorting.setCheckable(True)
117  action_toggle_sorting.setChecked(self.isSortingEnabled())
118 
119  # show menu
120  action = menu.exec_(self.header().mapToGlobal(pos))
121 
122  # evaluate user action
123  if action is action_toggle_auto_resize:
124  if auto_resize_flag:
125  self.header().setResizeMode(QHeaderView.Interactive)
126  else:
127  self.header().setResizeMode(QHeaderView.ResizeToContents)
128 
129  elif action is action_toggle_sorting:
130  self.setSortingEnabled(not self.isSortingEnabled())


rqt_py_common
Author(s): Dorian Scholz , Isaac Saito, Dirk Thomas
autogenerated on Mon May 16 2022 02:49:40