~ubuntu-branches/debian/sid/jokosher/sid

« back to all changes in this revision

Viewing changes to AddInstrumentDialog.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-07-19 18:49:10 UTC
  • Revision ID: james.westby@ubuntu.com-20060719184910-0av4ghk8vsjvcij3
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import gtk
 
3
import gtk.glade
 
4
import gobject
 
5
import os
 
6
from ConfigParser import SafeConfigParser
 
7
import Globals
 
8
import InstrumentViewer
 
9
import Project
 
10
import operator #for sorting instrument list
 
11
 
 
12
#=========================================================================
 
13
 
 
14
class AddInstrumentDialog:
 
15
        """ This class handles all of the processing associated with the
 
16
                Add Instrument dialog.
 
17
        """     
 
18
        #_____________________________________________________________________
 
19
 
 
20
        def __init__(self, project, parent):
 
21
                self.parent = parent
 
22
                self.project = project
 
23
                
 
24
                self.res = gtk.glade.XML(parent.GLADE_PATH, "AddInstrumentDialog")
 
25
 
 
26
                self.signals = {
 
27
                        "on_OK_clicked" : self.OnOK,
 
28
                        "on_Cancel_clicked" : self.OnCancel,
 
29
                }
 
30
                
 
31
                self.res.signal_autoconnect(self.signals)
 
32
                
 
33
                self.dlg = self.res.get_widget("AddInstrumentDialog")
 
34
                self.tree = self.res.get_widget("Instruments")
 
35
                
 
36
                self.okbutton = self.res.get_widget("okButton")
 
37
                self.okbutton.set_sensitive(False)
 
38
 
 
39
                self.tree.connect("item-activated", self.OnSelected)
 
40
                self.tree.connect("selection-changed", self.OnSelectionChanged)
 
41
 
 
42
                self.model = gtk.ListStore(str, gtk.gdk.Pixbuf, str, str, str)
 
43
                        
 
44
                for i in getCachedInstruments():
 
45
                        self.model.append(i)
 
46
                
 
47
                self.tree.set_model(self.model)
 
48
                        
 
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)
 
57
                
 
58
                self.dlg.set_icon(self.parent.icon)
 
59
                self.dlg.set_transient_for(self.parent.window)
 
60
                
 
61
        #_____________________________________________________________________
 
62
        
 
63
        def OnSelected(self, iconview, path):
 
64
                self.OnOK()
 
65
 
 
66
        #_____________________________________________________________________
 
67
                        
 
68
        def OnOK(self, button=None):
 
69
                sel = self.tree.get_selected_items()
 
70
                for i in sel:
 
71
                        currentItem = getCachedInstruments()[i[0]]
 
72
                        
 
73
                        filenameList = []
 
74
                        for i in currentItem[3].split(","):
 
75
                                filenameList.append(i.strip())
 
76
                                
 
77
                        if len(filenameList) == 1 and len(filenameList[0]) == 0:
 
78
                                filenameList = []
 
79
                                #this instrument has no imports, so add this instrument
 
80
                                self.project.AddInstrument(currentItem[0], currentItem[1], currentItem[4])
 
81
                
 
82
                        for k in instrumentPropertyList:
 
83
                                if len(filenameList) == 0:
 
84
                                        break
 
85
                                if k[2] in filenameList:
 
86
                                        self.project.AddInstrument(k[0], k[1], k[4])
 
87
                                        filenameList.remove(k[2])
 
88
        
 
89
                self.parent.UpdateDisplay()
 
90
                self.parent.undo.set_sensitive(True)
 
91
                self.dlg.destroy()
 
92
                
 
93
        #_____________________________________________________________________
 
94
        
 
95
        def OnCancel(self, button):
 
96
                self.dlg.destroy()
 
97
                
 
98
        #_____________________________________________________________________
 
99
 
 
100
        def OnSelectionChanged(self, button):
 
101
                sel = self.tree.get_selected_items()
 
102
 
 
103
                if len(sel) <= 0:
 
104
                        self.okbutton.set_sensitive(False)
 
105
                else:
 
106
                        self.okbutton.set_sensitive(True)
 
107
                        
 
108
        #_____________________________________________________________________
 
109
 
 
110
#=========================================================================
 
111
#static list of all the instrument files (to prevent having to reimport files)
 
112
instrumentPropertyList = []
 
113
 
 
114
def _cacheInstruments():
 
115
        global instrumentPropertyList
 
116
 
 
117
        if len(instrumentPropertyList) > 0:
 
118
                return
 
119
                
 
120
        basepath = os.path.dirname(os.path.abspath(__file__))
 
121
        instrpath = os.path.join(basepath, "Instruments")
 
122
        
 
123
        for path,dirs,files in os.walk(instrpath):
 
124
                for f in files:
 
125
                        if f[-6:] == ".instr":
 
126
                                config = SafeConfigParser()
 
127
                                config.read(os.path.join(instrpath, f))
 
128
                                
 
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')
 
132
                                else:
 
133
                                        continue
 
134
                                
 
135
                                pixbufPath = os.path.join(instrpath, "images", icon)
 
136
                                pixbuf = gtk.gdk.pixbuf_new_from_file(pixbufPath)
 
137
        
 
138
                                if config.has_option('core', 'import'):
 
139
                                        importfiles = config.get('core', 'import')
 
140
                                else:
 
141
                                        importfiles = ""
 
142
                                
 
143
                                instrumentPropertyList.append((name, pixbuf, f, importfiles, pixbufPath))
 
144
        
 
145
        #sort the instruments alphabetically
 
146
        #using the name (at index 0)
 
147
        instrumentPropertyList.sort(key=operator.itemgetter(0))
 
148
        
 
149
def getCachedInstruments():
 
150
        global instrumentPropertyList
 
151
        if len(instrumentPropertyList) == 0:
 
152
                        _cacheInstruments()
 
153
        return instrumentPropertyList
 
154
 
 
155
gobject.idle_add(_cacheInstruments)