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 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
00047 except AttributeError:
00048 setSectionResizeMode = self.header().setResizeMode
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
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
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
00119 action = menu.exec_(self.header().mapToGlobal(pos))
00120
00121
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())