From 2f82a8d853541f1b502088812cf64564e13e007a Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sun, 4 Jun 2017 00:24:55 +0200 Subject: [PATCH] Bugfix: new package is selected after it is created --- src/lay/laySaltManagerDialog.cc | 51 ++++++++++++++++++++++++++------- src/lay/laySaltManagerDialog.h | 1 + 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/lay/laySaltManagerDialog.cc b/src/lay/laySaltManagerDialog.cc index c3fd2ced7..cb14cf630 100644 --- a/src/lay/laySaltManagerDialog.cc +++ b/src/lay/laySaltManagerDialog.cc @@ -556,6 +556,25 @@ SaltManagerDialog::edit_properties () } } +void +SaltManagerDialog::set_current_grain_by_name (const std::string ¤t) +{ + SaltModel *model = dynamic_cast (salt_view->model ()); + if (!model) { + return; + } + + for (int i = model->rowCount (QModelIndex ()); i > 0; ) { + --i; + QModelIndex index = model->index (i, 0, QModelIndex ()); + SaltGrain *g = model->grain_from_index (index); + if (g && g->name () == current) { + salt_view->setCurrentIndex (index); + break; + } + } +} + void SaltManagerDialog::create_grain () { @@ -572,15 +591,12 @@ BEGIN_PROTECTED // select the new one SaltModel *model = dynamic_cast (salt_view->model ()); if (model) { - for (int i = model->rowCount (QModelIndex ()); i > 0; ) { - --i; - QModelIndex index = model->index (i, 0, QModelIndex ()); - SaltGrain *g = model->grain_from_index (index); - if (g && g->name () == target.name ()) { - salt_view->setCurrentIndex (index); - break; - } - } + + // NOTE: this is basically redundant (because it happens in the background later + // through dm_update_models). But we need this now to establish the selection. + model->update(); + + set_current_grain_by_name (target.name ()); } @@ -675,6 +691,15 @@ SaltManagerDialog::update_models () model->clear_messages (); + // Maintain the current index while updating + std::string current; + if (salt_view->currentIndex ().isValid ()) { + const lay::SaltGrain *g = model->grain_from_index (salt_view->currentIndex ()); + if (g) { + current = g->name (); + } + } + // Establish a message saying that an update is available for (Salt::flat_iterator g = mp_salt->begin_flat (); g != mp_salt->end_flat (); ++g) { SaltGrain *gm = m_salt_mine.grain_by_name ((*g)->name ()); @@ -685,6 +710,10 @@ SaltManagerDialog::update_models () model->update (); + if (! current.empty ()) { + set_current_grain_by_name (current); + } + if (mp_salt->is_empty ()) { list_stack->setCurrentIndex (1); @@ -695,8 +724,8 @@ SaltManagerDialog::update_models () list_stack->setCurrentIndex (0); details_frame->show (); - // select the first grain - if (model->rowCount (QModelIndex ()) > 0) { + // select the first grain if required + if (! salt_view->currentIndex ().isValid () && model->rowCount (QModelIndex ()) > 0) { salt_view->setCurrentIndex (model->index (0, 0, QModelIndex ())); } diff --git a/src/lay/laySaltManagerDialog.h b/src/lay/laySaltManagerDialog.h index 07f8820c1..a73e90c8b 100644 --- a/src/lay/laySaltManagerDialog.h +++ b/src/lay/laySaltManagerDialog.h @@ -164,6 +164,7 @@ private: int m_current_tab; SaltGrain *current_grain (); + void set_current_grain_by_name (const std::string ¤t); void update_models (); void update_apply_state (); void get_remote_grain_info (lay::SaltGrain *g, SaltGrainDetailsTextWidget *details);