From 307ea55027bd7b47f59d16dbb733aede731bf68e Mon Sep 17 00:00:00 2001 From: Aaron Barany Date: Thu, 24 Jan 2019 01:21:34 -0800 Subject: [PATCH] CMake: Fix scrolling when editing configuration Find the index for the new item from the model rather than attempting to find the item by the row count, which would often return the incorrect index. It also scrolls to the item so it's in view once it starts editing. Listen for layout change to scroll to the selected index. This ensures that when you name an item after adding it, it will scroll to the new location to edit the value afterward. Change-Id: Iaa338148a40b921398cfe95da977371a91965a58 Reviewed-by: Tobias Hunger --- .../cmakebuildsettingswidget.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakebuildsettingswidget.cpp b/src/plugins/cmakeprojectmanager/cmakebuildsettingswidget.cpp index 396bc00f68a..3ee12eeb3bd 100644 --- a/src/plugins/cmakeprojectmanager/cmakebuildsettingswidget.cpp +++ b/src/plugins/cmakeprojectmanager/cmakebuildsettingswidget.cpp @@ -171,6 +171,12 @@ CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc) m_configTextFilterModel->setFilterKeyColumn(-1); m_configTextFilterModel->setFilterCaseSensitivity(Qt::CaseInsensitive); + connect(m_configTextFilterModel, &QAbstractItemModel::layoutChanged, this, [this]() { + QModelIndex selectedIdx = m_configView->currentIndex(); + if (selectedIdx.isValid()) + m_configView->scrollTo(selectedIdx); + }); + m_configView->setModel(m_configTextFilterModel); m_configView->setMinimumHeight(300); m_configView->setUniformRowHeights(true); @@ -307,9 +313,13 @@ CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc) value = QString::fromLatin1("OFF"); m_configModel->appendConfiguration(tr(""), value, type); - QModelIndex idx; - idx = m_configView->model()->index( - m_configView->model()->rowCount(idx) - 1, 0); + const Utils::TreeItem *item = m_configModel->findNonRootItem([&value, type](Utils::TreeItem *item) { + ConfigModel::DataItem dataItem = ConfigModel::dataItemFromIndex(item->index()); + return dataItem.key == tr("") && dataItem.type == type && dataItem.value == value; + }); + QModelIndex idx = m_configModel->indexForItem(item); + idx = m_configTextFilterModel->mapFromSource(m_configFilterModel->mapFromSource(idx)); + m_configView->scrollTo(idx); m_configView->setCurrentIndex(idx); m_configView->edit(idx); });