diff --git a/pwiz_tools/Skyline/Controls/WizardPages.cs b/pwiz_tools/Skyline/Controls/WizardPages.cs index 07fe21faf6..29b1069a5e 100644 --- a/pwiz_tools/Skyline/Controls/WizardPages.cs +++ b/pwiz_tools/Skyline/Controls/WizardPages.cs @@ -50,6 +50,17 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) return base.ProcessCmdKey(ref msg, keyData); } } + + public class WizardPageControl : UserControl + { + /// + /// Allow controls in a wizard to preempt close requests by the wizard's form. Returns true iff the close request should continue, false if it should be canceled. + /// + public virtual bool CanWizardClose() + { + return true; + } + } } diff --git a/pwiz_tools/Skyline/FileUI/PeptideSearch/ImportPeptideSearchDlg.cs b/pwiz_tools/Skyline/FileUI/PeptideSearch/ImportPeptideSearchDlg.cs index 346dc039fe..7590dd02de 100644 --- a/pwiz_tools/Skyline/FileUI/PeptideSearch/ImportPeptideSearchDlg.cs +++ b/pwiz_tools/Skyline/FileUI/PeptideSearch/ImportPeptideSearchDlg.cs @@ -226,6 +226,12 @@ private static void AddPageControl(TControl pageControl, TabPage tabPa tabPage.Controls.Add(pageControl); } + private UserControl GetPageControl(TabPage tabPage) + { + // Assume each tabPage only has a single UserControl; if that changes this function will need to be updated + return tabPage.Controls.OfType().SingleOrDefault(); + } + public SrmDocument Document { get @@ -998,7 +1004,6 @@ private void InitiateSearch() btnNext.Enabled = false; btnCancel.Enabled = false; btnBack.Enabled = false; - ControlBox = false; AbstractDdaConverter.MsdataFileFormat requiredFormat = IsFeatureDetectionWorkflow @@ -1085,7 +1090,6 @@ private void SearchControlSearchFinished(bool success) { btnCancel.Enabled = true; btnBack.Enabled = true; - ControlBox = true; btnNext.Enabled = success; } @@ -1484,8 +1488,21 @@ public void CloseWizard(DialogResult result) DialogResult = result; } + private bool CanWizardClose() + { + var wizardPageControl = GetPageControl(wizardPagesImportPeptideSearch.SelectedTab) as WizardPageControl; + return wizardPageControl == null || wizardPageControl.CanWizardClose(); + } + protected override void OnFormClosing(FormClosingEventArgs e) { + // Ask current WizardPageControl if wizard is in a good state to close + if (!CanWizardClose()) + { + e.Cancel = true; + return; + } + // Close file handles to the peptide search library ImportPeptideSearch.ClosePeptideSearchLibraryStreams(Document); diff --git a/pwiz_tools/Skyline/FileUI/PeptideSearch/PeptideSearchResources.designer.cs b/pwiz_tools/Skyline/FileUI/PeptideSearch/PeptideSearchResources.designer.cs index 5ef37e3264..7faf49c743 100644 --- a/pwiz_tools/Skyline/FileUI/PeptideSearch/PeptideSearchResources.designer.cs +++ b/pwiz_tools/Skyline/FileUI/PeptideSearch/PeptideSearchResources.designer.cs @@ -775,6 +775,15 @@ public static string MessageBoxHelper_ValidateAdductListTextBox__0__must_contain } } + /// + /// Looks up a localized string similar to Cannot close wizard while the search is running. Do you want to cancel the search?. + /// + public static string SearchControl_CanWizardClose_Cannot_close_wizard_while_the_search_is_running_ { + get { + return ResourceManager.GetString("SearchControl_CanWizardClose_Cannot_close_wizard_while_the_search_is_running_", resourceCulture); + } + } + /// /// Looks up a localized string similar to Additional Settings. /// diff --git a/pwiz_tools/Skyline/FileUI/PeptideSearch/PeptideSearchResources.resx b/pwiz_tools/Skyline/FileUI/PeptideSearch/PeptideSearchResources.resx index a46314ad4a..d221116c91 100644 --- a/pwiz_tools/Skyline/FileUI/PeptideSearch/PeptideSearchResources.resx +++ b/pwiz_tools/Skyline/FileUI/PeptideSearch/PeptideSearchResources.resx @@ -362,4 +362,7 @@ Click 'Retry' to try building again after placing the original spectrum files in Adding detected features to document + + Cannot close wizard while the search is running. Do you want to cancel the search? + \ No newline at end of file diff --git a/pwiz_tools/Skyline/FileUI/PeptideSearch/SearchControl.cs b/pwiz_tools/Skyline/FileUI/PeptideSearch/SearchControl.cs index 04e78d3845..30874b083c 100644 --- a/pwiz_tools/Skyline/FileUI/PeptideSearch/SearchControl.cs +++ b/pwiz_tools/Skyline/FileUI/PeptideSearch/SearchControl.cs @@ -27,12 +27,13 @@ using pwiz.Common.Controls; using pwiz.Common.SystemUtil; using pwiz.Skyline.Alerts; +using pwiz.Skyline.Controls; using pwiz.Skyline.Util; using pwiz.Skyline.Util.Extensions; namespace pwiz.Skyline.FileUI.PeptideSearch { - public abstract partial class SearchControl : UserControl, IProgressMonitor + public abstract partial class SearchControl : WizardPageControl, IProgressMonitor { public Action UpdateUI; @@ -266,6 +267,22 @@ public void Cancel() btnCancel.Enabled = false; } + public override bool CanWizardClose() + { + if (btnCancel.Enabled) + { + if (DialogResult.Yes == MessageDlg.Show(Parent, + PeptideSearchResources.SearchControl_CanWizardClose_Cannot_close_wizard_while_the_search_is_running_, false, + MessageBoxButtons.YesNo)) + { + Cancel(); + SearchFinished += _ => ParentForm?.Close(); + } + return false; + } + return base.CanWizardClose(); + } + private void btnCancel_Click(object sender, EventArgs e) { Cancel(); diff --git a/pwiz_tools/Skyline/TestFunctional/DdaSearchTest.cs b/pwiz_tools/Skyline/TestFunctional/DdaSearchTest.cs index 9d2e835e51..e58486c703 100644 --- a/pwiz_tools/Skyline/TestFunctional/DdaSearchTest.cs +++ b/pwiz_tools/Skyline/TestFunctional/DdaSearchTest.cs @@ -407,13 +407,20 @@ private void TestSearch() { importPeptideSearchDlg.SearchControl.SearchFinished += (success) => searchSucceeded = success; importPeptideSearchDlg.BuildPepSearchLibControl.IncludeAmbiguousMatches = true; - - // Cancel search - if (!errorExpected) - importPeptideSearchDlg.SearchControl.Cancel(); }); - if (errorExpected) + if (!errorExpected) + { + SkylineWindow.BeginInvoke(new Action(importPeptideSearchDlg.Close)); // try to close (don't wait for return) + var cannotCloseDuringSearchDlg = WaitForOpenForm(); + Assert.AreEqual(PeptideSearchResources.SearchControl_CanWizardClose_Cannot_close_wizard_while_the_search_is_running_, + cannotCloseDuringSearchDlg.Message); + OkDialog(cannotCloseDuringSearchDlg, cannotCloseDuringSearchDlg.ClickNo); + + // Cancel search (but don't close wizard) + RunUI(importPeptideSearchDlg.SearchControl.Cancel); + } + else // errorExpected { var fastaFileNotFoundDlg = WaitForOpenForm(); var expectedMsg = new FileNotFoundException(GetSystemResourceString("IO.FileNotFound_FileName", GetTestPath(TestSettings.FastaFilename))).Message;