ocarina/trunk/window.py

300 lines
8.4 KiB
Python

import gobject
import pango
import pygtk
pygtk.require('2.0')
import gtk
import thread
from kiwi.ui.objectlist import Column, ObjectList
class Window(gtk.Window):
def __init__(self,onQuit,ops):
gtk.Window.__init__(self,gtk.WINDOW_TOPLEVEL)
print "Making window!"
self.ops = ops
self.tree = None
self.tooltip = gtk.Tooltips()
self.tagLabels = dict()
self.set_title("Ocarina")
# Call quit function when closed
self.connect("delete_event",onQuit)
self.set_icon_from_file("images/ocarina.png")
self.mainLayout = gtk.VBox(False,0)
self.add(self.mainLayout)
self.mainLayout.show()
self.makeMenuBar()
self.makeInfoPane()
self.makeList()
self.makeControls()
self.maximize()
self.show()
# Make initial info pane
def makeInfoPane(self):
self.infoFrame = gtk.Frame("Current Song:")
box = gtk.VBox(False,0)
self.infoFrame.add(box)
attr = pango.AttrList()
attr.insert(pango.AttrSize(12000, 0, -1))
self.tagLabels["title"] = self.makeLabel("Title: ?",box,attr)
self.tagLabels["artist"] = self.makeLabel("Artist: ?",box,attr)
self.tagLabels["album"] = self.makeLabel("Album: ?",box,attr)
box.show()
self.infoFrame.show()
#self.mainLayout.add(self.infoFrame)
self.mainLayout.pack_start(self.infoFrame,False,False,0)
# Set up a new label, add to container
# Goes into a HBox so that it is left justified
def makeLabel(self,text,container,attr):
box = gtk.HBox(False,0)
label = gtk.Label(text)
s = label.get_style().copy()
self.tooltip.set_tip(label,text,tip_private=None)
#label.set_max_width_chars(35)
label.set_attributes(attr)
label.show()
box.pack_start(label,False,False,0)
box.show()
container.pack_start(box,False,False,0)
return label
# Change label info
def changeInfo(self,tags):
keys = tags.keys()
labels = self.tagLabels.keys()
for key in keys:
if (key in labels) == True:
str = key.title()+": "+tags[key]
self.tagLabels[key].set_label(str)
self.tooltip.set_tip(self.tagLabels[key],str,tip_private=None)
self.tagLabels[key].show()
def changeFrameTitle(self,label):
self.infoFrame.set_label(label+"Current Track:")
# Reset label info
def resetInfo(self):
labels = self.tagLabels.keys()
for key in labels:
self.tagLabels[key].set_label(key.title()+": ?")
self.tooltip.set_tip(self.tagLabels[key],key.title()+": ?")
self.tagLabels[key].show()
def makeList(self):
title = Column('title')
title.expand = True
title.searchable = True
artist = Column('artist')
artist.expand = True
artist.searchable = True
album = Column('album')
album.expand = True
album.searchable = True
count = Column('count')
count.expand = True
#count.searchable = True
length = Column('length')
length.expand = True
#length.searchable = True
self.list = ObjectList([title,artist,album,count,length])
for num in self.ops.plist.list:
#print self.ops.plist.translate(num)
self.list.append(self.ops.plist.translate(num))
#self.list.append(num)
self.list.sort_by_attribute('artist')
self.list.connect("row-activated",self.ops.plist.selectSong,"clicked",self.list)
self.list.show()
self.mainLayout.add(self.list)
'''
# Make scrolled window, add to window
scroll = gtk.ScrolledWindow()
scroll.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
#self.mainLayout.pack_start(scroll,False,False,0)
self.mainLayout.add(scroll)
# Make and populate list
self.list = gtk.ListStore(int,str,str,str,int)
for num in self.ops.plist.list:
self.list.append(self.ops.plist.translate(num))
# Make treeview from list
if self.tree:
self.tree = None
self.tree = gtk.TreeView(self.list)
cell = gtk.CellRendererText()
cols = ["Id","Title","Artist","Album","Count"]
for i in range(len(cols)):
col = gtk.TreeViewColumn(cols[i],cell)
col.add_attribute(cell,'text',i)
col.set_resizable(True)
col.set_sort_column_id(i)
self.tree.append_column(col)
self.tree.connect("row-activated",self.ops.plist.selectSong,"clicked",self.list)
self.tree.connect("button_press_event",self.ops.click,"clicked",self.list,self.tree.get_selection())
self.tree.set_grid_lines(True)
scroll.add(self.tree)
self.tree.columns_autosize()
self.tree.show()
scroll.show()
#self.gotoCurSong()
'''
def gotoCurSong(self):
#return
if len(self.list) == 0:
return
#print self.list[0][0]
#print len(self.list)
selrow = 0
row = 0
for i in range(len(self.list)):
#print i
if self.list[i].id == self.ops.plist.curSong:
if i > 10:
selrow = i - 10
row = i
break
self.list.select(self.list[row],True)
#selrow = self.ops.plist.curSong - 10
#if selrow < 0:
# selrow = 0
#print selrow,row
#self.tree.scroll_to_cell(selrow,None,True,0,0)
#treesel = self.tree.get_selection()
#treesel.select_path(row)
# Use to make play/pause/next/etc buttons
def makeControls(self):
#controls = gtk.VBox(False,0)
row = gtk.HBox(False,0)
topRow = gtk.HBox(False,0)
# Make top row buttons
self.makeButton("play","images/play.png",None,self.ops.play,topRow)
self.makeButton("pause","images/pause.png",None,self.ops.pause,topRow)
self.makeButton("stop","images/stop.png",None,self.ops.stop,topRow)
self.makeButton("next","images/next.png",None,self.ops.next,topRow)
self.makeButton("info",None,"Info",self.ops.info,topRow)
#self.makeButton("plist",None,"Plist",self.ops.plist.showWindow,topRow)
test = gtk.VBox(False,0)
self.makeCheck("Random",self.ops.random,self.ops.plist.random,True,topRow)
topRow.show()
row.pack_end(topRow,False,False,0)
row.show()
#self.mainLayout.pack_start(topRow,False,False,0)
self.mainLayout.pack_start(row,False,False,0)
# Make progress bar, add below top row
pbar = gtk.ProgressBar()
pbar.set_fraction(0)
gobject.timeout_add(1000,self.ops.markProgress,pbar,"progress")
pbar.show()
self.mainLayout.pack_start(pbar,False,False,0)
# Show completed controls
#controls.show()
#self.add(controls)
# Make buttons and add to container
# Path is to an image
# Text is label text
# Func is callback function
def makeButton(self,name,path,text,func,container):
button = gtk.Button()
box = gtk.HBox(False,0)
box.set_border_width(0)
if path != None:
image = gtk.Image()
image.set_from_file(path)
image.show()
box.pack_start(image,False,False,0)
if text != None:
label = gtk.Label(text)
label.show()
box.pack_start(label,False,False,0)
box.show()
button.add(box)
button.connect("clicked",func,name)
button.show()
container.pack_start(button,False,False,0)
return button
# Make a checkbox and add to container
# Active is variable to check, status is value it has to mark as active
def makeCheck(self,name,func,active,status,container):
check = gtk.CheckButton(label=name)
if active == status:
check.set_active(1)
check.connect("toggled",func,name)
check.show()
container.pack_start(check,False,False,0)
return check
def makeMenuBar(self):
# Make menu bar
bar = gtk.MenuBar()
# This is the dropdown selections
# Make a new library option
library = gtk.MenuItem("Library")
lib = gtk.Menu()
newLib = gtk.MenuItem(label="New Library")
newLib.connect_object("activate",self.selectDir,self.ops.scanLib)
newLib.show()
lib.append(newLib)
# This shows in the toolbar
library.set_submenu(lib)
library.show()
bar.append(library)
# Make playback option
playback = gtk.MenuItem("Playback")
pback = gtk.Menu()
# Pause after current track
pafter = gtk.MenuItem(label="Pause After Current Track")
pafter.connect("activate",self.ops.afterTrack,"pafter",self.changeFrameTitle)
pafter.show()
pback.append(pafter)
# Quit after current track
qafter = gtk.MenuItem(label="Quit After Current Track")
qafter.connect("activate",self.ops.afterTrack,"qafter",self.changeFrameTitle)
qafter.show()
pback.append(qafter)
playback.set_submenu(pback)
playback.show()
bar.append(playback)
# Add to main layout
bar.show()
self.mainLayout.pack_start(bar,False,False,0)
def selectDir(self,func):
dirsel = gtk.FileChooserDialog(None,action=gtk.FILE_CHOOSER_ACTION_OPEN,buttons =(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
dirsel.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
response = dirsel.run()
file = ""
#dirsel = None
if response == gtk.RESPONSE_OK:
#print dirsel.get_filename(),'selected'
#dirsel.hide()
file = dirsel.get_filename()
#dirsel.hide()
#dirsel = None
#func(file)
#else:
dirsel.hide()
dirsel.destroy()
dirsel = None
thread.start_new_thread(func,("name",file))