Skip to content

Commit

Permalink
copy function in tables #8
Browse files Browse the repository at this point in the history
  • Loading branch information
fre-sch committed Feb 6, 2020
1 parent 366e64a commit 0e49a96
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/mhw_armor_edit/import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ def __init__(self, target_widget, default_attrs=None):
self.handle_import_action)
self.model_index = QModelIndex()

def set_model_index(self, model_index):
self.model_index = model_index
self.export_action.setEnabled(model_index and model_index.isValid())
self.import_action.setEnabled(model_index and model_index.isValid())

def populate_menu(self, menu):
menu.addAction(self.export_action)
menu.addAction(self.import_action)
Expand Down
51 changes: 47 additions & 4 deletions src/mhw_armor_edit/struct_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

from PyQt5.QtCore import (Qt, QAbstractTableModel, QSortFilterProxyModel,
pyqtSignal)
from PyQt5.QtGui import (QFont, QFontMetrics, )
from PyQt5.QtGui import (QFont, QFontMetrics, QKeyEvent, QKeySequence)
from PyQt5.QtWidgets import (QTableView, QLineEdit, QAction, QHeaderView,
QTreeView, QMenu,
QAbstractItemView)
QTreeView, QAbstractItemView, QApplication, QMenu,
QStyle)

from mhw_armor_edit.import_export import (ImportExportManager)
from mhw_armor_edit.utils import create_action

log = logging.getLogger()

Expand Down Expand Up @@ -105,8 +106,50 @@ def __init__(self, parent=None):
self.setHorizontalHeader(header)
self.setSortingEnabled(True)
self.setSelectionMode(QAbstractItemView.ContiguousSelection)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_context_menu)
self.import_export_manager = ImportExportManager(self)
self.import_export_manager.connect_custom_context_menu()
self.copy_action = create_action(
None, "Copy", self.copy_selection_to_clipboard)

def show_context_menu(self, point):
self.import_export_manager.set_model_index(self.indexAt(point))
context_menu = QMenu()
context_menu.addAction(self.copy_action)
context_menu.addAction(self.import_export_manager.export_action)
context_menu.addAction(self.import_export_manager.import_action)
context_menu.exec(self.mapToGlobal(point))

def keyPressEvent(self, event: QKeyEvent):
if event.type() == QKeyEvent.KeyPress \
and event.matches(QKeySequence.Copy):
self.copy_selection_to_clipboard()
else:
super().keyPressEvent(event)

def copy_selection_to_clipboard(self):
selected_indexes = self.selectionModel().selectedIndexes()
if not selected_indexes or len(selected_indexes) == 0:
return
model = self.model()
result = "\n".join(
"\t".join(row)
for row in self.selected_rows(model, selected_indexes)
)
cp = QApplication.clipboard()
cp.setText(result)

def selected_rows(self, model, selected_indexes):
row = []
last_row = selected_indexes[0].row()
for current in selected_indexes:
value = str(model.data(current, Qt.DisplayRole))
if last_row != current.row():
yield row
row = [value, ]
else:
row.append(value)
last_row = current.row()

def set_filter(self, section, filter_text):
log.debug("set_filter(section: %s, filter: %r)", section, filter_text)
Expand Down
1 change: 1 addition & 0 deletions src/mhw_armor_edit/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
("Bow", r"common\equip\bow.wp_dat_g"),
("Heavy Bowgun", r"common\equip\hbg.wp_dat_g"),
("Light Bowgun", r"common\equip\lbg.wp_dat_g"),
("Skill Data", r"common\equip\skill_data.skl_dat"),
)
HELP_CONTENT = """
<style>code {background-color:#EEE;}</style>
Expand Down

0 comments on commit 0e49a96

Please sign in to comment.