Tryag File Manager
Home
-
Turbo Force
Current Path :
/
home
/
cluster1
/
data
/
bu01
/
1121861
/
html
/
TMAT
/
tmat
/
Upload File :
New :
File
Dir
/home/cluster1/data/bu01/1121861/html/TMAT/tmat/preferences.py
from qt import * from twisted.plugin import getPlugins import tokenizer import myparser import plugins class PreferencesDialog(QTabDialog): def __init__(self, displayParent, master, config, changeFunctions={}): QTabDialog.__init__(self, displayParent, "Preferences") self.master=master self.config=config self.changeFunctions=changeFunctions self.pluginTab=PluginTab(self, config) self.insertTab(self.pluginTab, "&Plugins") self.displayTab=DisplayTab(self, config) self.insertTab(self.displayTab, "&Display") self.noteTab=NotesTab(self, config) self.insertTab(self.noteTab,"&Problem Notes") self.setOkButton() self.setApplyButton() self.setCancelButton() self.connect(self,SIGNAL("applyButtonPressed()"),self.applyPressed) self.connect(self,SIGNAL("cancelButtonPressed()"),self.cancelPressed) def defaultPressed(self): self.applyButtonPressed() def applyPressed(self): pluginChanges=self.pluginTab.applyChanges() displayChanges=self.displayTab.applyChanges() notesChanges=self.noteTab.applyChanges() self.config.writeSettings() self.changeFunctions["plugins"](pluginChanges) self.changeFunctions["display"](displayChanges) self.changeFunctions["notes"](notesChanges) def cancelPressed(self): pass class PluginTab(QWidget): def __init__(self, parent, config): QWidget.__init__(self, parent) self.config=config self.layout=QVBoxLayout(self) self.layout.setAutoAdd(1) self.config=config self.plugins={"Tokenizer":tokenizer.TokenizerPlugin, "Parser":myparser.ParserPlugin} self.name2Option={} self.plugin2List={} self.list2Plugin={} self.plugin2Button={} self.button2Plugin={} self.buttons=QButtonGroup() self.connect(self.buttons,SIGNAL("clicked(int)"),self.setupPressed) for plug in self.plugins: b=QHBox(self) QLabel(plug,b) list=QComboBox(False, b) self.plugin2List[plug]=list button=QPushButton("Setup",b) self.plugin2Button[plug]=button self.button2Plugin[button]=plug self.buttons.insert(button) self.reset() def selectionChanged(self, i): list=i.listBox() plug=self.list2Plugin[list] name=str(i.text()) button=self.plugin2Button[plug] option=self.name2Option[plug][name] button.setEnabled(option.preferences) def setupPressed(self, i): button=self.buttons.find(i) plug=self.button2Plugin[button] list=self.plugin2List[plug] name=str(list.currentText()) selected=self.name2Option[plug][name] PluginSetup(self,self.config,selected).exec_loop() def reset(self): self.name2Option={} for p in self.plugins: self.name2Option[p]={} name2Option=self.name2Option[p] plugs=getPlugins(self.plugins[p],plugins) list=self.plugin2List[p] for plug in plugs: n=plug.displayName name2Option[str(n)]=plug self.setPluginDefaults(plug) list.insertItem(n) selected=self.config.valueOf("Plugins/%s/Selected%s" % (p,p)) if selected in name2Option: list.setCurrentText(selected) def setPluginDefaults(self, plug): for attr in plug.preferences: if plug.preferences[attr]=="string": d="" elif plug.preferences[attr]=="int": d=0 elif plug.preferences[attr]=="list":d="" toget="Plugins/%s/%s" % (plug.displayName, attr) plug.values[attr]=self.config.valueOf(toget,default=d) def applyChanges(self): changes={} for p in self.plugins: list=self.plugin2List[p] selected=str(list.currentText()) oldSelected=self.config.valueOf("Plugins/%s/Selected%s" % (p,p)) changes[p]=(oldSelected<>selected) self.config.setValue("Plugins/%s/Selected%s" % (p,p),selected) return changes class PluginSetup(QDialog): def __init__(self, parent, config, plugin): QDialog.__init__(self, parent) self.config=config self.plugin=plugin self.layout=QGridLayout(self,len(self.plugin.preferences)+1,2) self.keyToInput={} i=1 for key in self.plugin.preferences: pType=self.plugin.preferences[key] try: val=self.plugin.values[key] except: val="" self.layout.addWidget(QLabel(key,self),i,1) if pType == "string": self.keyToInput[key]=QLineEdit(self) self.layout.addWidget(self.keyToInput[key],i,2) self.keyToInput[key].setText(val) elif pType == "int": if not(val): val=0 self.keyToInput[key]=QSpinBox(-1,999,1,self) self.layout.addWidget(self.keyToInput[key],i,2) try: self.keyToInput[key].setValue(int(val)) except: self.keyToInput[key].setValue(0) elif pType == "list": if not(val): val=[] self.keyToInput[key]=QListBox(self) self.layout.addWidget(self.keyToInput[key],i,2) i += 1 buttonbox=QHButtonGroup(self) self.layout.addMultiCellWidget(buttonbox,i,i,1,2) self.connect(QPushButton("OK",buttonbox),SIGNAL("clicked()"),self.accept) self.connect(QPushButton("Cancel",buttonbox),SIGNAL("clicked()"),self.reject) def accept(self): for k in self.keyToInput: type=self.plugin.preferences[k] if type == "string": v=cv=str(self.keyToInput[k].text()) elif type == "int": v=cv=int(str(self.keyToInput[k].text())) elif type == "list": cv=str(self.keyToInput[k]().text()) v=cv.split(";;") self.config.setValue("Plugins/%s/%s" % (self.plugin.displayName,k),cv) self.plugin.setValue(k,v) QDialog.accept(self) class myListBox(QWidget): def __init__(self, parent, label=""): QWidget.__init__(self) self.label=label self.setLayout(QVBoxLayout(self)) self.layout().setAutoAdd(1) self.listBox=QListBox(self) self.buttonBox=QHButtonGroup(self) self.connect(QPushButton("+",self.buttonBox),self.addItem) self.connect(QPushButton("-",self.buttonBox),self.delItem) def addItem(self, val=""): val, ok=QInputDialog.getText("Enter new %s value" % self.label, "Enter text for new %s value" % self.label) if ok: self.listBox.insertItem(val) def delItem(self): self.listBox.removeItem(self.listBox.currentItem()) def text(self): return ";;".join([str(self.listBox.text[i]) for i in range(self.listBox.count())]) class ColorPushButton(QPushButton): def setColor(self, color): self.color=color.name() brush = QBrush(Qt.SolidPattern) brush.setColor(color) palette = self.palette() palette.setBrush(QColorGroup.Background, brush) palette.setBrush(QColorGroup.Button, brush) palette.setBrush(QColorGroup.Base, brush) palette.setBrush(QColorGroup.Light, brush) palette.setBrush(QColorGroup.Midlight, brush) palette.setBrush(QColorGroup.Dark, brush) palette.setBrush(QColorGroup.Mid, brush) palette.setBrush(QColorGroup.Shadow, brush) self.setPalette(palette) self.repaint() class NotesTab(QWidget): def __init__(self, parent, config): QWidget.__init__(self, parent) self.config=config QVBoxLayout(self) self.layout().setAutoAdd(True) self.listBox=QListBox(self) bg=QHButtonGroup(self) self.connect(QPushButton("+",bg),SIGNAL("clicked()"),self.addItem) self.connect(QPushButton("-",bg),SIGNAL("clicked()"),self.delItem) self.connect(QPushButton("Edit",bg),SIGNAL("clicked()"),self.editItem) self.reset() def addItem(self): self.listBox.insertItem("") ok=self.editItem(self.listBox.count()-1) if not(ok): self.listBox.removeItem(self.listBox.count()-1) def delItem(self): self.listBox.removeItem(self.listBox.currentItem()) def editItem(self, i=None): if i is None: i=self.listBox.currentItem() oldText=self.listBox.text(i) val, ok=QInputDialog.getText("Note Type","Enter a value for this Problem Note type",QLineEdit.Normal, oldText) if ok: self.listBox.changeItem(val,i) return ok def reset(self): self.listBox.clear() vals=self.config.valueOf("Notes/Labels").split(";;") for v in vals: self.listBox.insertItem(v) def applyChanges(self): changes={} oldVals=self.config.valueOf("Notes/Labels").split(";;") oldVals.sort() newVals=[str(self.listBox.text(i)) for i in range(self.listBox.count())] newVals.sort() changes["Notes/Labels"] = (oldVals<>newVals) self.config.setValue("Notes/Labels",";;".join(newVals)) return changes class DisplayTab(QWidget): def __init__(self, parent, config): QWidget.__init__(self, parent) self.layout=QVBoxLayout(self) self.layout.setAutoAdd(1) self.config=config self.val2Button={} self.button2Val={} self.dir2Button={} self.button2Dir={} self.showOptions=("Morphemes","Glosses","Parses") self.colorOptions=("NonParsing","Ambiguous","Selected", "Unambiguous","Unparsed") self.directionOptions=("Right-to-Left","Left-to-Right") self.spaceOptions=("Lines","Paragraphs","Words") self.type2Button={} self.button2Type={} self.space2Box={} self.box2Space={} self.buttons=QButtonGroup() for val in self.showOptions: button=QCheckBox("Show %s" % val, self) self.val2Button[val]=button self.button2Val[button]=val self.directionBox=QVButtonGroup("Text Direction",self) for val in self.directionOptions: button=QRadioButton(val,self.directionBox) self.dir2Button[val]=button self.button2Dir[button]=val self.spaceBox=QVGroupBox("Spacing",self) for val in self.spaceOptions: b=QHBox(self.spaceBox) QLabel("Space Between %s" % val,b) box=QSpinBox(0,100,1,b) self.space2Box[val]=box self.box2Space[box]=val self.colorBox=QVGroupBox("Highlight Colors",self) for type in self.colorOptions: b=QHBox(self.colorBox) QLabel(type,b) button=ColorPushButton(b) self.buttons.insert(button) self.type2Button[type]=button self.button2Type[button]=type self.connect(self.buttons,SIGNAL("clicked(int)"),self.colorClicked) self.reset() def colorClicked(self, i): button=self.buttons.find(i) if button in self.button2Type: type=self.button2Type[button] newColor=QColorDialog.getColor(QColor(button.color),self) button.setColor(newColor) else: self.reset() def reset(self): for b in self.button2Val: val=self.button2Val[b] b.setChecked(self.config.valueOf("Display/Show%s" % val)) myDir=self.config.valueOf("Display/TextDirection") for b in self.button2Dir: dir=self.button2Dir[b] b.setChecked(dir==myDir) for s in self.spaceOptions: box=self.space2Box[s] box.setValue(int(self.config.valueOf("Display/Spacing/%s" % s))) self.colorBox.setEnabled(self.val2Button["Parses"].isChecked()) for b in self.button2Type: if self.colorBox.isEnabled(): type=self.button2Type[b] color=QColor(self.config.valueOf("Colors/Highlight/%s" % type)) else: color=QColor("lightGrey") b.setColor(color) def applyChanges(self): changes={} for dir in self.dir2Button: button=self.dir2Button[dir] if button.isChecked(): myDir=dir oldDir=self.config.valueOf("Display/TextDirection") changes["Display/TextDirection"]=(oldDir<>myDir) self.config.setValue("Display/TextDirection",myDir) for s in self.spaceOptions: box=self.space2Box[s] oldSpace=int(self.config.valueOf("Display/Spacing/%s" % s)) space=int(str(box.text())) changes[s]=(oldSpace<>space) self.config.setValue("Display/Spacing/%s" % s,space) for val in self.val2Button: button=self.val2Button[val] show=button.isChecked() oldShow=self.config.valueOf("Display/Show%s" % val) changes[val]=(oldShow<>show) self.config.setValue("Display/Show%s" % val, show) for type in self.type2Button: button=self.type2Button[type] color=button.color oldColor=self.config.valueOf("Colors/Highlight/%s" % type) changes[type]=(oldColor<>color) if self.val2Button["Parses"].isChecked(): self.config.setValue("Colors/Highlight/%s" % type, color) return changes