filechooser.py
1 # Touchy is Copyright (c) 2009 Chris Radek <chris@timeguy.com> 2 # 3 # Touchy is free software: you can redistribute it and/or modify 4 # it under the terms of the GNU General Public License as published by 5 # the Free Software Foundation, either version 2 of the License, or 6 # (at your option) any later version. 7 # 8 # Touchy is distributed in the hope that it will be useful, 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 # GNU General Public License for more details. 12 13 import os 14 15 class filechooser: 16 def __init__(self, gtk, emc, labels, eventboxes, listing): 17 self.labels = labels 18 self.eventboxes = eventboxes 19 self.numlabels = len(labels) 20 self.listing = listing 21 self.gtk = gtk 22 self.emc = emc 23 self.emccommand = emc.command() 24 self.fileoffset = 0 25 self.dir = os.path.join(os.getenv('HOME'), 'linuxcnc', 'nc_files') 26 self.reload(0) 27 28 def populate(self): 29 files = self.files[self.fileoffset:] 30 for i in range(self.numlabels): 31 l = self.labels[i] 32 e = self.eventboxes[i] 33 if i < len(files): 34 l.set_text(files[i]) 35 else: 36 l.set_text('') 37 if self.selected == self.fileoffset + i: 38 e.modify_bg(self.gtk.STATE_NORMAL, self.gtk.gdk.color_parse('#fff')) 39 else: 40 e.modify_bg(self.gtk.STATE_NORMAL, self.gtk.gdk.color_parse('#ccc')) 41 42 def select(self, eventbox, event): 43 n = int(eventbox.get_name()[20:]) 44 fn = self.labels[n].get_text() 45 if len(fn) == 0: return(fn) 46 self.selected = self.fileoffset + n 47 self.emccommand.mode(self.emc.MODE_MDI) 48 fn = os.path.join(self.dir, fn) 49 self.emccommand.program_open(fn) 50 self.listing.readfile(fn) 51 self.populate() 52 return(fn) 53 54 def select_and_show(self,fn): 55 self.reload(0) 56 numfiles = len(self.files) 57 fn = os.path.basename(fn) 58 self.fileoffset = 0 59 found = False 60 while True: 61 for k in range(self.numlabels): 62 n = k + self.fileoffset 63 if n >= numfiles: return # notfound 64 if self.files[n] == fn: 65 found = True 66 break # from for 67 if found: break # from while 68 self.fileoffset += self.numlabels 69 70 self.selected = n 71 fn = os.path.join(self.dir, fn) 72 self.listing.readfile(fn) 73 self.populate() 74 75 def up(self, b): 76 self.fileoffset -= self.numlabels 77 if self.fileoffset < 0: 78 self.fileoffset = 0 79 self.populate() 80 81 def down(self, b): 82 self.fileoffset += self.numlabels 83 self.populate() 84 85 def reload(self, b): 86 self.files = os.listdir(self.dir) 87 self.files = [i for i in self.files if i.endswith('.ngc') and 88 os.path.isfile(os.path.join(self.dir, i))] 89 self.files.sort() 90 self.selected = -1 91 self.populate()