@@ -211,9 +211,17 @@ void CConnectDlg::RequestServerList()
211211
212212 // update list combo box (disable events to avoid a signal)
213213 cbxDirectoryServer->blockSignals ( true );
214- // iCustomDirectoryIndex is non-zero only if eCentralServerAddressType == AT_CUSTOM, and represents
215- // the offset into cbxDirectoryServer after the last non-custom directory server
216- cbxDirectoryServer->setCurrentIndex ( static_cast <int > ( pSettings->eCentralServerAddressType ) + pSettings->iCustomDirectoryIndex );
214+ if ( pSettings->eCentralServerAddressType == AT_CUSTOM )
215+ {
216+ // iCustomDirectoryIndex is non-zero only if eCentralServerAddressType == AT_CUSTOM
217+ // find the combobox item that corresponds to vstrCentralServerAddress[iCustomDirectoryIndex]
218+ // (the current selected custom directory)
219+ cbxDirectoryServer->setCurrentIndex ( cbxDirectoryServer->findData ( QVariant ( pSettings->iCustomDirectoryIndex ) ) );
220+ }
221+ else
222+ {
223+ cbxDirectoryServer->setCurrentIndex ( static_cast <int > ( pSettings->eCentralServerAddressType ) );
224+ }
217225 cbxDirectoryServer->blockSignals ( false );
218226
219227 // Get the IP address of the directory server (using the ParseNetworAddress
@@ -249,7 +257,8 @@ void CConnectDlg::OnDirectoryServerChanged ( int iTypeIdx )
249257 // if iTypeIdx != AT_CUSTOM, then iCustomDirectoryIndex MUST be 0;
250258 if ( iTypeIdx >= AT_CUSTOM )
251259 {
252- pSettings->iCustomDirectoryIndex = iTypeIdx - AT_CUSTOM;
260+ // the value for the index into the vector vstrCentralServerAddress is in the user data of the combobox item
261+ pSettings->iCustomDirectoryIndex = cbxDirectoryServer->itemData ( iTypeIdx ).toInt ();
253262 iTypeIdx = AT_CUSTOM;
254263 }
255264 else
@@ -529,15 +538,39 @@ void CConnectDlg::OnServerAddrEditTextChanged ( const QString& )
529538
530539void CConnectDlg::OnCustomCentralServerAddrChanged ()
531540{
541+
542+ QString strPreviousSelection = cbxDirectoryServer->currentText ();
532543 UpdateDirectoryServerComboBox ();
533- // only update list if a custom server list is selected
544+ // after updating the combobox, we must re-select the previous directory selection
545+
534546 if ( pSettings->eCentralServerAddressType == AT_CUSTOM )
535547 {
536- // TODO: detect if the currently select custom directory still exists in the now potentially re-ordered vector,
537- // if so, then change to its new index. Issue #1899
538- pSettings->eCentralServerAddressType = static_cast <ECSAddType> ( AT_DEFAULT );
539- pSettings->iCustomDirectoryIndex = 0 ;
540- RequestServerList ();
548+ // check if the currently select custom directory still exists in the now potentially re-ordered vector,
549+ // if so, then change to its new index. (addresses Issue #1899)
550+ int iNewIndex = cbxDirectoryServer->findText ( strPreviousSelection, Qt::MatchExactly );
551+ if ( iNewIndex == INVALID_INDEX )
552+ {
553+ // previously selected custom directory has been deleted. change to default directory
554+ pSettings->eCentralServerAddressType = static_cast <ECSAddType> ( AT_DEFAULT );
555+ pSettings->iCustomDirectoryIndex = 0 ;
556+ RequestServerList ();
557+ }
558+ else
559+ {
560+ // find previously selected custom directory in the now potentially re-ordered vector
561+ pSettings->eCentralServerAddressType = static_cast <ECSAddType> ( AT_CUSTOM );
562+ pSettings->iCustomDirectoryIndex = cbxDirectoryServer->itemData ( iNewIndex ).toInt ();
563+ cbxDirectoryServer->blockSignals ( true );
564+ cbxDirectoryServer->setCurrentIndex ( cbxDirectoryServer->findData ( QVariant ( pSettings->iCustomDirectoryIndex ) ) );
565+ cbxDirectoryServer->blockSignals ( false );
566+ }
567+ }
568+ else
569+ {
570+ // selected directory was not a custom directory
571+ cbxDirectoryServer->blockSignals ( true );
572+ cbxDirectoryServer->setCurrentIndex ( static_cast <int > ( pSettings->eCentralServerAddressType ) );
573+ cbxDirectoryServer->blockSignals ( false );
541574 }
542575}
543576
@@ -910,11 +943,14 @@ void CConnectDlg::UpdateDirectoryServerComboBox()
910943 cbxDirectoryServer->addItem ( csCentServAddrTypeToString ( AT_GENRE_CLASSICAL_FOLK ) );
911944 cbxDirectoryServer->addItem ( csCentServAddrTypeToString ( AT_GENRE_CHORAL ) );
912945
913- for ( int i = 0 ; i < MAX_NUM_SERVER_ADDR_ITEMS; i++ )
946+ // because custom directories are always added to the top of the vector, add the vector
947+ // contents to the combobox in reverse order
948+ for ( int i = MAX_NUM_SERVER_ADDR_ITEMS - 1 ; i >= 0 ; i-- )
914949 {
915950 if ( pSettings->vstrCentralServerAddress [i] != " " )
916951 {
917- cbxDirectoryServer->addItem ( pSettings->vstrCentralServerAddress [i] );
952+ // add vector index (i) to the combobox as user data
953+ cbxDirectoryServer->addItem ( pSettings->vstrCentralServerAddress [i], i );
918954 }
919955 }
920956}
0 commit comments