6
from ConfigParser import SafeConfigParser
8
import InstrumentViewer
10
import operator #for sorting instrument list
12
#=========================================================================
14
class AddInstrumentDialog:
15
""" This class handles all of the processing associated with the
16
Add Instrument dialog.
18
#_____________________________________________________________________
20
def __init__(self, project, parent):
22
self.project = project
24
self.res = gtk.glade.XML(parent.GLADE_PATH, "AddInstrumentDialog")
27
"on_OK_clicked" : self.OnOK,
28
"on_Cancel_clicked" : self.OnCancel,
31
self.res.signal_autoconnect(self.signals)
33
self.dlg = self.res.get_widget("AddInstrumentDialog")
34
self.tree = self.res.get_widget("Instruments")
36
self.okbutton = self.res.get_widget("okButton")
37
self.okbutton.set_sensitive(False)
39
self.tree.connect("item-activated", self.OnSelected)
40
self.tree.connect("selection-changed", self.OnSelectionChanged)
42
self.model = gtk.ListStore(str, gtk.gdk.Pixbuf, str, str, str)
44
for i in getCachedInstruments():
47
self.tree.set_model(self.model)
49
self.tree.set_text_column(0)
50
self.tree.set_pixbuf_column(1)
51
self.tree.set_orientation(gtk.ORIENTATION_VERTICAL)
52
self.tree.set_selection_mode(gtk.SELECTION_MULTIPLE)
53
self.tree.set_columns(3)
54
self.tree.set_item_width(90)
55
self.tree.set_size_request(72, -1)
56
self.dlg.resize(350, 300)
58
self.dlg.set_icon(self.parent.icon)
59
self.dlg.set_transient_for(self.parent.window)
61
#_____________________________________________________________________
63
def OnSelected(self, iconview, path):
66
#_____________________________________________________________________
68
def OnOK(self, button=None):
69
sel = self.tree.get_selected_items()
71
currentItem = getCachedInstruments()[i[0]]
74
for i in currentItem[3].split(","):
75
filenameList.append(i.strip())
77
if len(filenameList) == 1 and len(filenameList[0]) == 0:
79
#this instrument has no imports, so add this instrument
80
self.project.AddInstrument(currentItem[0], currentItem[1], currentItem[4])
82
for k in instrumentPropertyList:
83
if len(filenameList) == 0:
85
if k[2] in filenameList:
86
self.project.AddInstrument(k[0], k[1], k[4])
87
filenameList.remove(k[2])
89
self.parent.UpdateDisplay()
90
self.parent.undo.set_sensitive(True)
93
#_____________________________________________________________________
95
def OnCancel(self, button):
98
#_____________________________________________________________________
100
def OnSelectionChanged(self, button):
101
sel = self.tree.get_selected_items()
104
self.okbutton.set_sensitive(False)
106
self.okbutton.set_sensitive(True)
108
#_____________________________________________________________________
110
#=========================================================================
111
#static list of all the instrument files (to prevent having to reimport files)
112
instrumentPropertyList = []
114
def _cacheInstruments():
115
global instrumentPropertyList
117
if len(instrumentPropertyList) > 0:
120
basepath = os.path.dirname(os.path.abspath(__file__))
121
instrpath = os.path.join(basepath, "Instruments")
123
for path,dirs,files in os.walk(instrpath):
125
if f[-6:] == ".instr":
126
config = SafeConfigParser()
127
config.read(os.path.join(instrpath, f))
129
if config.has_option('core', 'name') and config.has_option('core', 'icon'):
130
name = config.get('core', 'name')
131
icon = config.get('core', 'icon')
135
pixbufPath = os.path.join(instrpath, "images", icon)
136
pixbuf = gtk.gdk.pixbuf_new_from_file(pixbufPath)
138
if config.has_option('core', 'import'):
139
importfiles = config.get('core', 'import')
143
instrumentPropertyList.append((name, pixbuf, f, importfiles, pixbufPath))
145
#sort the instruments alphabetically
146
#using the name (at index 0)
147
instrumentPropertyList.sort(key=operator.itemgetter(0))
149
def getCachedInstruments():
150
global instrumentPropertyList
151
if len(instrumentPropertyList) == 0:
153
return instrumentPropertyList
155
gobject.idle_add(_cacheInstruments)