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 QAction, QDrag, QHeaderView, QIcon, QMenu, QTreeView
00035 
00036 
00037 class MessageTreeWidget(QTreeView):
00038 
00039     def __init__(self, parent=None):
00040         super(MessageTreeWidget, self).__init__(parent)
00041         self.setDragEnabled(True)
00042         self.sortByColumn(0, Qt.AscendingOrder)
00043 
00044         self.header().setResizeMode(QHeaderView.ResizeToContents)
00045         self.header().setContextMenuPolicy(Qt.CustomContextMenu)
00046         self.header().customContextMenuRequested.connect(self.handle_header_view_customContextMenuRequested)
00047 
00048         self._action_item_expand = QAction(QIcon.fromTheme('zoom-in'), 'Expand Selected', self)
00049         self._action_item_expand.triggered.connect(self._handle_action_item_expand)
00050         self._action_item_collapse = QAction(QIcon.fromTheme('zoom-out'), 'Collapse Selected', self)
00051         self._action_item_collapse.triggered.connect(self._handle_action_item_collapse)
00052         self.customContextMenuRequested.connect(self.handle_customContextMenuRequested)
00053 
00054     def startDrag(self, supportedActions):
00055         index = self.currentIndex()
00056         if not index.isValid():
00057             return
00058 
00059         item = self.model().itemFromIndex(index)
00060         path = getattr(item, '_path', None)
00061         if path is None:
00062             qWarning('MessageTreeWidget.startDrag(): no _path set on item %s' % item)
00063             return
00064 
00065         data = QMimeData()
00066         data.setText(item._path)
00067 
00068         drag = QDrag(self)
00069         drag.setMimeData(data)
00070         drag.exec_()
00071 
00072     @Slot('QPoint')
00073     def handle_customContextMenuRequested(self, pos):
00074         # show context menu
00075         menu = QMenu(self)
00076         self._context_menu_add_actions(menu, pos)
00077         menu.exec_(self.mapToGlobal(pos))
00078 
00079     def _context_menu_add_actions(self, menu, pos):
00080         if self.selectionModel().hasSelection():
00081             menu.addAction(self._action_item_expand)
00082             menu.addAction(self._action_item_collapse)
00083 
00084     def _handle_action_item_collapse(self):
00085         self._handle_action_set_expanded(False)
00086 
00087     def _handle_action_item_expand(self):
00088         self._handle_action_set_expanded(True)
00089 
00090     def _handle_action_set_expanded(self, expanded):
00091         def recursive_set_expanded(index):
00092             if index != QModelIndex():
00093                 self.setExpanded(index, expanded)
00094                 recursive_set_expanded(index.child(0, 0))
00095         for index in self.selectedIndexes():
00096             recursive_set_expanded(index)
00097 
00098     @Slot('QPoint')
00099     def handle_header_view_customContextMenuRequested(self, pos):
00100 
00101         # create context menu
00102         menu = QMenu(self)
00103 
00104         action_toggle_auto_resize = menu.addAction('Auto-Resize')
00105         action_toggle_auto_resize.setCheckable(True)
00106         auto_resize_flag = (self.header().resizeMode(0) == QHeaderView.ResizeToContents)
00107         action_toggle_auto_resize.setChecked(auto_resize_flag)
00108 
00109         action_toggle_sorting = menu.addAction('Sorting')
00110         action_toggle_sorting.setCheckable(True)
00111         action_toggle_sorting.setChecked(self.isSortingEnabled())
00112 
00113         # show menu
00114         action = menu.exec_(self.header().mapToGlobal(pos))
00115 
00116         # evaluate user action
00117         if action is action_toggle_auto_resize:
00118             if auto_resize_flag:
00119                 self.header().setResizeMode(QHeaderView.Interactive)
00120             else:
00121                 self.header().setResizeMode(QHeaderView.ResizeToContents)
00122 
00123         elif action is action_toggle_sorting:
00124             self.setSortingEnabled(not self.isSortingEnabled())


rqt_py_common
Author(s): Dorian Scholz
autogenerated on Fri Jan 3 2014 11:54:45