message_tree_widget.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #   * Redistributions of source code must retain the above copyright
00011 #     notice, this list of conditions and the following disclaimer.
00012 #   * Redistributions in binary form must reproduce the above
00013 #     copyright notice, this list of conditions and the following
00014 #     disclaimer in the documentation and/or other materials provided
00015 #     with the distribution.
00016 #   * Neither the name of the TU Darmstadt nor the names of its
00017 #     contributors may be used to endorse or promote products derived
00018 #     from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 from python_qt_binding.QtCore import Slot, QMimeData, QModelIndex, Qt, qWarning
00034 from python_qt_binding.QtGui import QDrag, QIcon
00035 from python_qt_binding.QtWidgets import QAction, QHeaderView, QMenu, QTreeView
00036 
00037 
00038 class MessageTreeWidget(QTreeView):
00039 
00040     def __init__(self, parent=None):
00041         super(MessageTreeWidget, self).__init__(parent)
00042         self.setDragEnabled(True)
00043         self.sortByColumn(0, Qt.AscendingOrder)
00044 
00045         try:
00046             setSectionResizeMode = self.header().setSectionResizeMode  # Qt5
00047         except AttributeError:
00048             setSectionResizeMode = self.header().setResizeMode  # Qt4
00049         setSectionResizeMode(QHeaderView.ResizeToContents)
00050         self.header().setContextMenuPolicy(Qt.CustomContextMenu)
00051         self.header().customContextMenuRequested.connect(self.handle_header_view_customContextMenuRequested)
00052 
00053         self._action_item_expand = QAction(QIcon.fromTheme('zoom-in'), 'Expand Selected', self)
00054         self._action_item_expand.triggered.connect(self._handle_action_item_expand)
00055         self._action_item_collapse = QAction(QIcon.fromTheme('zoom-out'), 'Collapse Selected', self)
00056         self._action_item_collapse.triggered.connect(self._handle_action_item_collapse)
00057         self.customContextMenuRequested.connect(self.handle_customContextMenuRequested)
00058 
00059     def startDrag(self, supportedActions):
00060         index = self.currentIndex()
00061         if not index.isValid():
00062             return
00063 
00064         item = self.model().itemFromIndex(index)
00065         path = getattr(item, '_path', None)
00066         if path is None:
00067             qWarning('MessageTreeWidget.startDrag(): no _path set on item %s' % item)
00068             return
00069 
00070         data = QMimeData()
00071         data.setText(item._path)
00072 
00073         drag = QDrag(self)
00074         drag.setMimeData(data)
00075         drag.exec_()
00076 
00077     @Slot('QPoint')
00078     def handle_customContextMenuRequested(self, pos):
00079         # show context menu
00080         menu = QMenu(self)
00081         self._context_menu_add_actions(menu, pos)
00082         menu.exec_(self.mapToGlobal(pos))
00083 
00084     def _context_menu_add_actions(self, menu, pos):
00085         if self.selectionModel().hasSelection():
00086             menu.addAction(self._action_item_expand)
00087             menu.addAction(self._action_item_collapse)
00088 
00089     def _handle_action_item_collapse(self):
00090         self._handle_action_set_expanded(False)
00091 
00092     def _handle_action_item_expand(self):
00093         self._handle_action_set_expanded(True)
00094 
00095     def _handle_action_set_expanded(self, expanded):
00096         def recursive_set_expanded(index):
00097             if index != QModelIndex():
00098                 self.setExpanded(index, expanded)
00099                 recursive_set_expanded(index.child(0, 0))
00100         for index in self.selectedIndexes():
00101             recursive_set_expanded(index)
00102 
00103     @Slot('QPoint')
00104     def handle_header_view_customContextMenuRequested(self, pos):
00105 
00106         # create context menu
00107         menu = QMenu(self)
00108 
00109         action_toggle_auto_resize = menu.addAction('Auto-Resize')
00110         action_toggle_auto_resize.setCheckable(True)
00111         auto_resize_flag = (self.header().resizeMode(0) == QHeaderView.ResizeToContents)
00112         action_toggle_auto_resize.setChecked(auto_resize_flag)
00113 
00114         action_toggle_sorting = menu.addAction('Sorting')
00115         action_toggle_sorting.setCheckable(True)
00116         action_toggle_sorting.setChecked(self.isSortingEnabled())
00117 
00118         # show menu
00119         action = menu.exec_(self.header().mapToGlobal(pos))
00120 
00121         # evaluate user action
00122         if action is action_toggle_auto_resize:
00123             if auto_resize_flag:
00124                 self.header().setResizeMode(QHeaderView.Interactive)
00125             else:
00126                 self.header().setResizeMode(QHeaderView.ResizeToContents)
00127 
00128         elif action is action_toggle_sorting:
00129             self.setSortingEnabled(not self.isSortingEnabled())


rqt_py_common
Author(s): Dorian Scholz, Isaac Saito
autogenerated on Wed May 3 2017 02:24:14