Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
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
00114 action = menu.exec_(self.header().mapToGlobal(pos))
00115
00116
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())