setools/setoolsgui/treeview.py
Steve Lawrence 8ecedcdf9c Override copy() and cut() functions for custom tree/table widgets
The Ctrl-C and Ctrl-X shortcuts are handled by the copy() and cut()
functions in the ApolMainWindow, which just get the currently focused
widget and call its function of the same name.

However, the custom SEToolsTableView and SEToolsTreeView widgets do not
use these functions to implement Ctrl-C/X, but instead override the
event() function and check if each received event is a copy/cut key
sequence. Functionally this is the same as copy() and cut(), but this
leads to an abort in newer versions for Fedora and/or PyQT5 (the reason
is not obvious).

To avoid the abort, and arguably make things a little more clear, this
overrides the copy() and cut() functions in these widgets, moves the
specialize copy logic into them, and removes the event() function.

Closes #77

Signed-off-by: Steve Lawrence <slawrence@owlcyberdefense.com>
2022-11-29 13:19:05 -05:00

60 lines
1.6 KiB
Python

# Copyright 2016, Chris PeBenito <pebenito@ieee.org>
#
# SPDX-License-Identifier: LGPL-2.1-only
#
#
from PyQt5.QtCore import Qt, QModelIndex
from PyQt5.QtGui import QKeySequence, QCursor
from PyQt5.QtWidgets import QAction, QApplication, QFileDialog, QMenu, QTreeWidget, \
QTreeWidgetItemIterator
class SEToolsTreeWidget(QTreeWidget):
"""QTreeWidget class extended for SETools use."""
def __init__(self, parent):
super(SEToolsTreeWidget, self).__init__(parent)
# set up right-click context menu
self.copy_tree_action = QAction("Copy Tree...", self)
self.menu = QMenu(self)
self.menu.addAction(self.copy_tree_action)
# connect signals
self.copy_tree_action.triggered.connect(self.copy)
def contextMenuEvent(self, event):
self.menu.popup(QCursor.pos())
def copy(self):
"""Copy the tree to the clipboard."""
items = []
inval_index = QModelIndex()
it = QTreeWidgetItemIterator(self)
prev_depth = 0
while it.value():
depth = 0
item = it.value()
parent = item.parent()
while parent:
depth += 1
parent = parent.parent()
if depth < prev_depth:
items.extend([" |" * depth, "\n"])
if depth:
items.extend([" |" * depth, "--", item.text(0), "\n"])
else:
items.extend([item.text(0), "\n"])
prev_depth = depth
it += 1
QApplication.clipboard().setText("".join(items))
def cut(self):
self.copy()