fixing the issue where program shifts to the first tab after actions like deletion or editing

This commit is contained in:
Bernd 2025-07-20 00:27:16 +05:00
parent fa99b176c1
commit 4bae408a9a

View File

@ -139,6 +139,31 @@ class AnimeTracker(QMainWindow):
random_act.triggered.connect(self.random_pick)
tools_menu.addAction(random_act)
def get_current_tab_identifier(self):
index = self.tab_widget.currentIndex()
if index == -1:
return None
tab_text = self.tab_widget.tabText(index)
if "Pre-2010" in tab_text:
return "pre"
else:
return int(tab_text.split(" ")[0])
def set_current_tab_by_identifier(self, identifier):
if identifier is None:
return
if identifier == "pre":
for i in range(self.tab_widget.count()):
if "Pre-2010" in self.tab_widget.tabText(i):
self.tab_widget.setCurrentIndex(i)
return
else:
for i in range(self.tab_widget.count()):
t = self.tab_widget.tabText(i)
if t.startswith(str(identifier) + " ("):
self.tab_widget.setCurrentIndex(i)
return
def load_tabs(self):
self.tab_widget.clear()
# Pre-2010 tab
@ -346,27 +371,35 @@ class AnimeTracker(QMainWindow):
default_season = ''
dialog = AnimeDialog(self, None, default_year, default_season)
if dialog.exec_() == QDialog.Accepted:
current_id = self.get_current_tab_identifier()
data = dialog.get_data()
self.backend.add_anime(data)
self.load_tabs()
self.set_current_tab_by_identifier(current_id)
def edit_anime(self, anime_id):
entry = self.backend.get_anime_by_id(anime_id)
if entry:
current_id = self.get_current_tab_identifier()
dialog = AnimeDialog(self, entry)
if dialog.exec_() == QDialog.Accepted:
data = dialog.get_data()
self.backend.edit_anime(anime_id, data)
self.load_tabs()
self.set_current_tab_by_identifier(current_id)
def delete_anime(self, anime_id):
if QMessageBox.question(self, "Confirm Delete", "Are you sure you want to delete this entry?") == QMessageBox.Yes:
current_id = self.get_current_tab_identifier()
self.backend.delete_anime(anime_id)
self.load_tabs()
self.set_current_tab_by_identifier(current_id)
def change_status(self, anime_id, new_status):
current_id = self.get_current_tab_identifier()
self.backend.change_status(anime_id, new_status)
self.load_tabs()
self.set_current_tab_by_identifier(current_id)
def add_new_year(self):
current_year = datetime.now().year
@ -374,6 +407,7 @@ class AnimeTracker(QMainWindow):
if ok:
self.backend.add_placeholders_for_year(year)
self.load_tabs()
self.set_current_tab_by_identifier(year)
def delete_year(self):
current_year = datetime.now().year
@ -414,8 +448,10 @@ class AnimeTracker(QMainWindow):
def import_csv(self):
file_name, _ = QFileDialog.getOpenFileName(self, "Import CSV", "", "CSV Files (*.csv)")
if file_name:
current_id = self.get_current_tab_identifier()
self.backend.import_from_csv(file_name)
self.load_tabs()
self.set_current_tab_by_identifier(current_id)
def export_csv(self):
file_name, _ = QFileDialog.getSaveFileName(self, "Export CSV", "anime_backlog.csv", "CSV Files (*.csv)")