PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/PowerSong/frmMain.Helper.vb

#
Visual Basic | 327 lines | 217 code | 79 blank | 31 comment | 0 complexity | d6c6d3973ef278b8d2bd847bb6389ff8 MD5 | raw file
  1. Imports System.IO
  2. Imports PowerSong.SongDatabase
  3. Imports PowerSong.SongDatabase.Items
  4. Imports PowerSong.Projection
  5. Imports PowerSong.SongDatabase.Logging
  6. Partial Public Class frmMain
  7. Private FActiveItem As BaseItem = Nothing
  8. Private FActiveStyle As Style = Nothing
  9. Private FActivePart As Integer = -1
  10. Private FPreviewProjector As Projector = Nothing
  11. Private FProjections As New List(Of Projector)
  12. Private FProjectionForm As frmMainProjection = Nothing
  13. #Region " Form Update Routines "
  14. Public Sub UpdateAll()
  15. UpdateTitle()
  16. UpdateItemList()
  17. UpdateCategoryList()
  18. UpdateActiveItem()
  19. UpdateControlVisibility()
  20. UpdateProjectorStyle()
  21. End Sub
  22. Private Sub UpdateTitle()
  23. Text = "PowerSong"
  24. If Database IsNot Nothing Then Text += " - " + DatabaseLocation
  25. End Sub
  26. Private Sub UpdateActiveItem()
  27. lstVerses.Items.Clear()
  28. If FActiveItem IsNot Nothing Then
  29. For Each Part As String In FActiveItem.Parts
  30. lstVerses.Items.Add(Part)
  31. Next
  32. End If
  33. End Sub
  34. Private Sub UpdateItemList()
  35. ' If the search box is not empty then do the search instead of showing all items
  36. If txtSearch.Text <> "" Then
  37. txtSearch_TextChanged(Nothing, EventArgs.Empty)
  38. Exit Sub
  39. End If
  40. lstItems.Items.Clear()
  41. lstItems.BeginUpdate()
  42. If Database IsNot Nothing Then
  43. If optSongs.Checked Then
  44. ' Add all songs with the selected categories
  45. Dim AddedSongs As New List(Of Guid)
  46. For Each SelectedCategory As ListItem(Of Guid) In lstCategories.CheckedItems
  47. For Each SongID As Guid In Database.Songs.GetCategorySongIDs(SelectedCategory.Key)
  48. ' Has the song already been added?
  49. If Not AddedSongs.Contains(SongID) Then
  50. Dim Item As New ListItem(Of Guid)(Database.Songs.GetSongTitle(SongID), SongID)
  51. lstItems.Items.Add(Item)
  52. AddedSongs.Add(SongID)
  53. End If
  54. Next
  55. Next
  56. lstItems.Sorted = True
  57. Else
  58. ' Add all bible chapters
  59. If BibleIndex IsNot Nothing Then
  60. lstItems.Sorted = False
  61. For Each Chapter As String In BibleIndex.GetAllChapters
  62. lstItems.Items.Add(New ListItem(Of String)(IO.Path.GetFileNameWithoutExtension(Chapter), Chapter))
  63. Next
  64. End If
  65. End If
  66. End If
  67. lstItems.EndUpdate()
  68. ' Update other controls
  69. UpdateItemCountLabel()
  70. UpdateControlVisibility()
  71. End Sub
  72. Friend Sub UpdateCategoryList()
  73. If optBible.Checked Then
  74. ' Show available bible translations
  75. lstCategories.Enabled = False
  76. lblSelectWhatToInclude.Enabled = False
  77. Else
  78. lstCategories.Enabled = True
  79. lblSelectWhatToInclude.Enabled = True
  80. ' Determine which categories are currently selected
  81. Dim CheckedItems As New List(Of Guid)
  82. For ItemIndex As Integer = 0 To lstCategories.Items.Count - 1
  83. If lstCategories.GetItemChecked(ItemIndex) = True Then
  84. Dim Item As ListItem(Of Guid) = lstCategories.Items(ItemIndex)
  85. CheckedItems.Add(Item.Key)
  86. End If
  87. Next
  88. ' Clear the list
  89. lstCategories.Items.Clear()
  90. ' Add the categories to the list
  91. If Database IsNot Nothing Then
  92. For Each Category As KeyValuePair(Of Guid, String) In Database.Categories.GetAllCategories
  93. Dim Item As New ListItem(Of Guid)(Category.Value, Category.Key)
  94. ' Select the category if it were previously selected
  95. Dim ItemIndex As Integer = lstCategories.Items.Add(Item)
  96. If CheckedItems.Contains(Category.Key) Then lstCategories.SetItemChecked(ItemIndex, True)
  97. Next
  98. End If
  99. End If
  100. ' Update available items
  101. UpdateItemList()
  102. End Sub
  103. Friend Sub UpdateItemCountLabel()
  104. If optSongs.Checked Then
  105. lblItemCount.Text = "Songs Found: (" + lstItems.Items.Count.ToString + ")"
  106. Else
  107. lblItemCount.Text = "Bible Chapters Found: (" + lstItems.Items.Count.ToString + ")"
  108. End If
  109. End Sub
  110. Private Sub UpdateControlVisibility()
  111. ' Item list
  112. btnAddSong.Enabled = optSongs.Checked
  113. btnDeleteSong.Enabled = optSongs.Checked AndAlso lstItems.SelectedIndex > -1
  114. btnAddToPlaylist.Enabled = lstItems.SelectedIndex > -1
  115. EditSongToolStripMenuItem1.Enabled = optSongs.Checked AndAlso lstItems.SelectedIndex > -1
  116. DeleteSongToolStripMenuItem.Enabled = optSongs.Checked AndAlso lstItems.SelectedIndex > -1
  117. AddToPlaylistToolStripMenuItem.Enabled = lstItems.SelectedIndex > -1
  118. ActivateToolStripMenuItem.Enabled = lstItems.SelectedIndex > -1
  119. ' Playlist
  120. LoadPlayListToolStripMenuItem.Enabled = True
  121. SavePlayListToolStripMenuItem.Enabled = True
  122. btnRemoveFromPlaylist.Enabled = lstPlaylist.SelectedIndex > -1
  123. btnSetImage.Enabled = lstPlaylist.SelectedIndex > -1
  124. btnDisplaySong.Enabled = lstPlaylist.SelectedIndex > -1
  125. btnConfigureSong.Enabled = lstPlaylist.SelectedIndex > -1
  126. RemoveToolStripMenuItem.Enabled = lstPlaylist.SelectedIndex > -1
  127. ChangeBacToolStripMenuItem.Enabled = lstPlaylist.SelectedIndex > -1
  128. ConfigurePlaylistItemToolStripMenuItem.Enabled = lstPlaylist.SelectedIndex > -1
  129. DisplayItemNowToolStripMenuItem.Enabled = lstPlaylist.SelectedIndex > -1
  130. LoadListToolStripMenuItem.Enabled = True
  131. SaveListToolStripMenuItem.Enabled = True
  132. AddTitleToolStripMenuItem.Enabled = True
  133. btnMoveUp.Enabled = lstPlaylist.SelectedIndex > -1 AndAlso lstPlaylist.SelectedIndex > 0
  134. btnMoveDown.Enabled = lstPlaylist.SelectedIndex > -1 AndAlso lstPlaylist.SelectedIndex < lstPlaylist.ItemCount - 1
  135. ' Projector preview
  136. btnGoToFull.Enabled = FActiveItem IsNot Nothing AndAlso ToggleFullScreenViewToolStripMenuItem.Checked
  137. btnGoToBackground.Enabled = FActiveItem IsNot Nothing AndAlso ToggleFullScreenViewToolStripMenuItem.Checked
  138. btnGoToBlack.Enabled = FActiveItem IsNot Nothing AndAlso ToggleFullScreenViewToolStripMenuItem.Checked
  139. GoToFullDisplayToolStripMenuItem.Enabled = btnGoToFull.Enabled
  140. GoToBackgroundOnlyToolStripMenuItem.Enabled = btnGoToBackground.Enabled
  141. GoToBlackToolStripMenuItem.Enabled = btnGoToBlack.Enabled
  142. ' Active song
  143. btnEditSong.Enabled = FActiveItem IsNot Nothing
  144. btnNext.Enabled = FActiveItem IsNot Nothing
  145. btnPrevious.Enabled = FActiveItem IsNot Nothing
  146. btnSetAnnouncement.Enabled = True
  147. EditSongToolStripMenuItem.Enabled = FActiveItem IsNot Nothing
  148. EditVerseToolStripMenuItem.Enabled = FActivePart <> -1
  149. AddVerseToolStripMenuItem.Enabled = FActiveItem IsNot Nothing
  150. End Sub
  151. #End Region
  152. #Region " Helper Methods "
  153. Private Function FindProjector(ByVal target As Control) As Projector
  154. For Each Projector As Projector In FProjections
  155. If Projector.Target Is target Then Return Projector
  156. Next
  157. Return Nothing
  158. End Function
  159. Private Sub ToggleProjector(ByVal target As Control)
  160. ' Find the projector
  161. Dim Projector As Projector = FindProjector(target)
  162. ' Determine which of the projectors is being toggled
  163. If target Is FProjectionForm Then
  164. ' The live view
  165. If Projector.Projecting Then FProjectionForm.Hide() Else FProjectionForm.Show()
  166. Projector.Projecting = Not Projector.Projecting
  167. ToggleFullScreenViewToolStripMenuItem.Checked = FProjectionForm.Visible
  168. Else
  169. ' The preview view
  170. Projector.Projecting = Not Projector.Projecting
  171. TogglePreviewToolStripMenuItem.Checked = Projector.Projecting
  172. End If
  173. ' Clear the projection area if no longer projecting
  174. If Not Projector.Projecting Then Projector.Target.CreateGraphics.Clear(Color.Black)
  175. End Sub
  176. Private Sub UpdateProjectorStyle()
  177. If FActiveStyle IsNot Nothing Then
  178. For Each Projector As Projector In FProjections
  179. ' Update background style
  180. Select Case FActiveStyle.BackgroundType
  181. Case Style.EBackgroundType.Image : Projector.SetBackgroundImage(FActiveStyle.DefaultBackgroundImage)
  182. Case Style.EBackgroundType.SolidColour : Projector.SetBackgroundColour(FActiveStyle.DefaultBackgroundColour)
  183. End Select
  184. ' Update projectlets
  185. For Each Projectlet As Projectlet In Projector.Projectlets
  186. Dim SubStyle As ProjectletStyle = FActiveStyle(Projectlet.Category)
  187. Projector.SetProjectletStyle(Projectlet, SubStyle)
  188. Next
  189. Next
  190. End If
  191. End Sub
  192. Private Sub CloseDatabase()
  193. ' If a song is active, record it as hidden
  194. If FActiveItem IsNot Nothing AndAlso TypeOf FActiveItem Is SongItem Then
  195. Database.Logging.Add(LogEntry.ItemType.Song, DirectCast(FActiveItem, SongItem).SongID, Now, LogEntry.Action.Hidden)
  196. End If
  197. ' Unload plugins
  198. If Plugins.Instance IsNot Nothing Then Plugins.Instance.UnloadAllPlugins()
  199. ' Clear variables
  200. DatabaseLocation = ""
  201. Database = Nothing
  202. FActiveItem = Nothing
  203. FActiveStyle = Nothing
  204. FActivePart = -1
  205. txtSearch.Text = ""
  206. ' Clear list boxes
  207. lstCategories.Items.Clear()
  208. lstPlaylist.Clear()
  209. lstVerses.Items.Clear()
  210. UpdateAll()
  211. End Sub
  212. Private Sub ChangeActivePart(ByVal newPart As Integer)
  213. FActivePart = newPart
  214. Dim NextSelectedIndex As Integer = FActivePart + 1
  215. If NextSelectedIndex >= lstVerses.Items.Count Then NextSelectedIndex = 0
  216. ' Only change the active part if there is at least one verse
  217. If NextSelectedIndex < lstVerses.Items.Count Then
  218. ' Change the text on the projector
  219. lstVerses.SelectedIndex = NextSelectedIndex
  220. lstVerses.ActiveIndex = FActivePart
  221. For Each Projector As Projector In FProjections
  222. ' Determine what text to display
  223. Dim Projectlet As Projectlet = Projector.ProjectletByName("Verse")
  224. Dim VerseText As String = Projectlet.PopulateTags(FActiveItem, FActivePart)
  225. ' Actually set the text
  226. Projector.SetTextWithFade(Projectlet, _
  227. FActivePart, _
  228. FActiveItem.Parts.Count, _
  229. VerseText, _
  230. FActiveStyle.VerseFadeDuration, _
  231. False)
  232. Next
  233. End If
  234. End Sub
  235. #End Region
  236. Private Sub HandleCompletedModeChange(ByVal newMode As Projector.EMode)
  237. If newMode = Projector.EMode.Fading Then Exit Sub
  238. ' Set the colour of the three buttons appropriately
  239. btnGoToBlack.ForeColor = SystemColors.ControlText
  240. btnGoToBackground.ForeColor = SystemColors.ControlText
  241. btnGoToFull.ForeColor = SystemColors.ControlText
  242. Select Case newMode
  243. Case Projector.EMode.Black : btnGoToBlack.ForeColor = SystemColors.MenuHighlight
  244. Case Projector.EMode.DisplayBackground : btnGoToBackground.ForeColor = SystemColors.MenuHighlight
  245. Case Projector.EMode.DisplayAll : btnGoToFull.ForeColor = SystemColors.MenuHighlight
  246. End Select
  247. End Sub
  248. End Class