Skip to content

Commit

Permalink
[fix] Simplify species actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kastakin committed May 22, 2023
1 parent f8ec329 commit fdc5f13
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 87 deletions.
20 changes: 13 additions & 7 deletions src/main/python/pyes/commands/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,21 @@ def __init__(
number: int,
):
QUndoCommand.__init__(self)
self.table = table
self.model = table.model().sourceModel()
self.counter = counter
self.position = position
self.number = number

def undo(self) -> None:
self.model.removeRows(position=self.position + self.number, rows=self.number)
self.counter.blockSignals(True)
self.counter.setValue(self.model.rowCount())
self.counter.blockSignals(False)
self.cleanup()

def redo(self) -> None:
self.model.insertRows(position=self.position, rows=self.number)
self.cleanup()

def cleanup(self):
self.counter.blockSignals(True)
self.counter.setValue(self.model.rowCount())
self.counter.blockSignals(False)
Expand All @@ -86,26 +88,30 @@ def __init__(
number: int,
):
QUndoCommand.__init__(self)
self.table = table
self.model = table.model().sourceModel()
self.counter = counter
self.position = position
self.number = number
self.removed_row = None

def undo(self) -> None:
self.model.insertRows(position=self.position - 2, rows=self.number)
self.model.insertRows(position=self.position - self.number, rows=self.number)
self.model._data.iloc[
(self.position - self.number) : (self.position), :
] = self.removed_rows
self.counter.blockSignals(True)
self.counter.setValue(self.model.rowCount())
self.counter.blockSignals(False)

self.cleanup()

def redo(self) -> None:
self.removed_rows = self.model._data.iloc[
(self.position - self.number) : (self.position), :
]
self.model.removeRows(position=self.position, rows=self.number)

self.cleanup()

def cleanup(self):
self.counter.blockSignals(True)
self.counter.setValue(self.model.rowCount())
self.counter.blockSignals(False)
Expand Down
2 changes: 1 addition & 1 deletion src/main/python/pyes/viewmodels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def insertRows(self, position, rows=1, index=QModelIndex()):
else:
# for row in range(rows):
self._data = pd.concat(
[self._data[: position + 1], empty_rows, self._data[position + 1 :]],
[self._data[:position], empty_rows, self._data[position:]],
ignore_index=True,
)

Expand Down
147 changes: 68 additions & 79 deletions src/main/python/pyes/windows/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,88 +830,61 @@ def moveCompDown(self):
)

def insertSpeciesAbove(self):
if self.tablesTab.currentIndex() == 0:
if self.speciesView.selectedIndexes():
row = self.speciesView.selectedIndexes()[0].row()
else:
row = 0
self.undostack.push(
SpeciesAddRows(self.speciesView, self.numSpecies, row - 1, 1)
)
elif self.tablesTab.currentIndex() == 1:
if self.solidSpeciesView.selectedIndexes():
row = self.solidSpeciesView.selectedIndexes()[0].row()
else:
row = 0
self.undostack.push(
SpeciesAddRows(self.solidSpeciesView, self.numPhases, row - 1, 1)
)
table = self.get_shown_tab()
if not table:
return

counter = self.get_shown_tab_counter()
selected_indexes = table.selectedIndexes()
if selected_indexes:
row = selected_indexes[0].row()
else:
row = 0

self.undostack.push(SpeciesAddRows(table, counter, row, 1))

def insertSpeciesBelow(self):
if self.tablesTab.currentIndex() == 0:
if self.speciesView.selectedIndexes():
row = self.speciesView.selectedIndexes()[0].row()
else:
row = self.speciesModel.rowCount() - 1
self.undostack.push(
SpeciesAddRows(self.speciesView, self.numSpecies, row, 1)
)
elif self.tablesTab.currentIndex() == 1:
if self.solidSpeciesView.selectedIndexes():
row = self.solidSpeciesView.selectedIndexes()[0].row()
else:
row = self.solidSpeciesModel.rowCount() - 1
table = self.get_shown_tab()
if not table:
return

self.undostack.push(
SpeciesAddRows(self.solidSpeciesView, self.numPhases, row, 1)
)
counter = self.get_shown_tab_counter()
selected_indexes = table.selectedIndexes()
if selected_indexes:
row = selected_indexes[0].row() + 1
else:
row = table.model().rowCount()

self.undostack.push(SpeciesAddRows(table, counter, row, 1))

def removeSpecies(self):
if self.tablesTab.currentIndex() == 0:
if self.speciesView.selectedIndexes() and self.speciesModel.rowCount() > 1:
if (
QMessageBox.question(
self,
"Deleting Species",
"Are you sure you want to delete the selected species?",
)
== QMessageBox.Yes
):
self.undostack.push(
SpeciesRemoveRows(
self.speciesView,
self.numSpecies,
self.speciesView.selectedIndexes()[0].row() + 1,
1,
)
)
elif self.tablesTab.currentIndex() == 1:
if self.solidSpeciesView.selectedIndexes():
if (
QMessageBox.question(
self,
"Deleting Species",
"Are you sure you want to delete the selected species?",
)
== QMessageBox.Yes
):
self.undostack.push(
SpeciesRemoveRows(
self.solidSpeciesView,
self.numPhases,
self.solidSpeciesView.selectedIndexes()[0].row() + 1,
1,
)
table = self.get_shown_tab()
if not table or (table == self.speciesView and self.numSpecies.value() == 1):
return

counter = self.get_shown_tab_counter()
selected_indexes = table.selectedIndexes()
if selected_indexes:
if (
QMessageBox.question(
self,
"Deleting Species",
"Are you sure you want to delete the selected species?",
)
== QMessageBox.Yes
):
self.undostack.push(
SpeciesRemoveRows(
table,
counter,
selected_indexes[0].row() + 1,
1,
)
else:
pass
)

def moveSpeciesUp(self):
if self.tablesTab.currentIndex() == 0:
table = self.speciesView
elif self.tablesTab.currentIndex() == 1:
table = self.solidSpeciesView
else:
table = self.get_shown_tab()
if not table:
return

selected_indexes = table.selectedIndexes()
Expand All @@ -921,11 +894,8 @@ def moveSpeciesUp(self):
self.undostack.push(SpeciesSwapRows(table, row, row - 1))

def moveSpeciesDown(self):
if self.tablesTab.currentIndex() == 0:
table = self.speciesView
elif self.tablesTab.currentIndex() == 1:
table = self.solidSpeciesView
else:
table = self.get_shown_tab()
if not table:
return

selected_indexes = table.selectedIndexes()
Expand All @@ -934,6 +904,25 @@ def moveSpeciesDown(self):
if row != (table.model().rowCount() - 1):
self.undostack.push(SpeciesSwapRows(table, row, row + 1))

def get_shown_tab(self):
if self.tablesTab.currentIndex() == 0:
table = self.speciesView
elif self.tablesTab.currentIndex() == 1:
table = self.solidSpeciesView
else:
table = None
return table

def get_shown_tab_counter(self):
table = self.get_shown_tab()
if self.tablesTab.currentIndex() == 0:
counter = self.numSpecies
elif self.tablesTab.currentIndex() == 1:
counter = self.numPhases
else:
counter = None
return counter

def calculate(self):
# Disable the button, one omptimization calculation at the time
self.calcButton.setEnabled(False)
Expand Down

0 comments on commit fdc5f13

Please sign in to comment.