OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
query_editor.py
Go to the documentation of this file.
1 from ost import mol
2 from PyQt4 import QtCore, QtGui
3 
4 class QueryEditorWidget(QtGui.QWidget):
5  def __init__(self, parent=None):
6  QtGui.QWidget.__init__(self, parent)
7  self.default_font_=QtGui.QTextCharFormat()
8  self.default_font_.setForeground(QtGui.QBrush(QtGui.QColor(0,0,0)))
9  self.error_font_=QtGui.QTextCharFormat()
10  self.error_font_.setForeground(QtGui.QBrush(QtGui.QColor(255,0,0)))
11  self.selection_edit_=QtGui.QTextEdit(self)
12  self.selection_edit_.setFixedHeight(40)
13  self.selection_edit_.updateGeometry()
14  self.status_=QtGui.QLabel(" ",self);
15  self.status_.setWordWrap(True)
16  self.status_.setMargin(0)
17  self.status_.setAlignment(QtCore.Qt.AlignRight)
18  self.status_.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum,
19  QtGui.QSizePolicy.Expanding))
20  vl=QtGui.QVBoxLayout()
21  vl.addWidget(self.selection_edit_)
22  self.no_bonds_=QtGui.QRadioButton('none')
23  self.ex_bonds_=QtGui.QRadioButton('exclusive')
24  self.in_bonds_=QtGui.QRadioButton('inclusive')
25  self.in_bonds_.setChecked(True)
26  self.match_res_=QtGui.QCheckBox('match residues')
27 
28  vl.setMargin(0)
29  vl.setSpacing(0)
30  self.setLayout(vl)
31  vl.addWidget(self.status_)
32  hl=QtGui.QHBoxLayout()
33  l=QtGui.QLabel("bonds:")
34 
35  hl.addWidget(l)
36  hl.addSpacing(5)
37  hl.addWidget(self.no_bonds_)
38  hl.addWidget(self.ex_bonds_)
39  hl.addWidget(self.in_bonds_)
40  hl.addStretch(1)
41  vl.addLayout(hl)
42  vl.addWidget(self.match_res_)
43 
44  self.changing_text_=False;
45  self.connect(self.selection_edit_,QtCore.SIGNAL("textChanged()"),
46  self._StartTimer)
47  self.timer_=QtCore.QTimer()
48  QtCore.QObject.connect(self.timer_, QtCore.SIGNAL('timeout()'),
49  self._UpdateMessage)
50 
51  def GetQueryFlags(self):
52  flags=0
53  if self.no_bonds_.isChecked():
54  flags|=mol.NO_BONDS
55  if self.ex_bonds_.isChecked():
56  flags|=mol.EXCLUSIVE_BONDS
57  if self.match_res_.isChecked():
58  flags|=mol.MATCH_RESIDUES
59  return flags
60 
61  def GetQuery(self):
62  return mol.Query(str(self.selection_edit_.toPlainText()))
63 
64  def GetQueryText(self):
65  return str(self.selection_edit_.toPlainText())
66 
67  def SetQueryFlags(self,flags):
68  self.no_bonds_.setChecked(flags & mol.NO_BONDS)
69  self.ex_bonds_.setChecked(flags & mol.EXCLUSIVE_BONDS)
70  self.in_bonds_.setChecked(not (flags & mol.NO_BONDS|mol.EXCLUSIVE_BONDS))
71  self.match_res_.setChecked(flags & mol.MATCH_RESIDUES)
72 
73  def SetQuery(self,query):
74  self.selection_edit_.setText(query)
75 
76  def _StartTimer(self):
77  self.timer_.stop()
78  self.timer_.start(500)
79 
80  def _UpdateMessage(self):
81  if self.changing_text_:
82  return
83  self.changing_text_ = True
84  query=self.GetQuery()
85 
86  cursor=self.selection_edit_.textCursor()
87  cursor.select(QtGui.QTextCursor.Document)
88  cursor.setCharFormat(self.default_font_)
89 
90  if query.IsValid():
91  self.status_.setText("")
92  else:
93  d=query.GetErrorDescription()
94  self.status_.setText("<font color='red'>%s</font>"%d.msg.strip())
95  #self.status_.setFixedSize(self.width(),self.status_.height())
96  cursor.movePosition(QtGui.QTextCursor.Start)
97  if d.range.Loc<len(query.string):
98 
99  cursor.movePosition(QtGui.QTextCursor.NextCharacter,
100  QtGui.QTextCursor.MoveAnchor, d.range.Loc)
101  cursor.movePosition(QtGui.QTextCursor.NextCharacter,
102  QtGui.QTextCursor.KeepAnchor, d.range.Length)
103  cursor.setCharFormat(self.error_font_)
104  self.changing_text_=False
105 
106 class QueryDialog(QtGui.QDialog):
107  def __init__(self, title, parent=None):
108  QtGui.QDialog.__init__(self, parent)
109  l=QtGui.QVBoxLayout(self)
110  self.setWindowTitle(title)
112  l.addWidget(self.editor)
113  l.addSpacing(10)
114  l3=QtGui.QHBoxLayout()
115  ab=QtGui.QPushButton('OK')
116  ab.setDefault(True)
117  cb=QtGui.QPushButton('Cancel')
118  l3.addStretch(1)
119  l3.addWidget(cb, 0)
120  l3.addWidget(ab, 0)
121  l.addLayout(l3)
122  QtCore.QObject.connect(cb, QtCore.SIGNAL('clicked()'), self.reject)
123  QtCore.QObject.connect(ab, QtCore.SIGNAL('clicked()'), self.accept)
124  self.connect(self.editor.selection_edit_,QtCore.SIGNAL("textChanged()"),self._CheckNewline)
125 
126  @property
127  def query_flags(self):
128  return self.editor.GetQueryFlags()
129  @property
130  def query(self):
131  return self.editor.GetQueryText()
132 
133  def event(self, e):
134  if e.type() == QtCore.QEvent.KeyPress and (e.key () == QtCore.Qt.Key_Return or e.key () == QtCore.Qt.Key_Enter):
135  self.accept()
136  return True
137  else:
138  return QtGui.QDialog.event(self, e)
139 
140  def _CheckNewline(self):
141  if self.editor.GetQueryText().endswith("\n"):
142  self.accept()
Selection Query.
Definition: query.hh:74