jameyc / tabmanager

A simple wxPython app for sorting and searching though guitar tablature and other musical notation.

This URL has Read+Write access

tabmanager / wxui_custom.py
100755 392 lines (353 sloc) 14.361 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python
 
## This file is part of Tab Manager
## Copyright: 2007 Jamey Campbell
##
## Tab Manager is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## Tab Manager is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see http://www.gnu.org/licenses/ .
 
import wx
import wxui
import database
import songlist
import prefs
 
############
##Main Frame
############
def tree_menu(self, event) :
    songid = self.tree_songs_list.GetPyData(event.GetItem())
    #if there's no songid, the user has right clicked an artist
    if songid :
        self.PopupMenu(TreePopupMenu(self, songid))
wxui.MainFrame.tree_menu = tree_menu
 
def adjust_view(self, event):
    selection = self.combo_view.GetValue()
    if (selection == "Artists"):
        songlist.mainlist.currentview = "artist"
        self.refresh_list()
    if (selection == "Genre"):
        songlist.mainlist.currentview = "genre"
        self.refresh_list()
    if (selection == "Favorites"):
        songlist.mainlist.currentview = "favorite"
        self.refresh_list()
    if (selection == "Study"):
        songlist.mainlist.currentview = "study"
        self.refresh_list()
    #if (selection == "Custom..."):
wxui.MainFrame.adjust_view = adjust_view
 
def refresh_list(self):
    songlist.mainlist.get_full_list()
    curartist = ""
    self.tree_songs_list.DeleteAllItems()
    root = self.tree_songs_list.AddRoot("MySongs")
    if (songlist.mainlist.currentview == "genre") :
        curgenre = None
        for song in songlist.mainlist.songs:
            if (song['genre'] != curgenre) :
                if (song['genre'] == "") :
                    thisgenre = self.tree_songs_list.AppendItem(root,"Unclassified")
                    curgenre = song["genre"]
                else:
                    thisgenre = self.tree_songs_list.AppendItem(root,song["genre"])
                    curgenre = song["genre"]
                ##
            if (song['artist'] != curartist) :
                thisbranch = self.tree_songs_list.AppendItem(thisgenre,song["artist"])
                thisitem = self.tree_songs_list.AppendItem(thisbranch, song["title"])
                self.tree_songs_list.SetPyData(thisitem, str(song["id"]))
                curartist = song["artist"]
            else :
                thisitem = self.tree_songs_list.AppendItem(thisbranch, song["title"])
                self.tree_songs_list.SetPyData(thisitem, str(song["id"]))
    else:
        for song in songlist.mainlist.songs:
            if (song['artist'] != curartist) :
                thisbranch = self.tree_songs_list.AppendItem(root,song["artist"])
                thisitem = self.tree_songs_list.AppendItem(thisbranch, song["title"])
                self.tree_songs_list.SetPyData(thisitem, str(song["id"]))
                curartist = song["artist"]
            else :
                thisitem = self.tree_songs_list.AppendItem(thisbranch, song["title"])
                self.tree_songs_list.SetPyData(thisitem, str(song["id"]))
            if (song['favorite'] == True) :
                self.tree_songs_list.SetItemTextColour(thisitem, wx.NamedColor('blue'))
            if (song['study'] == True) :
                self.tree_songs_list.SetItemBold(thisitem)
wxui.MainFrame.refresh_list = refresh_list
 
def toggle_favorite(self, event):
    songlist.currentsong.set_flags("favorite")
wxui.MainFrame.toggle_favorite = toggle_favorite
 
def toggle_study(self, event):
    songlist.currentsong.set_flags("study")
wxui.MainFrame.toggle_study = toggle_study
##
 
#
def list_expand(self, event):
    #known bug in wx libraries throws an error here, ignore for now, fix later
    self.tree_songs_list.ExpandAll()
    event.Skip()
wxui.MainFrame.list_expand = list_expand
 
def list_collapse(self, event):
    #known bug in wx libraries throws an error here, ignore for now, fix later
    self.tree_songs_list.CollapseAll()
    event.Skip()
wxui.MainFrame.list_collapse = list_collapse
##
#user picked quit menu, close the app
def do_quit(self, event):
    self.Close()
    event.Skip()
wxui.MainFrame.do_quit = do_quit
 
##
#user picked about menu item, show about box.
def do_about(self, event):
    aboutbox = wxui.AboutBox(None, -1, "")
    aboutbox.Show()
    event.Skip()
wxui.MainFrame.do_about = do_about
 
