diff --git a/src/main/python/pyes/commands/models.py b/src/main/python/pyes/commands/models.py index 1091c98..6498518 100644 --- a/src/main/python/pyes/commands/models.py +++ b/src/main/python/pyes/commands/models.py @@ -59,6 +59,7 @@ def __init__( number: int, ): QUndoCommand.__init__(self) + self.table = table self.model = table.model().sourceModel() self.counter = counter self.position = position @@ -66,12 +67,13 @@ def __init__( 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) @@ -86,6 +88,7 @@ def __init__( number: int, ): QUndoCommand.__init__(self) + self.table = table self.model = table.model().sourceModel() self.counter = counter self.position = position @@ -93,19 +96,22 @@ def __init__( 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) diff --git a/src/main/python/pyes/viewmodels/models.py b/src/main/python/pyes/viewmodels/models.py index 15bcfb9..001f75d 100644 --- a/src/main/python/pyes/viewmodels/models.py +++ b/src/main/python/pyes/viewmodels/models.py @@ -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, ) diff --git a/src/main/python/pyes/windows/window.py b/src/main/python/pyes/windows/window.py index 0eab90b..981cbc7 100644 --- a/src/main/python/pyes/windows/window.py +++ b/src/main/python/pyes/windows/window.py @@ -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() @@ -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() @@ -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)