00001 from ost import mol
00002 from PyQt4 import QtCore, QtGui
00003
00004 class QueryEditorWidget(QtGui.QWidget):
00005 def __init__(self, parent=None):
00006 QtGui.QWidget.__init__(self, parent)
00007 self.default_font_=QtGui.QTextCharFormat()
00008 self.default_font_.setForeground(QtGui.QBrush(QtGui.QColor(0,0,0)))
00009 self.error_font_=QtGui.QTextCharFormat()
00010 self.error_font_.setForeground(QtGui.QBrush(QtGui.QColor(255,0,0)))
00011 self.selection_edit_=QtGui.QTextEdit(self)
00012 self.selection_edit_.setFixedHeight(40)
00013 self.selection_edit_.updateGeometry()
00014 self.status_=QtGui.QLabel(" ",self);
00015 self.status_.setWordWrap(True)
00016 self.status_.setMargin(0)
00017 self.status_.setAlignment(QtCore.Qt.AlignRight)
00018 self.status_.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum,
00019 QtGui.QSizePolicy.Expanding))
00020 vl=QtGui.QVBoxLayout()
00021 vl.addWidget(self.selection_edit_)
00022 self.no_bonds_=QtGui.QRadioButton('none')
00023 self.ex_bonds_=QtGui.QRadioButton('exclusive')
00024 self.in_bonds_=QtGui.QRadioButton('inclusive')
00025 self.in_bonds_.setChecked(True)
00026 self.match_res_=QtGui.QCheckBox('match residues')
00027
00028 vl.setMargin(0)
00029 vl.setSpacing(0)
00030 self.setLayout(vl)
00031 vl.addWidget(self.status_)
00032 hl=QtGui.QHBoxLayout()
00033 l=QtGui.QLabel("bonds:")
00034
00035 hl.addWidget(l)
00036 hl.addSpacing(5)
00037 hl.addWidget(self.no_bonds_)
00038 hl.addWidget(self.ex_bonds_)
00039 hl.addWidget(self.in_bonds_)
00040 hl.addStretch(1)
00041 vl.addLayout(hl)
00042 vl.addWidget(self.match_res_)
00043
00044 self.changing_text_=False;
00045 self.connect(self.selection_edit_,QtCore.SIGNAL("textChanged()"),
00046 self._StartTimer)
00047 self.timer_=QtCore.QTimer()
00048 QtCore.QObject.connect(self.timer_, QtCore.SIGNAL('timeout()'),
00049 self._UpdateMessage)
00050
00051 def GetQueryFlags(self):
00052 flags=0
00053 if self.no_bonds_.isChecked():
00054 flags|=mol.NO_BONDS
00055 if self.ex_bonds_.isChecked():
00056 flags|=mol.EXCLUSIVE_BONDS
00057 if self.match_res_.isChecked():
00058 flags|=mol.MATCH_RESIDUES
00059 return flags
00060
00061 def GetQuery(self):
00062 return mol.Query(str(self.selection_edit_.toPlainText()))
00063
00064 def GetQueryText(self):
00065 return str(self.selection_edit_.toPlainText())
00066
00067 def SetQueryFlags(self,flags):
00068 self.no_bonds_.setChecked(flags & mol.NO_BONDS)
00069 self.ex_bonds_.setChecked(flags & mol.EXCLUSIVE_BONDS)
00070 self.in_bonds_.setChecked(not (flags & mol.NO_BONDS|mol.EXCLUSIVE_BONDS))
00071 self.match_res_.setChecked(flags & mol.MATCH_RESIDUES)
00072
00073 def SetQuery(self,query):
00074 self.selection_edit_.setText(query)
00075
00076 def _StartTimer(self):
00077 self.timer_.stop()
00078 self.timer_.start(500)
00079
00080 def _UpdateMessage(self):
00081 if self.changing_text_:
00082 return
00083 self.changing_text_ = True
00084 query=self.GetQuery()
00085
00086 cursor=self.selection_edit_.textCursor()
00087 cursor.select(QtGui.QTextCursor.Document)
00088 cursor.setCharFormat(self.default_font_)
00089
00090 if query.IsValid():
00091 self.status_.setText("")
00092 else:
00093 d=query.GetErrorDescription()
00094 self.status_.setText("<font color='red'>%s</font>"%d.msg.strip())
00095
00096 cursor.movePosition(QtGui.QTextCursor.Start)
00097 if d.range.Loc<len(query.string):
00098
00099 cursor.movePosition(QtGui.QTextCursor.NextCharacter,
00100 QtGui.QTextCursor.MoveAnchor, d.range.Loc)
00101 cursor.movePosition(QtGui.QTextCursor.NextCharacter,
00102 QtGui.QTextCursor.KeepAnchor, d.range.Length)
00103 cursor.setCharFormat(self.error_font_)
00104 self.changing_text_=False
00105
00106 class QueryDialog(QtGui.QDialog):
00107 def __init__(self, title, parent=None):
00108 QtGui.QDialog.__init__(self, parent)
00109 l=QtGui.QVBoxLayout(self)
00110 self.setWindowTitle(title)
00111 self.editor=QueryEditorWidget(self)
00112 l.addWidget(self.editor)
00113 l.addSpacing(10)
00114 l3=QtGui.QHBoxLayout()
00115 ab=QtGui.QPushButton('OK')
00116 ab.setDefault(True)
00117 cb=QtGui.QPushButton('Cancel')
00118 l3.addStretch(1)
00119 l3.addWidget(cb, 0)
00120 l3.addWidget(ab, 0)
00121 l.addLayout(l3)
00122 QtCore.QObject.connect(cb, QtCore.SIGNAL('clicked()'), self.reject)
00123 QtCore.QObject.connect(ab, QtCore.SIGNAL('clicked()'), self.accept)
00124 self.connect(self.editor.selection_edit_,QtCore.SIGNAL("textChanged()"),self._CheckNewline)
00125
00126 @property
00127 def query_flags(self):
00128 return self.editor.GetQueryFlags()
00129 @property
00130 def query(self):
00131 return self.editor.GetQueryText()
00132
00133 def event(self, e):
00134 if e.type() == QtCore.QEvent.KeyPress and (e.key () == QtCore.Qt.Key_Return or e.key () == QtCore.Qt.Key_Enter):
00135 self.accept()
00136 return True
00137 else:
00138 return QtGui.QDialog.event(self, e)
00139
00140 def _CheckNewline(self):
00141 if self.editor.GetQueryText().endswith("\n"):
00142 self.accept()