##
#User picked preferences menu item, bring up the preferences
#dialog box and fill it with values from the prefs module
def do_prefs(self, event):
    prefsframe = wxui.PrefsFrame(None, -1, "")
    prefsframe.prefs_use_mysql.SetValue(prefs.prefs.use_mysql)
    prefsframe.prefs_mysql_username.SetValue(prefs.prefs.sql_user)
    prefsframe.prefs_mysql_password.SetValue(prefs.prefs.sql_password)
    prefsframe.prefs_mysql_address.SetValue(prefs.prefs.sql_host)
    prefsframe.prefs_mysql_port.SetValue(str(prefs.prefs.sql_port))
    prefsframe.prefs_mysql_database.SetValue(prefs.prefs.sql_database)
    prefsframe.Show()
    event.Skip()
wxui.MainFrame.do_prefs = do_prefs
 
##
#Sets the contents of the text area to that of the currently selected song
# - usually called when the user double clicks a song.
def set_current_doc(self, event):
    currentselection = self.tree_songs_list.GetSelection()
    songid = self.tree_songs_list.GetPyData(currentselection)
    #Let's make sure we're getting a real song instead of clicking an artist
    # we can put an else in here later if we want to do something on artist clicks
    if (songid):
        songlist.currentsong.get_new_song(songid)
        self.text_contents.SetValue(songlist.currentsong.content)
        self.main_statusbar.SetStatusText(
            "Song: %s - %s" % (
            songlist.currentsong.artist,
            songlist.currentsong.title)
            ,0)
        self.main_statusbar.SetStatusText(
            "Genre: %s" % (str(songlist.currentsong.genre))
            ,1)
        self.main_statusbar.SetStatusText(
            "Key: %s" % (songlist.currentsong.key)
            ,2)
        if (songlist.currentsong.source == "None"):
            self.url_source.SetLabel("")
            self.url_source.SetURL("")
        else:
            self.url_source.SetLabel(str(songlist.currentsong.source))
            self.url_source.SetURL(str(songlist.currentsong.source))
        self.url_source.Refresh()
        if (songlist.currentsong.tuning != ""):
            self.label_tuning.SetLabel(songlist.currentsong.tuning)
        else:
            self.label_tuning.SetLabel("Standard Tuning")
        if (songlist.currentsong.favorite == 1):
            self.checkbox_favorite.SetValue(True)
        else:
            self.checkbox_favorite.SetValue(False)
        if (songlist.currentsong.study == 1):
            self.checkbox_study.SetValue(True)
        else:
            self.checkbox_study.SetValue(False)
            
    event.Skip()
wxui.MainFrame.set_current_doc = set_current_doc
 
##
#helper function to pass the menu edit commands to do_edit,
#determining if the user is editing existing or not
def do_menu_edit(self, event):
    #this is tied to the menu event id, so be careful of changes to the event
    if (event.GetId() == 3):
        #user is editing an existing doc
        do_edit(self, songlist.currentsong.id)
    else:
        do_edit(self)
    event.Skip()
wxui.MainFrame.do_menu_edit = do_menu_edit
 
##
 
def toggle_sidebar(self, event) :
    if (self.splitter.IsSplit() == True) :
        self.splitter.Unsplit(self.sidebar)
    else :
        self.sidebar.Show()
        self.splitter.SplitVertically(self.sidebar, self.main_view, 185)
        #self.splitter.UpdateSize()
    event.Skip()
wxui.MainFrame.toggle_sidebar = toggle_sidebar
 
##
#The user is editing a song. If we've recieved a songid, then we know
#it's an existing song. If not, the user wants to make a new one.
def do_edit(self, songid=None):
    self.songid = songid
    editframe = wxui.EditFrame(self, -1, "")
    if (self.songid):
        #User is editing an existing doc, do prep work
        editframe.editingExisting = True
        #note these are all passed their arguments though str() because
        #if the field is None, it will error. This gives us a blank string
        #object instead.
        editframe.text_artist.SetValue(songlist.currentsong.artist)
        editframe.text_title.SetValue(songlist.currentsong.title)
        editframe.combo_genre.SetValue(songlist.currentsong.genre)
        editframe.text_tuning.SetValue(str(songlist.currentsong.tuning))
        editframe.text_version.SetValue(str(songlist.currentsong.version))
        editframe.text_source.SetValue(str(songlist.currentsong.source))
        editframe.text_content.SetValue(songlist.currentsong.content)
        if(songlist.currentsong.study == 1):
            editframe.check_study.SetValue(True)
        else:
            editframe.check_study.SetValue(False)
 
        if(songlist.currentsong.favorite == 1):
            editframe.check_favorite.SetValue(True)
        else:
            editframe.check_favorite.SetValue(False)
 
        if(songlist.currentsong.key):
            editframe.combo_key.SetValue(songlist.currentsong.key)
    else:
        editframe.editingExisting = False
        #create a new doc
    editframe.Show()
wxui.MainFrame.do_edit = do_edit
 
##########
##Edit box
##########
 
#user hit help from the edit page, show them some useful info
def edit_help(self, event):
    print"You need to do something about the editbox's edit_help"
    event.Skip()
wxui.EditFrame.edit_help = edit_help
 
