1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-12-21 10:59:03 +00:00

Fix tab indices not aligned with stackwidget's

* The custom QStackWidget+QTabBar class did not manage the tabs properly because the indices in the stackwidget were not aligned with the ones in the tab bar.
* Properly disable exclude list action when it is the currently displayed widget.
* Merge action callbacks for triggering ignore list or exclude list to avoid repeating code and remove unused checks for tab visibility.
* Remove unused SetTabVisible() function.
This commit is contained in:
glubsy 2020-08-23 16:49:43 +02:00
parent 3382bd5e5b
commit 26d18945b1
2 changed files with 45 additions and 56 deletions

View File

@ -279,35 +279,24 @@ class DupeGuru(QObject):
def ignoreListTriggered(self): def ignoreListTriggered(self):
if self.use_tabs: if self.use_tabs:
# Fetch the index in the TabWidget or the StackWidget (depends on class): self.showTriggeredTabbedDialog(self.ignoreListDialog, "Ignore List")
index = self.main_window.indexOfWidget(self.ignoreListDialog) else: # floating windows
if index < 0:
# we have not instantiated and populated it in their internal list yet
index = self.main_window.addTab(
self.ignoreListDialog, "Ignore List", switch=True)
elif not self.ignoreListDialog.isVisible() and not self.main_window.isTabVisible(index):
index = self.main_window.addTab(
self.ignoreListDialog, "Ignore List", switch=True)
# self.main_window.showTab(self.ignoreListDialog)
self.main_window.setTabVisible(index, True)
self.main_window.setCurrentIndex(index)
else:
self.model.ignore_list_dialog.show() self.model.ignore_list_dialog.show()
def excludeListTriggered(self): def excludeListTriggered(self):
if self.use_tabs: if self.use_tabs:
index = self.main_window.indexOfWidget(self.excludeListDialog) self.showTriggeredTabbedDialog(self.excludeListDialog, "Exclude List")
if index < 0: else: # floating windows
index = self.main_window.addTab( self.model.exclude_list_dialog.show()
self.excludeListDialog, "Exclude List", switch=True)
elif not self.excludeListDialog.isVisible() and not self.main_window.isTabVisible(index): def showTriggeredTabbedDialog(self, dialog, desc_string):
index = self.main_window.addTab( """Add tab for dialog, name the tab with desc_string, then show it."""
self.excludeListDialog, "Exclude List", switch=True) index = self.main_window.indexOfWidget(dialog)
# self.main_window.showTab(self.excludeListDialog) # Create the tab if it doesn't exist already
self.main_window.setTabVisible(index, True) if index < 0: # or (not dialog.isVisible() and not self.main_window.isTabVisible(index)):
self.main_window.setCurrentIndex(index) index = self.main_window.addTab(dialog, desc_string, switch=True)
else: # Show the tab for that widget
self.excludeListDialog.show() self.main_window.setCurrentIndex(index)
def openDebugLogTriggered(self): def openDebugLogTriggered(self):
debugLogPath = op.join(self.model.appdata, "debug.log") debugLogPath = op.join(self.model.appdata, "debug.log")

View File

