Logo Search packages:      
Sourcecode: bg5ps version File versions

bg5psconf.py

#!/usr/bin/python

import pygtk
pygtk.require('1.2')
from gtk import *
from GtkExtra import *
import string,sys,os

class Application:
      def __init__(self,argv):
            self.win = GtkWindow(title="bg5psconf")
            self.win.connect("delete_event", mainquit)
            self.win.set_usize(350,350)
            
            self.variable_init()
            self.gui_init()

      def mainloop(self):
            mainloop()

      def variable_init(self):
            self.config_file = "/etc/bg5ps.conf"
            self.eng_to_chi = {'chineseFontPath':'中文字型的路徑',
                                 'fontName':'中文字型的檔案名稱',
                                 'oddPages':'是否輸出奇數頁',
                                 'evenPages':'是否輸出偶數頁',
                                 'size':'輸出字型的大小(pixel)',
                                 'topMargin':'上邊界寬度(pixel)',
                                 'bottomMargin':'下邊界寬度(pixel)',
                                 'leftMargin':'左邊界寬度(pixel)',
                                 'rightMargin':'右邊界寬度(pixel)',
                                 'lineSpace':'指定行距大小(pixel)',
                                 'chrSpace':'指定字距大小(pixel)'} 
            self.init_list = {}     # initial list
            self.new_list = {}      # after change list
      
            # open the configuration file
            cf= open(self.config_file,"r")
            str= cf.readline()
            count = 0 
            # while not end of file
            while str != '':
                  #when not comment lines or blank lines
                  if (str[:1] != '#' and str[:1] != '\n'):
                        # all but the last linefeed character
                        str = str[:-1]
                        # break into pairs
                        pair = string.split(str,'=')
                        # put them into a dictionary
                        self.init_list[pair[0]] = pair[1]
                  str = cf.readline()  
            cf.close()     


      def gui_init(self):
            self.box1 = GtkVBox()
            self.win.add(self.box1)
            self.box1.show()

            self.box2 = GtkVBox(spacing=10)
            self.box2.set_border_width(10)
            self.box1.pack_start(self.box2)
            self.box2.show()

            self.gen_entry()

            self.separator = GtkHSeparator()
            self.box1.pack_start(self.separator, expand=FALSE)
            self.separator.show()

            self.box2 = GtkHBox(spacing=10)
            self.box2.set_border_width(10)
            self.box1.pack_start(self.box2, expand=FALSE)
            self.box2.show()

            self.ok = GtkButton("確定儲存")
            self.ok.connect("clicked", self.save_conf)
            self.box2.pack_start(self.ok)
            self.ok.show()
            self.cancel = GtkButton("取消離開")
            self.cancel.connect("clicked", mainquit)
            self.box2.pack_start(self.cancel)
            self.cancel.show()
            self.win.show()


      def gen_entry(self):
            " function to generate label and text entry"
            self.table = GtkTable(9,len(self.init_list))
            self.entry = {}
            self.check = {}
            count = 0   # number of text entry
            count_check = 0   # number of check box
            count_down = 10

            for key in self.init_list.keys():
                  # catch configuration values from initial_list
                  if self.init_list[key] !='true' and self.init_list[key] != 'false':
                              label = GtkLabel(self.eng_to_chi[key])
                              label.set_alignment(JUSTIFY_LEFT, 0)
                              self.table.attach(label,1,4,count,count+1,xoptions=FILL,yoptions=FILL)
                              label.show()
                              self.entry[key] = GtkEntry()
                              self.entry[key].set_text(self.init_list[key])
                              self.table.attach(self.entry[key],4,8,count,count+1,xoptions=FILL,yoptions=FILL)
                              self.entry[key].show()
                              count = count + 1
                  else:
                              self.check[key] = GtkCheckButton(self.eng_to_chi[key])
                              #self.check[key].connect("clicked", mainquit)
                              self.table.attach(self.check[key],1,5,count_down,count_down+1,yoptions=FILL)
                              self.check[key].show()
                              count_down = count_down - 1
                              count_check = count_check + 1

            self.box1.pack_start(self.table)                         
            self.table.show()     

      def save_conf(self,aa):
            cf = open(self.config_file,"w")
            #print self.new_list
            for key in self.entry.keys():
                  text = self.entry[key].get_text()
                  comment = self.eng_to_chi[key]
                  line = '#' + comment + '\n'
                  line = line + key + '=' + text +'\n\n'
                  cf.write(line) 
            for key in self.check.keys():
                  if self.check[key].get_active():
                        text = 'true'
                  else:
                        text = 'false'
                  comment = self.eng_to_chi[key]
                  line = '#' + comment + '\n'
                  line = line + key + '=' + text + '\n\n'
                  cf.write(line)
            cf.close()
            mainquit()

if(__name__=="__main__"): 
      app = Application(sys.argv)
      app.mainloop()

Generated by  Doxygen 1.6.0   Back to index