From 3892288695fca47cc6a5041cefad209b083754bd Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Wed, 24 Apr 2024 11:02:26 +0100 Subject: [PATCH] Refactor list view item checking actions The list view item checking actions, such as "Select All" and "Invert Selection", have been refactored to their own methods (CheckAll, InvertCheck) to reduce repetition. These methods have been implemented in numerous forms across the code. This makes the codebase cleaner and more maintainable as the operations are abstracted out to separate methods. Signed-off-by: Ivandro Jao --- src/ui/Forms/ApplyDurationLimits.cs | 16 ++------- ...tationAlphaStylesCategoriesImportExport.cs | 16 ++------- src/ui/Forms/AutoBreakUnbreakLines.cs | 10 ++---- src/ui/Forms/BatchConvert.cs | 10 ++---- src/ui/Forms/ChangeCasingNames.cs | 10 ++---- src/ui/Forms/FixCommonErrors.cs | 36 ++++--------------- src/ui/Forms/MergeShortLines.cs | 10 ++---- src/ui/Forms/MergeTextWithSameTimeCodes.cs | 10 ++---- src/ui/Forms/ModifySelection.cs | 10 ++---- src/ui/Forms/MultipleReplace.cs | 20 +++-------- src/ui/Forms/MultipleReplaceExportImport.cs | 10 ++---- src/ui/Forms/Ocr/BinaryOcrTrain.cs | 7 +--- src/ui/Forms/Ocr/VobSubCharactersImport.cs | 6 +--- src/ui/Forms/Options/SettingsProfileExport.cs | 10 ++---- src/ui/Forms/SplitLongLines.cs | 10 ++---- .../Styles/SubStationAlphaStylesExport.cs | 10 ++---- src/ui/Forms/VTT/WebVttImportExport.cs | 10 ++---- src/ui/Forms/VTT/WebVttStylePicker.cs | 10 ++---- src/ui/Logic/UiUtil.cs | 30 ++++++++++++++++ 19 files changed, 70 insertions(+), 181 deletions(-) diff --git a/src/ui/Forms/ApplyDurationLimits.cs b/src/ui/Forms/ApplyDurationLimits.cs index e0beff8c3..efb6d40b6 100644 --- a/src/ui/Forms/ApplyDurationLimits.cs +++ b/src/ui/Forms/ApplyDurationLimits.cs @@ -338,21 +338,9 @@ namespace Nikse.SubtitleEdit.Forms } } - private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) - { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } - } + private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) => listViewFixes.CheckAll(); - private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) - { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } - } + private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) => listViewFixes.InvertCheck(); private void checkBoxCheckShotChanges_CheckedChanged(object sender, EventArgs e) { diff --git a/src/ui/Forms/Assa/SubStationAlphaStylesCategoriesImportExport.cs b/src/ui/Forms/Assa/SubStationAlphaStylesCategoriesImportExport.cs index 5b0e01072..2f8664d93 100644 --- a/src/ui/Forms/Assa/SubStationAlphaStylesCategoriesImportExport.cs +++ b/src/ui/Forms/Assa/SubStationAlphaStylesCategoriesImportExport.cs @@ -125,20 +125,8 @@ namespace Nikse.SubtitleEdit.Forms.Assa } } - private void ToolStripMenuItemSelectAll_Click(object sender, EventArgs e) - { - foreach (ListViewItem item in listViewCategories.Items) - { - item.Checked = true; - } - } + private void ToolStripMenuItemSelectAll_Click(object sender, EventArgs e) => listViewCategories.CheckAll(); - private void ToolStripMenuItemInverseSelection_Click(object sender, EventArgs e) - { - foreach (ListViewItem item in listViewCategories.Items) - { - item.Checked = !item.Checked; - } - } + private void ToolStripMenuItemInverseSelection_Click(object sender, EventArgs e) => listViewCategories.InvertCheck(); } } diff --git a/src/ui/Forms/AutoBreakUnbreakLines.cs b/src/ui/Forms/AutoBreakUnbreakLines.cs index 430b6f3fa..08a185aa7 100644 --- a/src/ui/Forms/AutoBreakUnbreakLines.cs +++ b/src/ui/Forms/AutoBreakUnbreakLines.cs @@ -251,18 +251,12 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } + listViewFixes.CheckAll(); } private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } + listViewFixes.InvertCheck(); } private void AutoBreakUnbreakLines_FormClosing(object sender, FormClosingEventArgs e) diff --git a/src/ui/Forms/BatchConvert.cs b/src/ui/Forms/BatchConvert.cs index 6fefb758c..9e8ec11fc 100644 --- a/src/ui/Forms/BatchConvert.cs +++ b/src/ui/Forms/BatchConvert.cs @@ -3977,18 +3977,12 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewConvertOptions.Items) - { - item.Checked = true; - } + listViewConvertOptions.CheckAll(); } private void inverseSelectionToolStripMenuItem_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewConvertOptions.Items) - { - item.Checked = !item.Checked; - } + listViewConvertOptions.InvertCheck(); } private void listViewInputFiles_ColumnClick(object sender, ColumnClickEventArgs e) diff --git a/src/ui/Forms/ChangeCasingNames.cs b/src/ui/Forms/ChangeCasingNames.cs index 5fb3da6ed..f796880ce 100644 --- a/src/ui/Forms/ChangeCasingNames.cs +++ b/src/ui/Forms/ChangeCasingNames.cs @@ -335,18 +335,12 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } + listViewFixes.CheckAll(); } private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } + listViewFixes.InvertCheck(); } private void toolStripMenuItem1SelectAll_Click(object sender, EventArgs e) diff --git a/src/ui/Forms/FixCommonErrors.cs b/src/ui/Forms/FixCommonErrors.cs index ea9b2a18f..65d35f812 100644 --- a/src/ui/Forms/FixCommonErrors.cs +++ b/src/ui/Forms/FixCommonErrors.cs @@ -1145,21 +1145,9 @@ namespace Nikse.SubtitleEdit.Forms listViewFixes.Sort(); } - private void ButtonSelectAllClick(object sender, EventArgs e) - { - foreach (ListViewItem item in listView1.Items) - { - item.Checked = true; - } - } + private void ButtonSelectAllClick(object sender, EventArgs e) => listView1.CheckAll(); - private void ButtonInverseSelectionClick(object sender, EventArgs e) - { - foreach (ListViewItem item in listView1.Items) - { - item.Checked = !item.Checked; - } - } + private void ButtonInverseSelectionClick(object sender, EventArgs e) => listView1.InvertCheck(); private void ListViewFixesSelectedIndexChanged(object sender, EventArgs e) { @@ -1370,18 +1358,12 @@ namespace Nikse.SubtitleEdit.Forms private void ButtonFixesSelectAllClick(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } + listViewFixes.CheckAll(); } private void ButtonFixesInverseClick(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } + listViewFixes.InvertCheck(); } private void ButtonFixesApplyClick(object sender, EventArgs e) @@ -1948,18 +1930,12 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } + listViewFixes.CheckAll(); } private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } + listViewFixes.InvertCheck(); } private void setCurrentFixesAsDefaultToolStripMenuItem_Click(object sender, EventArgs e) diff --git a/src/ui/Forms/MergeShortLines.cs b/src/ui/Forms/MergeShortLines.cs index 71ff16387..25e2d662c 100644 --- a/src/ui/Forms/MergeShortLines.cs +++ b/src/ui/Forms/MergeShortLines.cs @@ -346,18 +346,12 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } + listViewFixes.CheckAll(); } private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } + listViewFixes.InvertCheck(); } } } diff --git a/src/ui/Forms/MergeTextWithSameTimeCodes.cs b/src/ui/Forms/MergeTextWithSameTimeCodes.cs index 7d10bb2fb..3ce97098b 100644 --- a/src/ui/Forms/MergeTextWithSameTimeCodes.cs +++ b/src/ui/Forms/MergeTextWithSameTimeCodes.cs @@ -297,18 +297,12 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } + listViewFixes.CheckAll(); } private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } + listViewFixes.InvertCheck(); } private void checkBoxMakeDialog_CheckedChanged(object sender, EventArgs e) diff --git a/src/ui/Forms/ModifySelection.cs b/src/ui/Forms/ModifySelection.cs index 2419ff755..a25b2b9c9 100644 --- a/src/ui/Forms/ModifySelection.cs +++ b/src/ui/Forms/ModifySelection.cs @@ -642,18 +642,12 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } + listViewFixes.CheckAll(); } private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } + listViewFixes.InvertCheck(); } private void ModifySelection_Resize(object sender, EventArgs e) diff --git a/src/ui/Forms/MultipleReplace.cs b/src/ui/Forms/MultipleReplace.cs index bc42e7280..a7ab11a9e 100644 --- a/src/ui/Forms/MultipleReplace.cs +++ b/src/ui/Forms/MultipleReplace.cs @@ -570,18 +570,12 @@ namespace Nikse.SubtitleEdit.Forms private void buttonReplacesSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } + listViewFixes.CheckAll(); } private void buttonReplacesInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } + listViewFixes.InvertCheck(); } private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e) @@ -1446,18 +1440,12 @@ namespace Nikse.SubtitleEdit.Forms private void selectAllToolStripMenuItem_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewRules.Items) - { - item.Checked = true; - } + listViewRules.CheckAll(); } private void inverseSelectionToolStripMenuItem_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewRules.Items) - { - item.Checked = !item.Checked; - } + listViewRules.InvertCheck(); } private void ContextMenuStripListViewFixesOpening(object sender, System.ComponentModel.CancelEventArgs e) diff --git a/src/ui/Forms/MultipleReplaceExportImport.cs b/src/ui/Forms/MultipleReplaceExportImport.cs index f1886962a..e71207e71 100644 --- a/src/ui/Forms/MultipleReplaceExportImport.cs +++ b/src/ui/Forms/MultipleReplaceExportImport.cs @@ -136,18 +136,12 @@ namespace Nikse.SubtitleEdit.Forms private void selectAllToolStripMenuItem_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = true; - } + listViewExportStyles.CheckAll(); } private void inverseSelectionToolStripMenuItem_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = !item.Checked; - } + listViewExportStyles.InvertCheck(); } } } diff --git a/src/ui/Forms/Ocr/BinaryOcrTrain.cs b/src/ui/Forms/Ocr/BinaryOcrTrain.cs index 096bcc4c0..675dd8757 100644 --- a/src/ui/Forms/Ocr/BinaryOcrTrain.cs +++ b/src/ui/Forms/Ocr/BinaryOcrTrain.cs @@ -328,12 +328,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr private void SelectAll_Click(object sender, EventArgs e) { - listViewFonts.BeginUpdate(); - foreach (ListViewItem fontItem in listViewFonts.Items) - { - fontItem.Checked = true; - } - listViewFonts.EndUpdate(); + listViewFonts.CheckAll(); } public void InitializeDetectFont(BinaryOcrBitmap bob, string text) diff --git a/src/ui/Forms/Ocr/VobSubCharactersImport.cs b/src/ui/Forms/Ocr/VobSubCharactersImport.cs index 89881497f..47ef345d6 100644 --- a/src/ui/Forms/Ocr/VobSubCharactersImport.cs +++ b/src/ui/Forms/Ocr/VobSubCharactersImport.cs @@ -143,11 +143,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr private void buttonFixesSelectAll_Click(object sender, EventArgs e) { listView1.ItemChecked -= listView1_ItemChecked; - - foreach (ListViewItem item in listView1.Items) - { - item.Checked = true; - } + listView1.CheckAll(); foreach (ListViewData d in _data) { diff --git a/src/ui/Forms/Options/SettingsProfileExport.cs b/src/ui/Forms/Options/SettingsProfileExport.cs index d0a79dda4..f7518faac 100644 --- a/src/ui/Forms/Options/SettingsProfileExport.cs +++ b/src/ui/Forms/Options/SettingsProfileExport.cs @@ -76,18 +76,12 @@ namespace Nikse.SubtitleEdit.Forms.Options private void selectAllToolStripMenuItem_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = true; - } + listViewExportStyles.CheckAll(); } private void inverseSelectionToolStripMenuItem_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = !item.Checked; - } + listViewExportStyles.InvertCheck(); } } } diff --git a/src/ui/Forms/SplitLongLines.cs b/src/ui/Forms/SplitLongLines.cs index 3772883ec..9bf788e6c 100644 --- a/src/ui/Forms/SplitLongLines.cs +++ b/src/ui/Forms/SplitLongLines.cs @@ -718,10 +718,7 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { listViewFixes.ItemChecked -= listViewFixes_ItemChecked; - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = true; - } + listViewFixes.CheckAll(); listViewFixes.ItemChecked += listViewFixes_ItemChecked; GeneratePreview(false); } @@ -729,10 +726,7 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { listViewFixes.ItemChecked -= listViewFixes_ItemChecked; - foreach (ListViewItem item in listViewFixes.Items) - { - item.Checked = !item.Checked; - } + listViewFixes.InvertCheck(); listViewFixes.ItemChecked += listViewFixes_ItemChecked; GeneratePreview(false); } diff --git a/src/ui/Forms/Styles/SubStationAlphaStylesExport.cs b/src/ui/Forms/Styles/SubStationAlphaStylesExport.cs index 738ee61ce..3f5fe4a86 100644 --- a/src/ui/Forms/Styles/SubStationAlphaStylesExport.cs +++ b/src/ui/Forms/Styles/SubStationAlphaStylesExport.cs @@ -186,18 +186,12 @@ namespace Nikse.SubtitleEdit.Forms.Styles private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = true; - } + listViewExportStyles.CheckAll(); } private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = !item.Checked; - } + listViewExportStyles.InvertCheck(); } } } diff --git a/src/ui/Forms/VTT/WebVttImportExport.cs b/src/ui/Forms/VTT/WebVttImportExport.cs index 3868abfa5..6b4d49cd9 100644 --- a/src/ui/Forms/VTT/WebVttImportExport.cs +++ b/src/ui/Forms/VTT/WebVttImportExport.cs @@ -145,18 +145,12 @@ namespace Nikse.SubtitleEdit.Forms.VTT private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = true; - } + listViewExportStyles.CheckAll(); } private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = !item.Checked; - } + listViewExportStyles.InvertCheck(); } } } diff --git a/src/ui/Forms/VTT/WebVttStylePicker.cs b/src/ui/Forms/VTT/WebVttStylePicker.cs index 43eead8b8..31319e4e4 100644 --- a/src/ui/Forms/VTT/WebVttStylePicker.cs +++ b/src/ui/Forms/VTT/WebVttStylePicker.cs @@ -57,18 +57,12 @@ namespace Nikse.SubtitleEdit.Forms.VTT private void toolStripMenuItemSelectAll_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = true; - } + listViewExportStyles.CheckAll(); } private void toolStripMenuItemInverseSelection_Click(object sender, EventArgs e) { - foreach (ListViewItem item in listViewExportStyles.Items) - { - item.Checked = !item.Checked; - } + listViewExportStyles.InvertCheck(); } private void listViewExportStyles_SelectedIndexChanged(object sender, EventArgs e) diff --git a/src/ui/Logic/UiUtil.cs b/src/ui/Logic/UiUtil.cs index 9155a4e07..7f978693c 100644 --- a/src/ui/Logic/UiUtil.cs +++ b/src/ui/Logic/UiUtil.cs @@ -1121,6 +1121,36 @@ namespace Nikse.SubtitleEdit.Logic public static void AutoSizeLastColumn(this ListView listView) => listView.Columns[listView.Columns.Count - 1].Width = -2; + public static void CheckAll(this ListView lv) + { + lv.BeginUpdate(); + foreach (ListViewItem item in lv.Items) + { + item.Checked = true; + } + lv.EndUpdate(); + } + + public static void InvertCheck(this ListView lv) + { + lv.BeginUpdate(); + foreach (ListViewItem item in lv.Items) + { + item.Checked = !item.Checked; + } + lv.EndUpdate(); + } + + public static void UncheckAll(this ListView lv) + { + lv.BeginUpdate(); + foreach (ListViewItem item in lv.Items) + { + item.Checked = false; + } + lv.EndUpdate(); + } + public static void SelectAll(this ListView lv) { lv.BeginUpdate();