From e9ce64943ecdaa6ea06bd124b61b644345f352e6 Mon Sep 17 00:00:00 2001 From: Joerg Date: Mon, 30 Dec 2024 14:08:49 +0100 Subject: [PATCH] Replaced dynamic tab addition/removal with setTabVisible. Added methods to count and find the first visible tab. --- src/controllers/dlgprefcontroller.cpp | 74 ++++++++++++++------------- src/controllers/dlgprefcontroller.h | 4 ++ 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/controllers/dlgprefcontroller.cpp b/src/controllers/dlgprefcontroller.cpp index 2855bccfe9d..b2d10010e6e 100644 --- a/src/controllers/dlgprefcontroller.cpp +++ b/src/controllers/dlgprefcontroller.cpp @@ -82,7 +82,9 @@ DlgPrefController::DlgPrefController( m_GuiInitialized(false), m_bDirty(false), m_inputMappingsTabIndex(-1), - m_outputMappingsTabIndex(-1) { + m_outputMappingsTabIndex(-1), + m_settingsTabIndex(-1), + m_screensTabIndex(-1) { m_ui.setupUi(this); // Create text color for the file and wiki links createLinkColor(); @@ -307,6 +309,8 @@ DlgPrefController::DlgPrefController( // Store the index of the input and output mappings tabs m_inputMappingsTabIndex = m_ui.controllerTabs->indexOf(m_ui.inputMappingsTab); m_outputMappingsTabIndex = m_ui.controllerTabs->indexOf(m_ui.outputMappingsTab); + m_settingsTabIndex = m_ui.controllerTabs->indexOf(m_ui.settingsTab); + m_screensTabIndex = m_ui.controllerTabs->indexOf(m_ui.screensTab); } DlgPrefController::~DlgPrefController() { @@ -776,24 +780,35 @@ void DlgPrefController::slotMappingSelected(int chosenIndex) { // These tabs are only usable for MIDI controllers if (m_pController->getDataRepresentationProtocol() == DataRepresentationProtocol::MIDI && !mappingFilePath.isEmpty()) { - if (m_ui.controllerTabs->indexOf(m_ui.inputMappingsTab) == -1) { - m_ui.controllerTabs->insertTab(m_inputMappingsTabIndex, - m_ui.inputMappingsTab, - tr("Input Mappings")); - } - if (m_ui.controllerTabs->indexOf(m_ui.outputMappingsTab) == -1) { - m_ui.controllerTabs->insertTab(m_outputMappingsTabIndex, - m_ui.outputMappingsTab, - tr("Output Mappings")); - } + m_ui.controllerTabs->setTabVisible(m_inputMappingsTabIndex, true); + m_ui.controllerTabs->setTabVisible(m_outputMappingsTabIndex, true); + } else { - m_ui.controllerTabs->removeTab(m_ui.controllerTabs->indexOf(m_ui.inputMappingsTab)); - m_ui.controllerTabs->removeTab(m_ui.controllerTabs->indexOf(m_ui.outputMappingsTab)); + m_ui.controllerTabs->setTabVisible(m_inputMappingsTabIndex, false); + m_ui.controllerTabs->setTabVisible(m_outputMappingsTabIndex, false); } // Hide the entire QTabWidget if all tabs are removed - bool hasTabs = m_ui.controllerTabs->count() > 0; - m_ui.controllerTabs->setVisible(hasTabs); + m_ui.controllerTabs->setVisible(getNumberOfVisibleTabs() > 0); +} + +unsigned int DlgPrefController::getNumberOfVisibleTabs() { + unsigned int visibleTabsCount = 0; + for (int tabIdx = 0; tabIdx < m_ui.controllerTabs->count(); ++tabIdx) { + if (m_ui.controllerTabs->isTabVisible(tabIdx)) { + ++visibleTabsCount; + } + } + return visibleTabsCount; +} + +int DlgPrefController::getIndexOfFirstVisibleTabs() { + for (int tabIdx = 0; tabIdx < m_ui.controllerTabs->count(); ++tabIdx) { + if (m_ui.controllerTabs->isTabVisible(tabIdx)) { + return tabIdx; + } + } + return -1; } bool DlgPrefController::saveMapping() { @@ -1017,9 +1032,6 @@ void DlgPrefController::slotShowMapping(std::shared_ptr // the script files label if they are empty, // because without them the layout would be collapse. - // Remove all tabs first - m_ui.controllerTabs->clear(); - if (pMapping) { pMapping->loadSettings(m_pConfig, m_pController->getName()); auto settings = pMapping->getSettings(); @@ -1044,13 +1056,9 @@ void DlgPrefController::slotShowMapping(std::shared_ptr } // Show or hide the settings tab based on the presence of settings - if (pMapping && !pMapping->getSettings().isEmpty()) { - if (m_ui.controllerTabs->indexOf(m_ui.settingsTab) == -1) { - m_ui.controllerTabs->addTab(m_ui.settingsTab, tr("Mapping Settings")); - } - } else { - m_ui.controllerTabs->removeTab(m_ui.controllerTabs->indexOf(m_ui.settingsTab)); - } + + m_ui.controllerTabs->setTabVisible( + m_settingsTabIndex, pMapping && !pMapping->getSettings().isEmpty()); // If there is still settings that may be saved and no new mapping selected // (e.g restored default), we keep the the dirty mapping live so it can be @@ -1068,16 +1076,12 @@ void DlgPrefController::slotShowMapping(std::shared_ptr if (pMapping && CmdlineArgs::Instance().getControllerPreviewScreens()) { auto screens = pMapping->getInfoScreens(); bool hasScreens = !screens.isEmpty(); + m_ui.controllerTabs->setTabVisible(m_screensTabIndex, hasScreens); if (hasScreens) { - if (m_ui.controllerTabs->indexOf(m_ui.screensTab) == -1) { - m_ui.controllerTabs->addTab(m_ui.screensTab, tr("Screens preview")); - } slotShowPreviewScreens(m_pController->getScriptEngine().get()); - } else { - m_ui.controllerTabs->removeTab(m_ui.controllerTabs->indexOf(m_ui.screensTab)); } } else { - m_ui.controllerTabs->removeTab(m_ui.controllerTabs->indexOf(m_ui.screensTab)); + m_ui.controllerTabs->setTabVisible(m_screensTabIndex, false); } #endif @@ -1138,12 +1142,12 @@ void DlgPrefController::slotShowMapping(std::shared_ptr slotOutputControlSearch(); // Hide the entire QTabWidget if all tabs are removed - bool hasTabs = m_ui.controllerTabs->count() > 0; - m_ui.controllerTabs->setVisible(hasTabs); + m_ui.controllerTabs->setVisible(getNumberOfVisibleTabs() > 0); // Set the first visible tab as the current tab - if (hasTabs) { - m_ui.controllerTabs->setCurrentIndex(0); + int firstVisibleTabIndex = getIndexOfFirstVisibleTabs(); + if (firstVisibleTabIndex >= 0) { + m_ui.controllerTabs->setCurrentIndex(firstVisibleTabIndex); } } diff --git a/src/controllers/dlgprefcontroller.h b/src/controllers/dlgprefcontroller.h index 8af56b5f763..3b21cd24408 100644 --- a/src/controllers/dlgprefcontroller.h +++ b/src/controllers/dlgprefcontroller.h @@ -95,6 +95,8 @@ class DlgPrefController : public DlgPreferencePage { void applyMappingChanges(); bool saveMapping(); void initTableView(QTableView* pTable); + unsigned int getNumberOfVisibleTabs(); + int getIndexOfFirstVisibleTabs(); /// Set dirty state (i.e. changes have been made). /// @@ -139,4 +141,6 @@ class DlgPrefController : public DlgPreferencePage { bool m_bDirty; int m_inputMappingsTabIndex; // Index of the input mappings tab int m_outputMappingsTabIndex; // Index of the output mappings tab + int m_settingsTabIndex; // Index of the settings tab + int m_screensTabIndex; // Index of the screens tab };