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