Skip to content

Commit

Permalink
Added easy ways of selecting multiple things (spriteframes and xml ta…
Browse files Browse the repository at this point in the history
…ble rows)
  • Loading branch information
UncertainProd committed Jun 22, 2022
1 parent 8f485ee commit 4667ee7
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 14 deletions.
11 changes: 8 additions & 3 deletions src/spriteframe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from PIL import Image
from PyQt5.QtCore import QSize
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QCheckBox, QFrame, QPushButton, QWidget, QLabel
from PyQt5.QtWidgets import QCheckBox, QFrame, QPushButton, QWidget, QLabel, QApplication
from framedata import FrameData
from utils import SPRITEFRAME_SIZE, imghashes
from os import path
Expand Down Expand Up @@ -71,8 +71,13 @@ def mousePressEvent(self, event):
prevstate = self.select_checkbox.checkState()
newstate = 0 if prevstate != 0 else 1
self.select_checkbox.setChecked(newstate)
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ShiftModifier:
self.frameparent.ranged_selection_handler(self)
else:
print("Click with the left mouse button")
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ShiftModifier:
self.frameparent.ranged_deletion_handler(self)

# overriding the default enterEvent
def enterEvent(self, event):
Expand Down
47 changes: 47 additions & 0 deletions src/xmlpngUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,53 @@ def __init__(self, prefs):
# Note: Add any extra windows before this if your want the themes to apply to them
if prefs.get("theme", 'default') == 'dark':
self.set_theme(get_stylesheet_from_file("assets/app-styles.qss"))


def ranged_selection_handler(self, selected_spriteframe):
first_selected_spriteframe = None
for sprf in self.labels:
if sprf == selected_spriteframe:
break

if sprf.select_checkbox.checkState() != 0 and sprf != selected_spriteframe:
first_selected_spriteframe = sprf
break

if first_selected_spriteframe is not None:
start_selecting = False
for sprf in self.labels:
if sprf == first_selected_spriteframe:
start_selecting = True

if start_selecting:
# checks the box and adds it to the selected list
sprf.select_checkbox.setChecked(1)

if sprf == selected_spriteframe:
break

def ranged_deletion_handler(self, selected_spriteframe):
first_selected_spriteframe = None
for sprf in self.labels:
if sprf == selected_spriteframe:
break

if sprf.select_checkbox.checkState() != 0 and sprf != selected_spriteframe:
first_selected_spriteframe = sprf
break

if first_selected_spriteframe is not None:
start_selecting = False
for sprf in self.labels:
if sprf == first_selected_spriteframe:
start_selecting = True

if start_selecting:
# unchecks the box and removes it from the selected list
sprf.select_checkbox.setChecked(0)

if sprf == selected_spriteframe:
break


def open_gif(self):
Expand Down
81 changes: 70 additions & 11 deletions src/xmltablewindow.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
from PyQt5.QtWidgets import QTableWidgetItem, QWidget
from PyQt5.QtWidgets import QTableWidgetItem, QWidget, QMenu, QAction, QMessageBox, QLineEdit, QInputDialog
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor
from utils import temp_path_shortener, imghashes
import engine.spritesheetutils as spritesheetutils
from xmltablewindowUI import Ui_TableWidgetThing
from utils import display_msg_box

class XMLTableView(QWidget):
def __init__(self, table_headings):
super().__init__()
self.ui = Ui_TableWidgetThing()
self.ui.setupUi(self)

self.table_headings = table_headings
self.ui.xmltable.setColumnCount(len(table_headings))
self.ui.xmltable.setHorizontalHeaderLabels(table_headings)
self.ui.xmltable.contextMenuEvent = self.handle_context_menu_event

