#!/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()