@ -109,7 +109,7 @@ class TabWindow(QMainWindow):
self.menuList.add(self.menuHelp) self.menuList.add(self.menuHelp)
@pyqtSlot(int) @pyqtSlot(int)
def updateMenuBar(self, page_index=None): def updateMenuBar(self, page_index=-1):
if page_index < 0: if page_index < 0:
return return
current_index = self.getCurrentIndex() current_index = self.getCurrentIndex()
@ -142,7 +142,9 @@ class TabWindow(QMainWindow):
and not page_type == "IgnoreListDialog" else False) and not page_type == "IgnoreListDialog" else False)
self.app.actionDirectoriesWindow.setEnabled( self.app.actionDirectoriesWindow.setEnabled(
False if page_type == "DirectoriesDialog" else True) False if page_type == "DirectoriesDialog" else True)
# FIXME add ExcludeListDialog here too self.app.actionExcludeList.setEnabled(
True if self.app.excludeListDialog is not None
and not page_type == "ExcludeListDialog" else False)
self.previous_widget_actions = active_widget.specific_actions self.previous_widget_actions = active_widget.specific_actions
self.last_index = current_index self.last_index = current_index
@ -182,7 +184,6 @@ class TabWindow(QMainWindow):
def showTab(self, page): def showTab(self, page):
index = self.indexOfWidget(page) index = self.indexOfWidget(page)
self.setTabVisible(index, True)
self.setCurrentIndex(index) self.setCurrentIndex(index)
def indexOfWidget(self, widget): def indexOfWidget(self, widget):
@ -191,9 +192,6 @@ class TabWindow(QMainWindow):
def setCurrentIndex(self, index): def setCurrentIndex(self, index):
return self.tabWidget.setCurrentIndex(index) return self.tabWidget.setCurrentIndex(index)
def setTabVisible(self, index, value):
return self.tabWidget.setTabVisible(index, value)
def removeTab(self, index): def removeTab(self, index):
return self.tabWidget.removeTab(index) return self.tabWidget.removeTab(index)
@ -232,14 +230,13 @@ class TabWindow(QMainWindow):
# menu or shortcut. But this is useless if we don't have a button # menu or shortcut. But this is useless if we don't have a button
# set up to make a close request anyway. This check could be removed. # set up to make a close request anyway. This check could be removed.
return return
current_widget.close() # current_widget.close() # seems unnecessary
self.setTabVisible(index, False)
# self.tabWidget.widget(index).hide() # self.tabWidget.widget(index).hide()
self.removeTab(index) self.removeTab(index)
@pyqtSlot() @pyqtSlot()
def onDialogAccepted(self): def onDialogAccepted(self):
"""Remove tabbed dialog when Accepted/Done.""" """Remove tabbed dialog when Accepted/Done (close button clicked)."""
widget = self.sender() widget = self.sender()
index = self.indexOfWidget(widget) index = self.indexOfWidget(widget)
if index > -1: if index > -1:
@ -277,7 +274,7 @@ class TabBarWindow(TabWindow):
self.verticalLayout.addLayout(self.horizontalLayout) self.verticalLayout.addLayout(self.horizontalLayout)
self.verticalLayout.addWidget(self.stackedWidget) self.verticalLayout.addWidget(self.stackedWidget)
self.tabBar.currentChanged.connect(self.showWidget) self.tabBar.currentChanged.connect(self.showTabIndex)
self.tabBar.tabCloseRequested.connect(self.onTabCloseRequested) self.tabBar.tabCloseRequested.connect(self.onTabCloseRequested)
self.stackedWidget.currentChanged.connect(self.updateMenuBar) self.stackedWidget.currentChanged.connect(self.updateMenuBar)
@ -287,50 +284,48 @@ class TabBarWindow(TabWindow):
self.restoreGeometry() self.restoreGeometry()
def addTab(self, page, title, switch=True): def addTab(self, page, title, switch=True):
stack_index = self.stackedWidget.insertWidget(-1, page) stack_index = self.stackedWidget.addWidget(page)
tab_index = self.tabBar.addTab(title) self.tabBar.insertTab(stack_index, title)
if isinstance(page, DirectoriesDialog): if isinstance(page, DirectoriesDialog):
self.tabBar.setTabButton( self.tabBar.setTabButton(
tab_index, QTabBar.RightSide, None) stack_index, QTabBar.RightSide, None)
if switch: # switch to the added tab immediately upon creation if switch: # switch to the added tab immediately upon creation
self.setTabIndex(tab_index) self.setTabIndex(stack_index)
self.stackedWidget.setCurrentWidget(page)
return stack_index return stack_index
@pyqtSlot(int) @pyqtSlot(int)
def showWidget(self, index): def showTabIndex(self, index):
if index >= 0 and index <= self.stackedWidget.count() - 1: # The tab bar's indices should be aligned with the stackwidget's
if index >= 0 and index <= self.stackedWidget.count():
self.stackedWidget.setCurrentIndex(index) self.stackedWidget.setCurrentIndex(index)
# if not self.tabBar.isTabVisible(index):
self.setTabVisible(index, True)
def indexOfWidget(self, widget): def indexOfWidget(self, widget):
# Warning: this may return -1 if widget is not a child of stackedwidget # Warning: this may return -1 if widget is not a child of stackedwidget
return self.stackedWidget.indexOf(widget) return self.stackedWidget.indexOf(widget)
def setCurrentIndex(self, tab_index): def setCurrentIndex(self, tab_index):
# The signal will handle switching the stackwidget's widget
self.setTabIndex(tab_index) self.setTabIndex(tab_index)
# The signal will handle switching the stackwidget's widget
# self.stackedWidget.setCurrentWidget(self.stackedWidget.widget(tab_index)) # self.stackedWidget.setCurrentWidget(self.stackedWidget.widget(tab_index))
def setCurrentWidget(self, widget):
"""Sets the current Tab on TabBar for this widget."""
self.tabBar.setCurrentIndex(self.indexOfWidget(widget))
@pyqtSlot(int) @pyqtSlot(int)
def setTabIndex(self, index): def setTabIndex(self, index):
if index is None: if index is None:
return return
self.tabBar.setCurrentIndex(index) self.tabBar.setCurrentIndex(index)
def setTabVisible(self, index, value):
return self.tabBar.setTabVisible(index, value)
@pyqtSlot(int) @pyqtSlot(int)
def onRemovedWidget(self, index): def onRemovedWidget(self, index):
self.removeTab(index) self.removeTab(index)
@pyqtSlot(int) @pyqtSlot(int)
def removeTab(self, index): def removeTab(self, index):
# No need to remove the widget here: """Remove the tab, but not the widget (it should already be removed)"""
# self.stackedWidget.removeWidget(self.stackedWidget.widget(index))
return self.tabBar.removeTab(index) return self.tabBar.removeTab(index)
@pyqtSlot(int) @pyqtSlot(int)
@ -357,13 +352,18 @@ class TabBarWindow(TabWindow):
@pyqtSlot(int) @pyqtSlot(int)
def onTabCloseRequested(self, index): def onTabCloseRequested(self, index):
current_widget = self.getWidgetAtIndex(index) target_widget = self.getWidgetAtIndex(index)
if isinstance(current_widget, DirectoriesDialog): if isinstance(target_widget, DirectoriesDialog):
# On MacOS, the tab has a close button even though we explicitely # On MacOS, the tab has a close button even though we explicitely
# set it to None in order to hide it. This should prevent # set it to None in order to hide it. This should prevent
# the "Directories" tab from closing by mistake. # the "Directories" tab from closing by mistake.
return return
current_widget.close() # target_widget.close() # seems unnecessary
self.stackedWidget.removeWidget(current_widget) # Removing the widget should trigger tab removal via the signal
# In this case the signal will take care of the tab itself after removing the widget self.removeWidget(self.getWidgetAtIndex(index))
# self.removeTab(index)
@pyqtSlot()
def onDialogAccepted(self):
"""Remove tabbed dialog when Accepted/Done (close button clicked)."""
widget = self.sender()
self.removeWidget(widget)