# self.ui.xmltable.cellClicked.connect(self.handle_cell_click)
# self.ui.xmltable.cellActivated.connect(self.handle_cell_click)
# self.ui.xmltable.cellPressed.connect(self.handle_cell_click)
self.ui.xmltable.currentCellChanged.connect(self.handle_curr_cell_change)
self.ui.frame_preview_label.setStyleSheet("QFrame{ border: 1px solid black; }")
self.ui.xmltable.selectionModel().selectionChanged.connect(self.handle_cell_selection)
# self.ui.xmltable.selectionModel().selectionChanged.connect(self.handle_cell_selection)

# list[SpriteFrame]
self.tabledata = []
Expand All @@ -35,6 +40,55 @@ def __init__(self, table_headings):

self.was_opened = False

self.selected_cells = []

def handle_curr_cell_change(self, current_row, current_col, prev_row, prev_col):
self.selected_row_index = current_row
self.handle_display_stuff(self.selected_row_index)

def handle_context_menu_event(self, event):
self.menu = QMenu(self)
renameAction = QAction('Set Value', self)
renameAction.triggered.connect(lambda: self.set_value_handle())
self.menu.addAction(renameAction)
# add other required actions
self.menu.popup(QCursor.pos())

def set_value_handle(self):
_cells = self.ui.xmltable.selectedItems()
idx = -1
for _cell in _cells:
if not (_cell.flags() & Qt.ItemIsEditable):
display_msg_box(self, "Bad cell selection", "There are un-editable cells in your selection!\nSelect cells from the same column, valid columns being\nFrameX, FrameY, FrameWidth or FrameHeight", QMessageBox.Critical)
return
else:
if idx != -1 and _cell.column() != idx:
display_msg_box(self, "Multiple Columns Selected", "Your selection spans multiple columns. Make sure to select cells that belong to the same column, valid columns being\nFrameX, FrameY, FrameWidth or FrameHeight", QMessageBox.Critical)
return
else:
idx = _cell.column()

rows = [ x.row() for x in _cells ]
text, okPressed = QInputDialog.getText(self, f"Change Value of {self.table_headings[idx - 4]}", "New value:"+(" "*50), QLineEdit.Normal)
is_real_number = lambda s: s.isnumeric() or (s[0] == '-' and s[1:].isnumeric())
if okPressed and text != '' and is_real_number(text):
val = int(text)
old_selected_row_index = self.selected_row_index
old_selected_row = self.selected_row
for row_num in rows:
self.ui.xmltable.setItem(row_num, idx, QTableWidgetItem(str(val)))
self.selected_row_index = row_num
self.selected_row = self.tabledata[row_num]
self.handle_cell_change(row_num, idx)

# restoring things back to normal
self.selected_row_index = old_selected_row_index
self.selected_row = old_selected_row
self.set_true_frame()
else:
print("Text invalid / cancel was pressed")


def handle_framex_change(self, newval):
if self.canchange:
if self.selected_row:
Expand Down Expand Up @@ -146,15 +200,20 @@ def handle_cell_change(self, row, col):

self.set_true_frame()

def handle_cell_selection(self, selected, deselected):
if selected.indexes():
self.selected_row_index = selected.indexes()[-1].row()
self.handle_display_stuff(self.selected_row_index)
elif deselected.indexes():
self.selected_row_index = deselected.indexes()[-1].row()
self.handle_display_stuff(self.selected_row_index)
else:
print("Something's weird here")
# def handle_cell_selection(self, selected, deselected):
# if selected.indexes():
# self.selected_cells.extend(selected.indexes())
# self.selected_row_index = selected.indexes()[-1].row()
# self.handle_display_stuff(self.selected_row_index)
# elif deselected.indexes():
# for _cell in deselected.indexes():
# print(f"Removing: {_cell.row()}, {_cell.column()}")
# self.selected_cells.remove(_cell)
# self.selected_row_index = deselected.indexes()[-1].row()
# self.handle_display_stuff(self.selected_row_index)
# print(f'{[ (c.row(), c.column()) for c in self.selected_cells ]}')
# else:
# print("Something's weird here")

def handle_display_stuff(self, row):
self.selected_row = self.tabledata[row]
Expand Down

0 comments on commit 4667ee7

Please sign in to comment.