#user hit cancel in the edit page, close it
def edit_cancel(self, event):
    self.Close()
    event.Skip()
wxui.EditFrame.edit_cancel = edit_cancel
 
#user hit ok in the edit page, time to save the song or update
def edit_ok(self, event):
    songlist.worksong.artist = self.text_artist.GetValue()
    songlist.worksong.title = self.text_title.GetValue()
    songlist.worksong.tuning = self.text_tuning.GetValue()
    songlist.worksong.version = self.text_version.GetValue()
    songlist.worksong.source = self.text_source.GetValue()
    songlist.worksong.content = self.text_content.GetValue()
    songlist.worksong.key = self.combo_key.GetValue()
    songlist.worksong.genre = self.combo_genre.GetValue()
    #get the value of the checkboxes for favorite/study
    if self.check_favorite.GetValue() :
        songlist.worksong.favorite = 1
    else:
        songlist.worksong.favorite = 0
    if self.check_study.GetValue() :
        songlist.worksong.study = 1
    else:
        songlist.worksong.study = 0
 
    #now see if we're editing an existing song or not
    mainwindow = self.GetParent()
    if self.editingExisting :
        songlist.worksong.save_song(songlist.currentsong.id)
        #reload the text display
        songlist.currentsong.get_new_song(songlist.currentsong.id)
        mainwindow.text_contents.SetValue(songlist.currentsong.content)
    else :
        songlist.worksong.save_song()
    #refresh the tree view (should we reselect the current song?)
    mainwindow.refresh_list()
    self.Close()
    event.Skip()
wxui.EditFrame.edit_ok = edit_ok
 
###########
##Prefs box
###########
def do_prefs_help(self, event):
    print "Set up a help popup here!"
    event.Skip()
wxui.PrefsFrame.do_prefs_help = do_prefs_help
 
def do_prefs_cancel(self, event):
    self.Close()
    event.Skip()
wxui.PrefsFrame.do_prefs_cancel = do_prefs_cancel
 
#user wants to save new preferences, pull the values from the dialog
#box and throw them into the prefs variables. Then save the prefs.ini
def do_prefs_ok(self, event):
    prefs.prefs.use_mysql = self.prefs_use_mysql.GetValue()
    prefs.prefs.sql_user = self.prefs_mysql_username.GetValue()
    prefs.prefs.sql_password = self.prefs_mysql_password.GetValue()
    prefs.prefs.sql_host = self.prefs_mysql_address.GetValue()
    prefs.prefs.sql_port = self.prefs_mysql_port.GetValue()
    prefs.prefs.sql_database = self.prefs_mysql_database.GetValue()
    prefs.saveDbase("prefs.ini", prefs.prefs)
    self.Close()
    event.Skip()
wxui.PrefsFrame.do_prefs_ok = do_prefs_ok
 
###########
##About box
###########
def about_ok(self, event):
    self.Close()
    event.Skip()
wxui.AboutBox.about_ok = about_ok
 
###############
##Delete frame
###############
def confirm_delete(self, event):
    sql_statement = """DELETE FROM songs WHERE id=?"""
    sql_data = (self.songid,)
    sql = (sql_statement, sql_data)
    songlist.mainlist.connection.insert(sql)
    mainwindow = self.GetParent()
    mainwindow.refresh_list()
    self.Close()
    event.Skip()
wxui.DeleteFrame.confirm_delete = confirm_delete
 
###########
##other
###########
 
class ContentFontDialog(wx.FontDialog):
    def __init__(self):
        data = wx.FontData()
        data.SetInitialFont(somecanvasfont)
        data.SetColor(somecanvascolor)
    
 
#a right click popup menu for the treeview.
class TreePopupMenu(wx.Menu):
    def __init__(self, parent, songid):
        wx.Menu.__init__(self)
        self.songid = songid
        self.parent = parent
        item = wx.MenuItem(self, wx.NewId(), "Refresh")
        self.AppendItem(item)
        self.Bind(wx.EVT_MENU, self.OnItem1, item)
 
        item = wx.MenuItem(self, wx.NewId(),"Edit")
        self.AppendItem(item)
        self.Bind(wx.EVT_MENU, self.OnPopupEdit, item)
        
        item = wx.MenuItem(self, wx.NewId(),"Delete")
        self.AppendItem(item)
        self.Bind(wx.EVT_MENU, self.OnPopupDelete, item)
         
    def OnItem1(self, event):
        songid = self.GetPyData(event.GetItem)
        print "Refresh selected has songid of %s"%self.songid
 
    def OnPopupEdit(self, event):
        songlist.currentsong.get_new_song(self.songid)
        self.parent.text_contents.SetValue(songlist.currentsong.content)
        self.parent.do_edit(self.songid)
 
    def OnPopupDelete(self, event):
        confirmframe = wxui.DeleteFrame(self.parent, -1, "")
        confirmframe.songid = self.songid
        confirmframe.Show()